// Program 1 #include <stdio.h> int main() { FILE *input; int grades[ 40][ 5],id
ID: 3540484 • Letter: #
Question
// Program 1
#include <stdio.h>
int main()
{
FILE *input;
int grades[40][5],id[40],n;
int i = 0, j, high[5], low[5], count, sum[5];
double average[5];
input = fopen("input.txt","r");
if(input == NULL)
{
printf("Error opening input file! ");
return 0;
}
fscanf(input,"%d",&id[i]);
do
{
for(j=0;j<5;j++)
fscanf(input,"%d",&grades[i][j]);
n=fscanf(input,"%d",&id[++i]);
}while(n>0&&i<40);
count=i;
printf(" Student Quiz 1 Quiz 2 Quiz 3 Quiz 4 Quiz 5 ");
for(i=0;i<count;i++)
{
printf("%04d ",id[i]);
for(j=0;j<5;j++)
printf("%d ",grades[i][j]);
printf(" ");
}
for(i=0;i<5;i++)
{
high[i]=grades[0][i];
low[i]=grades[0][i];
sum[i]=grades[0][i];
for(j=1;j<count;j++)
{
sum[i]+=grades[j][i];
if(grades[j][i]>high[i])
high[i]=grades[j][i];
else if(grades[j][i]<low[i])
low[i]=grades[j][i];
}
average[i]=(double)sum[i]/count;
}
printf("High Score ");
for(i=0;i<5;i++)
printf("%d ",high[i]);
printf(" Low Score ");
for(i=0;i<5;i++)
printf("%d ",low[i]);
printf(" Average ");
for(i=0;i<5;i++)
printf("%.1f ",average[i]);
fclose(input);
}
// Program 2
#include <stdio.h>
int main(void)
{
/*Local Definitions*/
int num;
int sum = 0;
int larg = 0;
int smal = 99998;
int less = 0;
int tween = 1;
int count = 0;
long avg;
/*Statements*/
printf("Enter numbers with <return> (99999 to stop):");
scanf("%d", &num);
while (num != 99999) {
count++;
sum += num;
// Smallest integer
if (num < smal) {
smal = num;
}
// Largest integer
if (num > larg) {
larg = num;
}
// Integer less than 20
if (num < 20) {
less = 1;
}
// integer less than equal to and greater than equal to
if( num >= 10 || num <= 90 ) {
tween = 0;
}
// scan keyboard
scanf("%d", &num);
}
// average integers
avg = sum / count;
printf("The number of integers is: %d ", count);
printf("The sum of the integers is: %d ", sum);
printf("The average of the integers is: %ld ", avg);
printf("The smallest integer is: %d ", smal);
printf("The largest integer is: %d ", larg);
if (less = 1)
{
printf("At least one number was < 20: True ");
else
printf("At least one number was < 20: False ");
}
if (tween = 0)
{
printf("All numbers were (10 <= n >= 90): True ");
else
printf("All numbers were (10 <= n <= 90): False ");
}
return 0;
}
I have these two programs but have been stumped with errors when trying to add as many functions as possible. I believe it is partly due to the book mainly focusing on the main
function holding everything. Any help is much appreciated thanks!
Explanation / Answer
Program #1
#include <stdio.h>
void output(FILE *input)
{
int grades[40][5],id[40],n;
int i = 0, j, high[5], low[5], count, sum[5];
double average[5];
fscanf(input,"%d",&id[i]);
do
{
for(j=0;j<5;j++)
fscanf(input,"%d",&grades[i][j]);
n=fscanf(input,"%d",&id[++i]);
}while(n>0&&i<40);
count=i;
printf(" Student Quiz 1 Quiz 2 Quiz 3 Quiz 4 Quiz 5 ");
for(i=0;i<count;i++)
{
printf("%04d ",id[i]);
for(j=0;j<5;j++)
printf("%d ",grades[i][j]);
printf(" ");
}
for(i=0;i<5;i++)
{
high[i]=grades[0][i];
low[i]=grades[0][i];
sum[i]=grades[0][i];
for(j=1;j<count;j++)
{
sum[i]+=grades[j][i];
if(grades[j][i]>high[i])
high[i]=grades[j][i];
else if(grades[j][i]<low[i])
low[i]=grades[j][i];
}
average[i]=(double)sum[i]/count;
}
printf("High Score ");
for(i=0;i<5;i++)
printf("%d ",high[i]);
printf(" Low Score ");
for(i=0;i<5;i++)
printf("%d ",low[i]);
printf(" Average ");
for(i=0;i<5;i++)
printf("%.1f ",average[i]);
}
int main()
{
FILE *input;
input = fopen("input.txt","r"); // enter input file here
if(input == NULL)
{
printf("Error opening input file! ");
return 0;
}
output(input);
fclose(input);
}
program #2
#include <stdio.h>
int sum = 0;
int larg = 0;
int smal = 99998;
int less = 0;
int tween = 1;
int count = 0;
long avg;
void calc(int num)
{
count++;
sum += num;
// Smallest integer
if (num < smal) {
smal = num;
}
// Largest integer
if (num > larg) {
larg = num;
}
// Integer less than 20
if (num < 20) {
less = 1;
}
// integer less than equal to and greater than equal to
if( num >= 10 && num <= 90 ) {
tween = 0;
}
}
int averg()
{
return sum/count;
}
void output()
{
printf("The number of integers is: %d ", count);
printf("The sum of the integers is: %d ", sum);
printf("The average of the integers is: %ld ", avg);
printf("The smallest integer is: %d ", smal);
printf("The largest integer is: %d ", larg);
if (less == 1)
{
printf("At least one number was < 20: True ");
}
else
{
printf("At least one number was < 20: False ");
}
if (tween == 0)
{
printf("All numbers were (10 <= n >= 90): True ");
}
else
{
printf("All numbers were (10 <= n <= 90): False ");
}
}
int main(void)
{
/*Local Definitions*/
int num;
/*Statements*/
printf("Enter numbers with <return> (99999 to stop):");
scanf_s("%d", &num);
while (num != 99999) {
calc(num);
// scan keyboard
scanf_s("%d", &num);
}
// average integers
avg = averg();
output();
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.