Couple of questions for an array problem

Hi! I'm currently in a beginning C++ working on a programming lab assignment. Spent the past few days working on this and there are just a few things I cannot for the life of me figure out. Essentially, I am reading an input file that contains information about university name, location, enrollment number, yearly tuition, % freshman retention, % graduates in 6 sixs, year. I have the program finished out but there are a few things that I cannot identify why my code won't do as needed. I know I am asking for a lot of help here.. I am just going mad trying to solve it myself for a few days - I'm not expecting direct answers to these questions but anything that might have my brain jog some new ideas or things I may be missing as I am trying to learn after all. Thanks a lot in advance!

2) In the function int lowTuit. I am looking for the universities for the lowest tuition. In the set of data I have; there are two universities with the same exact lowest tuition; therefore I should be outputting both universities since they both are equal in value for tuition. However, the program will only output the first one it encounters sequentially in order. How can I get the other university to output as well?

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
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <cctype>

using namespace std;

#define MAX 1000

int getData(ifstream& inFile, string uni[], string state[], string city[], double yearlytuit[], int enroll[], double null[], double fresh[], double gradsix[]);
void output(ofstream& outFile, string uni[], string state[], double yearlytuit[], int enroll[], double fresh[], double gradsix[], int number);
double avgTuit(double yearlytuit[], int number);
void max(double yearlytuit[], string uni[]);
void collegesearch(ofstream& outFile, string uni[], string state[], string city[], double yearlytuit[], int enroll[], double fresh[], double gradsix[], int number);
int lowTuit(string uni[], double yearlytuit[], int number);
void sortselect(ofstream& outFile, string uni[], string state[], double yearlytuit[], int enroll[], double fresh[], double gradsix[], int number);

int main()
{
	string uni[MAX], state[MAX], city[MAX];
	int enroll[MAX], number, loc;
	//Null here is actually percent of students accepted but will not be used in anything hence nickname of null
	double yearlytuit[MAX], null[MAX], fresh[MAX], gradsix[MAX];
	double avgTuition, lowest;
	ifstream inFile;
	ofstream outFile;
	
	inFile.open("universities.txt");
	outFile.open("unicomp.txt");

	if(inFile.fail())
	{
		cout << "File does not exist" << endl;
		exit(100);
	}
	
	number = getData(inFile, uni, state, city, yearlytuit, enroll, null, fresh, gradsix);
	output(outFile, uni, state, yearlytuit, enroll, fresh, gradsix, number);
	avgTuition = avgTuit(yearlytuit, number);
	cout << "The average tuition for all universities is: " << avgTuition << endl;
	max(yearlytuit, uni);
	collegesearch(outFile, uni, state, city, yearlytuit, enroll, fresh, gradsix, number);
	loc = lowTuit(uni, yearlytuit, number);
	cout << uni[loc] << yearlytuit[loc];
	sortselect(outFile, uni, state, yearlytuit, enroll, fresh, gradsix, number);
	outFile << "\nUniversity                            " << "State  "  << "Tuition  " << "Enrollment  " << "%Fresh    " << "%Graduate\n";
	outFile << "                                                                  " << "Succeed   " << "in six years\n"; 
	for (int i = 0; i < number; i ++)
	{
	outFile << left << setw(40) << uni[i] << setw(6) << state[i] << setw(10) << setprecision(2) << fixed << yearlytuit[i]; 
	outFile << right << setw(6) << enroll[i] << setw(10) << fresh[i] * 100 << "%" << setw(10) << gradsix[i] *100 << "%" << endl;
	}
	
}

int getData(ifstream& inFile, string uni[], string state[], string city[], double yearlytuit[], int enroll[], double null[], double fresh[], double gradsix[])
{
	int count = 0;
	while (count < MAX && !inFile.eof())
		{
			getline(inFile, uni[count]);
			inFile >> state[count];
			getline(inFile, city[count]);
			//cout << uni[count] << endl << state[count] << " " << city[count] << endl;
			inFile >> yearlytuit[count] >> enroll[count] >> null[count] >> fresh[count] >> gradsix[count];
			//cout << enroll[count] << " " << unk[count] << " " << yearlytuit[count] << " " << fresh[count] << " " << gradsix[count] << endl;
			string tempstr; getline(inFile, tempstr);
			
			count++;
		}
	return count;
}

void output(ofstream& outFile, string uni[], string state[], double yearlytuit[], int enroll[], double fresh[], double gradsix[], int number)
{
	int count = 0;
	while (count < number)
	{
		outFile << uni[count] << endl << state[count] << endl << yearlytuit[count] << " " << enroll[count] << " " << fresh[count] << " " << gradsix[count] << " " << endl; 
		count++;
	}
}

double avgTuit(double yearlytuit[], int number)
{
	double avg = 0; 
	for (int i = 0; i < number; i++)
	{
		avg = avg + yearlytuit[i];
	}
	avg = avg / number;
	return avg;
}

void max(double yearlytuit[], string uni[])
{
	double maxtuit;
	cout << "What is the maximum tuition you can afford? ";
	cin >> maxtuit;
	cout << "The colleges you can afford to attend are:\n";
	for (int i = 0; i < yearlytuit[i]; i++)
	{
		if (maxtuit > yearlytuit[i])
		{
			cout << uni[i] << endl;
		}
	}
}

void collegesearch(ofstream& outFile, string uni[], string state[], string city[], double yearlytuit[], int enroll[], double fresh[], double gradsix[], int number)
{
	string statename;
	
	cout << "Enter a two-letter abbreviation for a state for all information for colleges within that state: ";
	cin >> statename;
	for (int i = 0; i < number; i ++)
	{
		if (statename == state[i])
		{
			outFile << uni[i] << "\n" << state[i] << " " << city[i]<< "\n";
			outFile << yearlytuit[i] << " " << enroll[i] << " " << fresh[i] << " " << gradsix[i] << "\n";
		}
	}
}


int lowTuit(string uni[], double yearlytuit[], int number)
{
	int loc;
	string univ;
	double lowest = yearlytuit[0];
	for (int i = 0; i < number; i++)
	{
		if (lowest > yearlytuit[i])
		{
			lowest = yearlytuit[i];
			loc = i;
		}
	}
	return loc;
}

void sortselect(ofstream& outFile, string uni[], string state[], double yearlytuit[], int enroll[], double fresh[], double gradsix[], int number)
{
	int i; int current;
	int smallestEnroll, tempEnroll;
	
	for (current = 0; current < number - 1; current ++)
	{
		smallestEnroll = 0;
		for (i = current + 1; i < number; i++)
		{
			if (enroll[i] < enroll[smallestEnroll])
				smallestEnroll = i;
		}
	
	tempEnroll = enroll[current];
	enroll[current] = enroll[smallestEnroll];
	enroll[smallestEnroll] = tempEnroll;
	}

}


Last edited on
In your function collegesearch, create a bool; collegeFound. Make it false to begin.

If you find a college, set that variable to true.

At the end of the function, if that variable is still false, print out "No college found".
Ugh got it. I just utilized a bool flag in my last lab and I totally forgot about it. It works perfectly in this scenario, thank you!
Last edited on
2. Replace line 45 with a loop that goes through all universities and prints all universities whose tuition is yearlytuit[loc]

3. Line 151 should be smallestEnroll = current;. You want to assume that the current item has the smallest enrollment of the ones left. If you're wrong, you update it at line 154 & 155.

Also sortselect adjusts the enrollment array but not any of the others. So after executing sortselect(), the enroll[i] does not correspond to uni[i]. You need to sway the values in both arrays at lines 158-160. Note that even after fixing this, the other arrays will not correspond to enroll and uni.

Have you learned about structures or classes yet? They would make this whole assignment much easier.
@dhayden

Sheesh.. I can't believe I set that to 0. I have the correct sorting now and yeah after I realized I posted this I forgot to sort the other columns as well w/ each element of the array to match the output so I added those in there. I have not learned about structures/classes. Arrays is the last thing I am learning in this course before moving onto intermediate C++. These labs are just much more difficult than any of the text readings/assignments I've done prior so I find myself missing simple reasoning in my code...

The problem I have with the lowIntuit using that loop is that the function int lowTuit returns loc = i where i is the location in the array corresponding to the lowest tuition. I am assuming that can only return 1 value when in reality there are 2 "loc = i" spots in my array that are equal yet a function will only return one. I added this code in main to replace line 45
1
2
	for (int i = 0; i < number; i++)
	cout << uni[loc] << yearlytuit[loc];


But this will just print out again the first location of i over and over again which I assume is because it stops after finding the first location
Can anyone else provide a hint for my lowIntuit function?
Your lowTuit function gives back one number; the position in the array of a college with the lowest tuition.

That's no good to you. Either you've got to come up with a way to have it return more than one number, or do something different. Maybe you could have it return the actual value of the lowest tuition, and then you go through the array and output every college with that tuition value.
Unfortunately I have to return the location of the lowest tuition in the array as part of the question requirements as listed here

Write one and only one function to return the subscript of the university with the lowest tuition. There
is no output in this function. Do not assume a sorted array. In main(), the name(s) of the university or
universities with this low tuition and the amount of the tuition are output to the screen
I was able to figure it out although I'm not too sure I understand my own code and how it is working? I get the output as desired
1
2
3
4
5
6
	loc = lowTuit(uni, yearlytuit, number);
	for (int i = 0; i < number; i++)
	{
		if (yearlytuit[loc] == yearlytuit[i])
			cout << uni[i] << " " << yearlytuit[i] << endl;
	}


After finding loc from the lowTuit function; I added and if statement that goes through each element in the array and if it finds one that is equal to the loc that was calculated it will output that as well.

Does this seem correct?
The requirements are themselves contradictory;
"return the subscript of the university with the lowest tuition"
clearly implies there will be one and one only such university, but the rest of the requirement makes it clear that there could be more than one -
"the name(s) of the university or universities with this low tuition".


Shoddy, contradictory requirements are pretty standard in the industry. You've certainly met both of those requirements.
Last edited on
Does this seem correct?
Yes, that's the right way to do it.
Topic archived. No new replies allowed.