help with array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ayman723
    New Member
    • Sep 2006
    • 40

    help with array

    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
  • saravanakumar
    New Member
    • Sep 2006
    • 11

    #2
    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.

    Comment

    • ayman723
      New Member
      • Sep 2006
      • 40

      #3
      thanks a million for your fast respond.

      Comment

      • D_C
        Contributor
        • Jun 2006
        • 293

        #4
        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.
        Code:
        # include < iostream >
        
        int main()
        {
          int value []= { 2,4,6,8,10 };
          int i=0;
          while ( i<5 )
          {
            value[i] *= value[i];
            std::cout << value[i] <<std::endl;
            i++;
          }
        return 0;
        }

        Comment

        Working...