473,394 Members | 1,702 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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 6641
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

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

Similar topics

1
by: JackieR | last post by:
I have taken over a project and would like to flowchart the entire solution. I have found flowchart programs that work on a single .cpp file, but nothing for the entire project. Does anyone know...
40
by: Elijah Bailey | last post by:
I want to sort a set of records using STL's sort() function, but dont see an easy way to do it. I have a char *data; which has size mn bytes where m is size of the record and n is the...
7
by: ritchie | last post by:
Hi all, I am new to this group and I have question that you may be able to help me with. I am trying to learn C but am currently stuck on this. First of all, I have a function for each sort...
21
by: yeti349 | last post by:
Hi, I'm using the following code to retrieve data from an xml file and populate a javascript array. The data is then displayed in html table form. I would like to then be able to sort by each...
48
by: Alex Chudnovsky | last post by:
I have come across with what appears to be a significant performance bug in ..NET 2.0 ArrayList.Sort method when compared with Array.Sort on the same data. Same data on the same CPU gets sorted a...
3
by: Paulers | last post by:
hello everyone, can anyone recommend a free flowchart framework for vb.net? I was drooling over Flowchart.net until I saw the $500.00 price tag. I realize that a lot of work went into...
19
by: William Gill | last post by:
I seem to be having a mentally bad period lately . My code is beginning to be terrible convoluted mess, and I shudder to think what it will be like to go back in a couple months and try to follow...
19
by: sameer | last post by:
1-develop a flowchart and then write a c program to display all prime number less than the number entered by the user.
3
by: raylopez99 | last post by:
This is an example of using multiple comparison criteria for IComparer/ Compare/CompareTo for List<and Array. Adapted from David Hayden's tutorial found on the net, but he used ArrayList so the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.