C++ While Loop with Arrays

  • Comp Sci
  • Thread starter Biosyn
  • Start date
  • #1
113
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.
 

Answers and Replies

  • #2
11,872
5,526
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
113
0
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
11,872
5,526
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
296
1
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
11,872
5,526
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 Threads on C++ While Loop with Arrays

Replies
8
Views
2K
  • Last Post
Replies
2
Views
2K
  • Last Post
Replies
7
Views
1K
Replies
4
Views
838
Replies
0
Views
679
  • Last Post
Replies
1
Views
2K
  • Last Post
Replies
2
Views
636
  • Last Post
Replies
6
Views
4K
  • Last Post
Replies
15
Views
5K
Replies
5
Views
1K
Top