Write a program that asks the user for a file name, ask the user for up to 30 in
ID: 3712713 • Letter: W
Question
Write a program that
asks the user for a file name,
ask the user for up to 30 input value
Write the user input values to the file.
Set only the current values in the array to zero
Re-read the contents of the file back into the array
Using the array, display the following data:
lowest number
highest number
total of the numbers
average of the numbers
Write the array back to a NEW file, in reverse order. Call this file reverse.txt.
Provide a "sentinel value" to allow the user to stop entering numbers. (if i only want 5 numbers, i should only have to enter 5 numbers, but if i want to enter 30, then i should be able to enter 30.)
Once you have the program working, create functions that calculate each of the items above. Your function should take an array and the size of the array as input, and return the value calculated. So you should have a function that finds the minimum, a different function that finds the maximum, another function that gets the total, and one that counts the number of numbers in the array divisible by a number the user chooses. Your program should print all the information - your functions should not.
this assignment covers:
loops
files
arrays
functions with input parameters
functions with return values
arithmetic functions
Explanation / Answer
//As you didn't specify language ,i wrote in c:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int getHighest(int *arr2,int num){
int high=arr2[0];//set highest value to index 0 value of array
for(int i=1;i<num;i++){//iterate through all the array values
if(arr2[i]>high){//if any array value is greater than high,then make array value high
high=arr2[i];
}
}
return high;//by the end of loop you will get highest value of array
}
int getLowest(int *arr2,int num){
int low=arr2[0];
for(int i=1;i<num;i++){
if(arr2[i]<low){//if any value of array is less than low value,make the array value low
low=arr2[i];
}
}
return low;//at the end you will get lowest value ..return it
}
int getTotal(int *arr2,int num){
int sum=0;
for(int i=0;i<num;i++)//iterate through loop
{
sum=sum+arr2[i];// add arr value of each index to sum
}
return sum ;// return total
}
float getAVg(int *arr2,int num){
int sum=0;
for(int i=0;i<num;i++)//iterate through loop
{
sum=sum+arr2[i];// add arr value of each index to sum
}
return sum/num ;// return average
}
int main(){
char fileName[30];
int num;
printf("Enter number of elements: ");
scanf("%d",&num);
int * arr1=(int *)malloc(sizeof(int)*num);//dynamicall create memory
int * arr2=(int *)malloc(sizeof(int)*num);
int i=0;
int j=0;
int highest,lowest,total;
float avg;
printf("Please Enter the file name path along with directory ");
scanf("%s",fileName);
FILE *file1;
FILE *file=fopen(fileName,"w+");//open file
if(file==NULL){
printf("Unable to open file ");
return 0;
}
printf("Enter values into array: and these values are entered in to file ");
for(i=0;i<num;i++){
scanf("%d",&arr1[i]);
fprintf(file,"%d ",arr1[i]);//write to file
}
fclose(file);//close the file
file1=fopen(fileName,"r");
if(file1 ==NULL){
printf("Unable to open file while reading");
return 1;
}
printf("reading from file starts ");
for(j=0;j<num;j++){
fscanf(file1,"%d",&arr2[i]);//read int arrays from file
}
highest=getHighest(arr2,num);//get highest,lowest,avg,total values here
lowest=getLowest(arr2,num);
total=getTotal(arr2,num);
avg=getAVg(arr2,num);
printf("Highest value :%d Lowest value:%d Total:%d Average:%f",highest,lowest,total,avg);
fclose(file1);
free(arr1);//free the memory
free(arr2);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.