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

c++ help sorting arrays

5
Does anyone know how to sort a 2D array diagonally? Lets suppose I've got the following array:

Expand|Select|Wrap|Line Numbers
  1. 2  5  7  4 8
  2. 3  11 14 5 2
  3. 6  3  12 9 1
  4. 7  15 11 4 2
  5. 8  16 13 5 1 
I want to create a function that sort them diagonally like this:

http://tinypic.com/view.php?pic=152dvk2&s=5#.Usb8v7RTMo4

I am a beginner in C++...
Please if anyone know please help me...
Jan 3 '14 #1
7 1497
Nepomuk
3,112 Expert 2GB
I would say the problem consists of two steps:
  1. Finding an algorithm that can convert a position in the 2D array to the number of the field in that array if you wrote them in order of use and vice versa
  2. Sorting the 2D array as if it were a 1D array
So, your code would look something like this:
Expand|Select|Wrap|Line Numbers
  1. typedef struct {
  2.     int x;
  3.     int y;
  4. } Coords;
  5.  
  6. Coords oneDtoTwoD(int pos);
  7.  
  8. int twoDtoOneD(int x, int y);
  9.  
  10. void sort(int** array) {
  11.     // Normal sorting algorithm but rather than taking
  12.     // the entry from position n you take that from
  13.     // oneDtoTwoD(n) and rather than putting the
  14.     // element into position n you put it into
  15.     // oneDtoTwoD(n).
  16.     // Alternatively the twoDtoOneD(x, y) function could
  17.     // be used to translate the 2D array into a 1D one
  18.     // and after that has been sorted you can turn it
  19.     // back to 2D with the oneDtoTwoD(n) function.
  20. }
  21.  
Jan 3 '14 #2
chry44
5
we didn't learn the algorithm method :/ the only method we learned is the bubble sort...
Jan 3 '14 #3
Nepomuk
3,112 Expert 2GB
Bubble sort is an algorithm. You can use that. :-)
Jan 3 '14 #4
chry44
5
oh really i didn't know that... :P
can you write the code for me please?
Jan 3 '14 #5
Nepomuk
3,112 Expert 2GB
No, I can't. And not only because that is against our posting guidelines but also because this is something you're supposed to learn. If you don't learn this you won't be able to solve more difficult tasks. We here at bytes.com will be happy to help you but we won't do your work for you.
Jan 3 '14 #6
chry44
5
oh okay :( but our teacher didn't teach us any of that what i am supposed to do... :/ please i am begging you i swear i tried everything...
Jan 3 '14 #7
Nepomuk
3,112 Expert 2GB
If you had tried everything you would have solved the problem. Learning to program isn't always easy, I know, but if you don't solve problems like this yourself you'll likely never be any good at it.

You said you learned about bubble sort. Do you understand that algorithm? Go through it again, trying to really understand it. Write it down more like a cooking recipe rather than computer code. Then, based only on the recipe, write it as code again. That way you'll make sure you really, truly understand it.

When it comes to the 2D array, the way that's probably easiest to understand is first converting it to a 1D array, sorting that and then converting it back. Here, look at this:
Expand|Select|Wrap|Line Numbers
  1.  1  2  4  7
  2.  3  5  8 11
  3.  6  9 12 14
  4. 10 13 15 16
This displays the order in which the elements in a 4x4 2D array would be accessed. So, if you translate that 2D array to this 1D array
Expand|Select|Wrap|Line Numbers
  1.  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15
it can easily be sorted. Then translate it back. It's very similar for the 5x5 array in that picture you posted.
Jan 3 '14 #8

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

Similar topics

11
by: Alan Searle | last post by:
I have been trying to get an array to sort on a particular column and have been having problems with my main key ... array_multisort($dirlist, SORT_DESC, SORT_STRING); This function takes an...
1
by: Darryl Woodford | last post by:
Hi, I have an array of object Anag containing a string and a list of other strings (anagram dictionary). That is, some records in the array may only have two fields (anagram and the one word...
4
by: Durand | last post by:
Hi all, I am using a sort method from Array class to order in ascending way. What can I do to descending way using same sort method? Thanks Durand
2
by: Mantorok | last post by:
Hi all I have an array of objects - each object contains a Surname and Forename property. What I want to do is sort these by Surname, then Forename. Is this possible? Thanks Kev
11
by: Paul Lautman | last post by:
I'm having some trouble understanding what is happening with some array sorting functions. In all cases, my compare function is: function compare($x, $y) { if ( $x == $y ) return 0; else if (...
2
by: zfareed | last post by:
I managed to sort a 2-dim array of lastnames but I am having trouble with printing the corresponding first names which are stored in a different 2-dim array. Here is my code for: ...
7
by: bkisa | last post by:
i m having problems with sorting arrays related to each others. i m reading the inputs from a file named input.txt and it is structured like 30 67 12 799 1 555 ... and its length i not defined...
7
by: aemado | last post by:
I am trying to write a program that reads in data from a user-specified file. I have to sort this file two ways, one using parallel arrays and the other using structs. I then have to output the...
0
by: Bull Horn | last post by:
This is basically what I have to do. I think I have the methods correct in terms of sorting, but how do I switch it from largest to smallest? The selectionSort method should take an array of...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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.