How Do You Write a C Program to Handle Student Marks and File Operations?

  • Thread starter Thread starter shermaine80
  • Start date Start date
AI Thread Summary
The discussion centers on two programming questions from a past exam paper focused on C programming. The first question requires writing a program that creates a file to store student marks, utilizing three arrays for first names, last names, and marks. Key requirements include initializing the arrays with sample data, checking for command line arguments, ensuring successful file creation, and looping through the arrays to write data to the file. The second question involves creating a function that returns the number of days in a month using a switch statement, with a follow-up request for a more efficient version without the switch statement. Participants emphasize the importance of translating the problem into code and suggest using command line argument handling and arrays for efficient solutions. Specific coding challenges are encouraged to be shared for more targeted assistance.
shermaine80
Messages
30
Reaction score
0
Hi guys,

I'm stucked at these 2 questions from past years exam paper. Can someone advise me how to do it?
Q(1) Write a C program,marks that writes student marks to a newly created file. The program expects a file name to be passed on the command line and iis invoked like this: marks student.txt

(part 1) Declare three arrays, firstname, lastname and stdmark that stores the student first name, last name and examination mark (0-100) respectively. Assume that there are 5 students. But the program must be easily changed to cater to more students.

Initialise the arrays with sample name strings and the following marks: 47, 73,27,65,68

(part 2) A check for the command line arguments to ensure that the student file is specified. If not, the error message, "usage: marks infile", should be printed, and the program exits.

(part 3) The file must be created successfully,. If not, the program exits.

(part 4) A loop to write each students's name and mark in the output file.

(part 5) Any other code for successfult processing of the file.
 
Last edited:
Technology news on Phys.org
Q(2)

Using only the switch statement, write C code for the integer function days(month).

The argument month is an integer in the range 1 to 12 (1 = January, 2 = February,...) The function retuens an integer value representing the number of days in that month. It returns 0 when month is invaild. Assume that the function will only be used this year.

You should write compact code.
Thirty days hath September,
April, June, and November;
All the rest have thirty-one,
Excepting February alone,
And it has twenty-eight days time,
But in leap years, February has twenty-nine

Write a more efficient version of days(month), this time without using the swicth statemnet
 
Show your code, tell us what you're having difficulty with, and ask specific questions. I'm not writing the code for you.

Edit : Here's a start.

For question 1. It's hard to realize what you could be having difficulty with... all you have to do is translate the question into code. If it's difficulty with accepting the command line invocation. That's what this declaration of main is for.
Code:
int main (int argc, char *argv[])
Where argc is the number of command line arguments that exist, and argv is an array of strings containing all of those arguments, the first one being the actual location of the executable by default. Just post your code so we can see where the difficulty lies.

For question 2. Use an array of integers. Return the appropriate item from the array based on the input. It sounds almost too easy, I'm wondering if he/she expects something more advanced.
 
Last edited:
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
Back
Top