Identify excessive bandwidth utilization or unexpected application traffic with SolarWinds Bandwidth Analyzer Pack.

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;
}
```

Experts Exchange Solution brought to you by

Enjoy your complimentary solution view.

Get every solution instantly with Premium.
Start your 7-day free trial.

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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.

./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;
}
```

>> void mySort(int a[], int num_elements);

That shouldn't be there.

void mySort(float a[], int num_elements)

However I now get an error

$ ./sorting

The input array is:

0.6230960.4908900.2634660.

Segmentation Fault (core dumped)

Any ideas on how to fix this?

```
#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;
}
```

The same is true for 'i'.

Note that you never modify 'i' - so I don't understand what you're trying to do with it.

Take a closer look at the selection sort algorithm :

http://en.wikipedia.org/wiki/Selection_sort

and make sure you implement it correctly.

The input array is:

0.3393660.0413830.8718220.

After sorting, the array is:

10709709231067790378107242

```
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;
}
```

2) since you're sorting floats, maybe you should use float's instead of ints.

3) you still only have one loop. Take a closer look at the selection sort algorithm - there are two nested loops.

Now, however, it says that i, min_element_index, and min_element_value are undeclared. Do you know why?

```
void mySort(float A[], int num_elements)
{
int sorted_size = 0;
for(sorted_size=0; sorted_size<num_elements; sorted_size++)
{
float min_element_index = sorted_size;
float min_element_value = A[sorted_size];
int i;
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;
}
```

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
C

From novice to tech pro — start learning today.

Experts Exchange Solution brought to you by

Enjoy your complimentary solution view.

Get every solution instantly with Premium.
Start your 7-day free trial.

Also post all the errors you are encountering.

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