1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

C++ While Loop with Arrays

  1. Nov 15, 2012 #1
    1. The problem statement, all variables and given/known data
    DGQ9Y.jpg



    2. Relevant equations



    3. The attempt at a solution

    Code (Text):

    #include <iostream>
    #include <cstdlib>
    using namespace std;
    using std::cout;
     
     
    int main()
    {
     
        int sum=0;
     
       int Array[] = {1,2,3,4,42,5,6};
       // int Array[] = {42,1,2,3};
       // int Array[] = {12,-10,42,10};
       // int Array[] = {1,2,3,4,5,6,21};
     
     
        for (int a=0; a<7; a++)
     
        //while(Array[a] != 42)    
     
        {
            if(Array[a]==42)
            break;
                sum+=Array[a];
     
            }
     
     
            cout << sum << endl;
     
            system("PAUSE");
            return 0;
    }
     
    I managed to use an if statement to complete this problem. Not sure how to do it with a while loop. I commented out my attempt.
     
  2. jcsd
  3. Nov 16, 2012 #2

    jedishrfu

    Staff: Mentor

    remember a for loop can be written as a while loop

    for( init index; loop-if-true-expression; increment-index) { ...do stuff here... }

    and the while would be:

    init-index
    while(loop-if-true-expression) {
    ...do stuff here...
    increment-index
    }
     
  4. Nov 16, 2012 #3

    Thank you! I fixed my solution:

    Code (Text):

    #include <iostream>
    #include <cstdlib>
    using namespace std;
    using std::cout;


    int main()
    {
       
        int sum=0;
       
       int Array[] = {1,2,3,4,42,5,6};
       // int Array[] = {42,1,2,3};
      // int Array[] = {12,-10,42,10};
       // int Array[] = {1,2,3,4,5,6,21};
           
       
                           //for (int a=0; a<7; a++)
        int a=0;    
        while(Array[a] != 42)    
           
        {
                        //  if(Array[a]==42)
                        //  break;
                sum+=Array[a];
                a++;
           
            }
       
       
            cout << sum << endl;

            system("PAUSE");
            return 0;
    }
     
     
  5. Nov 16, 2012 #4

    jedishrfu

    Staff: Mentor

    you solution is much better but if there's no 42 then you program will fall of the end of the world ie run past the array into memory unknown.

    so adjust your while condition to be:

    while (a<Array.size() && Array[a]!=42)

    Also rename your array to something that doesn't look like a keyword or name of a class as an example myArray
     
  6. Nov 16, 2012 #5
    Arrays are not objects in C++
     
  7. Nov 16, 2012 #6

    jedishrfu

    Staff: Mentor

    Yes, thanks I know that but I wanted to give the OP a hint of what was missing in his while loop expression.

    He could use the sizeof myArray / sizeof(int) to get the number of elements.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: C++ While Loop with Arrays
  1. While loop (Replies: 1)

  2. C++- while-loop (Replies: 7)

Loading...