C Programming! The goal of this homework is to solve a problem using an array, b
ID: 3760288 • Letter: C
Question
C Programming!
The goal of this homework is to solve a problem using an array, but I want you to also use several functions when working with this array.
Begin by copying the file, numbers.txt
numbers.txt =
377
538
917
461
168
186
684
242
940
338
786
730
888
641
617
93
48
The goal of the program is to show the list of numbers, as well as what percentage they are of the total in a nice little table, like this:
N %
123 18.4%
94 14.1%
451 67.5%
Of course, in this example I am only doing it for three numbers. The file has more.
The functions you are to write are:
void fillArray(int morgana [ ], int *numRead, int maxAllowed, FILE * inp); /* Is passed an empty array and a pointer to a file. The array is filled from the file and the number of items read in is returned as through the parameter list. The function also receives a limit on the number of items it is allowed to read in, which is the size of the array. */
int totalArray(int helena [ ], int numRead); returns a number that is total of all the items in the array. */
void writeTable(int ursula [], int numRead, int total); /* Writes out the table, showing what percentage each array element is of the total of all the array elements. There should be one decimal place in the display of the array. */
Five bonus points if you have your program write to both the screen and to an output file.
Explanation / Answer
Hi,
I have written the program incorporating all your requirements:
#include <stdio.h>
FILE*inp;
int morgana[];
int maxAllowed;
int numRead;
int total=0;
//This function reads the integer values from a file and adds those to an array one by one till the end of file
void fillArray(int morgana [ ],int maxAllowed, FILE * inp)
{
int morgana[] = {0};
int i = 0;
if (fp = fopen("votes.txt", "r")) {
while (fscanf(inp, "%d", &nums[i]) != EOF) {
++i;
maxAllowed++;
}
for (--i; i >= 0; --i)
printf("num[%d] = %d ", i, morgana[i]);
printf("Num of elements read = %d ",maxAllowed);
return 0;
fclose(fp);
}
//This function prints the number of elements of an array and the total sum of all elemnts of an array
int totalArray(int morgana [ ], int numRead)
{
int j=0;
int *ptr;
printf("Printing the number of elements of an array ");
int number_of_elements;
number_of_elements=sizeof(morgana)/sizeof(int);
printf("Length: %d ",number_of_elements);
printf("Now finding the sum of all the elements of an array ");
sum=0;
ptr=morgana; /* a=&a[0] */
for (int i=0;i<=number_of_elements;i++){
total=total+*ptr;
ptr++;
}
printf("Sum of elements of array =%d ",total);
}
//This function is used to calculate what percentage each element is of the total sum of an array
void writeTable(int morgana [], int numRead, int total){
ptr=morgana;
float value=0.0;
for(int i=0;i<=number_of_elements;i++){
value=(ptr/total*100);
printf("Now printing the table ");
printf(" N % ");
printf(" %d",ptr " %d",value);
}
return 0;
}
int main(void)
{
printf("Welcome....");
fillArray(int morgana [ ],int maxAllowed, FILE * inp);
totalArray(int morgana [ ], int numRead);
writeTable(int morgana [], int numRead, int total);
return 0;
}
Explainiation:
This program is designed as below:
1.Function fillArray[] will read the contents from a file already filled with integer data as:
1
4
6
it reads the contents of this file till the End of File and stores those values in an array of integers.I also prints the number of elements read.
2.Function totalArray reads all the values of an array one by one till the length of the array and displays trhe sum of all the elemnts of the array.
3.Function WriteTable finally displays percentage of each element to the total of all the elemnts in percentage.
Hope that helps...HAPPY ANSWERING!!!!!!
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.