Question

For the following task, I have written code in C and need help in determining the cause(s) of a segmentation fault which occurs when run.

**It prints the message on line 47 "printf("Reading the input file and writing data to output file simultaneously..."); then results in a segmentation fault (core dumped)
I am using mobaXterm v11.0 (GNU nano 2.0.9)

CSV (comma-separated values) is a popular file format to store tabular kind of data. Each record is in a separate line and each field in a record is separated (delimited) by a comma. For example, if we had a tabular data as shown in the following table.

James Martin 90 80 70 90
Mary Thompson 91 82 74 91
John Garcia 92 84 78 92

This data has three records, each record with 5 fields. A CSV file containing the above data would look as follows

James Martin,90,80,70,90

Mary Thompson,91,82,74,91

John Garcia,92,84,78,92

Some CSV files also contain a header on the first line signifying name of each field. A CSV file can be imported and exported by almost all popular spreadsheet or data processing applications. Since it is a simple text file, we can also view it in any text editor. For this project, write a C program, calc_grade.c, to take input filename and output filename from the command line. The program should read in the input CSV file, gradebook.csv, (passed from command line) which contains names and grades of all 6 projects, midterm, and final exam for 10 (hypothetical) students in this class. Notice that this CSV file has a header, 10 records, and 10 fields. Your program must calculate and output the name, average of project component, average of exam component, weighted total, and the grade letter for each student in output CSV file (filename passed from command line). Please refer below (or the syllabus) on how to perform the calculations. Your output CSV file must have a header, 10 records, and 5 fields, as shown below.

IMPORTANT:

1. Take input filename (argv[1]) and output filename (argv[2]) from the command line.

2. Average of exam component is simply the average of midterm and final.

3. Remember that project makes 40%, midterm makes 30%, and final makes 30% of the weighted total.

4. Limit the calculated values to 2 decimal places when writing to the file.

5. You may use the logic in project 1 to calculate the grade letter.

Example executions:

$ gcc -Wall -o calc_grade calc_grade.c

$ ./calc_grade gradebook.csv output.csv

Your output CSV file (output.csv) should look as follows

Name,Project,Exam,Weighted total,Grade letter

James Martin,95.00,89.50,91.70,A

Mary Thompson,100.00,80.00,88.00,B

John Garcia,80.00,70.00,74.00,C

Patricia Martinez,50.00,90.00,74.00,C-

Robert Robinson,70.00,65.00,67.00,D

Jennifer Clark,92.50,92.50,92.50,A

Michael Rodriguez,94.33,94.00,94.13,A

Linda Lewis,96.17,95.50,95.77,A

William Lee,98.00,97.00,97.40,A

Elizabeth Walker,40.00,60.00,52.00,F

Data in CSV file

Name Project 1 Project 2 Project 3 Project 4 Project 5 Project 6 Midterm Final
James Martin 100 80 70 90 100 100 95 84
Mary Thompson 100 100 100 91 100 100 75 85
John Garcia 80 88 72 60 100 80 68 72
Patricia Martinez 20 40 60 50 20 70 93 87
Robert Robinson 70 50 55 65 75 75 60 70
Jennifer Clark 95 90 90 95 90 95 95 90
Michael Rodriguez 96 92 94 96 92 96 96 92
Linda Lewis 97 94 98 97 94 97 97 94
William Lee 98 96 102 98 96 98 98 96
Elizabeth Walker 20 0 0 80 60 40 70 50

#include <stdio . h> #include <stdlib.h> #include <string.h> /* function decla ration */ void process File (char [], char [])

FILE reader, *writer; reader= fopen(infile, r); /*checking if input file was success open or not */ if (!reader) printf(er

char *prj4 token token; = strtok(NULL, char *prj5 token; = strtok(NULL, token; token strtok ( NULL , token; token = strtok (

projectGrades [0] projectGrades [1] projectGrades [2] projectGrades [3] = atof(prj4); projectGrades [4] = atof(prj5); project

char* getLetterGrade (double total) { static char grade [5]; if(total 100.0) =90.0 && total .strcpy(grade, A\0); = 80.0 &&

TEXT VERSION:

#include
#include
#include

/* function declaration */

void processFile (char [], char []);
void writeToOutput (FILE *, char [], char [], char [], char [], char [],
char [], char [], char [], char [], char []);

double getTotal (double [], double, double, double *, double *);
char* getLetterGrade(double);

/* main function */

int main (int argc, char **argv)
{
if (argc != 3)
{
printf("Error, input/output names must be supplied \n");
return -1;
}
char *infile = argv[1];
char *outfile = argv[2];

processFile(infile, outfile);
return 0;
}

/* function to read the input file line by line
process the grades and write the data to the csv simultaneously */

void processFile (char infile[], char outfile[])
{
FILE *reader, *writer;
reader = fopen(infile, "r");

/* checking if input file was success open or not */
if (!reader)
{
printf("error file not found");
return;
}
writer = fopen (outfile, "w");
char line[500];
fgets (line, 500, reader); /* header escape */

/* printing header to output */
fprintf(writer, "Name,Project,Exam,Weighted total,Grade letter\n"); // potential error?
printf("Reading the input file and writing data to output file simultaneously.. \n");

/* Reading input file line by line */
while(fgets(line,500,reader) != NULL)
{
char *token = strtok(line, " \t");
char *fname = token;
token = strtok(NULL, " \t");
char *lname = token;
token = strtok(NULL, " \t");
char *prj1 = token;
token = strtok(NULL, " \t");
char *prj2 = token;
token = strtok(NULL, " \t");
char *prj3 = token;
token = strtok(NULL, " \t");
char *prj4 = token;
token = strtok(NULL, " \t");
char *prj5 = token;
token = strtok(NULL, " \t");
char *prj6 = token;
token = strtok(NULL, " \t");
char *midterm = token;
token = strtok(NULL, " \t");
char *final = token;
token = strtok(NULL, " \t");

/* checking for newline char in grade of final exam string */
if (final [strlen(final) - 1] == '\n')
final [strlen(final) - 1] == '\0';
writeToOutput(writer, fname, lname, prj1, prj2, prj3, prj4,
prj5, prj6, midterm, final);
}
/* closing files */
fclose (reader);
fclose (writer);
/* printing success msg */
printf("\n Output file made successfully\n ");
}

/* helper function to write data to output file one at a time */

void writeToOutput (FILE *fpr, char fname[], char lname[], char prj1[], char prj2[], char prj3 [],
char prj4 [], char prj5[], char prj6[], char mid[], char final[])
{
double projectGrades[6];
double projectGrade = 0;
double avgExamGrade = 0; /* rename later? */
projectGrades[0] = atof(prj1);
projectGrades[1] = atof(prj2);
projectGrades[2] = atof(prj3);
projectGrades[3] = atof(prj4);
projectGrades[4] = atof(prj5);
projectGrades[5] = atof(prj6);
double midterm = atof(mid);
double finalExam = atof(final);
double total = getTotal(projectGrades, midterm, finalExam,
&projectGrade, &avgExamGrade);
char *letterGrade = getLetterGrade(total);
fprintf(fpr, "%s %s, %.2lf, %.2lf, %.2lf, %s\n", fname, lname, projectGrade,
avgExamGrade, total, letterGrade);
}

/* function to return the weighted total of grades and return the average project grade
and exam grade by reference */

// potentially misnamed parts here
double getTotal(double projectGrades[], double midterm, double finalExam,
double *avgProjectGrade, double *avgExamGrade)
{
double totalProjectGrades = (projectGrades[0] + projectGrades[1] + projectGrades[2] +
projectGrades[3] + projectGrades[4] + projectGrades[5]);
*avgProjectGrade = (totalProjectGrades / 6.0);
*avgExamGrade = ((midterm + finalExam) / 2.0);
double projectGrade = (*avgProjectGrade * 40.0 / 100.0);
double midGrade = midterm * 30.0 / 100.0;
double finalGrade = finalExam * 30.0 / 100.0;
double total = projectGrade + midGrade + finalGrade;
return total;
}

// comments

char* getLetterGrade(double total)
{
static char grade[5];
if(total >= 90.0 && total <= 100.0)
strcpy(grade, "A\0");
else if(total >= 80.0 && total < 90.0)
strcpy(grade, "B\0");
else if(total >= 75.0 && total < 80.0)
strcpy(grade, "C\0");
else if(total >= 70.0 && total < 75.0)
strcpy(grade, "C-\0");
else if(total >= 65.0 && total < 70.0)
strcpy(grade, "D\0");
else if(total >= 60.0 && total < 65.0)
strcpy(grade, "D-\0");
else if(total < 60)
strcpy(grade, "F\0");
return grade;
}

#include #include #include /* function decla ration */ void process File (char [], char []); void writeToOutput (FILE *, char [], char [], char [), char [), char [], char [], char [, char [], char []); char |double getTotal (double [], double, double, double |char* getLette rG rade (double ); double /* main function */ int main (int argc, char **argv) { if (argc 3) f printf("Error, input/output names must be supplied \n"); return 1; - } char infile = argv[1]; char *outfile = argv[2]; process File (infile, outfile); return 0;*change later? */ } /*function to read the input file line by line process the grades and write the data to the csv simultaneous ly */ void process File (char infile [ ] , char outfile [ ] )
FILE reader, *writer; reader= fopen(infile, "r"); /*checking if input file was success open or not */ if (!reader) printf("error file not found "); return; } writer char line[500]; fgets (line, 500, reader); /* header escape */ fopen (outfile, "w"); = /* printing header to output */ fprintf(writer, "Name,Project, Exam, Weighted total,Grade letter\n"); // potential error? printf("Reading the input file and writing data to output file simultaneously.. \n"); /* Reading input file line by line */ while (fgets (line,500, reader) != NULL) { char token strtok (line, \t"); = char fname token; = strtok (NULL, \t"); token = char *lname token; strtok (NULL, char *prjl = token; strtok (NULL, token \t"); token \t"); char *prj2 token; strtok (NULL, char *prj3 token; token = strtok (NULL, token \t"); = \t");
char *prj4 token token; = strtok(NULL, char *prj5 token; = strtok(NULL, token; token strtok ( NULL , token; token = strtok ( NULL, token; token= strtok ( NULL , \t"); = token char *prj6 \t"); = \t"); char midterm = \t"); char final \t"); /*checking for newline char in grade of final exam string */ '\n') 1] writeTo0utput (writer, fname, lname, prjl, prj2, prj3, prj4, if (final Îstrlen ( final ) - 1] final [strlen ( final ) == '\0'; == prj5, prj6, midterm, final); /* closing files */ fclose (reader) ; fclose (writer) ; /* printing success msg */ printf("\n Output file made successfully\n "); helper function to write data to output file one at a time */ void writeTooutput (FILE *fpr, char fname [] , char lname [ ], char prj1[], char prj 2 [ ] , char prj3 [, char prj4 char prj5í), char prj6[], char mid[], char finall]) double projectGrades [6]; double projectGrade = 0; double avgExamGrade = 0; /* rename later? */
projectGrades [0] projectGrades [1] projectGrades [2] projectGrades [3] = atof(prj4); projectGrades [4] = atof(prj5); projectGrades [5] double midte rm double finalExam double total atof(prj1); = atof(prj2); atof(prj3); = atof (prj6); atof(mid); atof(final); getTotal (p rojectGrades, midte rm, = = finalExam, &projectGrade, &avgExamGrade); getLetterGrade (total ) ; char letterG rade = fprintf(fpr, "$s %s, %.2lf, %.2lf, %.2lf, %s\n", fname, lname, projectGrade, avgExamGrade, total, lette rGrade); /* function to return the weighted total of grades and return the average project grade and grade by reference */ exam /potentially misnamed parts here double getTotal(double projectGrades [], double midterm, double finalExam, double avgProjectGrade, double avgExamG rade) (projectGrades [0] projectGrades [3] (totalProjectGrades / 6.0); finalExam) / 2.0); projectGrades [1] projectGrades [4] projectGrades [2] + projectGrades [5] ) ; double totalProjectGrades = + + *avgProjectGrade avgExamGrade = ( (midterm double projectGrade = (*avgP rojectGrade double midGrade = midterm * 30.0 / 100.0; final Exam = *40.0 100.0); double finalGrade double total = projectGrade return total; * 30.Θ 100.0 ; + midGrade+ finalGrade; =
char* getLetterGrade (double total) { static char grade [5]; if(total 100.0) =90.0 && total .strcpy(grade, "A\0"); = 80.0 && total else if(total = 75.0 && total strcpy(grade, "C\0"); else if(total >= 70.0 && total strcpy(grade, "C-\0"); =65.0 && total else if(total 80.0)
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Hi i am answering your question hope this will be helpful to you, kindly give a thumbs up if it helps you and do comment if you have any doubt regarding the same. one more thing i am not changing inside the logic the claculation logic will be same as you did.

code:-

#include<iostream>
#include<fstream>
#include<cstring>


/* function declaration */

void processFile (char [], char []);
void writeToOutput (FILE *, char [], char [], char [], char [], char [],
char [], char [], char [], char [], char []);

double getTotal (double [], double, double, double *, double *);
char* getLetterGrade(double);

/* main function */

int main (int argc, char **argv)
{
if (argc != 3)
{
printf("Error, input/output names must be supplied \n");
return -1;
}
char *infile = argv[1];
char *outfile = argv[2];
processFile(infile, outfile);
return 0;
}

/* function to read the input file line by line
process the grades and write the data to the csv simultaneously */

void processFile (char infile[], char outfile[])
{
FILE *reader, *writer;
reader = fopen(infile, "r");
  
/* checking if input file was success open or not */
if (!reader)
{
printf("error file not found");
return;
}
writer = fopen (outfile, "w");
char line[500];
fgets (line, 500, reader); /* header escape */
  
/* printing header to output */
fprintf(writer, "Name,Project,Exam,Weighted total,Grade letter\n"); // potential error?
printf("Reading the input file and writing data to output file simultaneously.. \n");

/* Reading input file line by line */
while(fgets(line,500,reader) != NULL)
{
char *token = strtok(line, " \t");
char *fname = token;
token = strtok(NULL, " \t");
char *lname = token;
token = strtok(NULL, " \t");
char *prj1 = token;
token = strtok(NULL, " \t");
char *prj2 = token;
token = strtok(NULL, " \t");
char *prj3 = token;
token = strtok(NULL, " \t");
char *prj4 = token;
token = strtok(NULL, " \t");
char *prj5 = token;
token = strtok(NULL, " \t");
char *prj6 = token;
token = strtok(NULL, " \t");
char *midterm = token;
token = strtok(NULL, " \t");
char *final = token;
token = strtok(NULL, " \t");
  
/* checking for newline char in grade of final exam string */
if (final [strlen(final) - 1] == '\n')
final [strlen(final) - 1] == '\0';
writeToOutput(writer, fname, lname, prj1, prj2, prj3, prj4,
prj5, prj6, midterm, final);
}
/* closing files */
fclose (reader);
fclose (writer);
/* printing success msg */
printf("\n Output file made successfully\n ");
}

/* helper function to write data to output file one at a time */

void writeToOutput (FILE *fpr, char fname[], char lname[], char prj1[], char prj2[], char prj3 [],
char prj4 [], char prj5[], char prj6[], char mid[], char final[])
{
double projectGrades[6];
double projectGrade = 0;
double avgExamGrade = 0; /* rename later? */
projectGrades[0] = atof(prj1);
projectGrades[1] = atof(prj2);
projectGrades[2] = atof(prj3);
projectGrades[3] = atof(prj4);
projectGrades[4] = atof(prj5);
projectGrades[5] = atof(prj6);
double midterm = atof(mid);
double finalExam = atof(final);
double total = getTotal(projectGrades, midterm, finalExam,
&projectGrade, &avgExamGrade);
char *letterGrade = getLetterGrade(total);
fprintf(fpr, "%s %s, %.2lf, %.2lf, %.2lf, %s\n", fname, lname, projectGrade,
avgExamGrade, total, letterGrade);
}

/* function to return the weighted total of grades and return the average project grade
and exam grade by reference */

// potentially misnamed parts here
double getTotal(double projectGrades[], double midterm, double finalExam,
double *avgProjectGrade, double *avgExamGrade)
{
double totalProjectGrades = (projectGrades[0] + projectGrades[1] + projectGrades[2] +
projectGrades[3] + projectGrades[4] + projectGrades[5]);
*avgProjectGrade = (totalProjectGrades / 6.0);
*avgExamGrade = ((midterm + finalExam) / 2.0);
double projectGrade = (*avgProjectGrade * 40.0 / 100.0);
double midGrade = midterm * 30.0 / 100.0;
double finalGrade = finalExam * 30.0 / 100.0;
double total = projectGrade + midGrade + finalGrade;
return total;
}

// comments

char* getLetterGrade(double total)
{
static char grade[5];
  
if(total >= 90.0 && total <= 100.0)
strcpy(grade, "A\0");
else if(total >= 80.0 && total < 90.0)
strcpy(grade, "B\0");
else if(total >= 75.0 && total < 80.0)
strcpy(grade, "C\0");
else if(total >= 70.0 && total < 75.0)
strcpy(grade, "C-\0");
else if(total >= 65.0 && total < 70.0)
strcpy(grade, "D\0");
else if(total >= 60.0 && total < 65.0)
strcpy(grade, "D-\0");
else if(total < 60)
strcpy(grade, "F\0");
return grade;
}

grade.txt // or any name you want

Name Project 1 Project 2 Project 3 Project 4 Project 5 Project 6 Midterm Final
James Martin 100 80 70 90 100 100 95 84
Mary Thompson 100 100 100 91 100 100 75 85
John Garcia 80 88 72 60 100 80 68 72
Patricia Martinez 20 40 60 50 20 70 93 87
Robert Robinson 70 50 55 65 75 75 60 70
Jennifer Clark 95 90 90 95 90 95 95 90
Michael Rodriguez 96 92 94 96 92 96 96 92
Linda Lewis 97 94 98 97 94 97 97 94
William Lee 98 96 102 98 96 98 98 96
Elizabeth Walker 20 0 0 80 60 40 70 50

output.txt // the second argument to the program

Name,Project,Exam,Weighted total,Grade letter
James Martin, 90.00, 89.50, 89.70, B
Mary Thompson, 98.50, 80.00, 87.40, B
John Garcia, 80.00, 70.00, 74.00, C-
Patricia Martinez, 43.33, 90.00, 71.33, C-
Robert Robinson, 65.00, 65.00, 65.00, D
Jennifer Clark, 92.50, 92.50, 92.50, A
Michael Rodriguez, 94.33, 94.00, 94.13, A
Linda Lewis, 96.17, 95.50, 95.77, A
William Lee, 98.00, 97.00, 97.40, A
Elizabeth Walker, 33.33, 60.00, 49.33, F

output:-

Reading the input file and writing data to output file simultaneously.. Output file made successfully

happy to help just try running the code above and check.

Add a comment
Know the answer?
Add Answer to:
For the following task, I have written code in C and need help in determining the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C Programming I was given this code to display the following as the output but it...

    C Programming I was given this code to display the following as the output but it does not seem to work. what is wrong? or can someone guide me through the steps? thanks! I have created the txt file named myfile.txt but the program will not compile. please help. I am forced to use Microsoft visual studio. This program makes a duplicate copy of a file (reads input from a file and write output to another file) 1 #include <stdio.h>...

  • C++. I need to alter this code below to the current specifications. Console Progressing Complete!!! Specifications...

    C++. I need to alter this code below to the current specifications. Console Progressing Complete!!! Specifications Included is an input file. Included is a function to aide, if needed Use the functions, enum, and namespace defined in Assignment 6. You will need to use a loop to read each student’s data You will need to load every homework grade into an array Calculate the homework average The homework average can be calculated while the array is loaded Move the printing...

  • Today assignment , find the errors in this code and fixed them. Please I need help...

    Today assignment , find the errors in this code and fixed them. Please I need help with find the errors and how ro fixed them. #include <iostream> #include <cstring> #include <iomanip> using namespace std; const int MAX_CHAR = 100; const int MIN_A = 90; const int MIN_B = 80; const int MIN_C = 70; double getAvg(); char determineGrade(double); void printMsg(char grade); int main() { double score; char grade; char msg[MAX_CHAR]; strcpy(msg, "Excellent!"); strcpy(msg, "Good job!"); strcpy(msg, "You've passed!"); strcpy(msg, "Need...

  • I need to change the following code so that it results in the sample output below...

    I need to change the following code so that it results in the sample output below but also imports and utilizes the code from the GradeCalculator, MaxMin, and Student classes in the com.csc123 package. If you need to change anything in the any of the classes that's fine but there needs to be all 4 classes. I've included the sample input and what I've done so far: package lab03; import java.util.ArrayList; import java.util.Scanner; import com.csc241.*; public class Lab03 { public...

  • I have this program that works but not for the correct input file. I need the...

    I have this program that works but not for the correct input file. I need the program to detect the commas Input looks like: first_name,last_name,grade1,grade2,grade3,grade4,grade5 Dylan,Kelly,97,99,95,88,94 Tom,Brady,100,90,54,91,77 Adam,Sandler,90,87,78,66,55 Michael,Jordan,80,95,100,89,79 Elon,Musk,80,58,76,100,95 output needs to look like: Tom Brady -------------------------- Assignment 1: A Assignment 2: A Assignment 3: E Assignment 4: A Assignment 5: C Final Grade: 82.4 = B The current program: import java.io.File; import java.io.FileNotFoundException; import java.io.FileWriter; import java.io.IOException; import java.util.Scanner; public class main {    public static void main(String[]...

  • I need help to fix this program it is written in c++ and needs to run...

    I need help to fix this program it is written in c++ and needs to run in visual studio. Program uses character strings from a file containing on the first line and answer key and every other line after is the students ID number followed by a space then their test answers.Program currently reads only test answer key and student ID and their answers of second line.  Sample data can be found below. TFTTFTFFFTTTFFTTFFFT HUB00123 TFTFFFTFTFTFTTTFTTTT SMU12456 FFTFTTFFTTTTTTFFTTFT #include #include #include...

  • In c programming The Consumer Submits processing requests to the producer by supplying a file name, its location and a character. It also outputs the contents of the file provided by the producer...

    In c programming The Consumer Submits processing requests to the producer by supplying a file name, its location and a character. It also outputs the contents of the file provided by the producer to the standard output. The Producer Accepts multiple consumer requests and processes each request by creating the following four threads. The reader thread will read an input file, one line at a time. It will pass each line of input to the character thread through a queue...

  • I want to change this code and need help. I want the code to not use...

    I want to change this code and need help. I want the code to not use parallel arrays, but instead use one array of struct containing the data elements, String for first name, String for last name,Array of integers for five (5) test scores, Character for grade. If you have any idea help would be great. #include #include #include #include using namespace std; const int NUMBER_OF_ROWS = 10; //number of students const int NUMBER_OF_COLUMNS = 5; //number of scores void...

  • I need to complete the C++ program for hotel reservation. In the main cpp you have...

    I need to complete the C++ program for hotel reservation. In the main cpp you have to display the hotel information from the hotel.txt based on customer preferences(which can be taken from the User.txt) For example, if the customer's budget is 200$, then only the hotels with prices below that amount should be displayed. Then the user has an option to choose which one to reserve for the particular time(which also can be found in the user.txt) The last two...

  • URGENT. Need help editing some C code. I have done most of the code it just...

    URGENT. Need help editing some C code. I have done most of the code it just needs the following added: takes an input file name from the command line; opens that file if possible; declares a C struct with three variables; these will have data types that correspond to the data types read from the file (i.e. string, int, float); declares an array of C structs (i.e. the same struct type declared in point c); reads a record from the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT