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
Click For Summary

Discussion Overview

The discussion focuses on using while loops with arrays in C++, particularly in the context of a homework problem. Participants explore how to convert a for loop into a while loop while addressing potential pitfalls in the implementation.

Discussion Character

  • Homework-related
  • Technical explanation
  • Conceptual clarification

Main Points Raised

  • One participant presents a solution using an if statement and seeks guidance on implementing a while loop instead.
  • Another participant explains the structural equivalence between for loops and while loops, providing a general template for conversion.
  • A later reply shares a revised solution using a while loop but notes a potential issue if the value 42 is not present in the array, suggesting a modification to the while condition.
  • Participants discuss the nature of arrays in C++, clarifying that they are not objects and suggesting the use of a different name for the array to avoid confusion with keywords.
  • One participant mentions using the sizeof operator to determine the number of elements in the array, indicating a method to avoid out-of-bounds access.

Areas of Agreement / Disagreement

Participants generally agree on the need to adjust the while loop condition to prevent accessing memory outside the array bounds. However, there is no consensus on the best naming conventions for arrays or the implications of treating arrays as objects.

Contextual Notes

Limitations include the assumption that the array contains the value 42 and the potential for undefined behavior if the while loop condition is not properly defined. The discussion also reflects varying interpretations of 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 8 ·
Replies
8
Views
2K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 17 ·
Replies
17
Views
2K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K