This is a VERY simple C programming question: Write a program, called compute_st
ID: 661980 • Letter: T
Question
This is a VERY simple C programming question:
Write a program, called compute_stats.c, which opens a data file, called numbers.txt, which contains of a list of positive integers, computes their sum and product, finds the largest number, and writes the results to an output file called statistics.txt. Test your program by creating a numbers.txt file and by inserting several number combinations in this file.
For example, if data file has the following numbers 2 3 5 7 9 4 6 12 your program should write the following to the output file
sum = 48
product = 544320
largest = 12
Explanation / Answer
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *file = fopen("numbers.txt", "r");
int numberArray[50];
int i;
int sum=0;
int product=1;
for (i = 0; i < 10; i++)
{
fscanf(file, "%2d", &numberArray[i]);
}
for (i = 0; i < 10; i++)
{
printf("Number is: %d ", numberArray[i]);
sum=sum+numberArray[i];
product=product*numberArray[i];
}
printf("%d",sum);
printf("%d",product);
fclose(file);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.