• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 4059
  • Last Modified:

Selection Sorting in C Programming

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

Open in new window

0
spngegirl01
Asked:
spngegirl01
1 Solution
 
sunnycoderCommented:
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.

http://www.experts-exchange.com/memberAgreement.jsp
0
 
fridomCEO/ProgrammerCommented:
0
 
Infinity08Commented:
>> 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
Managing Security Policy in a Changing Environment

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.

 
spngegirl01Author Commented:
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;
}

Open in new window

0
 
Infinity08Commented:
You have a ; at the end of this line :

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

That shouldn't be there.
0
 
spngegirl01Author Commented:
I fixed that but still get the same errors.  Any ideas?
0
 
Infinity08Commented:
Your mySort function takes an array of int as first argument. But the main function tries to pass it an array of float.
0
 
spngegirl01Author Commented:
I changed the int to float in this line:

void mySort(float a[], int num_elements)

However I now get an error

$ ./sorting
The input array is:
0.6230960.4908900.2634660.2409740.0606710.0681170.7037260.3868530.8798490.841884
Segmentation Fault (core dumped)

Any ideas on how to fix this?
0
 
Infinity08Commented:
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;
}

Open in new window

0
 
Infinity08Commented:
You haven't initialized 'sorted_size'. It could contain any value. You should initialize it to 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.
0
 
spngegirl01Author Commented:
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;
}

Open in new window

0
 
Infinity08Commented:
1) you still haven't initialized sorted_size - it needs to be initialized BEFORE it's used.

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.
0
 
spngegirl01Author Commented:
I found the other loop, thank you!

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

Open in new window

0
 
Infinity08Commented:
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.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now