help displaying 2 same answers from array

Write your question here.
I am a student who is new to c++ and wondering if there was a way to get my program to display more than 1 answer if they are the same. I am writing a c++ program for a class,using a two dimensional array, as well as several others to track sales amount at a car company. At the end of the program, the salesperson with the most and least amounts as well as the most amount of cars and least amount of cars are displayed. I have the program running mostly correctly, only problem I am having is getting it to display if one of the results has two answers.

My input data is as follows
<pre>
Car S A L E S P E R S O N
Model 1 2 3 4 5 6 7 8 Total Average
________________________________________________________________________________________
1 8 5 2 4 1 1 1 3 ?? ??
2 5 7 2 2 3 4 3 1 ?? ??
3 5 1 1 0 0 0 5 5 ?? ??
4 2 3 2 1 2 2 2 3 ?? ??
5 7 2 0 0 0 1 4 1 ?? ??
6 2 2 2 2 3 4 3 2 ?? ??
7 2 1 2 3 3 1 2 1 ?? ??
8 5 8 1 1 2 1 2 1 ?? ??
9 4 8 2 1 1 2 3 2 ?? ??
10 1 4 4 4 2 4 2 4 ?? ??
__________________________________________________________________________
Total ?? ?? ?? ?? ?? ?? ?? ?? ??
Average ?? ?? ?? ?? ?? ?? ?? ?? ??
</pre>

based on the data, car models 5 and 7 sold the least , so 2 answers need to be displayed.
based on the data sales people 1 and 2 sold the most so again there needs to be 2 answers displayed.

would anyone be able to advise me or give me a little help with this part? thanks in advanced


----------

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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
  
    // Lab09car.cpp : This file contains the 'main' function. Program execution begins and ends there.
    //
    
    #include <iostream>
    #include <iomanip>
    #include <fstream>
    using namespace std;
    
    int main()
    {
    	ifstream InFile;
    	int CarModel;
    	int NumCarModels = 10;
    	double CarModelSum[10], SalesPersonSum[8];
    	double CarModelAvg[10], SalesPersonAvg[8];
    	double CarModelAverage;
    	int SalesPerson;
    	int NumSalesPerson = 8;
    	int CarModelCount = 0;
    	int MaxModel, MinModel, MaxSalesPerson, MinSalesPerson;
    	InFile.open("E:/CSC133-01/CSC133LabAssignments/Lab09/Lab09inp.dat");
    	double CarSales[12][10];
    	int TotalTotals{};
    	double TotalAverages{};
    
    	cout << "\n________________________________________________________________________________________" << endl;
    	cout << "\n \t\t Automobile Sales Report For The Month Of June";
    	cout << "\n \t\t __________________________________________________";
    	cout << "\n \t\t Report Prepared By: " << endl;
    	cout << "\n Car \t\t\t\t SALESPERSON";
    	cout << "\n Model \t 1\t 2\t 3\t 4\t 5\t 6\t 7\t 8\t Total\t Average";
    	cout << "\n________________________________________________________________________________________" << endl;
    
    
    	//reads data into matrix from file
    	for (CarModel = 0;CarModel < NumCarModels; CarModel++)
    		{
    			for (SalesPerson = 0;SalesPerson < NumSalesPerson;SalesPerson++)
    			{
    				InFile >> CarSales[CarModel][SalesPerson];
    			}
    		}
    
    	for (int i = 0;i < 10;i++) 
    	{
    		CarModelSum[i] = 0;
    		for (int j = 0;j < 8;j++)
    			CarModelSum[i] += CarSales[i][j];
    			CarModelAvg[i] = (CarModelSum[i] * 1.0) / 8;
    	}
    
    	for (int i = 0;i < 8;i++) 
    	{
    		SalesPersonSum[i] = 0;
    		for (int j = 0;j < 10;j++)
    		SalesPersonSum[i] += CarSales[j][i];
    		SalesPersonAvg[i] = (SalesPersonSum[i] * 1.0) / 10;
    	}
    	for (int i = 0;i < 10;i++)
    	{
    		cout << "\n " << (i + 1);
    		for (int j = 0;j < 8;j++)
    		cout<<setprecision(0) << "\t " << CarSales[i][j];
    		cout << " \t " << CarModelSum[i] << "\t " << fixed << setprecision(2) << CarModelAvg[i];
    	}
    	cout << "\n________________________________________________________________________________________" << endl;
    	cout << "\n Total ";
    	for (int i = 0;i < 8;i++) 
    	{
    		cout << "\t " <<setprecision(0)<< SalesPersonSum[i];
    	}
    	for (int i = 0;i < 8;i++)
    	{
    		TotalTotals += SalesPersonSum[i];
    	}
    	cout << "\t" << setw(4)<< TotalTotals;
    	cout << "\n Average ";
    	for (int i = 0;i < 8;i++)
    	{
    		cout << fixed << setprecision(2) << SalesPersonAvg[i] << "\t ";
    	}
    	for (int i = 0;i < 8;i++)
    	{
    		TotalAverages += SalesPersonAvg[i];
    	}
    	TotalAverages = TotalAverages / 8;
    	cout << "\t"<<setw(5)<<TotalAverages;
    
    	MaxModel =0, MinModel = 0;
    	for (int i = 1;i < 10;i++)
    	{
    		if (CarModelSum[i] > CarModelSum[MaxModel])
    			MaxModel = i;
    		if (CarModelSum[i] < CarModelSum[MinModel])
    			MinModel = i;
    	}
    	MaxSalesPerson = MinSalesPerson = 0;
    	for (int i = 1;i < 8;i++)
    	{
    		if (SalesPersonSum[i] > SalesPersonSum[MaxSalesPerson])
    			MaxSalesPerson = i;
    		if (SalesPersonSum[i] < SalesPersonSum[MinSalesPerson])
    			MinSalesPerson = i;
    	}
    	cout << "\n________________________________________________________________________________________" << endl;
    	cout <<setprecision(0)<< "\n The Car Model that sold the most number of cars is the Model " << (MaxModel + 1) << ". There were " << CarModelSum[MaxModel] << " sold";
    	cout << "\n The Car Model that sold the least number of cars is the Model " << (MinModel + 1) << ". There were " << CarModelSum[MinModel] << " sold";
    	cout << "\n The Salesperson who sold the most number of cars is " << (MaxSalesPerson + 1) << ". That person sold " << SalesPersonSum[MaxSalesPerson] << " cars";
    	cout << "\n The Salesperson who sold the least number of cars is " << (MinSalesPerson + 1) << ". That person sold " << SalesPersonSum[MinSalesPerson] << " cars";
    	cout << "\n________________________________________________________________________________________" << endl;
    }
Last edited on
Once you have found the two end points one way is to query the data again with the relevant sales figure and list all people with the same results.
thanks for your help. how would i go about doing that. sorry, just a little overloaded at the moment, cant see things im missing
slow down and think it through.
if you had 5 values in an array, 10,9,1,8,2
and you want the two lowest, how would you do it?

int low, low2;
low = array[0];
low2 = array[1];
if(low > low2)
swap(low, low2);
for(dx = 2; dx < 5; dx++) // skip first elements, we already handled them in the init.
{
if array[dx] is less than low, then low2 = low and low = array[dx].
else
if array[dx] is less than low2, (implied, not less than low) then low2 = array[dx];
}

that can be cleaned up of course, but do you see?
you can use pointers or array index to keep track of what you want if its more than one integer; for your problem I think you want the row in the 2-d array where the data lives for the 2 values you were seeking.


you can use a similar idea to get the biggest and smallest ones. The point is you just loop over the data, find what you want, and keep updating the 2 values until its done, and at the end, the 2 values give you the answers.
Last edited on
how would i go about doing that
In pseudocode I had in mind the following:

Find the maximum and minimum as you have done.

Then,

Loop:
For each salesperson(n) n = 0 ... 8
Are sales for person n the same as the maximum (or minimum)?
Output person n, results blah, blah
Loop

This will work for any number of duplicated results
thank you all for your help and advice. i was able to get the desired results
Registered users can post here. Sign in or register to post.