Write down a program in C that should do the following: Accept a user input for
ID: 3820482 • Letter: W
Question
Write down a program in C that should do the following: Accept a user input for the size of data 'n' from the user. Randomly generate 'n' integers in the range 0 to 9. Save the randomly generated integers in a file called 'data.txt'. 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. Create a new file 'output.txt'. In this file write down the result of frequency count from step 4. Make use of the frequency count in step 4 to sort the data in the file 'data.txt' in ascending order. 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: Sorted data: 2, 2, 2, 2, 3, 3, 5, 5, 6, 7, 8, 9, 9Explanation / Answer
#include<stdio.h>
#include<conio.h>
#include <math.h>
#include <stdlib.h>
void main()
{
FILE *fptr;
FILE *fptr2;
int n=0,temp;
int num;
int iRand;
int count;
/* open for writing */
fptr = fopen("d:\data.txt", "w");
if (fptr == NULL)
{
printf("File does not exists ");
return;
}
printf("Enter the size of data ");
scanf("%d", &n);
for (n;n>0;n--)
{
iRand = (rand() % 9);
fprintf(fptr, "%d, ", iRand);
}
fclose(fptr);
fptr = fopen("d:\data.txt", "r");
fptr2 = fopen("d:\output.txt", "w");
fprintf(fptr2, "Integer count ");
for (n=0; n<=9;n++)
{
count=0;
rewind(fptr);
while( fscanf(fptr, "%d,", &num) > 0 ) // parse %d followed by ','
{
if(n==num)
{
count++;
}
}
fprintf(fptr2, "%d %d ", n,count);
}
fclose(fptr);
fclose(fptr2);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.