How to Use a While Loop with Arrays in C++?

  • Context: Comp Sci 
  • Thread starter Thread starter Biosyn
  • Start date Start date
  • Tags Tags
    Arrays C++ Loop
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
5 replies · 7K views
Biosyn
Messages
114
Reaction score
0

Homework Statement


DGQ9Y.jpg

Homework Equations


The Attempt at a Solution



Code:
#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.
 
Physics news on Phys.org
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
}
 
jedishrfu said:
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
}


Thank you! I fixed my solution:

Code:
#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;
}
 
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
 
jedishrfu said:
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

Arrays are not objects in C++
 
aralbrec said:
Arrays are not objects in C++

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.