The description of my problem is here-
Suppose I have an array of arrays-

1.1) 23445
1.2) 2144
1.3) 77988
1.4) 545
1.5) 52358

2.1) 2356
2.2)7878
2.3)531
2.4)78
2.5)78090

3.1) 578689
3.2) 90877
3.3) 1345
3.4) 422
3.5) 64

.....
....
...
Now i need to compare 1.1 , 2.1, 3.1 ... and find the greatest among them. In the same way wid 1.2, 2.2, 3.2.....1.3, 2.3, 3.3 Does anybody have an idea how to implement this one?

Recommended Answers

All 5 Replies

maybe first make an array of 5 elements (to store the 5 greatest)
then loop with the iterating int going from 0 to 4, here is how I would do it:

int[3][5] myArr = { {23445, 2144, 77988, 545, 52358},
                    {2356, 7878, 531, 78, 78090},
                    {578689, 90877, 1345, 422, 64} };
int[5] myArrOfResults;
for(int i = 0; i < 5; i++) {
    myArrOfResults[i] = (myArr[1][i] > myArr[2][i])? myArr[1][i]:myArr[2][i];
    if(myArr[3][i] > myArrOfResults[i]) { myArrOfResults[i] = myArr[3][]i; }
}

I hope I got my syntax right, I'm more familiar with Java than C.

Actually this is for only 3 sets of arrays right? but i wanted to compare a number of arrays around 80. and i have to compare all the one row elements n second row elements n so on...

Well this is not very efficient method but as you are using nearly 80 arrays it might come handy.

Just do this :

for(i=0;i<80;i++)
     for(j=0;j<80;j++)
          Temp_array[j] = Array[j][i];

In the above mentioned way you will move every element in first row to temporary array.And now apply some method on this temp array to find out the max value and move it to

Result_array[j] = maxof( Temp_Array[] );

Thats it simple to use but not highly efficient.

just add more if statements like if(myArr[next][i] > resArray[i]) { resArray[i] == myArr[next][i]; } like that. If you want to add to each array's length (so they're more than 5) then increase the for(int i = 0; [B]i < x[/B]; i++) where x is currently 4 (length - 1), and increase the length of the result array.

That would be a great plan if you were trying to write bad-looking and terribly inflexible code. You can do this with a loop, however, which will let you find the maximum of any amount of numbers you want. You should keep a variable holding the value that is the max so far and each time you find a value that is greater than the max, set the max to this new value.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.