|
-
May 2nd, 2009, 11:17 PM
#1
What is wrong with my sorting function?
What is wrong with my sorting function? I am trying to put numbers in ascending numbers.
Code:
void sort(int array[], int numts)
{
int lowIndex, lowest;
for (int count = 0; count < (numts-1); count++)
{
lowIndex = count;
lowest = array[count];
for (int index = count + 1; index < numts; index++)
{
if (array[index] < lowest)
{
lowest = array[index];
lowIndex = index;
}
}
array[lowIndex] = array[count];
array[count] = lowest;
}
return;
}
I am confused now for I believe I have tried everything.
-
May 3rd, 2009, 01:03 AM
#2
Re: What is wrong with my sorting function?
Looks right to me. What's wrong with it?
-
May 3rd, 2009, 01:20 AM
#3
Re: What is wrong with my sorting function?
It works fine:
Code:
#include <iostream>
#include <windows.h>
using std::cout;
void sort(int array[], int numts)
{
int lowIndex, lowest;
for (int count = 0; count < (numts-1); count++)
{
lowIndex = count;
lowest = array[count];
for (int index = count + 1; index < numts; index++)
{
if (array[index] < lowest)
{
lowest = array[index];
lowIndex = index;
}
}
array[lowIndex] = array[count];
array[count] = lowest;
}
return;
}
int main()
{
int arr[] = {1, 0, 2, 5, 4, 3, 7, 6, 2};
sort(arr, 9);
for(int i=0;i<sizeof(arr)/sizeof(int);i++) cout << arr[i] << "\n";
while(true) Sleep(1000);
return 0;
}
-
May 3rd, 2009, 09:07 AM
#4
Re: What is wrong with my sorting function?
Thanks for the responses...it wasn't being displayed, so I copied the showarray function and it showed...I am trying to add pointers to it though but all hell breaks lose then and I get this error (error C2664: 'showArray' : cannot convert parameter 1 from 'int *' to 'int *[]')....example...
Code:
/***This program dynamically allocates an array large enough to
hold a user-defined number of test scores***/
#include <iostream>
#include <iomanip>
using namespace std;
//Function prototypes
int getnumtestscores();
void gettestscores(int[], int);
void showArray(int *[], int);
void sort(int [], int);
double average(int[], int);
int main()
{
int numts = getnumtestscores();
int *testscores = new int[numts]; //To dynamically allocate an array
gettestscores(testscores, numts);
cout << "The testscores are: \n"; showArray (testscores, numts);
cout << "The testscores sorted are: \n"; sort (testscores, numts);
showArray (testscores, numts);
cout << "The average of all " << numts << " test scores is: "
<< fixed << showpoint << setprecision(2)
<< average(testscores, numts);
cout << endl;
// Free dynamically allocated array in memory
delete [] testscores;
// Make scores point to null
testscores = 0;
return 0;
}
int getnumtestscores()
{
int num;
cout << "\n How many test scores do you want to enter : ";
for(;;) // loop forever ... until return
{
cin >> num;
return num;
}
}
void gettestscores(int testscores[], int numts)
{
cout << "What are the testscores for the students?\n";
for (int count = 0; count < numts; count++)
{
cout << "Testscore " << setw(2) << (count+1) << ":";
if (testscores < 0) //Input validation
{
cout << "An invalid score was entered, please enter a score more than 0\n";
}
cin >> testscores[count];
}
}
void sort(int *array[], int numts)
{
int lowIndex, *lowest;
for (int count = 0; count < (numts-1); count++)
{
lowIndex = count;
lowest = array[count];
for (int index = count + 1; index < numts; index++)
{
if (*(array[index]) < *lowest)
{
lowest = array[index];
lowIndex = index;
}
}
array[lowIndex] = array[count];
array[count] = lowest;
}
return;
}
void showArray(int array[], int numts)
{
for (int count = 0; count < numts; count++)
cout << array[count] <<" \n";
cout << endl;
}
double average(int array[], int numts)
{
int total = 0;
for (int count = 0; count < numts; count++)
total +=array[count];
return total/numts;
}
-
May 3rd, 2009, 09:55 AM
#5
Re: What is wrong with my sorting function?
 Originally Posted by musique
Thanks for the responses...it wasn't being displayed, so I copied the showarray function and it showed...I am trying to add pointers to it though but all hell breaks lose then and I get this error (error C2664: 'showArray' : cannot convert parameter 1 from 'int *' to 'int *[]')....example...
It would help if you dropped the "[]" syntax for your array parameters. This should make it much more clear as to what you're passing to these functions. There are still several things inconsistent in your program.
When you pass an array, you are passing a pointer to the first element in the array. Therefore this definition:
Code:
void showArray(int array[], int numts);
Is no different than this one:
Code:
void showArray(int* array, int numts)
So you are never "passing arrays", you are always passing a pointer to the first element. Using "int *", should have been the argument type to all of your functions when dealing with arrays. For example:
Code:
void sort(int *array[], int numts)
This should be:
Code:
void sort(int *array, int numts)
You also should do more thorough checking in your function prototypes and your function definitions match.
Code:
void showArray(int *[], int);
//...
void showArray(int [], int) // ?????
{
//...
}
Regards,
Paul McKenzie
-
May 3rd, 2009, 04:43 PM
#6
Re: What is wrong with my sorting function?
Thanks, will change it. Pointers are so confusing though...still trying to work on it.
-
May 3rd, 2009, 04:45 PM
#7
Re: What is wrong with my sorting function?
Pointers are only as confusing as you let them be. In this context, think of a pointer simply as an array of unknown size (that's why you pass the size parameter too).
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|