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

Problem With Swapping 2d Char Arrays, Bubble Sort

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 7118
newb16
687 512MB
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 Expert Mod 8TB
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
id don't want use structures. it is a beginners program for practicing.
Jan 27 '09 #4
@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 Expert Mod 8TB
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
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
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
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...
10
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...
3
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...
5
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...
8
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,...
1
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...
1
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...
19
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,...
1
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...
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: 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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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
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
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.