Re: Need help with arrays
How about this
Code:
#include <iostream>
using namespace std;
int main(){
double alpha[50];
int counter;
for(counter = 0; counter <= 24; counter++)
{
alpha[counter]=counter*counter;
}
for(counter = 25; counter <= 49; counter++)
{
alpha[counter]=3*counter;
}
for(counter = 0; counter <= 49; counter++)
{
cout << " ";
cout << alpha[counter];
}
}
Re: Need help with arrays
Thanks! Now I see where my problem was. It was a mistake I should have learned not to make by now.
The statement
square = alpha[counter]
makes no sense. You must have alpha[counter] before the assignment statement because it's evaluated from right to left and you want square's value to be copied into the array alpha.
The same is true for several other statements in my code.
Re: Need help with arrays
more compact version, behnood, to show u where u can save code lines
Code:
#include <iostream>
using namespace std;
int main () {
double alpha[50];
// i^2
for (int i = 0; i < 25; ++i) // note the < 25 this is the same as <= 24 but the more common way
alpha[i] = i * i;
// 3*i
for (int i = 25; i < 50; ++i)
alpha[i] = i * 3;
// print
for (int i = 0; i < 50; ++i)
printf ("%d\n", alpha[i]);
}