I need to write a code that sorts a random array from least to greatest. My teacher gave us most of the code, however, we have to fill in what is missing. The problem is that I can't figure out what to fill in because everything I try creates some type of error with the variables or function. I know it should only be a few lines to fill in but I'm dying with this!

/******************************************************************* This program uses Selection Sorting to sort a random array in order from least to greatest. Alexandra Cabral ************************************************************************/#include <stdio.h>#include <stdlib.h>#include <time.h>/******Here is where my code Begins *******//******Here is where my code Ends*******/int main (){ float A[10], answer; int i; srand (time(NULL)); /* Seed the random number generator with the time */ printf("The input array is:\n"); for (i=0; i<10; i++) { /* rand() returns a random it. We want a random float, so divide by 32767 */ A[i] = (float)rand()/32767.0; printf("%f", A[i]); } printf("\n"); /* finish the line */ mySort(A,10); printf("After sorting, the array is:\n"); for (i=0; i<10; i++) { printf("%f", A[i]); } printf("\n"); return 0;}

Membership agreement does not permit us to do assignments for students. However, we are allowed to guide you to a solution. Post your efforts so far and we will try to get you on track.
Also post all the errors you are encountering.

>> The problem is that I can't figure out what to fill in

You have to create the mySort function, and implement selection sort in it.

The function is called like this :

mySort(A,10);

with two arguments : an array of floats, and the size of that array. Make sure that your function accepts those arguments, and then sorts the array using the selection sort algorithm.

0

The enterprise network environment is evolving rapidly as companies extend their physical data centers to embrace cloud computing and software-defined networking. This new reality means that the challenge of managing the security policy is much more dynamic and complex.

I've added in what I believed should have worked however, I apparently have a missing '(' and I don't understand what this means or how to fix it:

./sorting.c: In function 'main':
./sorting.c:47: warning: passing argument 1 of 'mySort' from incompatible pointer type

#include <stdio.h>#include <stdlib.h>#include <time.h>/******Here is where my code Begins *******/void mySort(int a[], int num_elements);{ int min_element_index = sorted_size; int min_element_value = a[sorted_size]; int i; int sorted_size; for (sorted_size=0; sorted_size < num_elements; sorted_size++) { if (a[i] < min_element_value) { min_element_index = i; min_element_value = a[i]; } } a[min_element_index] = a[sorted_size]; a[sorted_size] = min_element_value;}/******Here is where my code Ends*******/int main (){ float A[10], answer; int i; srand (time(NULL)); /* Seed the random number generator with the time */ printf("The input array is:\n"); for (i=0; i<10; i++) { /* rand() returns a random it. We want a random float, so divide by 32767 */ A[i] = (float)rand()/32767.0; printf("%f", A[i]); } printf("\n"); /* finish the line */ mySort(A,10); printf("After sorting, the array is:\n"); for (i=0; i<10; i++) { printf("%d", A[i]); } printf("\n"); return 0;}

Can you show the current code you are using ? Because your previous code will not compile due to usage of 'sorted_size' before it's declared.

0

spngegirl01Author Commented:

#include <stdio.h>#include <stdlib.h>#include <time.h>/******Here is where my code Begins *******/void mySort(float a[], int num_elements){ int sorted_size; int min_element_index = sorted_size; int min_element_value = a[sorted_size]; int i; for (sorted_size=0; sorted_size < num_elements; sorted_size++) { if (a[i] < min_element_value) { min_element_index = i; min_element_value = a[i]; } } a[min_element_index] = a[sorted_size]; a[sorted_size] = min_element_value;}/******Here is where my code Ends*******/int main (){ float A[10], answer; int i; srand (time(NULL)); /* Seed the random number generator with the time */ printf("The input array is:\n"); for (i=0; i<10; i++) { /* rand() returns a random it. We want a random float, so divide by 32767 */ A[i] = (float)rand()/32767.0; printf("%f", A[i]); } printf("\n"); /* finish the line */ mySort(A,10); printf("After sorting, the array is:\n"); for (i=0; i<10; i++) { printf("%d", A[i]); } printf("\n"); return 0;}

I've changed my part of the code to read as it is below. Now I get :

The input array is:
0.3393660.0413830.8718220.6124150.5727100.5934020.9660630.7789240.4258250.447249
After sorting, the array is:
10709709231067790378107242443910718804231071797156107184055010726220771072229617

void mySort(float A[], int num_elements){ int sorted_size; int min_element_index = sorted_size; int min_element_value = A[sorted_size]; int i; sorted_size = 0; for (i=sorted_size+1; i < num_elements; i++) { if (A[i] < min_element_value) { min_element_index = i; min_element_value = A[i]; } } A[min_element_index] = A[i]; A[i] = min_element_value;}

Well, you declared them inside the loop, but are trying to use them outside of the loop. That won't work :) The scope of the variables is the nearest enclosing block - once the block ends, the variables are not valid any more.

0

spngegirl01Author Commented:

Thank you so much!

0

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Also post all the errors you are encountering.

http://www.experts-exchange.com/memberAgreement.jsp