C++ Highest and Lowest array value.

Feb 6, 2016 at 9:13pm
This is what I'm required to write: "Write a program that allows the user to enter seven double values representing store sales for each day of one week. After all seven values are entered, display a nicely formatted sales summary report showing each day's sales along with the highest and lowest sales values. At the end of the summary, show the total weekly sales and the average daily sales."

***When you input numbers with decimals the program can not determine which is the highest and lowest value please help.***


#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
double valueArray[7];
float total = 0;
float dailyAverage = 0;
float max = 0;
float min = LONG_MAX;

cout << "Enter the seven daily sales amounts for this week: \n\n";
for (int i=0; i<7; i++)
{
cin >> valueArray[i];
total += valueArray[i];
if (valueArray[i] > max)
max = valueArray[i];
if (valueArray[i] < min)
min = valueArray[i];
}

cout << setprecision(2) << fixed;
cout << "Sales summary\n-------------\n\n";

for( int i = 0; i < valueArray[i]; i++)
{
cout << "Day " << i+1 << ":" << setw(12) << valueArray[i];
if (valueArray[i] == max){
cout << "***highest***";
}
if (valueArray[i] == min){
cout <<"***lowest***";
}
cout << endl;
}

for (double i = 0; i < 7; i++)
total += valueArray[7];
cout << " Weekly total: " << total << "\n";
dailyAverage = total / 7;
cout << "Daily average: " << dailyAverage << "\n";


return 0;
}
Feb 6, 2016 at 9:45pm
1
2
3
4
5
double valueArray[7]; 
float total = 0; 
float dailyAverage = 0; 
float max = 0; 
float min = LONG_MAX; 

You are mixing floats and doubles, and I think that this is a source of your problem.
Change all variables to double.

for( int i = 0; i < valueArray[i]; i++)
maybe for (int i = 0; i < 7; i++)?

1
2
for (double i = 0; i < 7; i++)
        total += valueArray[7];

vallueArray[i] maybe?
i should be an integer, it's not clever to use double counter in a loop.
And haven't you already calculated total in a first for loop?
Feb 6, 2016 at 10:51pm
Thank you so much, mixing floats and doubles was my problem. And i didn't realize I had already calculated total, I became a little frustrated trying to figure out why it wouldn't display the highest and lowest values that I didn't pay attention to that extra line of code. Now I'm having a different problem, I have to submit my program to a website and it gives me this error.

WeeklySales.cpp: In function 'int main()':
WeeklySales.cpp:12:16: error: 'LONG_MAX' was not declared in this scope
double min = LONG_MAX;
^
I don't know why that is because when I run the program on my computer I don't get any errors. Anyway that's a different problem, once again thank you for helping me.
Feb 6, 2016 at 11:21pm
Please edit your first post and place the code in code tags!
Feb 7, 2016 at 5:13am
I've solved that problem on my computer by including <limits.h>.
Also, you can avoid that:

1
2
3
4
5
6
7
8
9
10
11
12
cout << "Enter the seven daily sales amounts for this week: \n\n";
cin >> valueArray[0]; // read first element of array outside the loop
double max = valueArray[0], min = valueArray[0]; // initialize min and max
for (int i=1; i<7; i++) // read rest of the elements (note: i = 1)
{
    cin >> valueArray[i];
    total += valueArray[i];
    if (valueArray[i] > max)
        max = valueArray[i];
    if (valueArray[i] < min)
        min = valueArray[i];
}
Topic archived. No new replies allowed.