Help me to solve this question by adding dynamic memory allocation in the program at below: Modify your C program to rea
Posted: Tue Jul 12, 2022 8:21 am
Help me to solve this question by adding dynamic memoryallocation in the program at below:
Modify your C program to read numbers line by line froma text file using dynamic memory allocation.Your program should be able to read any amount of numbers in thetext file and compute the average.
Numbers in the text file is 10, 20, 30,40, 50. Average is30.
Output :
1 #include 2 #include Nmin 10 m co gi o mini 10 A CÓ ĐI 3 4 int main() { 5 6 7 10 11 12 13 14 15 16 char number [5]; int i, count = 0; double total = 0; double num = 5; FILE *file; file = fopen("textFile.txt","r"); if(file) { while (fgets (number,5, file) != NULL) { printf("%s", number); 17 } 18 } total + atof(number); count++; 19 printf("\nAverage 20 fclose(file); 21 return 0; 22} = %.2f\n", total/count);
10 20 30 40 50 Average 30.00
Modify your C program to read numbers line by line froma text file using dynamic memory allocation.Your program should be able to read any amount of numbers in thetext file and compute the average.
Numbers in the text file is 10, 20, 30,40, 50. Average is30.
Output :
1 #include 2 #include Nmin 10 m co gi o mini 10 A CÓ ĐI 3 4 int main() { 5 6 7 10 11 12 13 14 15 16 char number [5]; int i, count = 0; double total = 0; double num = 5; FILE *file; file = fopen("textFile.txt","r"); if(file) { while (fgets (number,5, file) != NULL) { printf("%s", number); 17 } 18 } total + atof(number); count++; 19 printf("\nAverage 20 fclose(file); 21 return 0; 22} = %.2f\n", total/count);
10 20 30 40 50 Average 30.00