c++ converting pseudo code for runtime


I am given the following information, i am trying to convert this in a function.
c ← 0
for i ← 0 to n - 1 do
s ← 0
for j ← 0 to i do
s ← s + A[0]
for k ← 1 to j do
s ← s + A[k]
if B[i] = s then
c ← c + 1
return c

This is what i have done so far and the errors i receive.
1
2
3
4
5
6
7
8
9
10
11
12
int Ex5(int A[], int B[], int MAX)
{
int c = 0;
for(int i = 0; i < MAX; i++)
int s = 0;
for(int j = 0; j < MAX; j++)
int s = s + A[0];
for(int k = 1; k < MAX; k++)
int s = s + A[k];
if(B[i] = s)
c = c + 1;
return c;

Last edited on
You should only declare s once. In your current code, you declare it three times; the additional definitions shadow the original definitions.
i have tried declaring s before only once however, i still receive s undeclared errors in those following lines.

Also i believe i have gotten the lines "0 to i do" wrong.

This is the updated code:
1
2
3
4
5
6
7
8
9
10
11
12
13
int Ex5(int A[], int B[], int MAX)
{
int c = 0;
for(int i = 0; i < MAX; i++)
int s = 0;
for(int j = 0; j < MAX; j++)
s = s + A[0];                          //returns errors
for(int k = 1; k < MAX; k++)
s = s + A[k];                          //returns errors
if(B[i] = s)                           //returns errors
c = c + 1;
return c;
}
Last edited on
You should always use curly braces. Your for-loops are not nested because you forgot to.
Last edited on
Ah, thank you i cant believe i didn't notice that. haha Thanks
Topic archived. No new replies allowed.