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

C program Invent a C program on anything but it must fit these rules. Any Theme

ID: 3775806 • Letter: C

Question

C program

Invent a C program on anything but it must fit these rules.

Any Theme (the purpose)

Everything must be a function and passed correctly in main (everything is a function).
Names of functions must make sense
Must have a struct (with an understanding name ).
Must have a menu system with switch cases
Adds to the Array
Removes from the Array
Does sorting
Does searching
Does some type of calculations
Allows the user to enter the number of things to store
Allocates disjoint memory and uses this memory
Output must make sense
Compiles and runs
Error checking...stops the user from doing bad things
Freeing Memory Correctly

Explanation / Answer

#include<stdio.h>

struct student
{
   int id;
   int marks;
};

void getMarks(struct student * s,int n)
{
   int i=0;
   for(i=0;i<n;i++)
   {
       printf("Enter ID of student ");
       scanf("%d",&(s->id));
       printf("Enter MARKS of student ");
       scanf("%d",&(s->marks));
       s++;

   }
}

int findRecord(struct student * s,int n,int id)
{
int i;
for ( i = 0; i < n; ++i)
{
   int t=(s->id);
  
   if(t==id)
   {

       printf(" Record is found ID of student %d Marks of student is%d",s->id,s->marks);
               return 0;

   }
   s++;

}

printf("Record not found ");
}

void printData(struct student * s,int n)
{
   int i=0;
   for(i=0;i<n;i++)
   {
       printf(" %d ",(s->id));
       printf("%d ",(s->marks));
       s++;

   }
}

void sortData(struct student * s,int n)
{int i,j,temp,tempid;
   for (i = 1; i < n; i++)
for (j = 0; j < n - i; j++) {
if (s[j].marks>s[j + 1].marks) {
temp = s[j].marks;
s[j].marks = s[j + 1].marks;
s[j + 1].marks= temp;

tempid = s[j].id;
s[j].id = s[j + 1].id;
s[j + 1].id= tempid;
}
}
}
int AverageMarks(struct student * s,int n)
{
   int i=0,sum=0;
   for(i=0;i<n;i++)
   {
      
       sum+=(s->marks);
       s++;

   }
   return sum/n;
}

int deleteData(struct student * s,int n,int id)
{

           int i;
for (i = 0; i < n; ++i)
{
   int t=(s->id);
  
   if(t==id)
   {

       printf(" Record is found ID of student %d Marks of student is%d",s->id,s->marks);

       while (i < n)
       {
       s[i - 1] = s[i];
       i++;
        }
        n--;
       
}
  
              

  
  

}

  
}

int main(int argc, char const *argv[])
{

int ch;
int n,id;

   printf("Enter number of students ");
           scanf("%d",&n);
           struct student arr[n];

   struct student * s=arr;
   getMarks(s,n);
           printData(s,n);


printf(" 1)Search Record ");
printf("2)Delete Record ");
printf("3)Avearage marks ");
printf("4)Sorted marks ");

scanf("%d",&ch);

switch(ch)
{
  
   case 1:
       printf("Enter ID to search ");
       scanf("%d",&id);
       findRecord(s,n,id);
       break;
   case 3:
   printf(" Average marks %d",AverageMarks(s,n));break;
   case 4:
   printf(" Sorted marks ");
   printf(" ID marks ");
   sortData(s,n);
   printData(s,n);
   break;
   case 2:
   printf("Enter ID to delete ");
   scanf("%d",&id);
   deleteData(s,n,id);
   n--;
   printData(s,n);break;


}
  
  
   /*int *marks=(int*)malloc(n*sizeof(int));*/

  
  
  
  

  
  
   return 0;
}

=====================================================================

akshay@akshay-Inspiron-3537:~/Chegg$ ./a.out
Enter number of students
3
Enter ID of student 1
Enter MARKS of student 20
Enter ID of student 2
Enter MARKS of student 30
Enter ID of student 3
Enter MARKS of student 10

1   20  
2   30  
3   10  
1)Search Record
2)Delete Record
3)Avearage marks
4)Sorted marks
2
Enter ID to delete
2

1   20  
2   30