#include <stdlib.h>
#include <stdio.h>
#include "main.h"
#define MAX_NUM_LENGTH 11
void usage(int argc, char** argv)
{
if(argc < 4) {
fprintf(stderr,
"usage: %s <input file 1> <input file 2> <output
file>\n",
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\n");
exit(EXIT_FAILURE);
} else {
fprintf(stdout, "The two input files have %d integers\n",
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\n",
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.
Program
#include <stdlib.h>
#include <stdio.h>
//#include "main.h"
#define MAX_NUM_LENGTH 11
void usage(int argc, char** argv)
{
if(argc < 4) {
fprintf(stderr,
"usage: %s <input file 1> <input file 2> <output
file>\n",
argv[0]);
exit(EXIT_FAILURE);
}
}
// Check the number of integers in the input files
int get_num_ints(char** argv)
{
FILE *f1, *f2;
f1 = fopen(argv[1], "r");
f2 = fopen(argv[2], "r");
if(f1==NULL ||f2==NULL)
{
fprintf(stderr,"File not exist\n");
exit(EXIT_FAILURE);
}
int n=0, m=0, i;
do
{
fscanf(f1, "%d", &i);
if(feof(f1)) break;
n++;
}while(1);
do
{
fscanf(f2, "%d", &i);
if(feof(f2)) break;
m++;
}while(1);
fclose(f1);
fclose(f2);
if(m==n) return n;
return -1;
}
// Allocate memory to store the integers
void allocate_mem(unsigned int** input_one, unsigned int**
input_two,
unsigned long int** output, int num_ints)
{
*input_one = (unsigned int*)malloc(num_ints*sizeof(unsigned
int));
*input_two = (unsigned int*)malloc(num_ints*sizeof(unsigned
int));
*output = (unsigned long int*)malloc(num_ints*sizeof(unsigned long
int));
if(*input_one==NULL || *input_two==NULL || *output==NULL)
{
fprintf(stderr,"Not enough memory\n");
exit(EXIT_FAILURE);
}
}
// Read the integers from the two input files
void get_ints(char** argv, unsigned int* input_one, unsigned int*
input_two,
unsigned long int* output, int num_ints)
{
FILE *f1, *f2;
f1 = fopen(argv[1], "r");
f2 = fopen(argv[2], "r");
if(f1==NULL ||f2==NULL)
{
fprintf(stderr,"File not exist\n");
exit(EXIT_FAILURE);
}
int i;
for(i=0; i<num_ints; i++)
{
fscanf(f1, "%d", input_one+i);
}
for(i=0; i<num_ints; i++)
{
fscanf(f2, "%d", input_two+i);
}
fclose(f1);
fclose(f2);
}
// Sum up the numbers
void sum_ints(unsigned int* input_one, unsigned int*
input_two,
unsigned long int* output, int num_ints)
{
int i;
for(i=0; i<num_ints; i++)
{
output[i] = input_one[i] + input_two[i];
}
}
// Store the result in the output file
void save_output(char** argv, unsigned int* input_one, unsigned
int* input_two,
unsigned long int* output, int num_ints)
{
FILE *f;
f = fopen(argv[3], "w");
int i;
for(i=0; i<num_ints; i++)
{
fprintf(f, "%ld\n", output[i]);
}
fclose(f);
}
/*main function */
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\n");
exit(EXIT_FAILURE);
}
else {
fprintf(stdout, "The two input files have %d integers\n",
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\n",
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;
}
a.txt
1
5
8
1
2
10
1991
11231245
b.txt
2
2
5
12
2
2
2
21235
Output:
fileRW a.txt b.txt c.txt
The two input files have 8 integers
3
7
13
13
4
12
1993
11252480
c.txt
3
7
13
13
4
12
1993
11252480
N.B. Whether you face any problem then share with
me in the comment section, I'll happy to help you.
Modified Code:
#include <stdlib.h>
#include <stdio.h>
//#include "main.h"
#define MAX_NUM_LENGTH 11
void usage(int argc, char** argv)
{
if(argc < 4) {
fprintf(stderr,
"usage: %s <input file 1> <input file 2> <output
file>\n",
argv[0]);
exit(EXIT_FAILURE);
}
}
// Check the number of integers in the input files
int get_num_ints(char** argv)
{
FILE *f1, *f2;
f1 = fopen(argv[1], "r");
f2 = fopen(argv[2], "r");
if(f1==NULL ||f2==NULL)
{
fprintf(stderr,"File not exist\n");
exit(EXIT_FAILURE);
}
int n=0, m=0, i;
char buf[MAX_NUM_LENGTH];
do
{
fgets(buf, MAX_NUM_LENGTH, f1);
if(feof(f1)) break;
n++;
}while(1);
do
{
fgets(buf, MAX_NUM_LENGTH, f2);
if(feof(f2)) break;
m++;
}while(1);
fclose(f1);
fclose(f2);
if(m==n) return n;
return -1;
}
// Allocate memory to store the integers
void allocate_mem(unsigned int** input_one, unsigned int**
input_two,
unsigned long int** output, int num_ints)
{
*input_one = (unsigned int*)malloc(num_ints*sizeof(unsigned
int));
*input_two = (unsigned int*)malloc(num_ints*sizeof(unsigned
int));
*output = (unsigned long int*)malloc(num_ints*sizeof(unsigned long
int));
if(*input_one==NULL || *input_two==NULL || *output==NULL)
{
fprintf(stderr,"Not enough memory\n");
exit(EXIT_FAILURE);
}
}
// Read the integers from the two input files
void get_ints(char** argv, unsigned int* input_one, unsigned int*
input_two,
unsigned long int* output, int num_ints)
{
FILE *f1, *f2;
f1 = fopen(argv[1], "r");
f2 = fopen(argv[2], "r");
if(f1==NULL ||f2==NULL)
{
fprintf(stderr,"File not exist\n");
exit(EXIT_FAILURE);
}
int i;
char buf[MAX_NUM_LENGTH];
for(i=0; i<num_ints; i++)
{
fgets(buf, MAX_NUM_LENGTH, f1);
input_one[i] = atoi(buf);
}
for(i=0; i<num_ints; i++)
{
fgets(buf, MAX_NUM_LENGTH, f2);
input_two[i] = atoi(buf);
}
fclose(f1);
fclose(f2);
}
// Sum up the numbers
void sum_ints(unsigned int* input_one, unsigned int*
input_two,
unsigned long int* output, int num_ints)
{
int i;
for(i=0; i<num_ints; i++)
{
output[i] = input_one[i] + input_two[i];
}
}
// Store the result in the output file
void save_output(char** argv, unsigned int* input_one, unsigned
int* input_two,
unsigned long int* output, int num_ints)
{
FILE *f;
f = fopen(argv[3], "w");
int i;
for(i=0; i<num_ints; i++)
{
fprintf(f, "%ld\n", output[i]);
}
fclose(f);
}
/*main function */
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\n");
exit(EXIT_FAILURE);
}
else {
fprintf(stdout, "The two input files have %d integers\n",
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\n",
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;
}
#include <stdlib.h> #include <stdio.h> #include "main.h" #define MAX_NUM_LENGTH 11 void usage(int argc, char** argv) { if(argc...
Would u help me fixing this CODE With Debugging Code with GDB #include <stdio.h> #include <stdlib.h> #define SIZE (10) typedef struct _debugLab { int i; char c; } debugLab; // Prototypes void PrintUsage(char *); void DebugOption1(void); void DebugOption2(void); int main(int argc, char **argv) { int option = 0; if (argc == 1) { PrintUsage(argv[0]); exit(0); } option = atoi(argv[1]); if (option == 1) { DebugOption1(); } else if (option == 2) { DebugOption2(); } else { PrintUsage(argv[0]); exit(0); } }...
devmem2.c #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <string.h> #include <errno.h> #include <signal.h> #include <fcntl.h> #include <ctype.h> #include <termios.h> #include <sys/types.h> #include <sys/mman.h> #define FATAL do { fprintf(stderr, "Error at line %d, file %s (%d) [%s]\n", \ __LINE__, __FILE__, errno, strerror(errno)); exit(1); } while(0) #define MAP_SIZE 4096UL #define MAP_MASK (MAP_SIZE - 1) int main(int argc, char **argv) { int fd; void *map_base = NULL, *virt_addr = NULL; unsigned long read_result, writeval; off_t target; int access_type = 'w'; if(argc...
#include <stdio.h> int main(int argc, char *argv[]) { int i; for (i = argc - 1; i > 0; i--) printf("%s ", argv[i]); printf("\n"); return 0; } can you explain this code in c and why use this function
Create a function, void allocate_mem(unsigned int** input_one, unsigned int** input_two, unsigned long int** output, int num_ints) in C. This function allocates enough 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 Do NOT hard code the size of the unsigned (long) integer arrays
C Language Programming. Using the program below - When executing on the command line only this program name, the program will accept keyboard input and display such until the user does control+break to exit the program. The new code should within only this case situation “if (argc == 1){ /* no args; copy standard input */” Replace line #20 “filecopy(stdin, stdout);” with new code. Open read a text file “7NoInputFileResponse.txt” that contains a message “There were no arguments on the...
The operating system is Ubuntu 18.04 hello.c #include <stdio.h> int main(int argc, char *argv[]) { printf("Hello world!\n"); return 0; } syscall-hello.c #include <unistd.h> #include <sys/syscall.h> char *buf = "Hello world!\n"; int main(int argc, char *argv) { size_t result; /* "man 2 write" to see arguments to write syscall */ result = syscall(SYS_write, 1, buf, 13); return (int) result; }Download and compile hello.ce and syscall-hello.com. Compile them statically and dynamically. How do the library and system calls produced by them compare...
Question:Many files on our computers, such as executables and many music and video files, are binary files (in contrast to text files). The bytes in these files must be interpreted in ways that depend on the file format. In this exercise, we write a program data-extract to extract integers from a file and save them to an output file. The format of the binary files in this exercise is very simple. The file stores n integers (of type int). Each...
include «stdio.h void displaymenu(void) printf(" printf" Enter Choicen") printf Set (a), Clear (c), Toggle (t)\n"): printf(" void printbinaryfunsigned char x) int main (int argc, char argvl) r mask atoi(argv[1): unsigned char maskatoi(argv[11): ensigned ahao data - ateilargvl21): char operation if argc 3 printf(" printf" n" printf("usage: %s FristArg SecondArgin", argle)); return;
lab4C.c file contains: #include <stdio.h> ..... #define SIZE 10 #define SIZE2 40 int main(int argc, char *argv[]) { char input[SIZE2]; char name[SIZE]; .... char resu[SIZE2], resu2[SIZE2], resu3[SIZE2]; printf("Enter name, age and wage (exit to quit): "); fgets(input, 40, stdin); while (...) { /* use fgets to read again */ printf("Enter name, age and wage (exit to quit): "); fgets(input, 40, stdin); } return 0; }3.1 Specification Develop an ANSI-C program that reads user information from the standard inputs, and outputs...
#include <stdio.h> .. int main(int argc, char *argv[]) { int base, power; printf("enter base and power: "); scanf("%d %d", &base, &power); while (base != -100){ double res = pow(base, power); double res2 = my_pow(base, power); printf("pow: %.4f\n", res); printf("my_pow: %.4f\n", res2); .... } return 0; } // this function should be RECURSIVE // should not use any loop here double my_pow(double base, double p) { } lab4pow.c file contains: 2.1 Specification Write an ANSI-C program that reads input from the...