473,513 Members | 2,519 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Sort array using flowchart

74 New Member
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 6649
Laharl
849 Recognized Expert Contributor
Double post, sorry. Read down.
Feb 15 '08 #2
Laharl
849 Recognized Expert Contributor
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
JWest46088
74 New Member
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
JWest46088
74 New Member
Does anybody have any ideas why the sort function isn't being called?
Feb 15 '08 #5
Ganon11
3,652 Recognized Expert Specialist
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
JWest46088
74 New Member
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
JWest46088
74 New Member
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
2124
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
4219
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
25147
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
3170
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
4407
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
8428
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
2362
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
4107
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
10540
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
7260
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7160
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
7384
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,...
1
7099
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7525
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...
0
5685
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4746
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3233
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
456
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.