How do I get the expected outputs for this following program?

CSCI 14

Assignment 9, Due Dec 2

Design and implement a program that allows the user to keep track of students in a college course. The program will function as a simple database in which students can be inserted, removed and updated. Additionally, the user can display students in order by name or by average, search for students whose average falls in a given range and drop students who are excessively absent. To store the data you can use parallel arrays as discussed in section 7.6.You will need several arrays, all managed in parallel. (Alternatively you may use an array of structs, but we don't expect you to do it that way since we haven't covered that topic yet.) For example, when you sort the array of last names you need to rearrange the elements of the other arrays at the same time so all the data for a particular student is at the same index. You don't need an array of grades, because each grade is computed from the average (e.g. an average of 92 means a grade of 'A').

The biggest part of this assignment is to come up with a good design, using multiple functions to do the different tasks. It will take you a few days to complete a good design so please don't wait until the "last minute" to start the assignment. For your design, try to think about what kinds of tasks there are. For example, you need to display a menu and get the user's choice in a couple of places, so it makes sense to have a function to do that. At a lower level, many of the tasks require the user to enter a number in a certain range (e.g. a non-negative number for average). That suggests a function whose sole job is to query the user for a number and loop until they enter one that's in the correct range. The more you break your code into small pieces (i.e. functions), the easier it will be to code and debug.

Here is sample code that shows sort, search, insert and display functions for parallel arrays. These aren't the exact functions that will be in your program; they are just examples to show how parallel arrays work.

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
const int MAXPEOPLE = 10;

void sortByWeight(double weights[], double heights[], int length) {
        for (int i = length-1; i > 0 ; i--) 
                for (int j = 0; j < i ; j++)
                        if (weights[j] > weights[j+1]) {
                                swap(weights[j],        weights[j+1]);
                                swap(heights[j],        heights[j+1]);
                        }
}

void sortByHeight(double weights[], double heights[], int length) {
        for (int i = length-1; i > 0 ; i--) 
                for (int j = 0; j < i ; j++)
                        if (heights[j] > heights[j+1]) {
                                swap(weights[j],        weights[j+1]);
                                swap(heights[j],        heights[j+1]);
                        }
}

void searchByWeight(int targetWeight, double weights[], double heights[], int length) {
        for (int i = 0; i < length; i++) 
                if (weights[i] >= targetWeight)
                        cout << weights[i] << "\t" << heights[i] << endl;
        cout << endl;
}

bool insert(double weight, double height, double weights[], double heights[], int & numPeople) {
        if (numPeople < MAXPEOPLE) {
                weights[numPeople] = weight;
                heights[numPeople] = height;
                numPeople++;
                return true;
        }
        else return false;
}

void displayAll(double weights[], double heights[], int numPeople) {
        cout << endl;
        for (int i = 0; i < numPeople; i++)
                cout << weights[i] << "\t" << heights[i] << endl;
        cout << endl;
}

int main() {
        double weights[MAXPEOPLE];
        double heights[MAXPEOPLE];
        int numPeople =0;
        insert(45.5,66.4,weights,heights,numPeople);
        insert(35.2,77.8,weights,heights,numPeople);
        insert(98.5,26.7,weights,heights,numPeople);
        insert(55.1,72.9,weights,heights,numPeople);
        return 0;
}

Last edited on
Here is my pseudocode:

Create variables/arrays for course information and student’s information
While output is illegal
Enter section number
Create variables to hold a choice for the first and second menus
Create constants for the first menu choices if user wants to add student or exit
Create constants for the second menu to add/remove a student, sort students by name/average, search student by average, update student, drop student for absences, or exit
Prompt user to enter course information
Display the course info
Display the menu and get the user’s choice
While user does not quit from the menu
While choice is invalid
Enter another value
End loop
If user does not exit from menu
Switch case choice
User chooses to add a student:
Enter student’s information
End switch
End if
End loop
User quits from menu

While user does not quit from the menu
While choice is invalid
Enter another value
End loop
If user does not exit from menu
Switch case choice
User chooses to add a student:
Enter student’s information
User chooses to remove student:
Enter student’s ID and remove student
User chooses to sort students by name:
Display course info
Display student’s information alphabetically
User chooses to sort student by average:
Display course info
Display student’s information from lowest to highest average
User chooses to search student by average:
Enter min and max average scores
Display student’s information
User chooses to update student:
Enter student’s ID and display student’s info
Display menu of whether to change first/last name, update absences/average, or return to the main menu
User chooses to drop student for absences:
Enter max number of absences
Ask yes or no before dropping students
Displays how many absences a student has (if necessary)
User decides whether to drop student
Displays how many students were dropped
End switch
End if
End loop
User exits from program
Print a farewell message

Function to add a student
Enter student ID
Enter first and last name
Enter number of absences
Enter student’s average
Display information
End function

Function to remove a student
For each student
Enter student ID
If student ID does not match
Display error message
Remove student
End function

Function to hold a student’s average
Create constants for grade thresholds
Determine the letter grade
End function

Function to sort students by name
For each student
Swap by last name
End loop
End function

Function to sort students by average
For each student
If low average is less than high average
Swap each student by average
End if
End loop
End function

Function to search students by average
For each student
While average is between min and max
Display student by that average
End Loop
End Loop
End Function

Function to update student
For each student
Enter student ID
If student ID does not match
Display error message
If user wants to change first name
Enter new first name
If user wants to change last name
Enter new last name
If user wants to update number of absences
Enter new number of absences
While output is illegal
Enter number of absences
If user wants to update average
Enter new average
While output is illegal
Enter new average
End loop
End function

Function to drop student for absences
For each student
Enter max number of absences
While user enters yes
Display message
Display student
While user enters yes
Choose whether to drop student
Display how many students were dropped
End loop
End function
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
#include<iostream>
#include<string>
using namespace std;

const int MAX_STUDENTS = 10;
const string EMPTY_STR = "";

const int ADD_CHOICE1 = 1,
		  EXIT_CHOICE1 = 2;

const int ADD_CHOICE2 = 1,
		  REMOVE_CHOICE = 2,
		  SORTNAME_CHOICE = 3,
		  SORTAVERAGE_CHOICE = 4,
		  SEARCHAVERAGE_CHOICE = 5,
		  UPDATE_CHOICE = 6,
		  DROP_CHOICE = 7,
		  EXIT_CHOICE2 = 8;

const int FIRSTNAME_CHOICE = 1,
		  LASTNAME_CHOICE = 2,
		  UPDATEABSENCES_CHOICE = 3,
		  UPDATEAVERAGE_CHOICE = 4,
		  QUITMENU_CHOICE = 5;

string firstNames[MAX_STUDENTS];
string lastNames[MAX_STUDENTS];
string studentIDs[MAX_STUDENTS];
double averages[MAX_STUDENTS];
int absences[MAX_STUDENTS];

void initializeIDs();
int getFirstFreeArrayPosition();
void addCourseDetails();
void addStudentDetails();
int addStudent(string firstName, string lastName, string studentID, double average, int absence);
int findStudent(string studentID);
int removeStudent(string studentID);
void printStudentDetails(int pos);
int grade(double average);
int sortByName(string lastName[], double average[]);
int sortByAverage(string lastName[], double average[]);
int searchByAverage(int targetAverage, int min, int max, string lastName[], double average[]);
int updateStudent(string studentID, string firstName, string lastName, int absences, int average);
void dropStudent(string studentID, int absences, int maxAbsences, double lastName[], int numStudents);
void changeFirstName(string studentID, string firstName, string lastName, int absences, int average);
void changeLastName(string studentID, string firstName, string lastName, int absences, int average);
void updateAbsences(string studentID, string firstName, string lastName, int absences, int average);
void updateAverage(string studentID, string firstName, string lastName, int absences, int average);
void displayTable(string course_code, int section_number, double average, int numStudents);
void showMenu1();
void showMenu2();
void showMenu3();

int main()
{
	int pos=addStudent(firstNames[MAX_STUDENTS],lastNames[MAX_STUDENTS],studentIDs[MAX_STUDENTS],averages[MAX_STUDENTS],absences[MAX_STUDENTS]);
	string course_code;
	int section_number;
	string studentID;
	string firstName;
	string lastName;
	double average;
	int targetAverage;
	int absence;
	int min;
	int max;
	double numStudents;
	int choice1;
	int choice2;

	addCourseDetails();
	do
	{
		showMenu1();
		cout << "Please enter an integer between 1 and 2 (inclusive) : ";
		cin >> choice1;

		while(choice1 < ADD_CHOICE1 || choice1 > EXIT_CHOICE1)
		{
			cout << "Out of range... try again: ";
			cin >> choice1;
		}
		while(!choice1)
		{
			cout << "Not a number... try again: ";
			cin >> choice1;
		}

		if(choice1 != EXIT_CHOICE1)
		{
			switch(choice1)
			{
				case ADD_CHOICE1:
					addStudentDetails();
					break;
			}
		}
	}
	while(choice1 != EXIT_CHOICE1);

	if(pos<MAX_STUDENTS)
	{
		printStudentDetails(pos);
	}

	do
	{
		showMenu2();
		cout << "Please enter an integer between 1 and 2 (inclusive) : ";
		cin >> choice2;

		while(choice2 < ADD_CHOICE2 || choice2 > EXIT_CHOICE2)
		{
			cout << "Out of range... try again: ";
			cin >> choice2;
		}

		while(!choice2)
		{
			cout << "Not a number... try again: ";
			cin >> choice2;
		}

		if(choice2 != EXIT_CHOICE2)
		{
			switch(choice2)
			{
				case ADD_CHOICE2:
					addStudentDetails();
					int addStudent(firstName, lastName, studentID, average, absence);
					initializeIDs();
					break;
				case REMOVE_CHOICE:
					removeStudent(studentID);
					break;
				case SORTNAME_CHOICE:
					sortByName(lastName[MAX_STUDENTS], average[]);
					displayTable(average, course_code, section_number, numStudents);
					break;
				case SORTAVERAGE_CHOICE:
					sortByAverage(lastName[], average[]);
					displayTable(average, course_code, section_number, numStudents);
					break;
				case SEARCHAVERAGE_CHOICE:
					searchByAverage(targetAverage, min, max, lastName[], average[]);
					displayTable(average, course_code, section_number, numStudents);
					break;
				case UPDATE_CHOICE:
					updateStudent(studentID, firstName, lastName, absence, average);
					displayTable(average, course_code, section_number, numStudents);
					break;
				case DROP_CHOICE:
					break;
			}
		}
	}
	while(choice2 != EXIT_CHOICE2);

	cout << "Bye!" << endl;
	return 0;
}

void initializeIds() 
{
	for(int i=0;i<MAX_STUDENTS;i++) 
	{
		studentIDs[i]=EMPTY_STR;
	}
}

int getFirstFreeArrayPosition() 
{
    for(int i=0;i<MAX_STUDENTS;i++)
	{
        if(studentIDs[i]=="") 
		{
			return i;
		}
	}
    return MAX_STUDENTS;    
}

void addCourseDetails()
{
	string course_code;
	int section_number;
	cout << "What is the course code? (e.g. 'CSCI14') ";
	cin >> course_code;
	cout << "What is the section number? ";
	cin >> section_number;
	while(section_number < 0)
	{
		cout << "Value can't be smaller than 0... try again: ";
		cin >> section_number;
	}
	while(!section_number)
	{
		cout << "Not a number... try again: ";
		cin >> section_number;
	}
	cout << "Course " << course_code << ", Section #" << section_number << " created." << endl;
}

void addStudentDetails()
{
	string studentID;
	string fullName;
	string firstName;
	string lastName;
	int absences;
	int average;
	cout << "Enter student ID: ";
	cin >> studentID;
	if(studentID == studentIDs[MAX_STUDENTS])
	{
		cout << "A student with that ID already exists." << endl;
	}
	cout << "Enter the student's first and last name:";
	cin >> firstName >> lastName;
	fullName = firstName + lastName;
	cout << "Enter the number of absences: ";
	cin >> absences;
	while(absences < 0)
	{
		cout << "Value can't be smaller than 0... try again: ";
		cin >> absences;
	}
	while(!absences)
	{
		cout << "Not a number... try again: ";
		cin >> absences;
	}
	cout << "Enter the student's current average: ";
	cin >> average;
	while(average < 0)
	{
		cout << "Value can't be smaller than 0... try again: ";
		cin >> average;
	}
	while(!average)
	{
		cout << "Not a number... try again: ";
		cin >> average;
	}
	initializeIDs();
	cout << "Student " << studentID << "  (" << fullName << ")  has been added." << endl;
}

int addStudent(string firstName, string lastName, string studentID, double average, int absence) 
{
    int pos=getFirstFreeArrayPosition();
    if(pos<MAX_STUDENTS) 
	{
		firstNames[pos] = firstName;
		lastNames[pos] = lastName;
		studentIDs[pos] = studentID;
		averages[pos] = average;
		absences[pos] = absence;
    }
    return pos;
}

int findStudent(string studentID) 
{
    int pos = MAX_STUDENTS;
    return pos;
}

int removeStudent(string studentID)
{
	int pos = findStudent(studentID);
	cout << "Enter student ID: ";
	cin >> studentID;
    if(pos<MAX_STUDENTS)
	{
		cout << "Student " << studentID << " (" << firstNames[MAX_STUDENTS] << lastNames[MAX_STUDENTS] << ")  has been removed." << endl;
		studentIDs[pos] = EMPTY_STR;
	}
	else
	{
		cout << "Student not found." << endl;
	}
    return pos;
}

int grade(double average)
{
	const int A_GRADE = 90,
			  B_GRADE = 80,
			  C_GRADE = 70,
			  D_GRADE = 60;

	if(average >= A_GRADE)
		cout << "A";
	else if(average >= B_GRADE)
		cout << "B";
	else if(average >= C_GRADE)
		cout << "C";
	else if(average >= D_GRADE)
		cout << "D";
	else if(average >= 0)
		cout << "F";
}

int sortByName(string lastName[], double average[])
{
	for(int i; i > 0; i--)
	{
		for(int j = 0; j < i; j++)
		{
			if(lastName[i] > lastName[j])
			{
				swap(lastName[i], lastName[j]);
				swap(average[i], average[j]);
			}
		}
	}
}

int sortByAverage(string lastName[], double average[])
{
	for(int i; i > 0; i--)
	{
		for(int j = 0; j < i; j++)
		{
			if(average[i] > average[j])
			{
				swap(average[i], average[j]);
				swap(lastName[i], lastName[j]);
			}
		}
	}
}
Last edited on
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
int searchByAverage(int targetAverage, int min, int max, string lastName[], double average[])
{
	for(int i; i < MAX_STUDENTS; i++)
	{
		cout << "Enter the minimum average score to look for: ";
		cin >> average[i];
		cout << "Enter the maximum average score to look for: ", average[i];
		cin >> targetAverage;
		if(average[i] >= targetAverage)
		{
			cout << average[i] << "\t" << lastName[i] << endl;
		}
		else
		{
			cout << "No students found in that range." << endl;
		}
		while(average[i] < 0)
		{
			cout << "Value can't be smaller than 0... try again: ";
			cin >> average[i];
		}
		while(!average[i])
		{
			cout << "Not a number... try again: ";
			cin >> average[i];
		}
		while(average[i] < min && average[i] > max)
		{
			cout << "Value can't be smaller than " << targetAverage << "... try again: ";
		}
	}
	cout << endl;
}

int updateStudent(string studentID, string firstName, string lastName, int absences, int average)
{
	int choice3;
	for(int i; i > 0; i++)
	{
		cout << "Enter student ID: ";
		cin >> studentID;
		if(studentID != studentIDs[MAX_STUDENTS])
		{
			cout << "Student not found." << endl;
		}
		cout << displayTable(studentID, firstName, lastName, absences, average) << grade(average) << endl;
		do
		{
			showMenu3();
			cout << "Please enter an integer between 1 and 5 (inclusive) : ";
			cin >> choice3;

			while(choice3 < FIRSTNAME_CHOICE || choice3 > QUITMENU_CHOICE)
			{
				cout << "Out of range... try again: ";
				cin >> choice3;
			}
			while(!choice3)
			{
				cout << "Not a number... try again: ";
				cin >> choice3;
			}

			if(choice3 != QUITMENU_CHOICE)
			{
				switch(choice3)
				{
				case FIRSTNAME_CHOICE:
					changeFirstName(studentID, firstName, lastName, absences, average);
					break;
				case LASTNAME_CHOICE:
					changeLastName(studentID, firstName, lastName, absences, average);
					break;
				case UPDATEABSENCES_CHOICE:
					updateAbsences(studentID, firstName, lastName, absences, average);
					break;
				case UPDATEAVERAGE_CHOICE:
					updateAverage(studentID, firstName, lastName, absences, average);
					break;
				}
			}
		}
		while(choice3 != QUITMENU_CHOICE);
		return;
	}
}

void dropStudent(string studentID, int absences, int maxAbsences, double lastName[], int numStudents)
{
	char ask;
	char drop;
	for(int i = 0; i > numStudents; i++)
	{
		cout << "Enter a maximum number of absences: ";
		cin >> maxAbsences;
		cout << "Ask before dropping (recommended) (y/n): ";
		cin >> ask;
		if(absences > maxAbsences)
		{
			if(ask == 'y' || ask == 'Y')
			{
				cout << lastName[i] << " " << studentID << " has " << absences << " absences." << endl;
				cout << "Drop this student? (y/n): ";
				cin >> drop;
				if(drop == 'y' || drop == 'y')
				{
					cout << (numStudents > 1 ? " students were " : " student was ") << "dropped." << endl;
					return;
				}
				else
				{
					cout << "0 students were dropped" << endl;
				}
			}
			else
			{
				cout << (numStudents > 1 ? " students were " : " student was ") << "dropped." << endl;
				return;
			}
		}
		else
		{
			cout << "0 students were dropped" << endl;
		}
	}
}

void changeFirstName(string studentID, string firstName, string lastName, int absences, int average)
{
	cout << "Enter the new First Name: ";
	cin >> firstName;
	cout << "Updated Student record:" << endl;
	cout << studentID << " " << firstName << "\t" << lastName << "\t" << absences << "\t" << average << "\t" << grade(average) << endl;
}

void changeLastName(string studentID, string firstName, string lastName, int absences, int average)
{
	cout << "Enter the new First Name: ";
	cin >> lastName;
	cout << "Updated Student record:" << endl;
	cout << studentID << " " << firstName << "\t" << lastName << "\t" << absences << "\t" << average << "\t" << grade(average) << endl;
}

void updateAbsences(string studentID, string firstName, string lastName, int absences, int average)
{
	cout << "Enter the number of absences: ";
	cin >> absences;
	while(absences < 0)
	{
		cout << "Value can't be smaller than 0... try again: ";
		cin >> absences;
	}
	while(!absences)
	{
		cout << "Not a number... try again: ";
		cin >> absences;
	}
	cout << "Updated Student record:" << endl;
	cout << studentID << " " << firstName << "\t" << lastName << "\t" << absences << "\t" << average << "\t" << grade(average) << endl;
}

void updateAverage(string studentID, string firstName, string lastName, int absences, int average)
{
	cout << "Enter the new average: ";
	cin >> average;
	while(average < 0)
	{
		cout << "Value can't be smaller than 0... try again: ";
		cin >> average;
	}
	while(!average)
	{
		cout << "Not a number... try again: ";
		cin >> average;
	}
	cout << "Updated Student record:" << endl;
	cout << studentID << " " << firstName << "\t" << lastName << "\t" << absences << "\t" << average << "\t" << grade(average) << endl;
}

void printStudentDetails(int pos) 
{
    cout << firstNames[pos];
    cout << lastNames[pos];
    cout << studentIDs[pos];
	cout << averages[pos];
	cout << absences[pos];
    cout << endl;
}

void displayTable(string course_code, int section_number, double average, int numStudents)
{
	int pos;
	cout << endl;
	for(int i = 0; i < numStudents; i++)
	{
		cout << "\t" << "Course: " << course_code << "\t" << "Section #" << section_number << endl;
		cout << endl;
		cout << endl;
		cout << "\t" << "ID" << "\t" << "FirstName  " << "LastName  " << "Absences " << "Average  " << "Grade" << endl;
		cout << printStudentDetails(pos) << grade(average) << endl;
	}
}

void showMenu1()
{
	cout << "Main Menu...\n"
		 << "1: Add Student\n"
		 << "2: Exit\n";
}

void showMenu2()
{
	cout << "Main Menu...\n"
		 << "1: Add Student\n"
		 <<	"2: Remove Student\n"
		 << "3: Sort by Name\n"
		 << "4: Sort by Average\n"
		 << "5: Search by Average\n"
		 << "6: Update Student\n"
		 << "7: Drop for Absence\n"
		 << "8: Exit\n";
}

void showMenu3()
{
	cout << "1: Change First Name\n"
		 << "2: Change Last Name\n"
		 << "3: Update # of absences\n"
		 << "4: Update average\n"
		 << "5: Return to Main Menu\n";
}
Last edited on
My question is: How can I be able to get the expected outputs for this major program? Are there a lot of lines that needs to be removed/changed? Let me know if I need to shorten my code because it looks too long.
Last edited on
Topic archived. No new replies allowed.