By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,389 Members | 1,844 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,389 IT Pros & Developers. It's quick & easy.

help with array

P: 40
hi all;

I need help with this simple program;

I was asked to declare an array of int type called value with 5 elements [ 2,4,6,8,10] . I want to use while structre to change the value of each element in the array to be the square of its original value. I made this code, but its not working,

# include < iostream >

int main()
{
int value []= { 2,4,6,8,10 };
int i=0 ,i++;

while ( i<5 )
{

std::cout<< value [i] * value [i]<<std::endl;
}

return 0;


}


thanks in advance
Oct 4 '06 #1
Share this Question
Share on Google+
3 Replies


P: 11
The problem is in the statement
int i=0 ,i++;


The correct code is given below

# include < iostream >

int main()
{
int value []= { 2,4,6,8,10 };
int i=0;
while ( i<5 )
{
std::cout<< value [i] * value [i]<<std::endl;
i++;
}
return 0;
}

The increment should be inside the while statement.
Oct 4 '06 #2

P: 40
thanks a million for your fast respond.
Oct 4 '06 #3

100+
P: 293
D_C
Actually, he said he wanted to change each value in the array to it's square. The above program outputs the square value, but doesn't change it in the array.
Expand|Select|Wrap|Line Numbers
  1. # include < iostream >
  2.  
  3. int main()
  4. {
  5.   int value []= { 2,4,6,8,10 };
  6.   int i=0;
  7.   while ( i<5 )
  8.   {
  9.     value[i] *= value[i];
  10.     std::cout << value[i] <<std::endl;
  11.     i++;
  12.   }
  13. return 0;
  14. }
Oct 4 '06 #4

Post your reply

Sign in to post your reply or Sign up for a free account.