Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Instead of using #define MAXSZ 75 to predefine the maximum size of arrays that h

ID: 3917332 • Letter: I

Question

Instead of using

     #define MAXSZ 75

to predefine the maximum size of arrays that hold input and output vectors, use malloc() to allocate the precise amount of memory needed by the app process at run-time. That is, after reading the first number from vectors_in.dat that specifies the size of the vectors, use malloc() inside read_vecs() to allocate contiguous memory for the 1-D arrays. read_vecs() is modified to accomplish dynamic memory allocation with function definition

     int read_vecs(double **, double **, int *);

which allows the address of A and B to be passed. Declare A and B as pointers to double and local to main(). C and D are also pointers to double but remain global. Call malloc() inside main() to allocate appropriate heap memory for C and D.

The legacy vector calculation functions remain backward compatible and need not be modified. Reuse their existing object files when creating an updated shared libary using Makefile. Also, use variable definitions such as

     CC = gcc
     OBJ = main.o read_vecs.o print_result.o calc_diff.o calc_sum.o calc_max.p calc_min.o

and CFLAGS, among others, that you find useful to create a more transparent, modular and extensible Makefile.

main.c

#include<stdio.h>

#define MAXSZ 75

#include "Declarations.h"

int main()

{

double A[MAXSZ];

double B[MAXSZ];

int vecdim = 0;

int retVal = read_vecs(A, B, &vecdim);//input vectors

if(retVal == -1)

{

printf("ERROR! Bad value supplied! ");

return -1;

}//check

calc_diff(A, B, vecdim);

calc_sum(A, B, vecdim);//get diff and sum

print_result(vecdim);//print result

  

}

read_vecs.c

#pragma once

#include<stdio.h>

#include"Declarations.h"

int read_vecs(double *A, double *B, int *vecdim)

{

int i;

printf("Enter the vectors' size: ");

int retVal = scanf("%d", vecdim);//enter verctor size

if(retVal == 0)

return -1;

if(*vecdim > 75)

{

printf("Size must be less than or equal to 75. ");

return -1;

}//check vector size

  

printf(" Enter the values of vector A and B seperated by space. ");

for(i = 0; i < *vecdim; i++)

{

retVal = scanf("%lf %lf", &A[i], &B[i]);//enter value

if(retVal != 2)

return -1;

}

}

Explanation / Answer

#include<stdio.h>
#include<stdlib.h>
//#include "Declarations.h"

int read_vecs(double **A, double **B, int *vecdim)

{

int i;

printf("Enter the vectors' size: ");

int retVal = scanf("%d", vecdim);//enter verctor size

if(retVal == 0)

return -1;

if(*vecdim > 75)

{

printf("Size must be less than or equal to 75. ");

return -1;

}

*A=(double* )malloc((*vecdim) * (sizeof(double)));

*B=(double* )malloc((*vecdim)* (sizeof(double)));

printf(" Enter the values of vector A and B seperated by space. ");

for(i = 0; i < *vecdim; i++)

{

retVal = scanf("%lf %lf", &A[0][i], &B[0][i]);//enter value

if(retVal != 2)

return -1;

}

}

int main()

{

double *A;

double *B;

int vecdim = 0;

int retVal = read_vecs(&A, &B, &vecdim);

if(retVal == -1)

{

printf("ERROR! Bad value supplied! ");

return -1;

}


//check

//calc_diff(A, B, vecdim);

//calc_sum(A, B, vecdim);//get diff and sum

//print_result(vecdim);//print result

  

}

for any query please comment.

please upvote if find it helpful.

Thank you.