First off thanks to Prelude for his help with generating the random numbers into a dynamic array. Now I had no problem getting the high and low value to return to main, but I cant seem to figure out the middle value. My array has 11 elements, so I know array[6] will be the mid value of a sorted array, but we were told not to do a formal sort, ie bubble, shell, quick, selection, insert, blah blah blah. I also know that if I sort just the first 6 element ascending or descending that I will get middle value by returning the 6th element; anyway here is my code as it is right now, any help would be appreciated, dont want to answer as much as a direction.
Code:
//------------------------Preprocessor Directives-----------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <conio.h>
//-------------------------Function Prototypes--------------------------------------
int getsmallest(int *wNum);
int getLargest( int*wNum);
int getMiddle(int *wNum);
//----------------------------Function Main----------------------------------------------
int main(void)
{
//-----------------------------Variable Declaration----------------------------------
int largest;
int smallest;
int i;
int middle;
int *wNum;
clrscr();
printf("EC03SRS996\n");
srand ( (unsigned)time ( NULL ) );
if ( ( wNum = malloc ( 11 * sizeof *wNum ) ) != NULL )
printf("Unsorted Sorted\n");
{
for ( i = 0; i < 11; i++ )
{
wNum[i] = ( rand() * 50 )/ RAND_MAX ;
printf(" %d\n",wNum[i]);
}
smallest = getsmallest(wNum);
printf(" %d\n",smallest);
largest = getLargest(wNum);
printf(" %d", largest);
middle = getMiddle(wNum);
printf(" %d", middle);
free ( wNum );
}
getch();
return 0;
}
//----------------------------End of Function Main--------------------------------------------
//-------------------------User definition of getsmallest-------------------------------------
int getsmallest(int *wNum)
{
int *pLast;
int *pSmallest;
int *pWalk;
pLast = wNum + 10;
for (pSmallest = wNum, pWalk = wNum +1; pWalk <= pLast;pWalk++)
if (*pWalk < *pSmallest)
pSmallest = pWalk;
return *pSmallest;
}
//------------------------User definition of getlargest----------------------------------------
int getLargest(int *wNum)
{
int *pLargest;
int *pLast;
int *pWalk;
pLast = wNum + 10;
for (pLargest = wNum, pWalk = wNum +1; pWalk <= pLast;pWalk++)
if (*pWalk > *pLargest)
pLargest = pWalk;
return *pLargest;
}
//---------------------User definition of getMiddle---------------------------------------------
int getMiddle(int *wNum)
{
int *pMiddle;
int *pLast;
int temp;
int *pFirst;
int *pWalk;
int middle;
pLast = wNum + 10;
for (pFirst = wNum, pWalk = wNum + 1;*pWalk <= *pLast ; pWalk++)
{
if (*pWalk <= *pMiddle)
temp = pFirst;
pFirst = pWalk;
pWalk = temp;
}
middle = wNum[6];
return middle;
}