When I fill the array my program works, but if I use the sentinel to end the loop (in this case -1) the output gets messed. Can anyone see what I'm doing wrong?
#include <iostream> // for console input/output
#include <iomanip> // for setprecision
using namespace std;
int main()
{
const int NUM_ARR =10; //array
int num_ent[NUM_ARR]; // what numbers will be enters in
int count = 0;
int totalsum;
int avg_num, low_num, number;
int new_num[NUM_ARR];
cout<<fixed<<showpoint<<setprecision(5);
cout<<"Enter up to 10 numbers"<<endl;
cout<<"or enter -1 to quit"<<endl;
cout<<"Enter number:";
cin>>number;
for(int count = 0; count<NUM_ARR; count++) //to enter numbers into array for lowest number
{
if(new_num[count] < low_num)
low_num=new_num[count];
}
totalsum = 0;
for(int count = 0; count<NUM_ARR; count++) //to enter numbers into array for sum
{
totalsum += num_ent[count];
}
for(int count = 0; count<NUM_ARR; count++) //to enter numbers into array for average
{
avg_num =totalsum/NUM_ARR;
}
cout<<"The lowest number is:"<<low_num<<endl;
cout<<"The sum of all the numbers is:"<< totalsum<<endl;
cout<<"The average of all the numbers is"<< avg_num<<endl;
#include<iostream>
usingnamespace std;
int main() {
int array[10] = {0};
int element = 0;
int counter = 0;
int mega = 0;
int average = 0;
cout << "Enter up to 10 positive integers, to stop entering integers enter -1" << endl;
while (true) {
if ((cin >> element) && (element != -1)) {
array[counter] = element;
counter++;}
elseif (counter == 9) {
break;}
else {break;}}
for (int i = 0; i < (counter + 1); i++) {
mega += array[i];}
average = mega/counter;
cout << "The amount of elements entered was " << counter << endl;
cout << "The average was " << average;
while (true) {}
return 0;}
This version allows for decimal point averages, I think, lawl :
#include<iostream>
usingnamespace std;
int main() {
int array[10] = {0};
double element = 0;
int counter = 0;
double mega = 0;
double average = 0;
cout << "Enter up to 10 positive numbers, to stop entering numbers enter -1" << endl;
while (true) {
if ((cin >> element) && (element != -1)) {
array[counter] = element;
counter++;}
elseif (counter == 9) {
break;}
else {break;}}
for (int i = 0; i < (counter + 1); i++) {
mega += array[i];}
average = mega/counter;
cout << "The amount of elements entered was " << counter << endl;
cout << "The average was " << average;
while (true) {}
return 0;}