Writing code for power module in c++, receiving several errors - c++

I'm writing code that doesn't use pow() in c++, but I am getting quite a few errors which I can't figure out:
double power (double X, unsigned int N)
{
double value;
unsigned int i = 1;
for (i = 1, i <= N, i++)
{
result = result * X;
}
if (finite(result))
{
return result;
}
else
{
return INFINITY;
}
}
Errors:
In function 'double power(double, unsigned int)':
Line 5: warning: right-hand operand of comma has no effect
Line 5: error: expected ';' before ')' token
Line 10: error: expected primary-expression before 'if'
Line 10: error: expected ';' before 'if'
Line 10: error: expected primary-expression before 'if'
Line 10: error: expected ')' before 'if'
Any help would be appreciated, thanks.

It should be for (i = 1; i <= N; i++).
In C++, the semicolon is usd to delimit the different parts of the for loop.

for (i = 1, i <= N, i++) => for (i = 1; i <= N; i++)
Get a good C book

Related

Why doesn't g++ recognize stoi even using C++11?

I have this function in C++11:
bool ccc(const string cc) {
vector<string> digits;
int aux;
for(int n = 0; n < cc.length(); ++n) {
digits.push_back(to_string(cc[n])); }
for(int s = 1; s < digits.size(); s += 2) {
aux = stoi(digits[s]);
aux *= 2;
digits[s] = to_string(aux);
aux = 0;
for(int f = 0; f < digits[s].length(); ++f) {
aux += stoi(digits[s][f]); }
digits[s] = to_string(aux);
aux = 0; }
for(int b = 0; b < digits.size(); ++b) {
aux += stoi(digits[b]); }
aux *= 9;
aux %= 10;
return (aux == 0); }
And I get this error when compiling with g++ with the -std=c++11 flag:
crecarche.cpp: In function ‘bool ccc(std::string)’:
crecarche.cpp:18:12: error: no matching function for call to ‘stoi(__gnu_cxx::__alloc_traits<std::allocator<char>, char>::value_type&)’
18 | aux += stoi(digits[s][f]); }
| ~~~~^~~~~~~~~~~~~~
But I used the stoi function after and I did not get any error with that line.
Why is the compiler throwing me this error and how can I fix it?
The error message is telling you that the argument you pass to stoi is of the type
__gnu_cxx::__alloc_traits<std::allocator<char>, char>::value_type&
which is a fancy way of saying char&. This happens because digits[s] is already of type string&, and subscribing it further gives you a char&.
It's not clear to me what you are trying to accomplish. Maybe you need to remove the extra subscript, or use digits[s][f] - '0' to compute the digit value. C++ requires that the decimal digits are represented by subsequent code points, so this works even in theoretical implementations which are not based on the ISO 646 subset of Unicode.

Creating a 4 State Mealy Finite State Machine using Verilog [HELP]

I'm trying to figure out how to get this to run without getting any errors.
module main;
(
input wire clk, reset;
input wire x, y;
output reg n, c;
s0 = 0,
s1 = 1,
s2 = 2,
s3 = 3;
state_reg;
state_next;
);
always(posedge clk, posedge reset)
begin
if (reset) begin
state_reg = s0;
end
else begin
state_reg = state_next;
end
end
always (x, y, state_reg) begin
state_next = state_reg;
n = 0;
c = 0;
case (state_reg)
s0 : begin
if (x == 0 && y == 0) begin
n = 0;
c = 0;
state_next = s0;
end
else if (x == 0 && y == 1) begin
n = 0;
c = 0;
state_next = s0;
end
else if (x== 1 && y == 0) begin
n = 0;
c = 0;
state_next = s0;
end
else if (x== 1 && y == 1) begin
n = 1;
c = 0;
state_next = s1;
end
end
s1 : begin
if (x == 0 && y == 0) begin
n = 0;
c = 0;
state_next = s0;
end
else if (x == 0 && y == 1) begin
n = 0;
c = 0;
state_next = s0;
end
else if (x== 1 && y == 0) begin
n = 0;
c = 0;
state_next = s0;
end
else if (x== 1 && y == 1) begin
n = 1;
c = 0;
state_next = s2;
end
end
s2 : begin
if (x == 0 && y == 0) begin
n = 0;
c = 0;
state_next = s0;
end
else if (x == 0 && y == 1) begin
n = 0;
c = 0;
state_next = s0;
end
else if (x== 1 && y == 0) begin
n = 0;
c = 0;
state_next = s0;
end
else if (x== 1 && y == 1) begin
n = 1;
c = 1;
state_next = s3;
end
end
s3 : begin
if (x == 0 && y == 0) begin
n = 0;
c = 0;
state_next = s0;
end
else if (x == 0 && y == 1) begin
n = 0;
c = 0;
state_next = s0;
end
else if (x== 1 && y == 0) begin
n = 0;
c = 1;
state_next = s3;
end
else if (x== 1 && y == 1) begin
n = 1;
c = 1;
state_next = s3;
end
end
endcase
endmodule
I feel like the code enough should show what I'm trying to do but in case it doesn't I've also attached an image of the logic schematic version (I'm not sure how to attach the .cct file of the schematic on this website due to me being new). Sorry guys this is somewhat my first time using Verilog so I'm really new to this but I have to write this for an honors project with zero guidance. So the output should be if I input x or y with either 0 or 1, it should switch to a different state depending on the conditions (s0, s1, s2, s3) etc. If I hit reset, it should go back to s0, if I hit clk or the clock variable, it should take the inputs x and y to decide what the next state should be.
EDIT: Here are the errors I'm getting.
jdoodle.v:2: syntax error
jdoodle.v:3: error: invalid module item.
jdoodle.v:6: syntax error
jdoodle.v:6: error: Invalid module instantiation
jdoodle.v:11: error: Invalid module instantiation
jdoodle.v:12: error: Invalid module instantiation
jdoodle.v:13: error: invalid module item.
jdoodle.v:14: syntax error
jdoodle.v:17: Syntax in assignment statement l-value.
jdoodle.v:18: syntax error
jdoodle.v:20: error: invalid module item.
jdoodle.v:21: syntax error
jdoodle.v:25: error: invalid module item.
jdoodle.v:26: syntax error
jdoodle.v:26: error: Invalid module instantiation
jdoodle.v:27: error: Invalid module instantiation
jdoodle.v:31: syntax error
jdoodle.v:31: error: Invalid module instantiation
jdoodle.v:32: error: Invalid module instantiation
jdoodle.v:33: error: Invalid module instantiation
jdoodle.v:36: syntax error
jdoodle.v:36: error: Invalid module instantiation
jdoodle.v:37: error: Invalid module instantiation
jdoodle.v:38: error: Invalid module instantiation
jdoodle.v:41: syntax error
jdoodle.v:41: error: Invalid module instantiation
jdoodle.v:42: error: Invalid module instantiation
jdoodle.v:43: error: Invalid module instantiation
jdoodle.v:46: syntax error
jdoodle.v:46: error: Invalid module instantiation
jdoodle.v:47: error: Invalid module instantiation
jdoodle.v:48: error: Invalid module instantiation
jdoodle.v:53: syntax error
jdoodle.v:53: error: Invalid module instantiation
jdoodle.v:54: error: Invalid module instantiation
jdoodle.v:55: error: Invalid module instantiation
jdoodle.v:58: syntax error
jdoodle.v:58: error: Invalid module instantiation
jdoodle.v:59: error: Invalid module instantiation
jdoodle.v:60: error: Invalid module instantiation
jdoodle.v:63: syntax error
jdoodle.v:63: error: Invalid module instantiation
jdoodle.v:64: error: Invalid module instantiation
jdoodle.v:65: error: Invalid module instantiation
jdoodle.v:68: syntax error
jdoodle.v:68: error: Invalid module instantiation
jdoodle.v:69: error: Invalid module instantiation
jdoodle.v:70: error: Invalid module instantiation
jdoodle.v:75: syntax error
jdoodle.v:75: error: Invalid module instantiation
jdoodle.v:76: error: Invalid module instantiation
jdoodle.v:77: error: Invalid module instantiation
jdoodle.v:80: syntax error
jdoodle.v:80: error: Invalid module instantiation
jdoodle.v:81: error: Invalid module instantiation
jdoodle.v:82: error: Invalid module instantiation
jdoodle.v:85: syntax error
jdoodle.v:85: error: Invalid module instantiation
jdoodle.v:86: error: Invalid module instantiation
jdoodle.v:87: error: Invalid module instantiation
jdoodle.v:90: syntax error
jdoodle.v:90: error: Invalid module instantiation
jdoodle.v:91: error: Invalid module instantiation
jdoodle.v:92: error: Invalid module instantiation
jdoodle.v:97: syntax error
jdoodle.v:97: error: Invalid module instantiation
jdoodle.v:98: error: Invalid module instantiation
jdoodle.v:99: error: Invalid module instantiation
jdoodle.v:102: syntax error
jdoodle.v:102: error: Invalid module instantiation
jdoodle.v:103: error: Invalid module instantiation
jdoodle.v:104: error: Invalid module instantiation
jdoodle.v:107: syntax error
jdoodle.v:107: error: Invalid module instantiation
jdoodle.v:108: error: Invalid module instantiation
jdoodle.v:109: error: Invalid module instantiation
jdoodle.v:112: syntax error
jdoodle.v:112: error: Invalid module instantiation
jdoodle.v:113: error: Invalid module instantiation
jdoodle.v:114: error: Invalid module instantiation
There are several syntax errors preventing this module from compiling.
The semicolon after the module name 'main' is a syntax error.
Semicolons in the IO list (after the module name are errors, need to be commas.
You are tying to declare a state variables is the IO list area (just after the word module . Its an error. Put the declarations after IO listing.
Search 'verilog state machine' to learn how and where to declare the state variables.
You need non-blocking assignments (<=) rather than non blocking assignments (=) in the always block with the clock.
Your always blocks were missing the timing control operator #.
You were missing a 'end' keyword at the end of the module.
The state machine does not have a :default state, which could cause some strange behavior.
I made these changes (did not add a :default state because it will modify your design intent).
Here is your code that now compiles using Mentor Questa on edaplayground.com
You did not post a testbench, there is no way to know if it behaves the way you want it to or not.
Search on 'basic verilog testbench' to get started on a testbench design.
module main
(
input wire clk,
input wire reset,
input wire x,
input wire y,
output reg n,
output reg c
);
localparam
s0 = 2'b00,
s1 = 2'b01,
s2 = 2'b10,
s3 = 2'b11;
reg [1:0] state_reg,state_next;
always #(posedge clk, posedge reset)
begin
if (reset) begin
state_reg <= s0;
end
else begin
state_reg <= state_next;
end
end
always #(x, y, state_reg) begin
state_next = state_reg;
n = 0;
c = 0;
case (state_reg)
s0 : begin
if (x == 0 && y == 0) begin
n = 0;
c = 0;
state_next = s0;
end
else if (x == 0 && y == 1) begin
n = 0;
c = 0;
state_next = s0;
end
else if (x== 1 && y == 0) begin
n = 0;
c = 0;
state_next = s0;
end
else if (x== 1 && y == 1) begin
n = 1;
c = 0;
state_next = s1;
end
end
s1 : begin
if (x == 0 && y == 0) begin
n = 0;
c = 0;
state_next = s0;
end
else if (x == 0 && y == 1) begin
n = 0;
c = 0;
state_next = s0;
end
else if (x== 1 && y == 0) begin
n = 0;
c = 0;
state_next = s0;
end
else if (x== 1 && y == 1) begin
n = 1;
c = 0;
state_next = s2;
end
end
s2 : begin
if (x == 0 && y == 0) begin
n = 0;
c = 0;
state_next = s0;
end
else if (x == 0 && y == 1) begin
n = 0;
c = 0;
state_next = s0;
end
else if (x== 1 && y == 0) begin
n = 0;
c = 0;
state_next = s0;
end
else if (x== 1 && y == 1) begin
n = 1;
c = 1;
state_next = s3;
end
end
s3 : begin
if (x == 0 && y == 0) begin
n = 0;
c = 0;
state_next = s0;
end
else if (x == 0 && y == 1) begin
n = 0;
c = 0;
state_next = s0;
end
else if (x== 1 && y == 0) begin
n = 0;
c = 1;
state_next = s3;
end
else if (x== 1 && y == 1) begin
n = 1;
c = 1;
state_next = s3;
end
end
endcase
end
endmodule

Why sublime text and VSCode not showing runtime error?

In the below code snippet, for example, take n as 9 and the array a's elements as {5,5,4,5,5,5,4,5,6}.
Sublime text and visual studio code are showing the correct output as 3 but Leetcode online ide is showing "runtime error: index 8 out of bounds for type 'int [k]' ". I want to know why sublime text and visual studio code are not showing the runtime error, and if there is an ide/text editor which shows runtime error, please recommend it to me.
int k = n - 1;
int temp[k];
for (int i = 0 ; i < k ; i++)
{
temp[i] = (a[i] - a[i + 1]);
}
int res = INT_MIN;
for (int i = 0 ; i < k ; )
{
int x = temp[i];
int c = 0;
while (x == temp[i])
{
i++;
c++;
}
res = max(res, c + 1);
}
cout<<res;
int k = n - 1;
int temp[k]; // variable length array, must be either #define k <constant num>
// or 'const int k'
The variable-length arrays (VLAs) aren't supported in C++ standard. The Sublime Text & VS Code doesn't matters at all, but the compiler.
You might've not enabled your compiler warnings, thus, you're unable to see any error. You can do it by appending -pedantic flag in your compilation option to see all warnings.

Can't write into a file using fstream - I can't seem to get why

I need to create a program using graph theory which takes as an input multiple undirected graphs and writes out either YES if it is a complete graph or NO if it is not a complete graph.
E.g.
for:
2 //number of graps
3 //number of lines of the first graph's matrix
0 1 1 //the first graph's matrix
1 0 1
1 1 0
4 //number of lines of the second graph's matrix
0 1 1 1 //the second graph's matrix
1 0 0 1
1 0 0 1
1 1 1 0
It should output:
YES
NO
I solved it this way:
#include <iostream>
#include <fstream>
using namespace std;
ifstream f("graf_complet.in");
ofstream g("graf_complet.out");
int verif(int matrice[][100], int t)
{
int c = 0;
bool ok = 1;
for (int i = 1; i <= t; i++)
{
c = 0;
for (int j = 1; j <= t; j++)
{
if (matrice[i][j] == 1)
{
c++;
}
}
if (c != (t - 1))
{
ok = 0;
break;
}
}
if (ok == 1)
{
return 1;
}
return 0;
}
int main()
{
int g, c = 0, t, matrice[100][100];
f >> g;
while (c != g)
{
f >> t;
for (int i = 1; i <= t; i++)
{
for (int j = 1; j <= t; j++)
{
f >> matrice[i][j];
}
}
if (verif(matrice, t))
{
g << "YES";
}
else
{
g << "NO";
}
c++;
}
}
Using cout instead of writing into files works, and I seem to get the proper results. When I try to write into the file with g<< (as I'm supposed to) I get:
Severity Code Description Project File Line Suppression State
Error C2297 '<<': illegal, right operand has type 'const char [3]' ConsoleApplication1 C:\Users\x\source\repos\ConsoleApplication1\ConsoleApplication1\ConsoleApplication1.cpp 52
Error C2297 '<<': illegal, right operand has type 'const char [3]' ConsoleApplication1 C:\Users\x\source\repos\ConsoleApplication1\ConsoleApplication1\ConsoleApplication1.cpp 56
Error (active) E2140 expression must have integral or unscoped enum type ConsoleApplication1 C:\Users\x\source\repos\ConsoleApplication1\ConsoleApplication1\ConsoleApplication1.cpp 52
Error (active) E2140 expression must have integral or unscoped enum type ConsoleApplication1 C:\Users\x\source\repos\ConsoleApplication1\ConsoleApplication1\ConsoleApplication1.cpp 56
I can't get why this happens tbh. It is not the first time I write into files this way and I've never encountered this. I think it might be something about the fact that the file which I'm taking the input from (so basically f>>) is not closed, it is still open.
Is that the reason?
If so, how can I still make the program work? If not, what is the issue causing the error?
Thanks.

ISO C++ forbids comparison between pointer and integer [-fpermissive]

The Code
int cycle_length(int i, int j) {
int cycleLength = 0;
for (int k = i; k <= j; k++) {
cout << algorithm(k) << endl;
if (algorithm(k) > cycle_length) {
cycleLength = algorithm(k);
}
}
return cycleLength;
}
ISO C++ forbids comparison between pointer and integer [-fpermissive]
I got this error in this line if ( algorithm(k) > cycle_length).
How is that, however, the same code works right in the main() ?? and what is this error mean ???
Added
algorithm is a function take an integer input and return an integer.
int algorithm(int number1) {
int counter = 1, number = number1;
do {
if (number % 2 == 0) {
number = number / 2;
counter++;
} else {
number = (3 * number) + 1;
counter++;
}
} while (number != 1);
return counter;
}
You are confusing the name of the function with your local variable of nearly the same name:
int cycle_length(int i, int j)
{
int cycleLength
Your function is called cycle_length, your variable is called cycleLength - yet you are using cycle_length further down.
The error message is slightly strange, because the compiler doesn't do "compare variable names with function names to see if there is one that is similar and then suggest that maybe you just typed it wrong" - it simply says "Hmm, you are comparing a function pointer [what you get from the name of a function] with an integer, that's not on!"