#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2007
    Posts
    41
    Rep Power
    8

    Question Sort array using flowchart HELP!


    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.
  2. #2
  3. Paris est magique!
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2004
    Location
    France!
    Posts
    370
    Rep Power
    96
    Code:
     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] << " ";
    
    
                  // what is that? certainly not a function call.
                  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;
          }

    Comments on this post

    • OmegaZero agrees
    etienne:~ > %blow
    fg: %blow: no such job


    There are 10 kind of people:
    - those who know binary
    - those who don't.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2007
    Posts
    765
    Rep Power
    929
    ...took too long to type
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2007
    Posts
    41
    Rep Power
    8
    I changed the function call to sort();

    I think that is how it should really look, but I'm getting an error that says:

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

    I have a function named void sort() so what's the problem?

    Originally Posted by etienne141
    Code:
     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] << " ";
    
    
                  // what is that? certainly not a function call.
                  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;
          }
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2007
    Posts
    41
    Rep Power
    8

    Segmentation fault


    Originally Posted by JWest46088
    I changed the function call to sort();

    I think that is how it should really look, but I'm getting an error that says:

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

    I have a function named void sort() so what's the problem?
    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?
  10. #6
  11. Paris est magique!
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2004
    Location
    France!
    Posts
    370
    Rep Power
    96
    A lot of possible reasons, basically because you are reading or writing to a wrong place in the memory

    For instance, in some place, you write into an_array[an_index], where an_index is not initalized, therefore may have any random value. If the value it has is beyond the array range, boom.

    Update the code you posted, with the actual function calls. Or run your program in a debugger. Or cout index values before using them, so you can know whether they are consistent.
    etienne:~ > %blow
    fg: %blow: no such job


    There are 10 kind of people:
    - those who know binary
    - those who don't.
  12. #7
  13. Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jun 2005
    Posts
    5,964
    Rep Power
    4852
    Truthfully, it would be good for you to understand how things work. C, to some extent, and C++ to some slightly greater extent, will attempt to save you from yourself.

    Other languages will, according to their publicity, deliver you to the promised land. If you strain them, the promise is problematic.

    I would suggest that you read the documentation. On the other hand, if you don't have a jaundiced eye, that particular recommendation sucks, and is of questionable value.
    Write no code whose complexity leaves you wondering what the hell you did.
    Politically Incorrect DaWei on Pointers Grumpy on Exceptions

IMN logo majestic logo threadwatch logo seochat tools logo