Create an application that reads a file\'s contents into an array, displays the
ID: 3827922 • Letter: C
Question
Create an application that reads a file's contents into an array, displays the array's contents as a report, and calculates and displays the total of the array's values. Here is the file. Modify the application that you created in Exercise 1 so it also displays the following using functions: The average of the values in the array The largest value in the array The smallest value in the array Write a am that displays the Roman numeral equivalent of any decimal number between 1 and 20that the user enters. The Roman numerals should be stored in an array of strings and the decimal number that the user enters should be used to locate the array element holding the Roman numeral equivalent The Program should have a loop that allows the user to continue entering numbers until an end sentinel of 0 is entered.Explanation / Answer
1.
//Answer to qustion 1
#include<stdio.h>
#define MAX_SIZE 200
void main() {
int *arr, count, i;
double sum;
FILE *fp = fopen("Sales.txt", "r"); //open Sales.txt file as read only
arr = malloc(sizeof(double)*MAX_SIZE);
sum = count = 0;
while (fscanf(fp,"%lf",&arr[count]) > 0) {
sum = sum + arr[count]; //update sum
count ++;
}
fclose(fp); //close file
printf("Contents of array are: ");
for (i=0; i<count; i++) //print array in for loop
printf("%lf ",arr[count]);
printf(" Sum = %lf ",sum); //print sum
}
2.
//Answer to question 2
#include<stdio.h>
#include<float.h>
#define MAX_SIZE 200
void main() {
int *arr, count, i;
double sum,avg,max,min;
FILE *fp = fopen("Sales.txt", "r");
arr = malloc(sizeof(double)*MAX_SIZE);
sum = count = 0;
min = DBL_MAX;
max = DBL_MIN;
while (fscanf(fp,"%lf",&arr[count]) > 0) {
sum = sum + arr[count]; //update sum
if (arr[count] > max) //decide and update max
max = arr[count];
if (arr[count] < min) //decide and update min
min = arr[count];
count ++;
}
fclose(fp);
avg = sum/count; //calc avg
printf("Contents of array are: "); //print all data
for (i=0; i<count; i++)
printf("%lf ",arr[count]);
printf(" Sum = %lf ",sum);
printf(" Average = %lf ",avg);
printf(" Largest element = %lf ",max);
printf(" Smallest element = %lf ",min);
}
3.
//Answer to question 3
#include<stdio.h>
void main() {
char *romans[] = {"I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX", "X", "XI", "XII", "XIII", "XIV", "XV", "XVI", "XVII", "XVIII", "XIX", "XX"}; //array fo romans
int input;
printf ("Enter a decimal number: "); //input number
scanf("%d", &input);
while(input != 0) {
printf ("%s ",romans[input - 1]); //print roman number
printf ("Enter a decimal number: "); //input number again
scanf("%d", &input);
}
}
I tried my best to keep the code as simple as possible. I have also commented the code to make your life easy. If incase you are facing any problem with the codes, please feel free to comment below. I shall be glad to help you with the code.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.