#include <stdio.h> #include <stdlib.h> int main(int argc, char *argv[]) { if (argc != 3)…
- User
#include <stdio.h> #include <stdlib.h>
int main(int argc, char *argv[]) { if (argc != 3) { fprintf(stderr, "Usage: %s <N_OPS> <N_ENTRIES>\n", argv[0]); return 1; }
int n_ops = atoi(argv[1]); int n_entries = atoi(argv[2]); int i, j; int *vector1, *vector2, *sum; // Allocate memory for vectors vector1 = (int *)malloc(n_entries * sizeof(int)); vector2 = (int *)malloc(n_entries * sizeof(int)); sum = (int *)malloc(n_entries * sizeof(int)); if (!vector1 || !vector2 || !sum) { fprintf(stderr, "Memory allocation failed\n"); return 1; } for (i = 0; i < n_ops; i++) { // Read pairs of vectors for (j = 0; j < n_entries; j++) { scanf("%d", &vector1[j]); } for (j = 0; j < n_entries; j++) { scanf("%d", &vector2[j]); } // Add vectors and write output for (j = 0; j < n_entries; j++) { sum[j] = vector1[j] + vector2[j]; printf("%d ", sum[j]); } printf("\n\n"); } // Free allocated memory free(vector1); free(vector2); free(sum); return 0;} the program must accept standard inputs
- Assistant
The provided C program is designed to read a certain number of pairs of integer vectors from standard input, add each corresponding pair of elements, and then output the resulting sum vectors. This operation is repeated
n_opstimes wheren_opsand the number of entries in each vector, , are provided by the command-line arguments. - User
write the run.sh program for it
- Assistant
A
run.shscript is a Bash script that you can use to compile and run a C program on Unix-based systems. Assuming that the given C program is saved in a file namedvector_sum.c, therun.shscript might look like the following:#!/bin/bash # Name of the C program file without the extension PROGRAM_NAME="vector_sum" # Number of operations and entries (can be changed according to needs or supplied as arguments) N_OPS=2 N_ENTRIES=4 # Check if the correct number of arguments is given if [ "$#" -ne 2 ]; then echo "Usage: $0 <N_OPS> <N_ENTRIES>" exit 1 fi N_OPS=$1 N_ENTRIES=$2 # Compile the C program echo "Compiling $PROGRAM_NAME.c..." gcc -o $PROGRAM_NAME $PROGRAM_NAME.c -Wall -Wextra # Check if the compilation was successful if [ $? -ne 0 ]; then echo "Compilation failed." exit 1 fi echo "Running $PROGRAM_NAME with N_OPS=$N_OPS and N_ENTRIES=$N_ENTRIES..." echo "Please, enter the $N_ENTRIES elements for each of the $N_OPS vector pairs:" # Run the compiled program with provided arguments ./$PROGRAM_NAME $N_OPS $N_ENTRIES