473,399 Members | 2,146 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,399 software developers and data experts.

Alphabetical Bubble Sort

16
Using Visual Studio 2005. Finally got this thing to compile, but now it prints an empty blank row for the first pass, so I guess there's an error somewhere in the function I have to show the Array.

It basically prints out the user prompt, then after you enter the number of names, such as 3... it prints out:

Enter a name: Enter a name:
Enter a name:

instead of

Enter a name:
Enter a name:
Enter a name:

After some checking it's not the function that reads in the names to the array that is faulty. The error seems to be in the function to display the array and I can't figure it out for the life of me.

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <cstring>
  3.  
  4. using namespace std;
  5.  
  6. //Constants
  7. const int MaxNames = 21;
  8. const int MaxChars = 16;
  9.  
  10. //Variables
  11. char name[MaxChars];
  12. char Array[MaxNames][MaxChars];
  13. int i, j, pass;
  14.  
  15. //Functions
  16. void ReadNames(char Array[MaxNames][MaxChars], int pass);
  17. void ShowArray(char Array[MaxNames][MaxChars]);
  18. void SortArray(char Array[MaxNames][MaxChars], int pass);
  19.  
  20.  
  21. void main()
  22.      {
  23. //User prompt
  24.      cout << "-------------------------------------------------------------------------------" << endl;
  25.      cout << "This program will allow you to enter names and sort them in alphabetical order." << endl;
  26.      cout << "-------------------------------------------------------------------------------" << endl;
  27.  
  28.      cout << "First, how many sets of names you will enter? (Must be less than 20): ";
  29.      cin >> pass;
  30.  
  31.      if (pass==0)
  32.     {
  33.     cout << "ERROR: You have entered 0 or a character. Try again." << endl;
  34. //do something, break a loop?
  35.         }
  36.  
  37.       while (pass < 0 || pass > 20)
  38.     { 
  39.     cout << "ERROR: You cannot enter less than 1 or more than 20 names. Try again: ";
  40.     cin >> pass;
  41.     }
  42.  
  43.       ReadNames(Array, pass);
  44.  
  45. //Display unsorted array
  46.       cout << "UNSORTED:" << endl;
  47. ShowArray(Array);
  48.  
  49. //SortArray
  50.      SortArray(Array, pass);
  51.  
  52. //Display sorted array
  53.       cout << endl << "SORTED:" << endl;
  54.       ShowArray(Array);
  55.       }
  56.  
  57. void ReadNames (char Array[MaxNames][MaxChars], int pass)
  58.      {
  59.      for (i=0; i<pass; i++)
  60.     {
  61.     cout << "Enter a name: ";
  62.     cin.getline(name, MaxChars);
  63.     for (j=0; j<MaxChars; j++)
  64.         {
  65.         Array[i][j]=name[j];
  66.         }
  67.     }
  68.      }
  69.  
  70. void ShowArray (char Array[MaxNames][MaxChars])
  71.      {
  72.      for (i=0; i<MaxNames; i++)
  73.     {
  74.     for (j=0; j<MaxChars; j++)
  75.         {
  76.         cout << Array[i][j];
  77.         }
  78.     cout << endl;
  79.     }
  80.       }
  81.  
  82. void SortArray (char Array[MaxNames][MaxChars], int pass)
  83.      {
  84.      bool swap;
  85.      int temp[MaxChars];
  86.  
  87.      do
  88.     {
  89.     swap=false;
  90.     for (i=0; i<MaxNames-1; i++)
  91.         {
  92.         if (Array[i][0]>Array[i+1][0])
  93.             {
  94.             for (j=0; j<MaxChars; j++)
  95.                 {
  96.                 temp[j]=Array[i][j];
  97.                 Array[i][j]=Array[i+1][j];
  98.                 Array[i+1][j]=temp[j];
  99.                 swap=true;
  100.                 }
  101.             }
  102.         }
  103.          } while(swap);
  104.     }
  105.  
Mar 29 '07 #1
0 1919

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

Similar topics

34
by: Mark Kamoski | last post by:
Hi-- Please help. I need a code sample for bubble sort. Thank you. --Mark
4
by: Chris | last post by:
I have a bubble sort for a 2-dimensional array that sorts a string,number pair based on the number. The code for the sort is as follows: Private Sub SortArray(ByRef roundarray(,) As String)...
2
by: yenra | last post by:
..how can I insert entry in a linked list and arrange my entries according to the name in alphabetical order? I need to make a student record which contains the name, yr level and course of the...
7
by: Ron Adam | last post by:
I have several applications where I want to sort lists in alphabetical order. Most examples of sorting usually sort on the ord() order of the character set as an approximation. But that is not...
4
Cyberdyne
by: Cyberdyne | last post by:
In your All Programs Menu, some of your programs are in alphabetical order and others are not. This makes it very difficult to seek out a program that may be hidden in a maze of program folders and...
1
by: Randeh | last post by:
Using Visual Studio 2005, right now my only error (for now) is something with the function prototype that I can't figure out for the life of me. Every data type is unexpected for my function. I'm...
6
by: Randeh | last post by:
Here's my working code for simple a 2-D alphabetical bubble sort. All I need now is to just make it case insensitive (ignores capitalization) and I've been trying to throw toupper or tolower in...
12
by: midknight5 | last post by:
Hello everyone, I am familiar with a normal bubble sort when dealing with an array of number but I am unsure as how to implement a sort when I have an array that is filled with classes which hold...
7
by: mahdiahmadirad | last post by:
Hi dears! I wrote a simple bubble sort algorithm. it works properly when we compare full arrays but i want to sort a 2d array according to a specific part of array. it has some problem to swapping...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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
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
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
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.