I am creating a program to create an array of integers, read in numbers inputted by the user until the user enters a certain number.
I would like to output the number of values entered which I think I accomplished below but I also need to sum the number of values entered which is where I am stumped.
const int SIZE = 100;
int count = 0;
int Array[SIZE];
for (int i = 0; i <= SIZE; i++)
{
count++;
cout << "Enter numbers followed by -1 when you are done " << ": ";
cin >> Array[i];
cout<<count-1;
int sum = 0; //THIS PART where I am Stuck
sum = sum + Array[i];
cout << "sum is" << sum;
#include <iostream>
usingnamespace std;
constint SIZE = 100;
int count = 0
int main ()
{
int Array[SIZE], temp;
cout << "Enter numbers followed by -1 when you are done;
cin>> temp;/// i'll use a temporal variable to check if value is equal to -1
while (count <100&& temp!=-1)
{
Array [i]=temp;
cin>> temp;
}
///use a loop display the values
}
// Example program
#include <iostream>
#include <string>
usingnamespace std;
int main()
{
constint SIZE = 100;
int count = 0;
int sum = 0;
int Array[SIZE];
for (int i = 0; i <= SIZE; i++)
{
count++;
cout << "Enter numbers followed by -1 when you are done " << ": ";
cin >> Array[i];
if(Array[i] > 0)
{
sum = sum + Array[i];
}
cout<<count-1 << endl;;
if (Array[i] == -1)
break;
}
cout << "Total numbers entered is: " << sum << endl;
}
Remember to declare
sum
before using it.
if you do this the result will be incorrect since you add -1 to sum. That's why i used if statement above.