Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C programming write a small program that does the following: Fills an array of d

ID: 3806073 • Letter: C

Question

C programming

write a small program that does the following: Fills an array of doubles by reading the values into the amay from an iavut 6le. Add all the double in the array the screen in a row. the number 2.64 to each of the elements in the array Prints all the new double valmes a the array on the screen in a row AND to ouvut file You must use least create a file called doublelaput br and put 25 double numbers itto the file. save the file in the same directory as your source code in your project. Declare an array of doubles of size 25. -Ask the user bow many doubles to get from the flle (k-25) Use a loop to read the doubles isro the array from the imput file (fscanf) Use a loop to print the doubles in the array onto e screen in a row, with a space between each Use a loop to add 2.64 to each tumber in the amay Use a loop to print the new values of the dombles in the array onto the screen in a row with a Open a file called doubleontptmt.tx Use a loop and finint the new values of the doubles in the array into the output file Use close to close both file pointers

Explanation / Answer

#include <stdio.h>
#include <stdlib.h>


void print(double arr[], int n)
{
int i;
for(i = 0; i < n; i++)
{
printf("%f ", arr[i]);
}
printf(" ");
}

void addNumToArray(double arr[], int n, double num)
{
int i;
for(i = 0; i < n; i++)
{
arr[i] += num;
}
}

int readFile(double arr[])
{

printf("How many doubles to read from file: ");
int n;
scanf("%d", &n);

int i = 0;
FILE * file;
file = fopen("doubleInput.txt" , "r");
if (file) {
for(i = 0; i < n; i++)
{
fscanf(file, "%lf", &arr[i]);
}
fclose(file);
}
return n;
}


int main()
{
int MAX_VALUE = 25;
double arr[MAX_VALUE];

// read file in array
int size = readFile(arr);

// print array
print(arr, size);

// add 2.64 to array
addNumToArray(arr, size, 2.64);

// print array
print(arr, size);

return 0;
}

Data in doubleInput.txt

2.3
63
2.5
6.2
1.2
3.2
1.5
6.9
8.9
7895.6
354
3214.6
2351.2
456.3
32145.6
584.3
65.2
14.5
65.23
3.2145
25.69
879.6
32145.6
96545.2
1254.5

Sample run

How many doubles to read from file: 10
2.300000 63.000000 2.500000 6.200000 1.200000 3.200000 1.500000 6.900000 8.900000 7895.600000
4.940000 65.640000 5.140000 8.840000 3.840000 5.840000 4.140000 9.540000 11.540000 7898.240000