Question

Question 4: C Programming (15 points) Create a program that asks the user for an integer number representing the number of students in a classroom. Your program will then malloc an array of struct students having the same number of cells as input by the users integer number. The student struct will have only two fields: student name and gpa. Your program then opens a CSV file

0 0
Add a comment Improve this question Transcribed image text
Answer #1
  • I have pasted the fully tested code below
  • I have also pasted example GPA.csv which assumes every line contains one tuple like in the format(name,gpa) without any spaces
  • It skips the lines and prints error on screen
  • At the end of program it prints the number of lines read and entries filled in array and average GPA
  • In case of any doubts just comment down
  • I have also pasted sample run of program

------------------Sample Run------------------

desperado:37.file-real-c -> r Number of students: 3 Line 3: GPA (-2.500000) can not be negative Read 4 line (s) Filled 3 entr

------------------GPA.CSV------------------

(John,1.5)
(Rambo,2.5)
(Student,3.6)

------------------STUDENT.C------------------

#include <stdio.h>

#include <string.h>

#include <stdlib.h>

#define MAX_SIZE 100

typedef struct student {

char name[100];

double gpa;

}student;

// void writeToFile(int size, employee Employees[]){

// FILE *fp;

// fp = fopen("employee.txt","w");

// fprintf(fp,"%d\n",size);

// for (int i = 0; i < size; ++i)

// {

// fprintf(fp,"%s\n%s\n",Employees[i].name,Employees[i].city);

// }

// fclose(fp);

// printf("Data successfully written to file employee.txt\n");

// }

// void readFromFile(int *s, employee Employees[]){

// int size = *s;

// FILE *fp;

// fp = fopen("employee.txt","r");

// fscanf(fp,"%d", &size);

// for (int i = 0; i < size; ++i)

// {

// fscanf(fp,"%[^\n]", Employees[i].name);

// fscanf(fp,"%[^\n]", Employees[i].city);

// }

// fclose(fp);

// *s = size;

// printf("Data successfully read from file employee.txt\n");

// }

// void modifyLastEmployee(int size, employee Employees[]){

// strncpy(Employees[size-1].city, "Kilburn", sizeof(Employees[size-1].city));

// }

// void printEmployees(int size, employee Employees[]){

// printf("%15s%15s\n","Name","City");

// for (int i = 0; i < size; ++i){

// printf("%15s%15s\n",Employees[i].name,Employees[i].city);

// }

// }

// void populateEmployees(int *size, employee Employees[]){

// int i = *size;

// strncpy(Employees[i].name, "H. Baker", sizeof(Employees[i].name));

// strncpy(Employees[i].city, "Orange", sizeof(Employees[i].city));

// i++;

// strncpy(Employees[i].name, "D. Rosso", sizeof(Employees[i].name));

// strncpy(Employees[i].city, "Madison", sizeof(Employees[i].city));

// i++;

// strncpy(Employees[i].name, "K. Tims", sizeof(Employees[i].name));

// strncpy(Employees[i].city, "Millburn", sizeof(Employees[i].city));

// i++;

// strncpy(Employees[i].name, "B. Williams", sizeof(Employees[i].name));

// strncpy(Employees[i].city, "Brooklyn", sizeof(Employees[i].city));

// i++;

// *size = i;

// }

// void addEmployees(int *s, employee Employees[]){

// int n;

// int size = *s;

// printf("Number of employees to add(MAX=%d): ",MAX_SIZE-size);

// scanf("%d",&n);

// if(size+n>MAX_SIZE){

// printf("Maximum number of employees you can add is %d\n",MAX_SIZE-size);

// return;

// }

// for (int i = 0; i < n; ++i){

// printf("Employee Name: ");

// scanf(" %[^\n]",Employees[size].name);

// printf("Employee City: ");

// scanf(" %[^\n]",Employees[size++].city);

// }

// *s = size;

// }

int main(){

//Ask for number of students

int n;

printf("Number of students: ");

scanf("%d",&n);

//Declaring array of size 'n'

student Students[n];

//Open file "GPA.CSV"

FILE *fp;

fp = fopen("GPA.CSV","r");

//If file does not exist then exit

if(!fp){

printf("File GPA.CSV does not exist.\n");

return 0;

}

// Read file line by line

char line[100];

int line_number = 1;

int i = 0;

double gpa_sum = 0;

while (fgets(line, 100, fp) != NULL && i<n){

// subtracting 1 to exclude new line symbol

int len = (unsigned)strlen(line);

if(line[len-1]=='\n'){

len--;

}

int j = 0;

if(line[0]=='(' && line[len-1]==')'){

int j = len-2;

for(j;j>1;j--){

if(line[j]==','){

break;

}

}

if(j==1 || j==len-2){

printf("Line %d not formatted properly\n", line_number);

}else{

char num[100];

for(int k=j+1,ki=0;k<len-1;k++,ki++){

num[ki] = line[k];

}

double g = atof(num);

if(g>=0){

gpa_sum += g;

Students[i].gpa = g;

for(int k=1;k<j;k++){

Students[i].name[k-1] = line[k];

}

i++;

}else{

printf("Skipping Line %d: GPA(%lf) can not be negative\n", line_number,g);

}

}

}else{

printf("Line %d not formatted properly\n", line_number);

}

line_number++;

}

printf("Read %d line(s)\n", line_number-1);

printf("Filled %d entr(y/ies) in array\n", i);

printf("Average GPA is %lf\n", gpa_sum/(i));

fclose(fp);

return 0;

}

Add a comment
Know the answer?
Add Answer to:
Question 4: C Programming (15 points) Create a program that asks the user for an integer...
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. 1.Create a program that does the following - Creates three pointers, a character pointer...

    C programming. 1.Create a program that does the following - Creates three pointers, a character pointer professor, and two integer pointers student_ids, grades - Using dynamic memory, use calloc to allocate 256 characters for the professor pointer - Prompts the professor for their name, and the number of students to mark. - Stores the professor’s name using the professor pointer and in an integer the number of students to mark. - Using dynamic memory, use malloc to allocate memory for...

  • Please write comments with the program. Python programming!! Part III - write student data In this...

    Please write comments with the program. Python programming!! Part III - write student data In this part the program should create a .csv file and fill it with generated student information. The program should: Ask the user for a name for the .csv file (use your own name for the exercise) Create the .csv file with columns named - ID, first-name, GPA Based on the information received in part I, generate and fill students information in the CSV file Tell...

  • Write C++ program (studentsGpa.cpp) uses dynamic allocation to create an array of strings. It asks the...

    Write C++ program (studentsGpa.cpp) uses dynamic allocation to create an array of strings. It asks the user to enter a number and based on the entered number it allocates the array size. Then based on that number it asks the user that many times to enter student’s names. What you need to do:  Add another array of doubles to store the gpa of each student as you enter them  You need to display both the student’s name and...

  • In Java Programming: Create a flow chart for a program that prompts for an integer N...

    In Java Programming: Create a flow chart for a program that prompts for an integer N (number of students) and M (number of test scores for each students), and allows the user to N*M real numbers. The program then calculates the average for each student and class average. (1) Prompt the user to input N students and M test scores, then display the inputs. Enter number of students: You entered: 2 Enter number of test scores: You entered: 3 Enter...

  • Kindly solve this using C PROGRAMMING ONLY. 4. Write a program marks.c which consists of a...

    Kindly solve this using C PROGRAMMING ONLY. 4. Write a program marks.c which consists of a main function and three other functions called. readmarks , changemarks (, and writemarks() The program begins by calling the function readmarks () to read the input file marksin. txt which consists of a series of lines containing a student ID number (an integer) and a numeric mark (a float). Once the ID numbers and marks have been read into arrays (by readmarks () the...

  • ***************C PROGRAMMING ONLY************* Demonstrate the ability to create an array on the stack Demonstrate the ability...

    ***************C PROGRAMMING ONLY************* Demonstrate the ability to create an array on the stack Demonstrate the ability to create an array on the heap allowing user to choose the number of values to store. Demonstrate the ability to store an array of Struct values on both the stack and the heap. Program Specifications: 1. Create a struct with at least 3 fields - any struct you want but explain it in your comments in the code. Populate at least 10 elements...

  • (C++ programming) Need help with homework. Write a program that can be used to gather statistical...

    (C++ programming) Need help with homework. Write a program that can be used to gather statistical data about the number of hours per week college students play video games. The program should perform the following steps: 1). Read data from the input file into a dynamically allocated array. The first number in the input file, n, represents the number of students that were surveyed. First read this number then use it to dynamically allocate an array of n integers. On...

  • Write a program that asks the user to input a 4-digit integer and then prints the...

    Write a program that asks the user to input a 4-digit integer and then prints the integer in reverse. Your program needs to implement a function reverse that will take in as input the 4-digit number and print it out in reverse. Your code will consist of two files: main.s and reverse.s. Main.s will ask for the user input number and call the function reverse. For ARM/Data Structure needs to be able to run on Pi Reverse.s will take in...

  • Write a C program that asks the user to think of an integer number from 1...

    Write a C program that asks the user to think of an integer number from 1 to 20. Have the computer guess what the number is by using a binary tree to determine the next guess. You can create the binary tree “by hand” by typing in a set of malloc commands and explicitly linking the nodes. (make the tree completely, then traverse the tree) Use a menu to provide the user with a selection to answer the results of...

  • Write a program which asks the user to enter an integer. Use switch statement to write...

    Write a program which asks the user to enter an integer. Use switch statement to write out the numeric word (such as ONE) if the user's input is 1; (see the sample run output for the usr input 2 or 3); otherwise, write OUT OF RANGE. Below are few sample runs: If the user enters a 1, the program will print: ONE TWO THREE Or, if the user enters a 2, the program will print: TWO THREE Or, if 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