?
Solved

Selection Sorting in C Programming

Posted on 2008-11-11
16
Medium Priority
?
4,053 Views
Last Modified: 2012-05-05
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
Comment
Question by:spngegirl01
16 Comments
 
LVL 45

Expert Comment

by:sunnycoder
ID: 22928472
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
 
LVL 24

Expert Comment

by:fridom
ID: 22928590
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 22928820
>> 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:spngegirl01
ID: 22930545
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
 
LVL 53

Expert Comment

by:Infinity08
ID: 22930578
You have a ; at the end of this line :

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

That shouldn't be there.
0
 

Author Comment

by:spngegirl01
ID: 22930924
I fixed that but still get the same errors.  Any ideas?
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 22930980
Your mySort function takes an array of int as first argument. But the main function tries to pass it an array of float.
0
 

Author Comment

by:spngegirl01
ID: 22932442
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
 
LVL 53

Expert Comment

by:Infinity08
ID: 22932469
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
 

Author Comment

by:spngegirl01
ID: 22932491

#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
 
LVL 53

Expert Comment

by:Infinity08
ID: 22932543
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
 

Author Comment

by:spngegirl01
ID: 22932740
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
 
LVL 53

Expert Comment

by:Infinity08
ID: 22932787
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
 

Author Comment

by:spngegirl01
ID: 22932897
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
 
LVL 53

Accepted Solution

by:
Infinity08 earned 2000 total points
ID: 22932945
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
 

Author Comment

by:spngegirl01
ID: 22933016
Thank you so much!
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

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

Join & Ask a Question