Hi im working on this small program the rules are as follows

Hi im working on this small program the rules are as follows,

(Write a small program that fills an array of integers input from a file, prints the integers on the screen in a column, and adds up all the integers in the array and prints the sum onto the screen and into an output file. You must use at least 3 functions

Create a file called numInput.txt and put 20 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 20.

- Ask the user how many integers to get from the file<= 20.

- Use a loop to read the integers into the array from the input file (fscanf).

- Use a loop to print the array onto the screen in a column.

- Use a loop to add up all the items in the array and store the sum

Open a file called resOut.txt

use fprintf to write the sum to the output file)

I have attempted to right the code but it seems to not work. Im typing in Language C, and anyone who could help would be greatly appreciated.

#define _CRT_SECURE_NO_WARNINGS

#include<stdio.h>

FILE *inPtr;

FILE *outPtr;

int nums[20];

int count = 0;

int i;

int sum;

int number;

int main()

{

FILE *inPtr;

int ar[20];

int num = 0;

inPtr = fopen(\"numInput.txt\", \"r\");

FillArray(num, ar, inPtr);

void FillArray(int number, int ar2[], FILE *fp);

{

int I;

printf(\"How many\");

scanf(\"%d\", &number);

for (i = 0, i < number; i++);

{

printf(\"%d\", nums[i]);

}

for(i = 0, i < number; i++);

{

sum = sum + nums[i];

}

fopen(\"resOut.txt\", \"w\");

fprintf(outPtr, \"The sum is %d\", sum);

fclose(inPtr);

fclose(outPtr);

}

}

Solution

#include<stdio.h>

//function declarations, ARRAY, SIZE, FILE POINTERS
void fillArray(int[], int, FILE*);
void printArray(int[], int);
void storeSum(int[], int, FILE*);

int main()
{
  
FILE *inPtr;
FILE *outPtr;
int nums[20];
int i, size;

//open both the files
inPtr = fopen(\"numInput.txt\", \"r\");
outPtr = fopen(\"resOut.txt\", \"w\");

printf(\"How many:\");
scanf(\"%d\", &size); //scanning size of input numbers

//fetch array data from input file
fillArray(nums, size, inPtr);
//print on screen
printArray(nums, size);
//store sum on output file
storeSum(nums, size, outPtr);

//free up both files
fclose(inPtr);
fclose(outPtr);

return 0;
}

void fillArray(int* nums, int size, FILE* in){
   int i;
   printf(\"\ Fetched %d values from input file:\", size);
   //passing reference of nums array & its size and filling it using fscanf
   for(i=0;i<size;i++)
       fscanf(in, \"%d\", &nums[i]);
}

void printArray(int* nums, int size){
   int i;
   //passing reference of nums array and printing

   for(i=0;i<size;i++)
       printf(\"%d \", nums[i]);
}

void storeSum(int* nums, int size, FILE* out){
   int i, sum=0;
   for(i=0;i<size;i++){
       //adding up all values
       sum += nums[i];
   }
  
   printf(\"\ Stored sum to output file: %d\", sum);
   ///printing to output file
   fprintf(out, \"Sum is %d\", sum);
}

Hi im working on this small program the rules are as follows, (Write a small program that fills an array of integers input from a file, prints the integers on t
Hi im working on this small program the rules are as follows, (Write a small program that fills an array of integers input from a file, prints the integers on t
Hi im working on this small program the rules are as follows, (Write a small program that fills an array of integers input from a file, prints the integers on t

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site