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: -
void BubbleSort (void)
-
{
-
char mapped[number][21]={""};
-
for(i=0;i<number;i++)
-
for(j=0;j<15;j++)
-
mapped[i][j]=student[i][j+15];
-
-
cout<<endl<<"mapped List:"<<endl;
-
for(i=0;i<number;i++)
-
cout<<mapped[i]<<endl;
-
//now bubble sorting
-
bool done = false;
-
while (!done)
-
{
-
done = true;
-
for (int n=0; n<number-1; n++)
-
if (strcmp(mapped[n], mapped[n+1]) > 0)
-
{
-
char temp[length+1];
-
strcpy(temp,student[n]);
-
strcpy(student[n], student[n+1]);
-
strcpy(student[n+1], temp);
-
done = false;
-
}
-
}
-
cout<<endl<<"Sorted Student List:"<<endl;
-
for(i=0;i<number;i++)
-
cout<<student[i]<<endl; //nothing displayed!!!
-
}
-
and definiton of student array, if u wanna know: -
#define number 20
-
#define length 31
-
-
char student[number][length];
-
7 7150
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.
Banfa 9,065
Recognized Expert Moderator Expert
instead of
char students[20][30];
why don't you use -
struct student_t {
-
char first[15];
-
char last[15];
-
} students[20];
-
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.
id don't want use structures. it is a beginners program for practicing.
@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: -
char* mapped = new char[15];
-
what about 2d arrays. is this true? -
char* mapped = new char[number][15];
-
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 - 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.
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: -
while (!done)
-
{
-
done = true;
-
for (int n=0; n<number-1 ; n++)
-
if (strcmp(mapped[n], mapped[n+1]) > 0)
-
{
-
char temp1[length+1],temp2[length+1];
-
-
strcpy(temp1,student[n]);
-
strcpy(temp2, mapped[n]);
-
-
strcpy(student[n], student[n+1]);
-
strcpy(mapped[n], mapped[n+1]);
-
-
strcpy(student[n+1], temp1);
-
strcpy(mapped[n+1], temp2);
-
-
done = false;
-
}
-
}
-
thank u very much!
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: -
while (!done)
-
{
-
done = true;
-
for (int n=0; n<number-1 ; n++)
-
if (strcmp(mapped[n], mapped[n+1]) > 0)
-
{
-
char temp1[length+1],temp2[length+1];
-
-
strcpy(temp1,student[n]);
-
strcpy(temp2, mapped[n]);
-
-
strcpy(student[n], student[n+1]);
-
strcpy(mapped[n], mapped[n+1]);
-
-
strcpy(student[n+1], temp1);
-
strcpy(mapped[n+1], temp2);
-
-
done = false;
-
}
-
}
-
thank u very much!
Sign in to post your reply or Sign up for a free account.
Similar topics |
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.
|
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;
|
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.
|
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
|
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)
{
| |
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...
|
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?
|
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...
|
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...
|
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,...
|
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...
| |
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...
|
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...
|
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...
|
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...
|
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();...
|
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...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| | |