write a program that scans from a file and finds the following information: a. f
ID: 3626713 • Letter: W
Question
write a program that scans from a file and finds thefollowing information: a. finds how many integers in file , for example ( 40 20 20 20) it should say 4 , if its like (2 22 2 1 2) it should say 5. b. Finds and prints the sum and the average of the integers. b. Finds and prints the largest and the smallest integer.
am using pico. use EOF plz, (IT IS NOT A FIXED GROUP OF NUMBERS, it could be 2 3 4 numbers or as many numbers.) , the program has to scan and stops at EOF, dont use #includes unless u have to , because i might now know what they are. and don't assume that there is no negative numbers. and i have no idea how to find the biggest and lowest answer. would be better if they are separated into different functions. please explain by detail .
am new to this kind of stuff , use WHILE for loops , no arrays. don't use anything else plz , i need to learn this stuff step by step. thank you
here is my code so far
#include <stdio.h>
int main (void)
int x;
int sum;
{
/* i think integer would be this */
while ( scanf("%d", &x != EOF));
/* i think sum would be ? */
sum = idk ;/
}
thank you
Explanation / Answer
please rate - thanks
message me if any problems or questions
it was simpler to do without functions, can change that if necessary
#include <stdio.h>
int main (void)
{
int x,large,small,sum=0,count=0;
FILE *input;
input = fopen("input.txt","r");
if(input == NULL)
{ printf("Error opening input file 1! ");
return 0;
}
printf("The data: (");
fscanf(input,"%d", &x );
printf("%d ",x);
count++;
large=x; //initialize data to first number
small=x;
sum=x;
while ( fscanf(input,"%d", &x) != EOF)
{printf("%d ",x);
sum+=x;
if(x>large) //if this number is larger the the largest
large=x; //make it the largest
else if(x<small) //if smaller then the smallest
small=x; //make it the smallest
count++;
}
printf(") largest number: %d smallest number: %d average %.2f number of numbers: %d ",large,small,sum/(double)count,count);
fclose(input);
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.