468,457 Members | 1,608 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,457 developers. It's quick & easy.

Sort array using flowchart

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:

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int n;  
  5. int a[6];
  6.  
  7. int main()
  8. {       
  9.         int i;
  10.  
  11.         cout << "How many elements do you want in the array?\nNote: It must be less than or equal to five and greater than 0.  ";
  12.         cin >> n;
  13.         cout << endl;
  14.  
  15.         if((n > 5) || (n < 1))
  16.         {
  17.                 cerr << "The size of the array is not valid!\n\n";
  18.  
  19.                 return 0;
  20.         }
  21.  
  22.         for(i = 1; i <= n; i++)
  23.         {
  24.                 cout << "Enter a number to put in the array:  ";
  25.                 cin >> a[i];
  26.         }
  27.  
  28.         cout << "\nOriginal array:  ";
  29.  
  30.         for(i = 1; i <= n; i++)
  31.                 cout << a[i] << " ";
  32.  
  33.         void sort(int n, int a[]);
  34.  
  35.         cout << "\nSorted array:    ";
  36.  
  37.         for(i = 1; i <= n; i++)
  38.                 cout << a[i] << " ";
  39.  
  40.         cout << endl;
  41.  
  42.         return 0;
  43. }
  44.  
  45. void sort(int n, int a[])
  46. {
  47.         int j;
  48.  
  49.         cout << "sort test before call to move\n";
  50.  
  51.         for(j = 1; j <= n-1; j++)
  52.         {
  53.                 if(a[j] > a[j+1])
  54.                         int move(int a[], int j);
  55.         }
  56.  
  57.         cout << "sort test after call to move\n";
  58.  
  59.         return;
  60. }
  61.  
  62. int move(int a[], int j)
  63. {               
  64.         int k;
  65.         int temp = a[j+1];
  66.         a[j+1] = a[j];
  67.  
  68.         cout << "move test before call to findkay\n";
  69.  
  70.         int Findkay(int k, int j, int a[], int temp);
  71.  
  72.         cout << "move test after call to findkay\n";
  73.  
  74.         a[k] = temp;
  75.  
  76.         return k;
  77. }        
  78.  
  79. int Findkay(int k, int j, int a[], int temp)
  80. {       
  81.         k = j;
  82.         int sw = 0;
  83.  
  84.         while((k > 1) && (sw = 0))
  85.         {
  86.                 if(a[k-1] > temp)
  87.                 {
  88.                         a[k] = a[k-1];
  89.                         k = k-1;
  90.                 }
  91.                 else
  92.                         sw = 1;
  93.         }
  94.  
  95.         cout << "findkay test\n";
  96.  
  97.         return k;
  98. }
  99.  
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.
Feb 15 '08 #1
7 6078
Laharl
849 Expert 512MB
Double post, sorry. Read down.
Feb 15 '08 #2
Laharl
849 Expert 512MB
I'm not at all convinced this compiles, given that when you call sort(), you effectively redeclare its parameters by including 'int' before them. Also, if you have n and your array defined in global scope, you don't need to pass them to functions since they're global.
Feb 15 '08 #3
I'm not at all convinced this compiles, given that when you call sort(), you effectively redeclare its parameters by including 'int' before them. Also, if you have n and your array defined in global scope, you don't need to pass them to functions since they're global.
It does compile, and I deleted the parameters of n and a[] from the functions.

Here is the program execution:

How many elements do you want in the array?
Note: It must be less than or equal to five and greater than 0. 4

Enter a number to put in the array: 5
Enter a number to put in the array: 2
Enter a number to put in the array: 3
Enter a number to put in the array: 5

Original array: 5 2 3 5
Sorted array: 5 2 3 5
Feb 15 '08 #4
Does anybody have any ideas why the sort function isn't being called?
Feb 15 '08 #5
Ganon11
3,652 Expert 2GB
Have you used functions before? The way you are trying to call the functions, you are actually defining new functions, and never call them. If you are working from a book, take a look at a few examples of how to call functions.

Also, why are you only using the indexes 1, 2, 3, 4, and 5 of your int array? You should know that array indeces begin at 0, so the first 5 elements of your array are actually a[0], a[1], a[2], a[3], and a[4]. Thus, you would want to use a for loop like this to fill/check/sort your array:

Expand|Select|Wrap|Line Numbers
  1. for (int i = 0; i < ARRAY_SIZE; i++) { //ARRAY_SIZE is how many elements are in the array
  2.   // Do whatever:  Maybe you want to input into the array?
  3.   cout << "Enter the " << i + 1 << "th term: ";
  4.   cin >> a[i];
  5.   cout << endl;
  6.  
  7.   // Or print the array?
  8.   cout << "a[" << i << "]: " << a[i] << endl;
  9. }
Feb 15 '08 #6
Have you used functions before? The way you are trying to call the functions, you are actually defining new functions, and never call them. If you are working from a book, take a look at a few examples of how to call functions.

Also, why are you only using the indexes 1, 2, 3, 4, and 5 of your int array? You should know that array indeces begin at 0, so the first 5 elements of your array are actually a[0], a[1], a[2], a[3], and a[4]. Thus, you would want to use a for loop like this to fill/check/sort your array:

Expand|Select|Wrap|Line Numbers
  1. for (int i = 0; i < ARRAY_SIZE; i++) { //ARRAY_SIZE is how many elements are in the array
  2.   // Do whatever:  Maybe you want to input into the array?
  3.   cout << "Enter the " << i + 1 << "th term: ";
  4.   cin >> a[i];
  5.   cout << endl;
  6.  
  7.   // Or print the array?
  8.   cout << "a[" << i << "]: " << a[i] << endl;
  9. }

I changed the function calls. Instead of void sort(int n, int a[]); that I had before, I changed it to sort(); like it should be, but I'm getting an error that says:

error: no matching function for call to `sort()'

The reason I am starting at array index 1 is because that is how the flowchart has it, and I am following the flowchart.
Feb 15 '08 #7
Prototypes help for the thing to work... ugh.

Getting a segmentation fault now. Should the sort function not be void?

It gets up to the test cout statement "move test after call to findkay"

Why am I getting a segmentation fault?
Feb 15 '08 #8

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by JackieR | last post: by
40 posts views Thread by Elijah Bailey | last post: by
7 posts views Thread by ritchie | last post: by
21 posts views Thread by yeti349 | last post: by
48 posts views Thread by Alex Chudnovsky | last post: by
3 posts views Thread by Paulers | last post: by
19 posts views Thread by William Gill | last post: by
19 posts views Thread by sameer | last post: by
reply views Thread by NPC403 | last post: by
1 post views Thread by subhajit12345 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.