Answered! I have a file with 100 random numbers. I need to create a program that reads the numbers from the file into an array….

I have a file with 100 random numbers. I need to create a program that reads the numbers from the file into an array. Then it should find the largest number in the array and write back out a filed called “result.txt”. Results should contain the number that was the highest as well as the line number that it was on in the file. For example: the largest number was 1457 which is found on line 45

Expert Answer

 #include <stdio.h>

int main()
{
int num[100] ;
int i = 0;
FILE * fp;

if (fp = fopen(“Integers.txt”, “r”)) {
while (fscanf(fp, “%d”, &num[i]) != EOF) {
++i;
}
fclose(fp);
}

int max;
int pos;
max=num[0];
for(int j=0;j<100;j++)
{
if(num[j]>max){
max=num[j];
pos=j+1;
}}
FILE *fptr;
fptr = fopen(“results.txt”, “w”);
if (fptr == NULL)
{
printf(“File does not exists n”);

}
printf(“max = %d at line %d “,max,pos);
fprintf(fptr,”max = %d at line %d “,max,pos);
fclose(fptr);
return 0;
}

Still stressed from student homework?
Get quality assistance from academic writers!