C++ While Loop with Arrays

In summary, the OP attempted to solve a problem involving arrays, but did not provide a solution. He included code to check if an array element was equal to 42, but if it wasn't, the code added that element to a sum. If the sum reached the size of the array, the program would stop. He compared his solution to another user's, which was better but used an if statement that would fall off the end of the world if there wasn't a 42 in the array. He provided a solution that uses sizeof to check the size of the array and a while loop that will only add an element to the sum if that element isn't already in the sum.
  • #1
Biosyn
115
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
  • #2
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
}
 
  • #3
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;
}
 
  • #4
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
 
  • #5
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++
 
  • #6
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.
 

Related to C++ While Loop with Arrays

1. What is a while loop in C++?

A while loop is a control flow statement in C++ that allows a block of code to be executed repeatedly as long as a given condition is true. It consists of a condition and a block of code, and the code will continue to execute as long as the condition remains true.

2. How do I use a while loop with arrays in C++?

To use a while loop with arrays in C++, you can first define an array and then use a while loop to iterate through the array. The condition of the while loop can be set to check for the length of the array, so the loop will continue to execute until it reaches the end of the array.

3. Can I use a while loop to search for a specific element in an array in C++?

Yes, you can use a while loop to search for a specific element in an array in C++. You can set the condition of the while loop to check for the desired element, and use an index variable to keep track of the current position in the array. The loop will continue to execute until the desired element is found or until the end of the array is reached.

4. How do I avoid an infinite loop when using a while loop with arrays in C++?

To avoid an infinite loop when using a while loop with arrays in C++, it is important to ensure that the condition of the loop will eventually become false. This can be achieved by using a counter variable to keep track of the number of iterations, or by setting a condition that will eventually be met to end the loop.

5. Can I use a while loop with arrays to modify array elements in C++?

Yes, you can use a while loop with arrays to modify array elements in C++. You can use the loop to iterate through the array and use the index variable to access and modify each element as needed. Just make sure to update the condition of the loop accordingly to avoid any unintended infinite loops.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
3
Views
827
  • Engineering and Comp Sci Homework Help
Replies
8
Views
974
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
15
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
24
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
17
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
Back
Top