I am still needing understanding of why my largest_element function is not working properly.
What I gather is that 30 elements are randomly generated in an array. The values of which are between 1 and 200. (DONE)
Then another function is called to find the largest element. Now I don't know if I need to return the index of the largest element of the array (that is how it seems) and use this in the selection sorting (ascend_sort and descend_sort) functions or not.
Can someone explain???
finding largest_element
sorting functions
PHP Code:
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#define NUMELEM 30
#define MAXELEM 200
void ascend_sort(int a[],int n);
void descend_sort(int a[], int n);
void random_array(int a[], int n);
int largest_element (int a[], int f, int l);
main()
{
int array[NUMELEM];
int last=0;
/* create array */
random_array(array,NUMELEM);
/* need to call function largest_element and assign value*/
/* returned to a variable to be used in the selection_sort */
/* functions named ascend_sort and descend_sort */
last=largest_element(array,array[0],array[NUMELEM-1]);
printf("%d",last);
}
/* find largest element */
/* FUNCTION: largest_element : my resource text says to */
/* make i and j point to the first element of array (but it */
/* does not assume a[0] is the first element for some */
/* reason; f and l are representing first and last elements */
/* of array passed */
int largest_element(int a[], int f, int l)
{
int i=f;
int j=f;
do {
i++;
if (a[i] > a[j]) {
j=i;
}
} while (i != l);
return j;
}
/* sort in ascending order using selection sorting */
void ascend_sort(int a[],int n)
{
int largest;
int temp;
}
/* sort in descending order using selection sorting */
void descend_sort(int a[], int n)
{
}
void random_array(int a[], int n)
{
int i;
for (i=0; i < NUMELEM ; i++)
{
a[i] = rand()%MAXELEM+1;
printf("%3d ",a[i]);
if ((i+1)%10==0)
printf("\n");
}
}