Hey guys.

I'm learning by myself the C Development... But I've got a serious problem with one my application.

The exercice is:
Create a board of 10 lines and 3 columns.
Scanf this board.
Check the 3 highest numbers of each line, add them
And just printf the highest sum of the three.

I've tried during two weeks... But nothing happen...

#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <windows.h>

main ()

{
    int Tab[5][3]={0}, i, j, GrandA = 0, GrandB = 0, GrandC = 0;

    for(i = 0; i < 3; i++)
    {
        for(j = 0; j < 5; j++)
        {
            scanf("%d", &Tab[j][i]);
        }
    }

    for(i = 0; i < 3; i++)
    {
        for(j = 0; j < 5; j++)
        {
            printf("%d\t", Tab[j][i]);
        }
        printf("\n");
    }

    for(i = 0; i < 3; i++)
    {
        printf("\n");
        GrandA = 0;
        GrandB = 0;
        GrandC = 0;
        for(j = 0; j < 5; j++)
        {
            if(GrandA < Tab[j][i])
            {
                GrandA = Tab[j][i];
                Tab[j][i] = 0;
                j = 0;
                i = 0;
            }

            if(GrandB < Tab[j][i])
            {
                GrandB = Tab[j][i];
                Tab[j][i] = 0;
                j = 0;
                i = 0;
            }
            if(GrandC < Tab[j][i])
            {
                GrandC = Tab[j][i];
                Tab[j][i] = 0;
            }
        }
    printf("%d ", GrandA);
    printf("%d ", GrandB);
    printf("%d ", GrandC);
    }

}

That's what I've done...

Be gentle I'm new and English isn't my mother language.

Thanks for helping me.

Recommended Answers

All 8 Replies

your program looks ok until line 28. At that point all you have to do is sum up the three numbers on each of the 5 rows of the 2d array and display the largest sum value. You don't need GrandA, GrandB, or GrandC. just int sum and int largest_sum.

How much i could understand, i think you are trying to do this..

#include <stdlib.h>
#include <stdio.h>

int main ()

{
    int Tab[5][3]={0}, i, j, GrandA = 0, GrandB = 0, GrandC = 0,Sum;

    for(i = 0; i < 3; i++)
    {
        for(j = 0; j < 5; j++)
        {
            scanf("%d", &Tab[j][i]);
        }
    }

    for(i = 0; i < 3; i++)
    {
        for(j = 0; j < 5; j++)
        {
            printf("%d\t", Tab[j][i]);
        }
        printf("\n");
    }

   for(j = 0; j < 5; j++)
     {
      if(GrandA < Tab[j][0])
           GrandA = Tab[j][0];
      if(GrandB < Tab[j][1])
           GrandB = Tab[j][1];
      if(GrandC < Tab[j][2])
           GrandC = Tab[j][2];
     }
    Sum=GrandA+GrandB+GrandC;
    printf("%d ", GrandA);
    printf("%d ", GrandB);
    printf("%d ", GrandC);
    printf("\n %d",Sum);
    
  return 0;
  
 }

What???

#include <stdlib.h>
#include <stdio.h>

int main ()

{
    int Tab[5][3]={0}, i, j, sum = 0, largest_sum = 0;
    
    for(i = 0; i < 3; i++)
    {
        for(j = 0; j < 5; j++)
        {
            scanf("%d", &Tab[j][i]);
        }
    }

    for(i = 0; i < 3; i++)
    {
        for(j = 0; j < 5; j++)
        {
            printf("%d\t", Tab[j][i]);
        }
        printf("\n");
    }
   largest_sum = 0;
   for(j = 0; j < 5; j++)
     {
       sum = 0;
       for(i = 0; i < 3; i++)
          sum += Tab[j][i];
       if( sum > largest_sum)
            largest_sum = sum;
     }
    printf("\n %d",largest_sum);
    
  return 0;
  
 }

Here is the problem in my programm and in yours.
It add the biggest numbers of all the boards and then printf the sum
but it has to add the 3 biggest numbers per lines, sum it and printf the biggest sum of the three

Well I think I misunderstood the problem. Ignore my previous post.

Ohh.. I also misunderstood your question.. please ignore my above post....

you want to sum of 3 highest numbers of line1 in GrandA ... and so on.....

line1: 1 3 4 5 6 GrandA= 6+5+4

line2: 5 6 7 8 3 GrandB= 8+7+6

line3: 5 4 4 3 2 GrandC= 5+4+4

I think that's your question...

If Above is your question then the code could be...

#include <stdlib.h>
#include <stdio.h>

int main ()

{
    int Tab[5][3], i, j, GrandA = 0, GrandB = 0, GrandC = 0;
    int index_a=0,index_b=0,index_c=0,big_a,big_b,big_c;

    for(i = 0; i < 3; i++)
    {
        for(j = 0; j < 5; j++)
        {
            scanf("%d", &Tab[j][i]);
        }
    }

    printf("\n\n");

    for(i = 0; i < 3; i++)
    {
        for(j = 0; j < 5; j++)
        {
            printf("%d\t", Tab[j][i]);
        }
        printf("\n");
    }

    for(i = 0; i < 3; i++)
    {
        printf("\n");
        big_a = 0;
        big_b = 0;
        big_c = 0;
        
        for(j = 0; j < 5; j++)
        {
            if(big_a < Tab[j][0])
            {
                big_a = Tab[j][0];
                index_a=j;
            }

            if(big_b < Tab[j][1])
            {
                big_b = Tab[j][1];
                index_b=j;
                
            }
            if(big_c < Tab[j][2])
            {
                big_c = Tab[j][2];
                index_c=j;
            }
         }
        GrandA +=big_a;
        GrandB +=big_b;
        GrandC +=big_c;
        Tab[index_a][0]=Tab[index_b][1]=Tab[index_c][2]=0;
   
    }
    printf("%d ", GrandA);
    printf("%d ", GrandB);
    printf("%d ", GrandC);

 return 0; 
}

If its not your question then please describe it with example...

Thanks for your helps.
Vinitmittal you're right, thanks a lot and thanks to the others

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.