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

Write down a program in C that should do the following: 1. Accept a user input f

ID: 3820484 • Letter: W

Question

Write down a program in C that should do the following: 1. Accept a user input for the size of data 'n' from the user. 2. Randomly generate 'n' integers in the range 0 to 9. 3. Save the randomly generated integers in a file called 'data.txt'. 4. Read the input file 'data.txt' and generate the frequency count of the integers in this file. Frequency count means the number of occurrences of each integer in the data. 5. Create a new file 'output.txt'. In this file write down the result of frequency count from step 4. 6. Make use of the frequency count in step 4 to sort the data in the file 'data.txt' in ascending order. 7. Write down the sorted data in the output file output.txt after the frequency count. if you generated the following data: 2, 8, 9, 2, 7, 6, 5, 2, 9, 3, 3, 2, 5, then the data.txt file should have frequency count like:

Explanation / Answer

#include <stdio.h>
int main()
{
FILE *fptr;
char ch;
int n,i,temp,j;
printf("enter array size ");
scanf("%d",&n);
int a[n],b[n];
fptr = fopen("data.txt", "w");
if (fptr == NULL)
{
printf("File does not exists ");
return;
}
else
{
fprintf(fptr, " INTEGER ");
for(i=0;i<n;i++)
{ a[i]=rand() % n;
b[i]=0;
fprintf(fptr, " %d ", a[i]);
}

}
for(i=0;i<n;i++)
{

if(a[i]==0)
b[0]=b[0]+1;
else if(a[i]==1)
b[1]=b[1]+1;
else if(a[i]==2)
b[2]=b[2]+1;
else if(a[i]==3)
b[3]=b[3]+1;
else if(a[i]==4)
b[4]=b[4]+1;
else if(a[i]==5)
b[5]=b[5]+1;
else if(a[i]==6)
b[6]=b[6]+1;
else if(a[i]==7)
b[7]=b[7]+1;
else if(a[i]==8)
b[8]=b[8]+1;
else if(a[i]==9)
b[9]=b[9]+1;
}
fptr = fopen("output.txt", "w");
if (fptr == NULL)
{
printf("File does not exists ");
return;
}
else
{
fprintf(fptr, " INTEGER ");
fprintf(fptr, " Count ");
for(i=0;i<n;i++)
{
fprintf(fptr, " %d ", a[i]);
fprintf(fptr, " %d ", b[i]);
}

}

for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
fptr = fopen("output.txt", "a");
if (fptr == NULL)
{
printf("File does not exists ");
return;
}
else
{
fprintf(fptr,"sorted data:");
for(i=0;i<n;i++)
{
fprintf(fptr, " %d ", a[i]);
}
}
}

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