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

Write a C program to: Task1 Store the information of n struct (Array of struct)

ID: 3691852 • Letter: W

Question

Write a C program to: Task1 Store the information of n struct (Array of struct) for (company or college or....) in a text file. Task2 Build the functions in the header file (Example: function to sort data in descending order or to find the average or................) and restored processed information in the same text file. Task3 Develop the program to use simple dynamic data structures. Task4 Execute the functions using switch statement (for example: if the user choose I the first function is implemented, and if the user choose 2 the Second function is implemented............).

Explanation / Answer

company.c

#include<stdio.h>
#include<conio.h>
#include<malloc.h>
struct company
{
   int sal;
};
void main()
{
   struct company *c;
   FILE *fp;
   int i,n;

   clrscr();
   fp=fopen("company.txt","w+");
   printf("Enter n value");
   scanf("%d",&n);
   c=(struct company*)malloc(n*sizeof(struct company));
   printf("Enter company salaries");
   for(i=0;i<n;++i)
   {
       scanf("%d",&(c+i) -> sal);
       fprintf(fp,"%d ",(c+i)->sal);
   }
   getch();
}

function.h

#include<stdio.h>
void sortedorder()
{
   FILE *fp;
   int i=0;
   int a[20],s,j,temp;

   fp=fopen("COMPANY.txt","r");
   for(i=0;i<5;i++)
   {
       fscanf(fp,"%d",&a[i]);
   }
   for(i=0;i<5;i++)
   {
       s=i;
       for(j=i+1;j<5;j++)
       {
           if(a[s]>a[j])
           {
               s=j;
           }
       }
       if(i!=s)
       {
           temp=a[i];
           a[i]=a[s];
           a[s]=temp;
       }
   }
   fp=fopen("COMPANY.txt","w");
   for(i=0;i<5;i++)
   fprintf(fp,"%d",a[i]);
}
void average()
{
   FILE *fp;
   int i,sum=0,a[20];
   float avg;
   fp=fopen("COMPANY.txt","r");
   for(i=0;i<5;i++)
   {
       fscanf(fp,"%d",&a[i]);
       sum+=a[i];
   }
   avg=sum/5;
   printf("The average value:::%f",avg);
}

Main.c

#include<stdio.h>
#include<conio.h>
#include "function.h"
void main()
{
   int ch;
   printf("1.Sorted Order 2.Average of File Elements Enter your choice ");
   scanf("%d",&ch);
   switch(ch)
   {
   case 1:       sortedorder();
           break;
   case 2:   average();
           break;
   default:
           printf("Wrong choice");
   }
}

COMPANY.txt

23 34 12 56 45

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