Question

In: Computer Science

In C, For this project, you have been tasked to read a text file with student...

In C, For this project, you have been tasked to read a text file with student grades and perform several operations with them. First, you must read the file, loading the student records. Each record (line) contains the student’s identification number and then four of the student’s numerical test grades. Your application should find the average of the four grades and insert them into the same array as the id number and four grades. I suggest using a 5th array position to hold the computed average. Once the file has been read and the averages calculated and stored in the array, you must sort the list by grade in descending order. Finally, the records should be printed to screen in descending final grade order with statistics at the end of the file for the class average, highest and lowest grades.

Requirements: Your program should make use of the following functions. 1. A function to read the file into an array of arrays. This function should take an empty multi-dimensional array from the caller. The function should ask the user for the name of the file. You may assume the file name entered will be accurate and accessible. You may omit this requirement and hard code the path to the file. You will be given a test file (shown below). I suggest hard coding the filename while you develop your project to save typing it over and over while you test. 2. A function to compute the grade for each array/record. This function should access the array and average the test grades, inserting the final average into the last position for each record as a float. 3. A function to sort the array of arrays by final grade in descending order (highest grade first). This function should take the entire array. 4. A function to print out the results of the operation to screen calculating the required totals and formatting the output appropriately. This function should take the entire array as well. Your program is expected to only deal with fractional values. Therefore, you are required to use a multi-dimensional array of type float or double. Your program will not have to resize your array; you may hard code the size of the array using a constant (#define). The test files will have 10 records/students, a student number followed by 4 grades. You may not use any global variables save for the use of a constant for array size if you wish as shown often in some examples.

output:

Sample Output: Student Id Test 1 Test 2 Test 3 Test 4 Final ******************************************************************

7899 92 90 88 86 89

6814 85 86 92 88 87.75

8234 77 87 84 98 86.5

7654 76 87 84 88 83.75

3534 86 81 84 73 81

7284 56 81 87 98 80.5

7234 76 81 84 78 79.75

7934 76 91 84 65 79

9901 45 78 79 80 70.5

6465 87 54 68 72 70.25

******************************************************************

File:Print out sorted grades Load Data from file Compute Grade Sort Records Main/Compute Grades.c

Input File (unsorted) grades.txt

Student Id Test 1 Test 2 Test 3 Test 4

6814 85 86 92 88

7234 76 81 84 78

6465 87 54 68 72

7899 92 90 88 86

9901 45 78 79 80

8234 77 87 84 98

7934 76 91 84 65

7284 56 81 87 98

7654 76 87 84 88

3534 86 81 84 73

Solutions

Expert Solution

c code:

#include <stdio.h>
#include <stdlib.h>

//get the inputs from file
void inputs(float line[][6])
{
FILE* fin;
char filename[50];
int i=0;

//get the file name
printf("Enter the input file name: ");
scanf("%s",&filename);

fin =fopen(filename,"r"); //open the file in read mode

//if not file exits
if(!fin)
{
printf("\nCan not open input file\n");
exit(1);
}
//read the file
else
{
while(!feof(fin) ) //if not end of file read character
{
fscanf(fin,"%f%f%f%f%f",&line[i][0],&line[i][1],&line[i][2],&line[i][3],&line[i][4]);
i++;
}
}
}

//calculate the grade
void grade(float line[][6])
{
float total =0;
for(int i=0;i<10;i++)
{
total =0;
total= line[i][1]+line[i][2]+line[i][3]+line[i][4];
line[i][5] =total/4; //average of all marks

}
}

//sort the grade in descending order
void sort(float line[][6])
{
float tmp[6];
//check the order and sort
for(int i=0;i<10;i++){
for(int j=i+1;j<10;j++)
{
if(line[i][5]<line[j][5]){
for (int k = 0; k <6 ; k++)
tmp[k] = line[i][k];

for (int k = 0; k < 6; k++)
line[i][k] = line[j][k];

for (int k = 0; k < 6; k++)
line[j][k] = tmp[k];
}
}
}
}

//print the id marks and grade
void print(float line[][6])
{
printf("\n\nStudent Id \tTest 1 \tTest 2 \tTest 3 \tTest 4 \tFinal");
printf("\n ******************************************************************");
for(int i=0;i<10;i++)
printf("\n %.f\t\t%.f\t%.f\t%.f\t%.f\t%.2f",line[i][0],line[i][1],line[i][2],line[i][3],line[i][4],line[i][5]);

printf("\n ******************************************************************\n\n");


}
int main()
{
float line[10][6];
  
//call all the functions
inputs(line);
grade(line);
sort(line);
print(line);

return 0;
}

output:

//for any clarification please do comments. if you found this solution useful, please give me thumbs up. thank you.


Related Solutions

HW_6a - Read a text file Create a new C++ project and name it as:   Numbers...
HW_6a - Read a text file Create a new C++ project and name it as:   Numbers Create a text file and     save it as:   data.txt Create and save the file      in a C++ project      in the Resource folder. Enter the following numbers:        3                                                              4                                                              5       Note:   After you enter the 5, don’t press the enter key. Save and close the file. Add another file and name it:   Source.cpp Write one statement that declares a file...
C++ Parse text (with delimiter) read from file. If a Text file has input formatted such...
C++ Parse text (with delimiter) read from file. If a Text file has input formatted such as: "name,23" on every line where a comma is the delimiter, how would I separate the string "name" and integer "23" and set them to separate variables for every input of the text file? I am trying to set a name and age to an object and insert it into a vector. #include <iostream> #include <vector> #include <fstream> using namespace std; class Student{    ...
C Programming How do you read in a text file line by line into a an...
C Programming How do you read in a text file line by line into a an array. example, i have the text file containing: line 1: "qwertyuiop" line 2: "asdfghjkl" line 3: "zxcvbnm" Then in the resulting array i get this: array:"qwertyuiopasdfghjklzxcvbnm"
C++ Question 2 You will read in data about the planets from a text file, and...
C++ Question 2 You will read in data about the planets from a text file, and then print out the data about that planet when the user requests the data. You will need to create a Planet Class (since this assignment does not cover templates, you are expected to split your class into .cpp and .h files), as well as a .cpp/.h library with the functions described below Planet Class For the first phase of the question, you will create...
For c language. I want to read a text file called input.txt for example, the file...
For c language. I want to read a text file called input.txt for example, the file has the form. 4 hello goodbye hihi goodnight where the first number indicates the n number of words while other words are separated by newlines. I want to store these words into a 2D array so I can further work on these. and there are fewer words in the word file than specified by the number in the first line of the file, then...
You are on a project selection committee and you have been tasked to find the NPV...
You are on a project selection committee and you have been tasked to find the NPV of two potential projects, Project A and Project B. The cash flows and discount rates are as shown below. Project A Year r Cash Flow Year 0 -$8,000 Year 1 5% $5,000 Year 2 5% $5,000 Project B Year r Cash Flow Year 0 -$1,000 Year 1 5% $2,411.90              You find that the NPV of both projects is $1,297. a. Can you make...
Write a C program to run on unix to read a text file and print it...
Write a C program to run on unix to read a text file and print it to the display. It should count of the number of words in the file, find the number of occurrences of a substring, and take all the words in the string and sort them (ASCII order). You must use getopt to parse the command line. There is no user input while this program is running. Usage: mywords [-cs] [-f substring] filename • The -c flag...
The C++ problem: Student marks are kept in a text file as a single column. Each...
The C++ problem: Student marks are kept in a text file as a single column. Each student may have a different number of assessments and therefore scores. The data recorded in the file for each student start with the number of scores for the student. This is followed by the student id and then several marks student scored in various assessments, one score per line. A small segment of the file might look like the following: (file name is marks.txt)...
Your assignment is to write a C++ program to read a text file containing the information...
Your assignment is to write a C++ program to read a text file containing the information of the employees of a company, load them into memory and perform some basic human resources operations. This assignment will help you practice: multiple file programming, classes, public and private methods, dynamic memory allocation, constructors and destructors, singly linked list and files. Implementation This lab assignment gives you the opportunity to practice creating classes and using dynamic memory in one of the required classes....
In this lab, you will learn to read data from a sequential-access text file. To read...
In this lab, you will learn to read data from a sequential-access text file. To read data from a file, you need one FileStream object and one StreamReader object. The StreamReader object accepts the FileStream object as its argument. To read data from a sequential-access text file, here's what you need to do: From the desktop, open Visual Studio 2017. From the menu bar, navigate to File > New > Project. In the New Project window, select Windows Forms App...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT