Hey there guys, I have a problem with a program i am making.
I will show you the basic problem.
int x[50];
for(int y=0; y<51; y++;)
{
x[y]=y; //this is the first problem. I can't define which element of the array
//x[50] with a variable, y.
//Now i have the second problem. How can I add all the elements of
//x[50] together?
//Is this use of accumulate correct?
int z=accumulate(x[0], x[50], 0);
cout<<z<<endl;
First of all if you have defined an array as x[50] then its subscriptors are in the range 0 - 49. So the loop for(int y=0; y<51; y++;) is incorrect. Also you have placed a semicolon after y++. It is a syntax error.
Shall be
for ( int i = 0; i < 50; i++ )
And do not use name y as an index. It is a bad style. It is better to use for indexes names as i, j, k, l, m, n.
As for calculation of the sum then you can write
int sum = std::accumulate( std::begin( x ), std::end( x ), 0 );
Do not forget to include header <iterator> and <numeric>
Ok thank you, I wrote this code very quickly. What about defining an element with a variable?
ex:
x[y]=y;
It gives me an error, and I need to do it for another program I am writing.
The only issue I see here is the semi-colon after y++. You can name your variables in anyway you please. For instance, you could name a doublemy_house_consists_of_bricks if you wanted to; there's nothing stopping you. Don't let anyone tell you otherwise.
for(int addtodenominator=1; addtodenominator<101; addtodenominator+=2)
{
int count1= count/2;
int count2=count/2;
int x[50];
if(count1==count2){
x[count]=(-1/addtodenominator);
}
if(count1!=count2){
x[count]=1/addtodenominator; //It gives me the error "Array subscript
//is not an integer."