#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", 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; } write a make.sh and run.sh file for this code and A README file which must be submitted along with your project. It contains an initial header which you must complete (replace the dummy entries with your name, B-number and email address at which you would like to receive project-related email). After the header you may include any content which you would like read during the grading of your project.
- Assistant
To support the compilation and execution of the provided C program, you need to create a
make.shscript for compiling the program, arun.shscript for running the compiled program, and aREADMEfile containing project-related information and instructions.Below are the content for the
make.sh,run.sh, andREADME.mdfiles:make.sh
#!/bin/bash # This script compiles the vector addition program. gcc -o vector_add vector_add.c -Wall if [ $? -eq 0 ]; then echo "Compilation successful. You can now run the program using ./vector_add or using the run.sh script." else echo "Compilation failed." fiMake this script executable by running:
chmod +x make.shrun.sh
#!/bin/bash # This script runs the vector addition program with the given arguments. if [ "$#" -ne 2 ]; then echo "Usage: $0 <n_ops> <n_entries>" exit 1 fi ./vector_add $1 $2Make this script executable by running:
chmod +x run.shREADME.md
# Vector Addition Program ## Author Information * Name: Your Full Name * B-number: BXXXXXXXX * Email: your-email@example.com ## Project Description This project contains a C program that adds pairs of integer vectors. It takes two command-line arguments: the first is the number of operations to perform, and the second is the number of entries in each vector. ## Compilation Instructions To compile the program, run the following command: ```bash ./make.shExecution Instructions
To run the program, use the
run.shscript with the required arguments:./run.sh <n_ops> <n_entries>Where
<n_ops>is the number of vector pairs to add and<n_entries>is the number of elements in each vector.Make sure that the
make.shis executable and has run without errors before attempting to use therun.shscript.Additional Notes
- If you encounter any compilation issues, please make sure GCC is installed on your system.
- Ensure the input provided to the program matches the expected format, as erroneous input may cause unexpected behavior.
- For any questions or concerns, please contact me at the provided email address.
Note that the Markdown format (with .md extension) is used for the README to take advantage of its simple formatting. The README should be updated with your actual personal and project information. Don't forget to rename your C source file to `vector_add.c` if it's currently named differently, or change the gcc compilation line in `make.sh` to match your actual source file name.