In: Computer Science
Write a program that gathers input from the user and writes the
information out to a file (output.txt).
Your main method should gather the input, calculate the average,
and write the output. You should have a separate method that writes
the output to a file. You can have other methods as well if you
choose. However, you MUST have at least one other method in
addition to the main method.
Inputs:
Student Number
Name
Class name
Grades 1-5 (5 individual grades)
Output: Student Number: [student number]
Student Name: [student name]
Class: [class name]
Grade 1: [grade 1]
Grade 2: [grade 2]
Grade 3: [grade 3]
Grade 4: [grade 4]
Grade 5: [grade 5]
Average: [grade average]
Example output.txt file:
Student Number: 12345
Student Name: Marty McFly
Class: Music Theory
Grade 1: 90
Grade 2: 85
Grade 3: 70
Grade 4: 99
Grade 5: 96
Average: 88
Since you havn't mentioned the language i have written it in c, If you have any queries please comment
SOLUTION:
#include <stdio.h>
#include <string.h>
double average(int grades[]){
double avg=0;
int i;
for(i=0;i<5;i++)
{
avg+=grades[i];
}
return avg/5;
}
int main()
{
FILE *filePointer;
int studentNumber,i,j,grades[5];
char studentName[40];
//So, the file can be opened as
filePointer = fopen("dataFile.txt", "a+");
//Read data Student Number
printf("Student Number ");
scanf("%d",&studentNumber);
//Add it to the file by using filePrint
operation
fprintf(filePointer, "%s %d \n", "Student Number ",
studentNumber);
//read studet Name and update it to file
printf("Student Name ");
scanf("%s",&studentName);
fprintf(filePointer, "%s %d \n", "Student Name ",
studentName);
//Read grades and add it to th file as done
above
for(i=0;i<5;i++)
{
printf("Enter Grade %d",i);
scanf("%d",&grades[i]);
fprintf(filePointer, "%s %d %d \n
", "Grade ",i, grades[i]);
}
double avg=average(grades);
fprintf(filePointer, "%s %d %ld \n ", "Average is ",i,
avg);
}
CODE IMAGE :
INPUT :
OUTPUT FILE: