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

Separate Compilation For this lab, you will get some small practice compiling an

ID: 3883291 • Letter: S

Question

Separate Compilation

For this lab, you will get some small practice compiling and linking a program using make (and writing programs using arrays). Make sure to read the given code carefully, so that you understand what is being given to functions as input and what is being returned as output.

First, you are given the contents of the file array.h below (which may not be modified):

void print_array(const int nums[], const unsigned int size);

int find_min_loc(const int nums[], const unsigned int size);

and contents of the file main.c below (which also may not be modified):

#include <stdio.h>

#include "array.h"

int main(int args, char* argv[])

{

const int vals[] = {1, 5, 3, 8, 9, 7, 0, 4};

const int size = 8;

  print_array(vals, size);

printf(" ");

printf(" ");

int min_loc = find_min_loc(vals, size);

printf("The min value %d was found at index %d ",

          vals[min_loc], min_loc);

return 0;

}

You will need to create the files :

array.c

Makefile

Such that when all the files are in one directory (in UNIX) typing

make

followed by

./main

Will produce the following output:

$ make

gcc -Wall -c array.c

gcc -Wall -c main.c

gcc array.o main.o -o main

$ ./main

[1, 5, 3, 8, 9, 7, 0, 4]

The min value 0 was found at index 6

When the program correctly compiles - using make, and runs –

Explanation / Answer

#include <stdio.h>
#include "array.h"
int main(int args, char* argv[])
{
const int vals[] = {1, 5, 3, 8, 9, 7, 0, 4};
const int size = 8;

print_array(vals, size);
printf(" ");

printf(" ");
int min_loc = find_min_loc(vals, size);
printf("The min value %d was found at index %d ",
vals[min_loc], min_loc);

return 0;
}
void print_array(const int nums[], const unsigned int size) {
int i;
printf("[");
for(i=0;i<size;i++) {
if(i == size-1) {
printf("%d", nums[i]);
} else {
printf("%d, ", nums[i]);
}
}
printf("]");
printf(" ");
}
int find_min_loc(const int nums[], const unsigned int size) {
int i;
int min = nums[0];
int minIndex = 0;
for(i=0;i<size;i++) {
if(min > nums[i]){
min = nums[i];
minIndex = i;
}
}   
return minIndex;
}

Output:

sh-4.4$ g++ -o main *.cpp                                                                                                                                                                            

sh-4.4$ main                                                                                                                                                                                         

[1, 5, 3, 8, 9, 7, 0, 4]                                                                                                                                                                             

                                                                                                                                                                                                     

                                                                                                                                                                                                     

The min value 0 was found at index 6

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote