Assignment purpose Interactive program using loops, arrays, and input and output
ID: 3738848 • Letter: A
Question
Assignment purpose Interactive program using loops, arrays, and input and output file processing **Submit source code (prog6.c) through Canvas . One source code file (unformatted text) will be submitted . The file name must match the assignment . The code should be tested and run on a Microsoft compiler before it is uploaded onto Canvas . The code must be submitted on time in order to receive credit (11:59 PM on the due date) . Late submissions will not be accepted or graded . All programming assignments are individual work, sharing code is considered cheating Description Write a small program that fills an array of integers input from a file . prints the integers on the screen in a column adds up all the integers in the array prints the sum onto the screen and into an output file .finds the smallest integer in the array prints the value of the smallest integer onto the screen and into the output file . You must use at least 3 user defined functions to break up the program Create a file called numinput txt and put 12 integers in the file Save the file in the same directory as your source code in your project - Declare an array of integers of size 12 - Use a loop to read the integers into the array from the input file (iseant). - Use a loop to print the numbers from the array onto the screen in a column - Use a loop to add up all the items in the urray and store the sum Use a loop to locate the smallest intege? the array and store the smallest value Open a file called resOut txt use tprintí to write the sum and the smattest vaiue to the output tileExplanation / Answer
#include <stdio.h>
#include <stdlib.h>
main()
{
int arr[12]; //up to 12 element int array
int i;
for(i=0;i<12;i++) //initialize array with 0
arr[i]=0;
FILE *fp1;
i=0; //clean up and again assign to 0
if ((fp1=fopen("numInput.txt","r"))==NULL)
{
printf("numInput.txt failed to open ");
return 1;
}
else
while((fscanf(fp1,"%d",&arr[i]))!=EOF) //scanf and check EOF
{
i++;
}
printArray(arr); //this is the method to print the array elements
int sum,min;
sum=printSum(arr); //printsum is the method to calculate the sum of the array
min=printmin(arr); // this is the method to calculate min of the array
writefile(sum,min); // this is the method to write the sum and min of the array
}
void printArray(int arr[])
{
int i;
for(i=0;i<12;i++)
printf("arr[%d] = %d ",i,arr[i]);
}
int printSum(int arr[])
{
int i,sum=0;
for(i=0;i<12;i++)
sum+=arr[i];
return sum;
}
int printmin(int arr[])
{
int i;
int min=arr[0];
for(i=1;i<12;i++) {
if(arr[i]<min){
min=arr[i];
}
}
return min;
}
void writefile(int sum,int min)
{
char sentence[1000];
FILE *fptr;
fptr = fopen("resOut.txt", "w");
if(fptr == NULL)
{
printf("Error!");
exit(1);
}
fprintf(fptr,"sum of array = %d", sum);
fprintf(fptr," min of array = %d", min);
fclose(fptr);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.