I am trying to write a program based off of a flowchart. The program is supposed to sort an array. I am having trouble getting it to sort though. I know it has something to do with my code, but I don't know what or where.
Here is my code:
So, the sort doesn't work at all. The user inputs the number of elements they want in the array and goes through the algorithm and is supposed to print out the sorted array. However, I don't think the sort function is even being called because I put in cout statements to make sure that it went to the function and the statement isn't being printed out.
Here is my code:
Code:
#include <iostream>
using namespace std;
int n;
int a[6];
int main()
{
int i;
cout << "How many elements do you want in the array?\nNote: It must be less than or equal to five and greater than 0. ";
cin >> n;
cout << endl;
if((n > 5) || (n < 1))
{
cerr << "The size of the array is not valid!\n\n";
return 0;
}
for(i = 1; i <= n; i++)
{
cout << "Enter a number to put in the array: ";
cin >> a[i];
}
cout << "\nOriginal array: ";
for(i = 1; i <= n; i++)
cout << a[i] << " ";
void sort(int n, int a[]);
cout << "\nSorted array: ";
for(i = 1; i <= n; i++)
cout << a[i] << " ";
cout << endl;
return 0;
}
void sort(int n, int a[])
{
int j;
cout << "sort test before call to move\n";
for(j = 1; j <= n-1; j++)
{
if(a[j] > a[j+1])
int move(int a[], int j);
}
cout << "sort test after call to move\n";
return;
}
int move(int a[], int j)
{
int k;
int temp = a[j+1];
a[j+1] = a[j];
cout << "move test before call to findkay\n";
int Findkay(int k, int j, int a[], int temp);
cout << "move test after call to findkay\n";
a[k] = temp;
return k;
}
int Findkay(int k, int j, int a[], int temp)
{
k = j;
int sw = 0;
while((k > 1) && (sw = 0))
{
if(a[k-1] > temp)
{
a[k] = a[k-1];
k = k-1;
}
else
sw = 1;
}
cout << "findkay test\n";
return k;
}
Comment