Hello everyone,
I am new to C++ and I am taking an online class in programming C++ and I am completely lost in this chapter. I have been working on a problem for the past week and cannot seem to make any progress. If someone could please tell what I am doing wrong I would greatly appreciate it. I have attached my pseudocode and the source file. I know there is more than one error. The first problem I have is that I should only be able to input 15 numbers, but I am able to input 16. I am not sure how to write the code to subtract one on the array. My second problem is how to convert the "for i = 1 to counter" line to C++. Thanks for your help in advance.
Design the logic for a program that allows a user to enter 15 numbers, then display each number and it's difference from the numeric average of the numbers entered.
Start
Declarations
num SIZE = 15
num numbers[SIZE]
num counter = 0
num total = 0
num average = 0
num diffFromAvg = 0
num SENTINEL = -1
output "Please enter a positive number: "
input numbers[counter]
while ((counter < 15) AND (numbers[counter] <> SENTINEL))
counter = counter + 1
total = total + numbers[counter]
output "Please enter a positive number: "
input numbers[counter]
endwhile
if (counter > 0) then
average = total/counter
for i = 1 to counter
diffFromAvg = numbers[i] - average
output "Number[",i,"]: ",numbers[i]," Difference from Average is ",diffFromAvg
endfor
else
output "Processing incomplete. No values in the array."
endif
Stop
#include <iostream>
#include <string>
usingnamespace std;
int main()
{
constint SIZE = 15;
int numbers[SIZE];
int counter = 0;
int total = 0;
int average = 0;
int diffFromAvg = 0;
constint SENTINEL = -1;
cout << "Please enter a positive number or " << SENTINEL << " to quit: ";
cin >> numbers[counter];
while((counter < 15) && (numbers[counter] != SENTINEL))
{
counter++;
total = total + numbers[counter];
cout << "Please enter a postive number or " << SENTINEL << " to quit: ";
cin >> numbers[counter];
}
if(counter > 0)
{
average = total/counter;
for (int i = 1; i < SIZE; i++)
{
diffFromAvg = numbers[i] - average;
cout << "Number[ " << i << " ]: " << numbers[i] << " Difference from Average is " << diffFromAvg << endl;
}
}
else
{
cout << "Processing incomplete. No values in the array." << endl;
}
return 0;
}
Thank you for your reply. I noticed the change with total. If you take more code and compile it, it does nothing that it is suppose to. I am so lost right now. I would pull my hair out but I have none left!!!!