Can you write a new code in C  language with the given values, just like this output?

QUESTION

Can you write a new code in C  language with the given values, just like this output?

There are two files named group1.txt and group2.txt that contain course information and grades of each student for each class. I will calculate each course average for each group and show it in simple bar graph. Use “*” and “#”characters for group1 and group2, respectively. I will see the number -999 at the end of each line in the input files. This value is used for line termination and you can use it to verify that you have arrived at the end of the line. The averages of each group should also be calculated and printed at the end of the file.

Group 1:

CSC 80 100 70 80 72 90 89 100 83 70 90 73 85 90 -999

ENG 80 90 80 94 90 74 78 63 83 80 90 -999

HIS 90 70 80 70 90 50 89 83 90 68 90 60 80 -999

MTH 74 80 75 89 90 73 90 82 74 90 84 100 90 79 -999

PHY 100 83 93 80 63 78 88 89 75 -999

Group 2:

CSC 90 75 90 75 80 89 100 60 80 70 80 -999

ENG 80 80 70 68 70 78 80 90 90 76 -999

HIS 100 80 80 70 90 76 88 90 90 75 90 85 80 -999

MTH 80 85 85 92 90 90 74 90 83 65 72 90 84 100 -999

PHY 90 93 73 85 68 75 67 100 87 88 -999

 

SUMMARY

Two files called group1 and group2 are stored with data of scores of few subjects. From both files, the data is read and the average score for each subject is calculated and printed as the given output. A bar graph is also shown.

 

EXPLANATION

Two file pointers g1 and g2  point to both the files h=group1 and group2 in reading mode. The subject names of group1 and group 2 are stored in two 2D character arrays.

Two float arrays named sum1 and sum2 contain the sum of all subject’s scores in group1 and group2 respectively. Each value is read into the array from sum1 and sum2, the average value is calculated.

The final result is shown in the form of a bar graph using ‘*’s and ‘#’ for the two groups. (group1, group2)

 

CODE

#include <stdio.h>
int main()
{
/* to have both the files */
FILE *g1;
FILE *g2;
/* ooening both the files in read mode */
g1=fopen("group1.txt","r");
g2=fopen("group2.txt","r");
char c[10][10];
char c1[10][10];
int cc=0,cc1=0;
int num1[10][20],num2[10][20];
/* getting the first string */
fscanf(g1,"%s",c[cc]);
float sum1[10],sum2[10];
/* to store the sum of score of each subject */
for(int i=0;i<5;i++)
{
sum1[i]=0;
sum2[i]=0;
}
int p1=0,p2=0;
/* processing the data in the first file */
while(1)
{
int num1;
fscanf(g1,"%d",&num1);
p1++;
if(num1==-999)
{
p1--;
sum1[cc]=sum1[cc]/p1;
cc++;
if(cc==5)
break;
fscanf(g1,"%s",c[cc]);
p1=0;
continue;
}
sum1[cc]+=num1;
}

fscanf(g2,"%s",c1[cc1]);
/* processing the data in the second file */
while(1)
{
int num2;
fscanf(g2,"%d",&num2);
p2++;
if(num2==-999)
{
p2--;
sum2[cc1]=sum2[cc1]/p2;
cc1++;
if(cc1==5)
break;
fscanf(g2,"%s",c1[cc1]);
p2=0;
continue;
}
sum2[cc1]+=num2;
}
/* printing the result in the proper format */
printf("Course Course 0 10 20 30 40 50");
printf(" 60 70 80 90 100\n");
printf("ID Average ");
printf("|....|....|....|....|....|....|....|....|....");
printf("|....|\n");
for(int i=0;i<5;i++)
{
printf("%s %.2f ",c[i],sum1[i]);
int p1=sum1[i]/2;
int p2=sum2[i]/2;
for(int i=0;i<(p1);i++)
printf("*");
printf("\n");
printf(" %.2f ",sum2[i]);
for(int i=0;i<(p2);i++)
printf("#");
printf("\n\n");
}
}

 

OUTPUT

 

 

Also, read the question based on functions and write code

Share this post

Leave a Reply

Your email address will not be published. Required fields are marked *