Sort array using flowchart

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • JWest46088
    New Member
    • Sep 2006
    • 74

    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:

    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;
    }
    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.
  • Laharl
    Recognized Expert Contributor
    • Sep 2007
    • 849

    #2
    Double post, sorry. Read down.

    Comment

    • Laharl
      Recognized Expert Contributor
      • Sep 2007
      • 849

      #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.

      Comment

      • JWest46088
        New Member
        • Sep 2006
        • 74

        #4
        Originally posted by Laharl
        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

        Comment

        • JWest46088
          New Member
          • Sep 2006
          • 74

          #5
          Does anybody have any ideas why the sort function isn't being called?

          Comment

          • Ganon11
            Recognized Expert Specialist
            • Oct 2006
            • 3651

            #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:

            [CODE=cpp]for (int i = 0; i < ARRAY_SIZE; i++) { //ARRAY_SIZE is how many elements are in the array
            // Do whatever: Maybe you want to input into the array?
            cout << "Enter the " << i + 1 << "th term: ";
            cin >> a[i];
            cout << endl;

            // Or print the array?
            cout << "a[" << i << "]: " << a[i] << endl;
            }[/CODE]

            Comment

            • JWest46088
              New Member
              • Sep 2006
              • 74

              #7
              Originally posted by Ganon11
              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:

              [CODE=cpp]for (int i = 0; i < ARRAY_SIZE; i++) { //ARRAY_SIZE is how many elements are in the array
              // Do whatever: Maybe you want to input into the array?
              cout << "Enter the " << i + 1 << "th term: ";
              cin >> a[i];
              cout << endl;

              // Or print the array?
              cout << "a[" << i << "]: " << a[i] << endl;
              }[/CODE]

              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.

              Comment

              • JWest46088
                New Member
                • Sep 2006
                • 74

                #8
                Segmentation fault

                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?

                Comment

                Working...