- #1
Biosyn
- 115
- 0
Homework Statement
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.