In: Computer Science
Why is the output for LowestSales a negative number? I am using Visual Studio.
_______________________________________*_________________________________________
int main()
{
const int STORE = 10;
double Sales[STORE];//array for sales
double HighestSales = Sales[], LowestSales = Sales[];
//arrays to capture highest and lowest sales
double AverageSale = 0.00; //stores average sale
amount
for (int i = 0; i < 10; i++) // gather sales
from 10 stores
{
cout << "Enter Store "
<< (i + 1) << " daily sales: \n";
cin >> Sales[i]; //prompt the
user to enter the daily sales
int HighStoreNo = 0, LowStoreNo =
0; //initialize variables for high store number and low store
number
if (HighestSales > Sales[i]
&& (isdigit(Sales[i]) != 0) //check to see if this the
highest sale, and check for positive integer
{
HighestSales =
Sales[i]; //assign value to array member to store 'highest
sales'
HighStoreNo = i;
//if highest value, assign stored value
}
if (LowestSales <=
Sales[i] && (isdigit(Sales[i]) != 0); //assign value to
array member to store 'lowest sales'
LowestSales = Sales[i];
LowStoreNo = i; //if lowest value,
assign stored value
}
AverageSale += Sales[i]; //keep accumulating sales
total
}
cout << fixed << showpoint <<
setprecision(2);//sets output precision to two decimals
AverageSale = AverageSale/10; //calculate the average
sale by dividing by 10 stores
cout << "Highest Sale: " << HighestSales
<< ", Store number: " << (HighStoreNo + 1) <<
endl; //highest sale
cout << "Lowest Sale: " << LowestSales
<< ", Store number: " << (LowStoreNo + 1) <<
endl; //lowest sale
cout << "Average Sale: " << AverageSale
<< endl; //average sale
Code - corrected
#include <iostream>
#include <cctype>
#include <iomanip>
using namespace std;
int main()
{
const int STORE = 10;
double Sales[STORE];//array for sales
double HighestSales =0, LowestSales = 999999; //arrays to capture
highest and lowest sales
double AverageSale = 0.00; //stores average sale amount
int HighStoreNo = 0, LowStoreNo = 0; //initialize
variables for high store number and low store number
for (int i = 0; i < 10; i++) // gather sales from 10
stores
{
cout<< "Enter Store " << (i + 1) << " daily
sales: \n";
cin>> Sales[i]; //prompt the user to enter the daily
sales
HighStoreNo = 0;
LowStoreNo = 0; //initialize
variables for high store number and low store number
if(HighestSales < Sales[i] && Sales[i]>=0) //check
to see if this the highest sale, and check for positive
integer
{
HighestSales = Sales[i]; //assign value to array member to store
'highest sales'
HighStoreNo = i; //if highest value, assign stored value
}
if(LowestSales > Sales[i] && Sales[i]>=0){ //assign
value to array member to store 'lowest sales'
LowestSales = Sales[i];
LowStoreNo = i; //if lowest value, assign stored value
}
AverageSale += Sales[i]; //keep accumulating sales total
}
cout<< fixed << showpoint <<
setprecision(2);//sets output precision to two decimals
AverageSale = AverageSale/10; //calculate the average sale by
dividing by 10 stores
cout<< "Highest Sale: " << HighestSales << ",
Store number: " << (HighStoreNo + 1) << endl; //highest
sale
cout<< "Lowest Sale: " << LowestSales << ", Store
number: " << (LowStoreNo + 1) << endl; //lowest
sale
cout<< "Average Sale: " << AverageSale << endl;
//average sale
return 0;
}
Screenshots -
pls do give a like , thank you