Click here to Skip to main content
15,851,701 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Please solve my following problem!
suppose we have a code like;
struct team
    int runs[15];
    char *name[15];
} ind,pak,aus;

// now first murge these int arrays in a single array
int d[45];
for(int i=0; i<15; i++)

Now , I sort them using any method,
finally I get sorted values but I need the names adjacent to these runs.
pak.runs[5] is placed at d[14] after sorting , then how can i display the corresponding name at[5] after sorting?
I hope you understand my question...
Please help me!
Updated 15-Mar-11 23:20pm
JF2015 16-Mar-11 1:40am    
Edited for code formatting.
Emilio Garavaglia 16-Mar-11 4:21am    
More code formatting

As a quick solution,
Copy all 3x15 names into single array in the above for loop.
While sorting d[45] array, exchange the name[] values according to the sorting index(i do not know which algorithm you are using for sorting but you will exchange d[] values through indexes, at the same time you exchange the values).I am not considering any performance issues here.
Share this answer
Check Solution from santosh for the code implementation of this solution
Dalek Dave 16-Mar-11 4:45am    
That would work.
How about using STL instead? You can use std::list<std::string> class, which has the method sort.

Share this answer
Dalek Dave 16-Mar-11 4:45am    
Ah, Sage Advice!
Sergey Alexandrovich Kryukov 16-Mar-11 4:50am    
Thank you, Dalek,
Nuri Ismail 16-Mar-11 5:07am    
Good advice, my 5.

Personally I'd use std::multimap and already gave an example to the OP with this approach.
In this case the container does everything automatically behind the scene. :)
Sergey Alexandrovich Kryukov 16-Mar-11 5:11am    
I see. (Already voted :-)
I did not bother to dig into the code, offered the simplest container which would work better than OP's programming from scratch (which still would be good to learn, of course).
This may help you.
Before sorting of array you should fill name array like,

char *dn[45];

for(int j=0; j<15; j++)

now your sorting logic may differ but with linear sorting with do same,

void selectionSort(int arr[],char namearray[], int size)
     int indexOfMin, pass, j;

     for (pass = 0; pass < size - 1; pass++)
           indexOfMin = pass;

           for (j = pass + 1; j < size; j++)
                 if (arr[j] < arr[indexOfMin])
                       indexOfMin = j;

           swap(arr[pass], arr[indexOfMin]);
}// end selectionSort()

void swap(int& x, int& y)
     int temp;
     temp = x;
     x = y;
     y = temp;
}// end swap()
void swapName(char& x, char& y)
     int temp;
     temp = x;
     x = y;
     y = temp;
}// end swapNames
Share this answer
Dalek Dave 16-Mar-11 4:44am    
Good Answer. 5!
[no name] 16-Mar-11 4:59am    
Thanks dalek,
Nuri Ismail 16-Mar-11 5:07am    
Good advice indeed! 5 :)
[no name] 16-Mar-11 6:08am    
Thanks nuri
mbue 16-Mar-11 14:01pm    
brr, bubble sort ;)
You already have your C-style solution.
I want to give you a simple C++ - style alternative.

Here is the code:
#include <iostream>
#include <string>
#include <map>

int main()
	// Your data structure:
	static const int runs_size = 15;
	struct team
		int runs[runs_size];
		std::string name;
	} ind, pak, aus;

	// Now we will fill some data. 
	// The next block has nothing to do with the solution.
	// It is just an example initialization for testing.
	// ====================== Example initialization ==========================
	// First clear evrything
	memset(&ind, 0, sizeof(ind));
	memset(&pak, 0, sizeof(pak));
	memset(&aus, 0, sizeof(aus));

	// Fill names: = "India"; = "Pakistan"; = "Australia";

	// Fill some runs:
	ind.runs[0] = 10; ind.runs[5] = 20; ind.runs[10] = 25;
	pak.runs[1] = 17; pak.runs[2] = 18; pak.runs[12] = 23;
	aus.runs[4] = 10; aus.runs[5] = 19; aus.runs[6] = 24;
	// =======================================================================

	// ======================== Here the important part begins ===========================
	typedef std::multimap<int, const team*, std::greater<int> > mmap_runs_teams_t;
	typedef std::pair<int, const team*> mmap_pair_t;

	mmap_runs_teams_t mmap_runs_teams;
	for (int i = 0; i < runs_size; i++)
		mmap_runs_teams.insert(mmap_pair_t(ind.runs[i], &ind));
		mmap_runs_teams.insert(mmap_pair_t(pak.runs[i], &pak));
		mmap_runs_teams.insert(mmap_pair_t(aus.runs[i], &aus));
	// ===== And here the important part ends - That's it! This is your solution! ;) =====

	// Now check the results:
	std::cout << "Best runs: " << std::endl;
	for (mmap_runs_teams_t::iterator it = mmap_runs_teams.begin(); it != mmap_runs_teams.end(); it++)
		std::cout << "\nRun: " << it->first << "\tTeam: " << it->second->name;

	return 0;

The output of this program:
Best runs:
Run: 25 Team: India
Run: 24 Team: Australia
Run: 23 Team: Pakistan
Run: 20 Team: India
Run: 19 Team: Australia
Run: 18 Team: Pakistan
Run: 17 Team: Pakistan
Run: 10 Team: India
Run: 10 Team: Australia
........... all zero runs to the end..........

From the code above you can see that the actual solution is less then 10 lines of code! :)
This is possible with the Standard C++ Library. I used std::multimap[^] as a container.

Intentionally I will not explain how this solution works.
As an exercise, you can check out the documentation for std::multimap.
This way you can understand how this code works.

If you have some questions after reading the documentation, please feel free to ask here. :)
Share this answer
Sergey Alexandrovich Kryukov 16-Mar-11 5:08am    
Remarkable effort, my 5.
Nuri Ismail 16-Mar-11 5:10am    
Thank you very much! :)
[no name] 16-Mar-11 6:10am    
Woh Nice effort
my 5+
Nuri Ismail 16-Mar-11 6:21am    
Thank you, Santosh! :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900