My code so far.
prompt: The program then takes the following approach to sort the numbers in the array:
- it stores the value of n in a temporary variable temp,
- it searches for the largest number in the array, starting with position 0 and ending with position temp-1 (use the previous algorithm for finding the largest element of an array),
- it then switches the field that stores the largest value with the field indexed by temp-1,
- the program decrements temp by 1 (temp--),
- the program repeats steps b-d until temp is decremented to 1 in step d.
The program then prints the numbers of the array to the screen on a single line. Please note that in step c., the program does not need to switch values if the largest value is already located in temp-1. Also, for this program, use the previously implemented algorithm for finding the largest value in a set of numbers but apply it to an array and implement it as a function with the following signature:
Had to email him already asking clarification. My code so far. I believe I got it to sort correctly and now I need to have it change (step C). Professor gave us hint of change by reference not value. Didn't think we could do that in C
Code:
#include <stdio.h>
int main(){
int i,n,j;
float arr[100];
printf("Enter total number of elements(1 to 100): ");
scanf("%d",&n);
printf("\n");
for(i=0;i<n;++i) /* Stores number entered by user. */
{
printf("Please enter Number %d: ",i+1);
scanf("%f",&arr[i]);
}
for(i=1;i<n;++i) /* Loop to store largest number to arr[0] */
{
if(arr[0]<arr[i]) /* Change < to > if you want to find smallest element*/
arr[0]=arr[i];
}
for(i=1;i<n;++i)
if(arr[0] >= n)
{arr[i]==n;
n - 1;
}
printf("Largest element = %.2f",arr[0]);
printf("\n"); //Drop a line
printf("%i",arr[i]);
return 0;
}