selection sorting

Nov 23, 2019 at 3:45pm
hey guys, I am fairly new to c++ and I was trying to finish this code for my last lab. I've tried almost everything but some reason I can not get my numbers to present themselves correctly.

#include <iostream>
using namespace std;

void sSort(int [],int);
void myArray(const int [], int);

int main()
{
const int size = 10;
int values[size];
int n;
cout << " Enter any some numbers : ";
cin >> n;
myArray (values,size);

sSort(values, size);

cout << " These are your ascending numbers :" << endl;
myArray (values,size);
return 0;


}

void sSort(int array[], int size)
{

int start, minIndex, minValue;

for (start = 0; start < (size -1); start++)
{
minIndex = start;
minValue = array[start];
for(int index = start + 1; index < size; index++)
{

if (array[index] < minValue)
{

minValue = array[index];

minIndex = index;

}
}

array[minIndex] = array[start];
array[start] = minValue;
}
}
void myArray(const int array[], int size)
{
for (int count = 0; count < size; count++)

cout << array[count] << " ";

cout << endl;
}
Nov 23, 2019 at 4:05pm
I've tried almost everything but some reason I can not get my numbers to present themselves correctly.

What does this even mean?

Please show what you have inputted into the program, the output of the program, and your expected output with the given input.

Where are you even assigning any values to your array elements?

Nov 23, 2019 at 4:06pm
How is this going to fill your value array with 10 values?

1
2
3
int n;
cout << " Enter any some numbers : ";
cin >> n;

Nov 23, 2019 at 5:07pm
you got myArray function right.
reading into the array is pretty much the same idea:
cin >> values[index];
you are not reading anything into the array, and then printing the uninitialized array which is full of junk.
Last edited on Nov 23, 2019 at 5:08pm
Nov 25, 2019 at 5:51am
Modify your main() function as below:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int main()
{
const int size = 10;
int values[size];

cout << " Enter any some numbers : ";
  for(int i=0; i<size; i++)
  {
    cin>>values[i];
  }

sSort(values, size);

cout << " These are your ascending numbers :" << endl;
myArray (values,size);
return 0;
}


For more information about sorting algorithms, I would suggest visiting:
https://www.alphacodingskills.com/algo/algo-tutorial.php
Topic archived. No new replies allowed.