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

1. Create a file named lab3.c. Copy the following code into your program. This i

ID: 3887159 • Letter: 1

Question

1. Create a file named lab3.c. Copy the following code into your program. This is the initial version of your "test program". You will modify this file as you go through the process of writing and testing the functions in this assignment. The comments are there to show examples of what you might do to test your other functions. include #include #include "helperfunctions.h" #include "sort ingfunctions.h" ide fine ARRAYS IZE 10 int main (void) t // dynamically allocate memory space for an array int * arrayptr = (int *) malloc (ARRAYS I ZE * sizeof (int)); // fill the array with random integers // print the array, 10 elements per line // sort the array using selection sort // print the array, 10 elements per line // fill the array again with random integers // print the array // sort with insertion sort // print the array

Explanation / Answer

lab3.c

#include <stdio.h>
#include <stdlib.h>
#include "helperfunctions.h"
#include "sortingfucntions.h"
#include "helperfunctions.c"
#include "sortingfucntions.c"

#define ARRAYSIZE 10

int main()
{
//dynamically allocate memory space for an array
int *arr=(int*)malloc(ARRAYSIZE*sizeof(int));

//fill the array with random number of digits
fillarray(arr);

//print the array, 10 elements per line
printArray(arr);

//sort using the selection sort
selectionSort(arr,ARRAYSIZE);

//print the array, 10 elements per line
printArray(arr);

//fill the array again with the random integers
fillarray(arr);

//sort the array with the insertion sort
insertionSort(arr,ARRAYSIZE);

//print the array
printArray(arr);
return 0;
}

sortingfunctions.h

#ifndef SORTINGFUNCTIONS_H_INCLUDED
#define SORTINGFUNCTIONS_H_INCLUDED

#endif // SORTINGFUNCTIONS_H_INCLUDED

int selectionSort(int *const data,int size);
int insertionSort(int *const data,int size);

helperfunctions.h

#ifndef HELPERFUNCTIONS_H_INCLUDED
#define HELPERFUNCTIONS_H_INCLUDED

#endif // HELPERFUNCTIONS_H_INCLUDED

void swap(int *num1,int *num2);
void fillArray(int *const data,int size,int min,int max);
void neatPrint (int * const data,int size, int numPerLine,int fieldSize);

sortingfunctions.c

int selectionSort(int * const data,int size)
{
int i,j;
int count;
count=0;
int min_index;
for(i=0;i<size-1;i++)
{
for(j=i+1;j<size;j++)
{
count++;
if(*(data+j)<*(data+min_index))
{
min_index=j;
}
}
swap((data+min_index),(data+i));
}
return count;
}

int insertionSort(int * const data,int size)
{
int i,key,j;
int count;
count=0;

for(i=1;i<size;i++)
{
key = *(data+i);
j = i-1;

while ((j >= 0) && (*(data+j) > key))
{
count++;
*(data+j+1) = *(data+j);
j = j-1;
}
*(data+j+1) = key;
}
return count;
}

helperfucntions.c

void swap(int *num1,int *num2)
{
int t;
t=*num1;
*num1=*num2;
*num2=t;
}

void fillArray(int * const data,int size,int min,int max)
{
int i,diff;
diff=max-min;

for(i=0;i<size;i++)
{
*(data+i)=min+(rand()%diff);
}
}

void neatPrint(int * const data,int size,int numPerLine,int fieldSize)
{
int i;
int count;
count=0;
for(i=0;i<size;i++)
{
if (count>numPer)
{
count=0;
printf(" ");
}
else
{
printf("%d ",*(data+i));
}
}
}