int loadArray(char* filename): This function takes the input file name and opens
ID: 3559634 • Letter: I
Question
int loadArray(char* filename): This function takes the input file name and opens the file. If unable to open the file, return 0. If able to open the file, it loads the data from the text file into the global structure array and then returns the size of the input (the first line of the text file is the size).
void printArray(int size): This function takes an integer size and displays the data stored in the global structure array as shown in the sample output below. The output should be correctly formatted.
int findHighestgrade(int size): This functions takes an integer which is the size of the global structure array and finds the student with the highest grade. It returns the index of the student with the highest grade.
float averageClassgrade(int size): This functions takes an integer which is the size of the global structure array and computes the average grade for the class.
main():
the in input is :
7
211 75
293 62
306 97
252 84
532 53
798 91
332 88
Sample output
[sm3z5@babbage HW-4]$ ./a.out
insuffiecient argument
[sm3z5@babbage HW-4]$ ./a.out input.txt
Student record
ID Grade
211 75
293 62
306 97
252 84
532 53
798 91
332 88
Student Statistics
The student with the highest grade has the id 306 with the grade 97
The average is 78.57
Explanation / Answer
// getAverage used to find the average of the grades entered
public static double getAverage(int g[]) {
double sum = 0.0;
for (int i = 0; i < g.length; i++) {
sum += g[i];
}
return sum / g.length;
}
// getHighest used to find highest grade entered
public static int getHighest(int g[]) {
int hi = g[0];
for (int i = 1; i < g.length; i++) {
if (g[i] > hi) {
hi = g[i];
}
}
return hi;
}
// getLowest used to find lowest grade entered
public static int getLowest(int g[]) {
int lo = g[0];
for (int i = 1; i < g.length; i++) {
if (g[i] < lo) {
lo = g[i];
}
}
return lo;
}
// sortGrades method used to sort grades in descending order
public static int[] sortGrades(int grades[]) {
for (int i = 0; i < grades.length; i++) {
int largest = i;
for (int j = largest + 1; j < grades.length; j++) {
if (grades[j] > grades[largest]) {
largest = j;
}
}
int temp = grades[largest];
grades[largest] = grades[i];
grades[i] = temp;
//grades[largest] = temp;
}
return grades;
}
// sortNamesByGrades method used to sort names in grades(descending) order
public static String[] sortNamesByGrades(String names[], int grades[]) {
for (int i = 0; i < grades.length; i++) {
int largest = i;
for (int j = largest + 1; j < grades.length; j++) {
if (grades[j] > grades[largest]) {
largest = j;
}
}
int temp = grades[largest];
grades[largest] = grades[i];
grades[i] = temp;
String tempString = names[i];
names[i] = names[largest];
names[largest] = tempString;
}
return names;
}
// sortNames method used to sort names in ascending order
public static String[] sortNames(String[] names) {
for (int i = 0; i < names.length - 1; i++) {
int smallest = i;
for (int j = i + 1; j < names.length; j++) {
if (names[j].compareTo(names[smallest]) < 0) {
smallest = j;
}
}
String temp = names[i];
names[i] = names[smallest];
names[smallest] = temp;
}
return names;
}
// sortGradesByNames method used to sort grades in name(ascending) order
public static int[] sortGradesByNames(String[] names, int[] grades) {
for (int i = 0; i < names.length - 1; i++) {
int smallest = i;
for (int j = i + 1; j < names.length; j++) {
if (names[j].compareTo(names[smallest]) < 0) {
smallest = j;
}
}
String temp = names[i];
names[i] = names[smallest];
names[smallest] = temp;
// Why doesn't this work
int tempInt = grades[i];
grades[i] = grades[smallest];
grades[smallest] = tempInt;
}
return grades;
}
// putArray method used to display array
public static String putArray(String names[], int grades[],
String heading) {
String output = heading + " ";
for (int i = 0; i < names.length; i++) {
output += names[i];
int spaces = heading.length();
while (spaces > names[i].length()) {
output += " ";
spaces--;
}
output += grades[i] + " ";
}
return output;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.