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

  • Thread starter Thread starter shermaine80
  • Start date Start date
Click For Summary
SUMMARY

This discussion focuses on writing a C program to handle student marks and file operations, specifically addressing two exam questions. The first question requires creating a program that writes student names and marks to a file specified via command line arguments, utilizing arrays for storage. The second question involves implementing a function to return the number of days in a given month using a switch statement, with an emphasis on efficiency and compact code. Key programming concepts include command line argument handling, file operations, and array manipulation in C.

PREREQUISITES
  • C programming language fundamentals
  • Understanding of command line arguments in C
  • File handling in C
  • Array usage and manipulation in C
NEXT STEPS
  • Learn about C file I/O operations using fopen, fwrite, and fclose
  • Explore command line argument parsing in C
  • Study efficient array handling techniques in C
  • Investigate alternative methods for implementing switch-case logic in C
USEFUL FOR

C programmers, computer science students preparing for exams, and anyone looking to improve their skills in file operations and array manipulation in C.

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:

Similar threads

Replies
81
Views
8K
  • · Replies 75 ·
3
Replies
75
Views
7K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 10 ·
Replies
10
Views
26K
Replies
1
Views
4K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 3 ·
Replies
3
Views
6K
  • · Replies 6 ·
Replies
6
Views
6K
  • · Replies 5 ·
Replies
5
Views
4K
Replies
3
Views
2K