Dynamic Array conversion

For this assignment I need to rewrite the following program and use a dynamic
array of 10 components of type int to store the number of cars sold by each salesperson.

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
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
 int cars[10];
 int sum = 0;
 int max;
 int salesPerson;
 ifstream inFile;
 inFile.open("cars.txt");
 for (int j = 0; j < 10; j++)
 {
 inFile >> cars[j];
 sum = sum + cars[j];
 }
 cout << "The total number of cars sold = " << sum << endl;
 max = cars[0];
 salesPerson = 1;
 for (int j = 1; j < 10; j++)
 if (max < cars[j])
 {
 max = cars[j];
 salesPerson = j+1;
 }
 cout << "The salesperson selling the maximum number of cars is salesperson "
 << salesPerson << endl;
 cout << "Salesperson " << salesPerson << " sold "
 << max << " cars last month." << endl;
 cout << endl;
 inFile.close();
 return 0;
}


I have tried to get a dynamic array to work but the output is incorrect every time. Here is the input file and expected output.

cars.txt:
5 6 11 8 3 4 1 7 2 5

OUTPUT:
The total number of cars sold = 52
The salesperson selling the maximum number of cars is salesperson 3
Salesperson 3 sold 11 cars last month.

Any input is greatly appreciated!
-J
Last edited on
Hello WakelessFoil,
There are only 2 changes need in the program. 1 is in line 6 to use "new" to create a dynamic array. and the other it to add "delete" before the end of the program, (before the return), to free the memory and avoid a memory leak.

Andy
You actually don't need to use an array or dynamic memory for this. Also, there is no checking that the file has opened OK and there is no checking that the data has been read OK from the file

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
#include <iostream>
#include <fstream>
using namespace std;

int main()
{
	ifstream inFile("cars.txt");

	if (!inFile.is_open()) {
		cout << "Cannot open file\n";
		return 1;
	}

	int sum = 0;
	int maxsold = -1;
	int salesPerson = -1;

	for (int sold = 0, j = 1; inFile >> sold; ++j) {
		sum += sold;

		if (sold > maxsold) {
			maxsold = sold;
			salesPerson = j;
		}
	}

	cout << "The total number of cars sold = " << sum << endl;
	cout << "The salesperson selling the maximum number of cars is salesperson " << salesPerson << endl;
	cout << "Salesperson " << salesPerson << " sold " << maxsold << " cars last month." << endl;
}


However, based upon the original code and using dynamic memory, consider:

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
#include <iostream>
#include <fstream>
using namespace std;

int main()
{
	const int max_cars = 10;

	ifstream inFile("cars.txt");

	if (!inFile.is_open()) {
		cout << "Cannot open file\n";
		return 1;
	}

	int* cars = new int[max_cars] {};
	int sum = 0;
	int noread = 0;

	for (; noread < max_cars && inFile >> cars[noread]; ++noread)
		sum += cars[noread];

	cout << "The total number of cars sold = " << sum << endl;

	int max = cars[0];
	int salesPerson = 1;

	for (int j = 1; j < noread; ++j)
		if (max < cars[j]) {
			max = cars[j];
			salesPerson = j + 1;
		}

	cout << "The salesperson selling the maximum number of cars is salesperson " << salesPerson << endl;
	cout << "Salesperson " << salesPerson << " sold " << max << " cars last month." << endl;

	delete[] cars;
}



The total number of cars sold = 52
The salesperson selling the maximum number of cars is salesperson 3
Salesperson 3 sold 11 cars last month.

Topic archived. No new replies allowed.