I have this program, which currently has 2 functions (not including main): #incl
ID: 3556366 • Letter: I
Question
I have this program, which currently has 2 functions (not including main):
#include
#include
#define MAX 50
typedef struct{
int id;
int grade;
} data;
data students[MAX];
int loadArray(char* filename)
{
FILE *f1; // file pointer
int n; // store the no of records
int i=1;
char c[100]; // stroing the word while reading the the file word by word
f1=fopen(filename,"r");
/* fopen returns 0, the NULL pointer, on failure */
if(f1 == 0)
{
printf( "Could not open file " );
return 0; // file error no record exists so returning 0
}
else
{
fscanf(f1,"%s",c);
n=atoi(c); // reading the first word which is no of records
while(i<=n) // looping n times
{
fscanf(f1,"%s",c);
students[i].id=atoi(c); // converting string to int
fscanf(f1,"%s",c);
students[i].grade=atoi(c);
i++;
}
fclose(f1);
}
return n;
}
void printArray(int size)
{
int i=1;
printf(" Student record ");
printf("ID Grade ");
while(i<=size)
{
printf("%d %d ",students[i].id,students[i].grade);
i++;
}
}
main(int argc, char *argv[])
{
int rec;
if(argc != 2) // argc should be 2 for correct execution
{
printf( "Please provide proper command line argument ");
}
else
{
// argv[1] is a filename to open
rec=loadArray(argv[1]);
if(rec>0) // checking record exists then print.
printArray(rec);
}
}
I need these 4 functions added to this program (2 of them are basically the same though):
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.
int findLowestgrade(int size): Same as above, except it returns the index of the student with the lowest 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. The average that is computed is returned.
int writeContent(char* fname, int size, int low, int high, float avg): This function takes the output file name,
size of the global structure array, index of the lowest grade, index of the highest grade, and the average grade. This
function will write the following information to the output file:
1. All the student information stored in the global structure array.
2. Information of the student with the lowest grade.
3. Information of the student with the highest grade.
4. Average class grade.
Input File Contents (input.txt):
(7 is just the size of the first row)
Update File Contents (update.txt):
(3 is just the size of the first row)
OUTPUT:
[XXXXXX@babbage HW-4]$ ./a.out input.txt update.txt output.txt
Student record
ID Grade
211 75
293 62
306 97
252 84
532 53
798 91
332 88
The student with the highest grade has the id 306 with the grade 97
The student with the lowest grade has the id 532 with the grade 53
The average grade for the class 78.57
Updated student record
ID Grade
211 75
293 62
306 97
252 84
532 53
798 91
332 88
301 98
293 62
316 57
Student Statistics after update operation
The student with the highest grade has the id 301 with the grade 98
The student with the lowest grade has the id 532 with the grade 53
The average grade for the class 77.36
int load_data(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 print_data(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 update_data(char * filename, int n): This function load the new student information from the update.txt file. It is similar to the load_data function it takes the (update) file name and the number of students whose information is already stored in the global structure array. It opens a text file (update.txt), reads new student information, and then adds the new student information to the existing student information in the global structure array. If unable to open the file, return zero otherwise, return the size of the
Explanation / Answer
i am givi ng so me methodes which will be useful..thanks
// 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;
}