#include <iostream>
#include <cstdlib>
usingnamespace std;
int main ()
{
int SIZE = 12;
int numbers[SIZE];
int value = 0 ;
int counter = 0;
int total = 0;
double average = 0;
double diffFromAvg = 0;
int SENTINEL =-1;
int i;
cout << "Please enter a positive number: " << endl;
cin >> value;
while ((counter < SIZE) and (value != SENTINEL))
{
total = total + value;
counter = counter + 1;
}
if (counter != SIZE)
{
cout<< "Please enter a postive number:" <<endl;
cin>>value;
}
if (counter > 0)
{
average= total/counter;
{
for (i==0; counter-1;)
{
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;
}
system ("PAUSE");
return 0;
#include <iostream>
#include <cstdlib>
usingnamespace std;
int main ()
{
int SIZE = 12;
int numbers[SIZE];
int value = 0 ;
int counter = 0;
int total = 0;
double average = 0;
double diffFromAvg = 0;
int SENTINEL =-1;
int i;
cout << "Please enter a positive number: " << endl;
cin >> value;
while ((counter < SIZE) and (value != SENTINEL))
{
total = total + value;
counter = counter + 1;
}
if (counter != SIZE)
{
cout<< "Please enter a postive number:" <<endl;
cin>>value;
}
if (counter > 0)
{ // ???
average= total/counter;
{ // ???
for (i==0; counter-1;)
{
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;
}
// your else has no IF
// You have no closing braces for lines 32 and 34
// Your opening brace on line 34 is not necessary in this case.
system ("PAUSE");
return 0;
}
Your else has no IF
You have no closing braces for lines 32 and 34.
Your opening brace on line 34 is not necessary in this case.
EDIT:for (i==0; counter-1;)
This is also wrong, and error prone.
= is for assignment, == is for equality.
I suggest doing: for (int i = 0; i < counter; i++)
and removing your i variable on line 16 (
EDIT 2:
Because size is used as the size of an array, it needs to be const or constexpr. constint SIZE = 12;
#include <iostream>
#include <cstdlib>
usingnamespace std;
int main ()
{
constint SIZE = 12;
int numbers[SIZE];
int value = 0 ;
int counter = 0;
int total = 0;
double average = 0;
double diffFromAvg = 0;
int SENTINEL = -1;
while ((counter < SIZE) and (value != SENTINEL))
{
cout << "Please enter a postive number:" << endl;
cin >> value;
total = total + value;
counter = counter + 1;
}
if (counter > 0)
{
average = total/counter;
for (int i = 0; i < counter; 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. No this does not involve rainfall. As far as far as the value of numbers [i] goes, i'm lost, been trying to figure that one out. Here is the pseudo code I was given,
// Start
// Declarations
// num SIZE = 12
// num numbers[SIZE]
// num value = 0
// num counter = 0
// num total = 0
// num average = 0
// num diffFromAvg = 0
// num SENTINEL = -1
//
// output "Please enter a positive number: "
// input value
// while ((counter < SIZE) AND (value <> SENTINEL) )
// total = total + value
// numbers[counter] = value
// counter = counter + 1
// if (counter <> SIZE)
// output "Please enter a positive number: "
// input value
// endif
// endwhile
//
// if (counter > 0) then
// average = total/counter
// for i = 0 to counter - 1
// 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