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
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;
}
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:-
happy to help just try running the code above and check.
For the following task, I have written code in C and need help in determining the...
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...
This is done in c programming and i have the code for the
programs that it wants at the bottom i jut dont know how to call
the functions
Program 2:Tip,Tax,Total
int main(void)
{
// Constant and Variable Declarations
double costTotal= 0;
double taxTotal = 0;
double totalBill = 0;
double tipPercent = 0;
// *** Your program goes here ***
printf("Enter amount of the bill: $");
scanf("%lf", &costTotal);
printf("\n");
// *** processing ***
taxTotal = 0.07 * costTotal;
totalBill...
I need help finding what is wrong with this code, it is for a CS course I am taking which codes in C (I am using XCode on Mac to code). This is the assignment: "Write a program that performs character processing on 10 characters read in from a file, and writes the results to output files. The program should read from “input.dat”. The program should write the ASCII values of the characters to “output_ascii.dat”. The program should print statistics...
/*This program uses queueing to grab 20 random fortunes from a .txt file FortuneCookies.txt.*It also traverses the fortunes both forwards and backwards.*This program is a recursive binary search tree with hashing.*For: Jack Cole*By: Radhika Chhibbar*Date: December 19, 2011*///Inclusive lirbaries#include<stdio.h>#include<windows.h>#include<tchar.h>#include<stdlib.h>#include<time.h>#include<string.h>//define how many elements are in the message array#define CHAR_NUM 140#define pLEFT pLeft#define pRIGHT pRight//structure of chars ints and shortstypedef struct{char msg[CHAR_NUM];short Tx;short Rx;char Priority;int seq_num;char later[25];}msg;//point link to struct nodetypedef struct node *link;//structure of the linking in the .txt filestruct node{link...
C++ HELP I need help with this program. I have done and compiled this program in a single file called bill.cpp. It works fine. But I need to split this program in three files 1. bill.h = contains the class program with methods and variables 2. bill.cpp = contains the functions from class file 3. main.cpp = contains the main program. Please split this program into three files and make the program run. I have posted the code here. #include<iostream>...
Can some help me with my code I'm not sure why its not working. Thanks In this exercise, you are to modify the Classify Numbers programming example in this chapter. As written, the program inputs the data from the standard input device (keyboard) and outputs the results on the standard output device (screen). The program can process only 20 numbers. Rewrite the program to incorporate the following requirements: a. Data to the program is input from a file of an...
C Programming Question
Hi, I have the following code and in my submission I'm supposed
to only include function definitions (i.e. implementations) in the
file. However, I'm not NOT required to write the main,
struct definitions and function prototypes.
Could someone help me fix this code?
Text Version:
#include<stdio.h>
#include<stdlib.h>
#include <string.h>
struct ip_address {
int octet_1;
int octet_2;
int octet_3;
int octet_4;
};
typedef struct ip_address ip_address_t;
void print_ip_address(ip_address_t ip1){
printf("%d.%d.%d.%d",
ip1.octet_1,ip1.octet_2,ip1.octet_3,ip1.octet_4);
}
int is_valid(ip_address_t ip1){
if(ip1.octet_1 < 0...
#include <stdio.h>#include <stdlib.h>#include <string.h>#include <errno.h>#define fileNameLength 256int main(){//Local variableschar inputFileName[fileNameLength] = {0}; //filenamechar inByte;FILE *inputFile, *outputFile; //file pointers for input and outputint shiftValue = 0; //shift value// array to store chatacter countsint freqTable[fileNameLength]= {0};int totalChars=0;int idx;//Get a file name, and then open the file //Loop variableunsigned short int cipher;//to calculate relative frequency and entropydouble relFrequency, entropy = 0.0;// reads input filenameprintf("Enter an input file name: ");scanf("%s", inputFileName);// reads shift valueprintf("Enter an unsigned short int: ");scanf("%d", &shiftValue);// initializing array values to...
Please program in C++ and document the code as you go so I can understand what you did for example ///This code does~ Your help is super appreciated. Ill make sure to like and review to however the best answer needs. Overview You will revisit the program that you wrote for Assignment 2 and add functionality that you developed in Assignment 3. Some additional functionality will be added to better the reporting of the students’ scores. There will be 11...
Hello I need help fixing my C++ code. I need to display in the
console the information saved in the file as well have the content
saved in the file output in a fixed position like the screenshot.
Thanks.
CODE
#include<iostream>
#include<fstream>
#include<iomanip>
using namespace std;
//main function
int main()
{
//variable to store student id
int id;
//variables to store old gpa(ogpa), old course
credits(occ), new course credits(ncc), current gpa(cur_gpa),
cumulative gpa(cum_gpa)
float ogpa,...