Also post all the errors you are encountering.

http://www.experts-exchang

Solved

Posted on 2008-11-11

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

16 Comments

Also post all the errors you are encountering.

http://www.experts-exchang

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/wi

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

Title | # Comments | Views | Activity |
---|---|---|---|

Way to scan the path | 41 | 158 | |

How to Deploy a Windows C++ Application With Libraries | 9 | 126 | |

A way to create a macro table? (or: a #define that expands to multiple #defines) | 6 | 46 | |

Arduino EDI - Programming language | 3 | 31 |

Join the community of 500,000 technology professionals and ask your questions.

Connect with top rated Experts

**22** Experts available now in Live!