Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
• Status: Solved
• Priority: Medium
• Security: Public
• Views: 774

Syntax Error Declaring Array & error C2133: 'pi' : unknown size

I am writing this code to solve an array problem but i keep getting errors.

i want to declare that pi[0] = [1,0,0,0,0] but it keeps giving me an error. I am trying to declare that because i want to evaluate   pi[n] = (pi[n-1]) * 3 for a set of n numbers until  pi[n] - pi[n-1] <= 200
I used the for loop insde because i want n to increase and put it in the do while loop so i can tell it to stop.

#include <iostream.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <iomanip>
using namespace std;

int main()
{
int n;
double pi[0] =  [1,0,0,0,0];

do
{

for (n=1; n>100; n++)

double pi[n];

pi[n] = (pi[n-1]) * 3;
cout<<pi[n];

}

while pi[n] - pi[n-1] <= 200;

return 0;
}
0
mrperfect75000
• 5
• 3
1 Solution

Commented:
double pi[0] is an array of zero doubles. What's that supposed to mean? If you want an array of 5 values initialised to (1,0,0,0,0) then it's

double a[] = {1,2,3,4};

You need {...} to delimit the scope of the for () loop
i.e.
for( n = 1 ; n > 100 ; n++ )
{
.... do stuff
}

You can't declare an array variable with a dynamic size (i.e. from a variable), so double pi[n] isn't valid.

Something like double *pi = new double[n]
..use it, then
delete [] pi;

You're also hiding the original pi variable by your second declaration

... IN general there's lots of other guff wrong, too. You're kind of on the wrong track here.

I presume this is homework?
0

Author Commented:
Yes it is, If i can correct those errors i will give it a shot again...Thanx for the direction....is there a better way to go about this? I'm really lost
0

Commented:
What is pi? A 5-element array?

If so, then what does pi[n-1] * 3 mean?

Or, easier:

Tell me what pi[0], pi[1], pi[2], pi[3] are...

I'm not quite sure what you're trying to do yet..
0

Author Commented:
pi is a 5 element array. but since i know p[0] and a formula for p[n] i want to computer values for
p[n] for n up to a certain number. doesnt matter what this number is as far as a get a couple of values for p[n].

pi[n-1] * 3:    I want n to keep increasing starting from n = 1. I want 5 values [1,0,0,0,0] initialised in pi[0]. so that  when n = 1, ........ pi[n-1] = p[0]. then i want to assing p[n] to equal p[n-1]  multiplied by 3.

I hope that helps. Thanks a lot
0

Commented:
So

pi[0] = { 1 , 0 , 0 , 0 , 0 }

pi[1] = 3 * pi[0] = 3 * { 1 , 0 , 0 , 0 , 0 }.
What's that? Does it equal { 3 , 0 , 0 , 0 , 0 }

If so, then what does p[1] - p[0] equal? { 2 , 0 ,  0,  0, 0 } ??

I don't know how to multiply or subtract an array... What's your rule?

0

Commented:
I think you want to generate pi as 1,3,9,27,81 . . . where p[n]=p[n-1]*3 and p[0]=1. See in the following sample of code to generate numbers up to 5 terms

#include <iostream>
using namespace std;

const int M = 5;
int main()
{
int n;
double pi[M];

n=1;
pi[0]=1.0;
cout<< "pi[0] = " << pi[0]<<endl;

do
{
pi[n] = (pi[n-1]) * 3;
cout<< "pi[" << n << "] = " << pi[n]<<endl;
++n;
}while(n<M);
return 0;
}
Output:
pi[0] = 1
pi[1] = 3
pi[2] = 9
pi[3] = 27
pi[4] = 81

Otherwise clearly tell what you want to do.
-Mahesh
0

Commented:
Strangely enough I could have written that code, too, Smpoojary. But this is a homework question, and you don't answer homework questions verbatim on EE.
0

Author Commented:
Thanks Guys.
What i am trying to do is this....I understand the concept i just cannot code it in C++ ...i dont know what i am missing.

ok....

pi(0) = [1,0,0,0,0] that is a row vector.

n can be a number 1,2,3,..........i want it to increase.

now...pi(n) = pi(n-1) * 3
pi(1) = pi(1-1) * 3 = pi(0) * 3
pi(2) = pi(2-1) * 3 = Pi(1) * 3
pi(3) = pi(3-1) * 3 = Pi(2) * 3
pi(4) = pi(4-1) * 3 = Pi(3) * 3

I want n to keep increasing like that for a certain number of times so i can get a few results of
pi(n)

Thank you

0

Commented:
Yes. I undestand the iterative procedure to generate pi(n) from pi(n-1).

What I don't understand is how to multiply a 'row vector' by the scalar constant 3.

All I can think of is, say pi(1) = [3,0,0,0,0], pi(2) = [9,0,0,0,0] etc. Which doesn't need a program to do anything clever with.....

So, I request again... what are the actual values of pi(1), pi(2) etc.?
0

Featured Post

• 5
• 3
Tackle projects and never again get stuck behind a technical roadblock.