|
CS256 - Principles of Structured Design
Fall 2021
|
Displaying ./code/sternflCode/h6/p02.c
/*
p02.c - nums1000.txt is a file of 1000 randomly generated numbers that currently
resides in the /u1/h1/jcompton5 directory
open that file, use the fscanf function in a while loop to read the file,
and find the sum, the largest number, and the smallest number in the
file.
You can assume that the number will be no bigger than 100,000, all
numbers are positive, and all the numbers are integers.
*/
#include <stdio.h>
int main(){
//variable declarations
int num; // an int for storing the number
int sum = 0; // sum, to keep track of the sum
int largest = 0; // place to store the largest number, initalize to zero
int smallest = 100001; // place to store the smallest number, initialize to 100001
//open the file specified
// use fscanf inside a while loop to read the file
//add num to the sum
//check if the current number is bigger than the largest
//if so, make that the new largest
// check if the current number is smaller than the smallest
// if so, make that the new smallest.
//then print out the sum, largest, and smallest numbers
printf("The sum is %d\n", sum);
printf("The largest number is %d\n", largest);
printf("The smallest number is %d\n", smallest);
return 0;
}
|