Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Shell Programming: Submit a single pdf file that shows the contents of a C progr

ID: 3778716 • Letter: S

Question

Shell Programming:

Submit a single pdf file that shows the contents of a C program that solves the problem below.

•      Suppose there is file where each line is made up of the first name of a student followed by a score (the first name and score are separated by a space), and there are five such lines in the file (i.e. first names of 5 students and their scores). This file will be fed to the C program as a standard input (i.e. using < operator. For example, ./a.out < datafile)

•      Write a C program to do the following tasks.

            Put the first names into an array called names

            Put the corresponding scores into an array called scores

            Print names and corresponding scores (i.e. output should look the same as the input file)

            Calculate the average of all scores, and print it.

            Using a function that you create, sort the arrays in terms of scores (in ascending order)

            Print sorted names and scores.

            Find the highest scorer, and print the information.

            Find the lowest scorer, and print the information.

Explanation / Answer

#include<stdio.h>
#include<string.h>

#define members 5 //Total number of lines present in the file
#define name_length 51 //Assuming number of characters in the name to be 51

void print(char (*)[name_length],float *,int);//Function prototype for printing the name,marks
float average(float *,int);//Function prototype for calculatin the average
void sort(char (*)[name_length],float *,int);//Function prototype to sort the marks and correspoding names

int main(int argc,char **argv)
{

   char names[members][name_length];//Assuming each name consists atmost 50 characters
   float marks[members];//Assuming marks as float
  
   int i = 0;

   for(i=0;i<members;i++) //Reading names and marks from file and put it into array
   {
       scanf("%s %f",names[i],&marks[i]);
   }

   print(names,marks,members);//Printing names and marks using print function

   float avg = average(marks,members);//Calculating the average
  
   printf("average of marks = %f ",avg);

  
   sort(names,marks,members);//Calling the function which sorts marks and correspoding names
  
   print(names,marks,members);// Printing names and marks which are already in ascending order
  
   printf("Highest Scorer = %s Marks = %f ",names[members-1],marks[members - 1]); //Printing the Highest scorer from sorted array

   printf("Lowest Scorer = %s Marks = %f ",names[0],marks[0]);//Printing the lowest score

}


void print(char (*names)[name_length], float *marks,int count)//Printing the names and marks
{

   int i = 0;
   for(i=0;i<count;i++)
   {
       printf("%s %f ",names[i],marks[i]);
   }

   return;
}

float average(float *marks,int count)//Function to calculate average
{
   float sum = 0.0f;
   int i = 0;
  
   for(i = 0; i < count; i++)
   {
       sum = sum + marks[i];
   }
  
   return sum/count;

}

void sort(char (*names)[name_length],float *marks,int count)//Bubble sort
{

   int i,j;
   float temp_marks;
   char temp_name[51];
  
   for(i = 0;i < (count - 1); i++)
   {
       for(j = 0;j < (count - i - 1) ; j++)
       {
           if(marks[j] > marks[j+1])
           {
               temp_marks = marks[j];
               marks[j] = marks[j+1];
               marks[j+1] = temp_marks;
              
               strcpy(temp_name,names[j]);
               strcpy(names[j],names[j+1]);
               strcpy(names[j+1],temp_name);
           }
       }

   }

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote