Arrays Highest/Smallest

Hey guys back for some more help :)

I'm onto Arrays and I'm meant to print out the highest array and its value
so I'm just wondering how thats coded.

I'm asking for a solution I guess and an explanation behind it.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include<iostream>
using namespace std;
int main ()
{double Department[10], sales, totsales=0, salesAVG, SalesDiff, LowSale, HighSale;
 int Deptno;
 
     //Setting Deptno to 0
     for(Deptno=0; Deptno<10; Deptno++)
          Department[Deptno] = 0.0;

     //Storing sales to Department
     for(Deptno=0; Deptno<10; Deptno++)
         {
          cout << "\n Enter sales of Department " << Deptno+1;
          cin >> sales;
          Department[Deptno]=sales;
          totsales= totsales + sales;
         }
         
     //Print out sales of each Department
     for(Deptno=0; Deptno<10; Deptno++)
     {
       cout << "\n Sales for department "<< Deptno+1<<"="<< Department[Deptno];
     }
     
     // Print average sales
     
     salesAVG = totsales/10;
     cout<< "\n Sales for each department= " << salesAVG <<endl;
     
     //Random access
     cout<< "\n Enter department number 1-10 ";
     cin >> Deptno;
     cout << "\n Sales for department " << Deptno << " = " << Department[Deptno-1] << endl;
     
     // Difference in sales
     for (Deptno=0;Deptno<10;Deptno++)
     {
         cout << "\n Department " << Deptno+1 << " Sales = " << Department[Deptno]
              //This line is continuing
              <<" Difference= "<< Department[Deptno]-salesAVG << endl;
     }
     
     //Print Department with the highest sales and the sales
     for (Deptno=0; Deptno<10; Deptno++)
     {
         HighSale=Department[Deptno-1] >
     
system("pause");
return 0;
}


Thanks in advance :)
try to compare each array :
1
2
3
4
5
6
7
8
9
10
11
double highest = Department[0] /*assume that the first array's value is the highest/*

/*then u can compare whether the next array is higher or not, until the last array.*/
for(std::size_t index = 1 ; index < 10; index++)
{ 
   if(Department[index] > highest)
   highest = Department[index];
}

std::cout<<"the highest value is = "<<highest; //print highest value;
Awesome thanks for that :D
It was the solution I needed. Well I had a vague idea but it was making it run in a circle so thanks again :D

Vote+ Sendy Hipo
Topic archived. No new replies allowed.