You are here:

C/sorting two dimensional array in turbo c

Advertisement


Question
my prof gave us a prob: input 25 numbers in two dimensional array like this 1 2 3 4 5
         5 4 3 2 1
         7 8 4 5 5
         4 6 3 5 1
then sort both diagonal in ascending and descending form
i have already did the diagonal from top left to bottom right but the diagonal from lower left to top right gives me headache.
this is my syntax in top left to bottom right:
for(x=1;x<=4;x++){
     for(y=x+1;y<=5;y++)
        if (a[x][x]>a[y][y])
           { temp=a[x][x];
             a[x][x]=a[y][y];
             a[y][y]=temp;
           }
     }
please can you help me how you sort diagonally from bottom left to upper right. i cant get the logic how to do it thank you in advance

Answer
Hi.

You've done a good job with the algorithm to sort the diagonal.  Realistically speaking, there is no difference between sorting top-left to bottom-right than sorting bottom-left to top-right.  The only thing you have to change is the index you are referencing.  Instead of going from [0][0] to [4][4] you are going from [4][0] to [0][4].

With that in mind, give this another shot and see what you can come up with.  If you are still having issues, let me know and I'll help you a bit more.

C

All Answers


Answers by Expert:


Ask Experts

Volunteer


Joseph Moore

Expertise

I've been programming in one form or another since my brother taught me BASIC when I was 6. I've been programing professionally since I was 20, first web development with HTML, JS, DHTML, CSS, etc., then I became a video game developer, writing code in C, C++, C#, SQL, assembly, and various scripting languages. I've even written my own scripting languages, custom designed for the games I was making. I also dabble in Java, PHP, and Perl. I've worked on pretty much every aspect of game development, including graphics, audio, gameplay, tool, UI, input, animation, and physics.

Experience

I've been writing C code for 12 years, both on my own in my spare time and professionally.

Organizations
IGDA

Education/Credentials
Bachelor of Science in Game Design and Development, Full Sail University, Winter Park, FL

Awards and Honors
Salutatorian and Advanced Achiever Awards at Full Sail; Independent Games Festival Student Showcase winner, 2004; Featured article on Gamasutra about an experimental game developed in 2004

©2012 About.com, a part of The New York Times Company. All rights reserved.