array

Hello guys,
This assignment ask for input the file, calculate sum, find the highest, sort, etc ., of array. I stuck on output function, which the professor required the placement 2 for the competitor. After calculate the sum of rep for workout 2(which is given in the data source), I need to place the highest one #1, the next highest one is #2 and so on. I know I need to do a loop somewhere, but i don't know where :). please help me out


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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;

#define MAX 1000

//Declare functions
int getData(ifstream& inFile, string[], int[], int[], int[], int []);
int highestRep (int sum[], int number);
void sortData (int rep2[], int number, string names[], int place1[], int rep1[], int sum[]);
void outputHighest (string names[], int sum[], int number);
void outputHeading ();
void output (string names[],int place1[], int rep1[], int rep2[], int sum[], int number);
int main()
{
	//Declare variables
	ifstream inFile;
	string names[MAX];
	int place1[MAX];
	int rep1[MAX];
	int rep2[MAX];
	int sum[MAX];
	int number;
	
	//Open file
	inFile.open("data7.txt");
	if (inFile.fail())
    {
	cout << "No such file" << endl;
	system ("pause");
	exit (100);
    }

	//Input data
	number = getData(inFile, names, place1, rep1, rep2, sum);

	//Output competitor with highest amount of reps
	outputHighest ( names,  sum,  number);
	
	//Sort Data
	sortData ( rep2,  number,  names,  place1,  rep1,  sum);

	//Output
	 outputHeading ();
	 output (names, place1, rep1, rep2, sum,  number);
	
	 inFile.close ();
	system("pause");
	return 0;
}//main
////////////////////////////////////////////
int getData(ifstream& inFile, string names[], int place1[], int rep1[], int rep2[], int sum[])
{
	int count = 0;

	//getline(inFile, names[0]);
	string first, last;
	int num;
	int s;
	while(count < MAX && !inFile.eof())
	{
		inFile >> first >> last;
		names[count]=first + " " + last;
		cout << names[count] << endl;
	
		inFile >> place1[count] >> rep1[count];
		s = 0;
		for (int i=0; i<12; i++)
		{
			inFile >> num;
			s += num;
		}
		rep2[count] = s;

		for (int j = 0; j < 12 ; j++)
		{	
			sum[count] = rep1[count] + rep2[count];
		}
		cout << place1[count] << " " << rep1[count] << " " << rep2[count] << " " << sum [count] << endl;
		count++;
	}
	return count;
}//getData 
/////////////////////////////////////

int highestRep (int sum[], int number)
{
	int highest = sum[0];
	
	for (int i = 1; i < number; i++)
	{
		if (sum[i] > sum [highest])
			highest = i;
		
	}

	return highest;
}//highest rep competitor
/////////////////////////////////////////////////

void sortData (int rep2[], int number, string names[], int place1[], int rep1[], int sum[])
{
	int current, walker, maxIndex, temp;
	string tempS;

	for (current = 0; current < number -1; current++)
	{
		maxIndex = current;
		for (walker = current; walker < number; walker++)
		{
			if (rep2[walker] > rep2[maxIndex])
				maxIndex = walker;
		}//for walker

		temp = rep2[current];
		rep2[current] = rep2[maxIndex];
		rep2[maxIndex] = temp;

		tempS = names[current];
		names[current] = names[maxIndex];
		names[maxIndex] = tempS;

		temp = place1[current];
		place1[current] = place1[maxIndex];
		place1[maxIndex] = temp;

		temp = rep1[current];
		rep1[current] = rep1[maxIndex];
		rep1[maxIndex] = temp;

		temp = sum[current];
		sum[current] = sum[maxIndex];
		sum[maxIndex] = temp;
	} 
	return;
}
////////////////////////////////////////////////////////
void outputHighest (string names[], int sum[], int number)
{
	int highest;
	highest = highestRep(sum, number);
	cout << "Competitor with highest rep from both workout: " << names[highest] <<endl;
	cout << "Totoal number of reps: " << sum[highest]<<endl;
return;
}
//////////////////////////////////////////////////////////////////

void outputHeading ()
{
	cout << setw(5) << "Name" << setw(30) << "Workout 2" << setw(13) << "Workout 1 " << setw(10) << "Placement 2" << setw(20) << "Combined Placement" <<endl;
return;
}
//////////////////////////////////////////////////////////////////////
	
void output (string names[],int place1[], int rep1[], int rep2[], int sum[], int number)
{

	
	for (int i = 0; i < number ; i++)
	{
		
		cout <<left << setw(25)<<names[i]<<right << setw(10) << rep2[i] << setw(10) << right << place1[i] << setw(10) << right << rep2[i] << setw(10) << right << sum[i] << endl;
		
	}

	return;
}

/*example output:
  Name                   Total Rep   Placement 1   Placement 2   Combined
                          Workout 2                              Placement 
Valerie Calhoun            312          83           1             84
Alessandra Pichelli        271          33           2             35
Chyna Cho                  261          27           3             30
...*/
Topic archived. No new replies allowed.