472,958 Members | 2,368 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,958 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 7064
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: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.