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

C PROGRAMMING Develope a C program using the EWARM simulator that implements and

ID: 3560906 • Letter: C

Question

C PROGRAMMING

Develope a C program using the EWARM simulator that implements and tests 3 utility functions for processing arrays. Start with the following program skeleton:

# include <stdio.h>

# include <stdlib.h>

# define SEED 123

int x[10];

void initArray (int a[ ], int size, int min, int max, int seed);

void printArray (int a[ ], int size, char name[ ]);

void sortArray (int a[ ], int size);

int main ( )

{

initArray (x, 10, 10, 100, SEED);

printArray(x, 10, "array x");

sortArray (x, 10);

printArray (x, 10, "array x Sorted");

return 0;

}

void printArray (int a[ ], int size, char name[ ])

{

}

void initArray (int a[ ], int size, int min, int max, int seed)

{

}

void sortArray (int a[ ], int size)

{

}

The function printArray should print the string name followed by the elements of array a in a column. The function initArray should initialize array a to random values between min and max; it should seed the random number generator with seed before generating these values. The function sortArray should sort the a in ascending order. The function sortArray tests these functions; it initialize array x, prints it, sort it, and then prints it again. A sample runs follows.

array x:

25

63

63

95

35

41

33

85

91

19

array x Sorted:

19

25

33

35

41

63

63

85

91

95

The program will sort the same random array each time it runs., unless the define statement is changed to specify a different seed.

Explanation / Answer

# include <stdio.h>
# include <stdlib.h>
# define SEED 123

int x[10];
void initArray (int a[ ], int size, int min, int max, int seed);
void printArray (int a[ ], int size, char name[ ]);
void sortArray (int a[ ], int size);

int main ( )
{
   initArray (x, 10, 10, 100, SEED);
   printArray(x, 10, "array x");
   sortArray (x, 10);
   printArray (x, 10, "array x Sorted");
   return 0;
}

void printArray (int a[ ], int size, char name[ ])
{
   int i = 0;

   printf("%s ", name);
   for (i = 0; i < size; i++)
   {
       printf("%d ", a[i]);
   }
}

void initArray (int a[ ], int size, int min, int max, int seed)
{
   int i = 0;
   srand(seed);
   for (i = 0; i < size; i++)
   {
       a[i] = (rand() % (max - min + 1) + min);
   }
}

void sortArray (int a[ ], int size)
{
   int i = 0;
   int j = 0;
   int tmp = 0;

   for (i = 0; i < size; i++)
   {
       for (j = 0; j < size; j++)
       {
           if (a[i] > a[j])
           {
               tmp = a[i];
               a[i] = a[j];
               a[j] = tmp;
           }
       }
   }
}