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

  • Thread starter Thread starter Biosyn
  • Start date Start date
  • Tags Tags
    Arrays C++ Loop
Click For Summary
The discussion focuses on using a while loop to sum elements in an array until a specific value (42) is encountered. The original code utilized a for loop, but the user sought to convert it into a while loop. A solution was provided, but it was noted that the while loop could lead to accessing out-of-bounds memory if 42 is not present. Suggestions included adjusting the while condition to check the array size and renaming the array to avoid conflicts with C++ keywords. The conversation emphasizes the importance of proper loop conditions and array handling in C++.
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.
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 8 ·
Replies
8
Views
1K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 17 ·
Replies
17
Views
2K
  • · Replies 15 ·
Replies
15
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 24 ·
Replies
24
Views
2K