mrperfect75000
asked on
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.
PLease help..... what am i doing wrong?
#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;
}
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.
PLease help..... what am i doing wrong?
#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;
}
ASKER
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
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..
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..
ASKER
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
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
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?
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?
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
#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
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.
ASKER
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
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
Use new instead.
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?