Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

#include #include #include main.h #define MAX_NUM_LENGTH 11 void usage(int argc, char** argv) { if(argc < 4) { fprintf(stderr, usage: %s , argv[0]); exit(EXIT_FAILURE); } }

#include
#include
#include "main.h"
#define MAX_NUM_LENGTH 11
void usage(int argc, char** argv)
{
if(argc < 4) {
fprintf(stderr,
"usage: %s ",
argv[0]);
exit(EXIT_FAILURE);
}
}
/* This function takes in the two input file names (stored in argv) and
determines the number of integers in each file.
If the two files both have N integers, return N, otherwise return -1.
If one or both of the files do not exist, it should exit with EXIT_FAILURE.
input parameters:
char** argv
return parameters:
-1 if the two input files have different number of integers
N if the two input files both have N integers
*/
int get_num_ints(char** argv)
{
}
/* This function allocates engough memory to the three arrays to store
num_ints elements each.
This function should exit with EXIT_FAILURE if the program fails to allocate
the memory.
input parameters:
unsigned int* input_one
unsigned int* input_two
unsigned long int* output
int num_ints
return parameters:
none
*/
void allocate_mem(unsigned int** input_one, unsigned int** input_two,
unsigned long int** output, int num_ints)
{
}
/* This function reads in num_ints integers from the two input files and
stores them in input_one (first input file) and input_two (second input
file).
If one or both of the files do not exist, it should exit with EXIT_FAILURE.
input parameters:
char** argv
unsigned int* input_one
unsigned int* input_two
int num_ints
return parameters:
none
*/
void get_ints(char** argv, unsigned int* input_one, unsigned int* input_two,
unsigned long int* output, int num_ints)
{
}
/* This function does an element-wise addition between the two input arrays
(input_one and input_two) of size num_ints and stores the result in the
output array (output).
input parameters:
unsigned int* intput_one
unsigned int* intput_two
unsigned long int* output
int num_ints
return parameters:
none
*/
void sum_ints(unsigned int* input_one, unsigned int* input_two,
unsigned long int* output, int num_ints)
{
}
/* This function saves the summed output to an output file, whose name was
specified with the program execution (i.e., argv[3]).
The file should containe one integer value per line, similarly to how the
input files were stored.
input parameters:
char** argv
unsigned int* intput_one
unsigned int* intput_two
unsigned long int* output
int num_ints
return parameters:
none
*/
void save_output(char** argv, unsigned int* input_one, unsigned int* input_two,
unsigned long int* output, int num_ints)
{
}
/* This program takes in three text file names as input.
The first two files contain N integers (where N >=0 and N <= 1,000,000)
whose values range from 0 to 4294967295 (i.e., 32-bit unsigned integers).
The last file is the output file that this program should generate.
For all three files, there should be one integer per line.
For each line in the two input files, read in the two integers, add them, and
then store the sum in the output file.
Repeat this process until all integers have been read from the input files.
For example, if the two input files have eight integerse each
input file 1 input file 2 output file
--------------------------------------------------------
1 2 3
5 2 7
8 5 13
1 12 13
2 2 4
10 2 12
1991 2 1993
11231245 21235 11252480
*/
int main(int argc, char** argv)
{
usage(argc, argv);
// Check the number of integers in the input files
int num_ints = get_num_ints(argv);
if(num_ints == -1) {
fprintf(stderr, "ERR: The two input files have different # of ints ");
exit(EXIT_FAILURE);
} else {
fprintf(stdout, "The two input files have %d integers ", num_ints);
}
unsigned int* input_one = NULL;
unsigned int* input_two = NULL;
unsigned long int* output = NULL;
// Allocate memory to store the integers
allocate_mem(&input_one, &input_two, &output, num_ints);
// Read the integers from the two input files
get_ints(argv, input_one, input_two, output, num_ints);
// Sum up the numbers
sum_ints(input_one, input_two, output, num_ints);
for(int i = 0; i < num_ints; i++) printf("%ld ", output[i]);
// Store the result in the output file
save_output(argv, input_one, input_two, output, num_ints);
free(input_one);
free(input_two);
free(output);
return 0;
}
Fill out the five empty functions above, please do not change any given code, do not add more functions.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions