473,770 Members | 1,861 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem With Swapping 2d Char Arrays, Bubble Sort

5 New Member
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 this array. please help me.

my scenario:
assume that we have a big 2d char array for example students[20][30] for 20 persons an 30 character for each person. first 15 chars contains first name and the rest is last name.
no i want to sort this array according to last name.

my Idea:
i defined char mapped[number][16]={""} and mapped 2nd 15 chars of student array. so mapped[i]=student[i] but the result is NULL. I don't now why!!!
when showing student there is nothing to display on screen.
someone help!

here is new bubble sort function:
Expand|Select|Wrap|Line Numbers
  1. void BubbleSort (void)
  2. {
  3.    char mapped[number][21]={""};
  4.       for(i=0;i<number;i++)
  5.         for(j=0;j<15;j++)
  6.             mapped[i][j]=student[i][j+15];
  7.  
  8.     cout<<endl<<"mapped List:"<<endl;
  9.     for(i=0;i<number;i++)
  10.         cout<<mapped[i]<<endl;
  11.                 //now bubble sorting
  12.                 bool done = false;
  13.                 while (!done)
  14.                 {
  15.                     done = true;
  16.                     for (int n=0; n<number-1; n++)
  17.                     if (strcmp(mapped[n], mapped[n+1]) > 0)
  18.                     {
  19.                        char temp[length+1];
  20.                                strcpy(temp,student[n]);
  21.                        strcpy(student[n], student[n+1]);
  22.                        strcpy(student[n+1], temp);
  23.                        done = false;
  24.                      }
  25.                 }
  26.     cout<<endl<<"Sorted Student List:"<<endl;
  27.     for(i=0;i<number;i++)
  28.         cout<<student[i]<<endl; //nothing displayed!!!
  29. }
  30.  
and definiton of student array, if u wanna know:
Expand|Select|Wrap|Line Numbers
  1. #define number 20
  2. #define length 31
  3.  
  4. char student[number][length];
  5.  
Jan 27 '09 #1
7 7150
newb16
687 Contributor
but the result is NULL.
result of what?

Anyway, you haven't allocated memory for 'mapped'. Then you don't swap mapped when you swap 'students', swap them as well or make some compare() function that compares two strings accordingto your layout.
Jan 27 '09 #2
Banfa
9,065 Recognized Expert Moderator Expert
instead of

char students[20][30];

why don't you use

Expand|Select|Wrap|Line Numbers
  1. struct student_t {
  2.     char first[15];
  3.     char last[15];
  4. } students[20];
  5.  
Then it is easy to isolate the first and last names.

NOTE the structure uses the same amount of space as your array (assuming no padding) and provides space for names up to 14 characters in length.
Jan 27 '09 #3
mahdiahmadirad
5 New Member
id don't want use structures. it is a beginners program for practicing.
Jan 27 '09 #4
mahdiahmadirad
5 New Member
@newb16
result of output.
so, if i want to allocate memory for mapped, how should i define and declare it. i know how to allocate memory for 1d arrays:

Expand|Select|Wrap|Line Numbers
  1. char* mapped = new char[15];
  2.  
what about 2d arrays. is this true?
Expand|Select|Wrap|Line Numbers
  1. char* mapped = new char[number][15];
  2.  
Jan 27 '09 #5
Banfa
9,065 Recognized Expert Moderator Expert
Firstly I would say a structure with 2 arrays in it is easier to manage than using a single array of char to hold 2 strings and therefore better suited for a beginner.

Secondly since you are using C++ you should be using std::string not arrays of char, you may as well learn the right way of doing things from the outset.

The allocation you would need would be
Expand|Select|Wrap|Line Numbers
  1.     char (*mapped)[15] = new char[20][15];
note, the parenthesise are important as they change the declaration from an array of pointers to a pointer to an array.

And finally in your original algorithm you use mapped to decided if entries in student need swapping but you never change the order of entries in mapped so the condition in the algorithm is always feed a list that still needs sorting.
Jan 27 '09 #6
mahdiahmadirad
5 New Member
thank u dear Banfa!
I learned how to allocate 2D arrays. very usefull. but I found out my mistake in another way and now swapping the original array fixed. I should swap mapped array and student array concerted and till i just swap student it keeps making the same comparisons over and over and over....
so the corrected code:
Expand|Select|Wrap|Line Numbers
  1. while (!done)
  2. {
  3.     done = true;
  4.     for (int n=0; n<number-1 ; n++)
  5.         if (strcmp(mapped[n], mapped[n+1]) > 0)
  6.         {
  7.                 char temp1[length+1],temp2[length+1];
  8.  
  9.                         strcpy(temp1,student[n]);
  10.                         strcpy(temp2, mapped[n]);
  11.  
  12.             strcpy(student[n], student[n+1]);
  13.                         strcpy(mapped[n], mapped[n+1]);
  14.  
  15.             strcpy(student[n+1], temp1);
  16.                         strcpy(mapped[n+1], temp2);
  17.  
  18.                  done = false;
  19.         }
  20. }
  21.  
thank u very much!
Jan 27 '09 #7
mahdiahmadirad
5 New Member
thank u dear Banfa!
I learned how to allocate 2D arrays. very usefull. but I found out my mistake in another way and now swapping the original array fixed. I should swap mapped array and student array concerted and till i just swap student it keeps making the same comparisons over and over and over....
so the corrected code:
Expand|Select|Wrap|Line Numbers
  1. while (!done)
  2. {
  3.     done = true;
  4.     for (int n=0; n<number-1 ; n++)
  5.         if (strcmp(mapped[n], mapped[n+1]) > 0)
  6.         {
  7.                 char temp1[length+1],temp2[length+1];
  8.  
  9.                         strcpy(temp1,student[n]);
  10.                         strcpy(temp2, mapped[n]);
  11.  
  12.             strcpy(student[n], student[n+1]);
  13.                         strcpy(mapped[n], mapped[n+1]);
  14.  
  15.             strcpy(student[n+1], temp1);
  16.                         strcpy(mapped[n+1], temp2);
  17.  
  18.                  done = false;
  19.         }
  20. }
  21.  
thank u very much!
Jan 27 '09 #8

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

Similar topics

270
3990
by: Jatinder | last post by:
I found these questions on a web site and wish to share with all of u out there,Can SomeOne Solve these Porgramming puzzles. Programming Puzzles Some companies certainly ask for these things. Specially Microsoft. Here are my favorite puzzles. Don't send me emails asking for the solutions.
10
12757
by: VA | last post by:
I got the following function to swap table columns from somewhere on the Internet function swapColumns (table, colIndex1, colIndex2) { if (table && table.rows && table.insertBefore && colIndex1 != colIndex2) { for (var i = 0; i < table.rows.length; i++) { var row = table.rows; var cell1 = row.cells; var cell2 = row.cells;
3
2476
by: ritchie | last post by:
Hi all! Still working on this program! Just to recap, I am writing a program to sort an array with four different sort algorythms. I am having a little trouble at the moment though! Now, I am trying to calculate, with each sort, how many times during the sort the array elements are compared and swapped.
5
3980
by: jab3 | last post by:
(again :)) Hello everyone. I'll ask this even at risk of being accused of not researching adequately. My question (before longer reasoning) is: How does declaring (or defining, whatever) a variable **var make it an array of pointers? I realize that 'char **var' is a pointer to a pointer of type char (I hope). And I realize that with var, var is actually a memory address (or at
8
8016
by: meendar | last post by:
This is my program on swap of strings.i just used pointer for swapping the string. can anyone help me why it is not working? #include<iostream.h> #include<string.h> void swap(char *char1, char *char2) {
1
5315
by: xoinki | last post by:
hi experts, I need a little help in debugging this code.. would u pleeze kindly help me? here this program sends a datagram every 10 seconds and on reception it cheks whether the source IP is already written in file or not.. if it is not already entered into the file then a file is opened and that IP is entered. the problem is with this part.. if the filewrite() function is tested independently it is working but in this program...
1
5661
by: guest | last post by:
I am doing a program for Intro to Computer Programming where we take an array of strings and we must sort them alphabetically. we are supposed to use a bubble sort, but i know the code if meant for integers. How do i get a bubble sort to put names in alphabetical order?
19
4780
by: rmr531 | last post by:
First of all I am very new to c++ so please bear with me. I am trying to create a program that keeps an inventory of items. I am trying to use a struct to store a product name, purchase price, sell price, and a taxable flag (a Y/N char) and then write this all out to a file (preferably just a plain old text file) and then read it in later so that I can keep a running inventory. The problem that I am running into is when I write to the...
1
3122
by: chiefychf | last post by:
I'm working on a school project and I am having a few issues... The program calls for three arrays a,b,c that have to be sorted, then compared to even or odd and stored in arrays d & e, then merge a,b,c into another array f.. I can do two arrays, but I have issues when trying to do all three and when I do the even/odd compare I only get 2 numbers processed. here is some of the code... //*******************Function...
0
9453
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10254
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10099
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9904
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8929
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7451
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6710
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5354
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3607
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.