Question

4. Write a program marks.c which consists of a main function and three other functions called. readmarks , changemarks (, and

Kindly solve this using C PROGRAMMING ONLY.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

// C program to read a file containing student id and marks and output the updated details to output file
#include <stdio.h>
#include <stdlib.h>
# define MAX 20 // maximum number of students in file

// function declaration
int readmarks(FILE *fp, int studentID[], float marks[]);
int changemarks(int studentID[], float marks[], int size, int id, float mark);
void writemarks(FILE *fp, int studentID[], float marks[], int size);

int main(void) {

   int studentID[MAX]; // array to store student id
   float marks[MAX]; // array to store student marks
   int size; // number of students read
   int id;
   float mark;

   // open the input file
   FILE *fp = fopen("marksin.txt","r");
   // if input file can be opened
   if(fp != NULL)
   {
       size = readmarks(fp, studentID, marks); // call readmarks to read the data from file and return the number of records read
       fclose(fp); // close the input file
       // loop that continues till the user wants
       do
       {
           // input the student id whise marks is to be updated
           printf("Enter student id whose marks is to be updated (0 to exit) : ");
           fflush(stdout);
           scanf("%d",&id);
           if(id == 0) // check if user wants to exit
               break;
           // input the updated marks
           printf("Enter updated marks : ");
           fflush(stdout);
           scanf("%f",&mark);
           // call changemarks to update and return the outcome of the operation
           int result = changemarks(studentID, marks,size, id,mark);
           if(result == 0) // if operation is not successful
               printf("That student is not in the marks list\n");
       }while(1);

       // open the output file
       fp = fopen("marksout.txt","w");
       // call writemarks to output the details to output file
       writemarks(fp,studentID, marks, size);
       fclose(fp); // close the output file

   }else
       printf("\nUnable to open file : marksin.txt");
   return EXIT_SUCCESS;
}

// function to read student id and marks in arrays and return the number of records read
int readmarks(FILE *fp, int studentID[], float marks[])
{
   int count = 0;
   // loop till end of file
   while((!feof(fp)) && (count < MAX))
   {
       fscanf(fp,"%d %f",&studentID[count],&marks[count]); // read a record
       count++; // increment the count
   }

   return count; // return the count
}

// function to update the marks of student with id to mark
int changemarks(int studentID[], float marks[], int size, int id, float mark)
{
   // loop over the array
   for(int i=0;i<size;i++)
   {
       if(studentID[i] == id) // if id is present,update the marks
       {
           marks[i] = mark;
           return 1; // successful
       }
   }

   return 0; // student id doesn't exist
}

// function to output the data student ID and marks to output file in the same format as input file
void writemarks(FILE *fp, int studentID[], float marks[], int size)
{
   if(size > 0) // if number of records > 0
   {
       // loop over the array
       for(int i=0;i<size;i++)
       {
           if(i == size-1) // if last record, then don't add new line
           {
               fprintf(fp,"%d %f",studentID[i],marks[i]);
           }else // else add a new line
               fprintf(fp,"%d %f\n",studentID[i],marks[i]);
       }
   }
}
//end of program

Output:

Input file:

| 11234467 2165775.5 32315 80 | 42299 85 58896 58

Console:

Enter student id whose marks is to be updated (0 to exit) : 2315 Enter updated marks : 72 Enter student id whose marks is to

Output file:

11234 67.000000 2165775.500000 32315 72.000000 42299 85.000000 | 58896 50.000000

Add a comment
Know the answer?
Add Answer to:
Kindly solve this using C PROGRAMMING ONLY. 4. Write a program marks.c which consists of a...
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
  • CIS 22A C++ Project Exam Statistics Here is what your program will do: first it welcomes...

    CIS 22A C++ Project Exam Statistics Here is what your program will do: first it welcomes the user and displays the purpose of the program. It then prompts the user to enter the name of an input file (such as scores.txt). Assume the file contains the scores of the final exams; each score is preceded by a 5 characters student id. Create the input file: copy and paste the following data into a new text file named scores.txt DH232 89...

  • C++ Programming

    PROGRAM DESCRIPTIONIn this project, you have to write a C++ program to keep track of grades of students using structures and files.You are provided a data file named student.dat. Open the file to view it. Keep a backup of this file all the time since you will be editing this file in the program and may lose the content.The file has multiple rows—each row represents a student. The data items are in order: last name, first name including any middle...

  • Hi this program must be completed using a Eclipes Compiler for Java only . This is...

    Hi this program must be completed using a Eclipes Compiler for Java only . This is and intro to java class , so only intro methods should be used for this assignment. Please include a copy of the input data and a sample of the out put data . Thanks a bunch Homework-Topic 9-Donations Write a complete program to do the following: The main program calls a method to read in (Erom an input file) a set of people's three-digit...

  • Write a complete JAVA program to do the following program. The main program calls a method...

    Write a complete JAVA program to do the following program. The main program calls a method to read in (from an input file) a set of people's three-digit ID numbers and their donations to a charity (hint: use parallel arrays)Then the main program calls a method to sort the ID numbers into numerical order, being sure to carry along the corresponding donations. The main program then calls a method to print the sorted 1ists in tabular form, giving both ID...

  • 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...

  • write the program in c++ Pointen & A???ys: Write u function that is passed a pointer...

    write the program in c++ Pointen & A???ys: Write u function that is passed a pointer to a float array, and the size the array as an int. The function should return a pointer to the maximum element in the array The function should be: int *maxp(float* ap, int size) Write a main() program to lest the function with different input arrays. Grading Criteria (1 point each): Uses cin/cout correct function definition prompts user for input, reads it in correct...

  • In C++, write a complete program that receives a series of student records from the keyboard...

    In C++, write a complete program that receives a series of student records from the keyboard and stores them in three parallel arrays named studentID and courseNumber and grade. All arrays are to be 100 elements. The studentID array is to be of type int and the courseNumber and grade arrays are to be of type string. The program should prompt for the number of records to be entered and then receive user input on how many records are to...

  • Write the functions needed to complete the following program as described in the comments. Use the...

    Write the functions needed to complete the following program as described in the comments. Use the input file course.txt and change the mark of student number 54812 to 80. /* File: course.cpp A student's mark in a certain course is stored as a structure (struct student as defined below) consisting of first and last name, student id and mark in the course.  The functions read() and write() are defined for the structure student.   Information about a course is stored as a...

  • IN C language Write a C program that prompts the user to enter a line of...

    IN C language Write a C program that prompts the user to enter a line of text on the keyboard then echoes the entire line. The program should continue echoing each line until the user responds to the prompt by not entering any text and hitting the return key. Your program should have two functions, writeStr andcreadLn, in addition to the main function. The text string itself should be stored in a char array in main. Both functions should operate...

  • C++ Write a program with the following elements: in main() -opens the 2 files provided for...

    C++ Write a program with the following elements: in main() -opens the 2 files provided for input (Lab_HW9_2Merge1.txt and Lab_HW9_2Merge2.txt) -calls a global function to determine how many lines are in each file -creates 2 arrays of the proper size -calls a global function to read the file and populate the array (call this function twice, once for each file/array) -calls a global function to write out the 'merged' results of the 2 arrays *if there are multiple entries for...

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