A program to find largest number from 5 row by 5 column matrix

This is a discussion on A program to find largest number from 5 row by 5 column matrix within the C Programming forums, part of the General Programming Boards category; I made the code that stores and prints 5 row by 5 column values but I have no idea how ...

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    45

    A program to find largest number from 5 row by 5 column matrix

    I made the code that stores and prints 5 row by 5 column values but I have no idea how find the largest number from them.What should I do to find the largest number?If I use if-else then it would be very tedious but I think there is way out with for loop but still I can't frame the logic.
    Here is the code
    Code:
    #include<stdio.h>
    main()
    {
     int lnum[5][5];
     int i,j;
     for(i=0;i<=4;i++)
     {
      printf("\n");
      for(j=0;j<=4;j++)
      {
       printf("Enter the number");
       scanf("%d",&lnum[i][j]);
      }
     }
     for(i=0;i<=4;i++)
     {
      printf("\n");
      for(j=0;j<=4;j++)
      {
       printf("%d",lnum[i][j]);
      }
     }
    getch();
    }
    C enlightened

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,453
    Well create a variable called
    int largestSoFar;

    and mentally work your way though your print loop, imagining that you're trying to find the largest number so far, and what you might be doing with a variable called largestSoFar
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Sep 2012
    Posts
    45
    Quote Originally Posted by Salem View Post
    Well create a variable called
    int largestSoFar;

    and mentally work your way though your print loop, imagining that you're trying to find the largest number so far, and what you might be doing with a variable called largestSoFar
    Salem,I am still unable to frame the logic,do I have to use strings or structures?
    C enlightened

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,453
    > do I have to use strings or structures?
    No, you start with pencil and paper.

    You draw out a 5x5 grid, fill it with some numbers and then look at each one in turn to figure out which the biggest is.

    Until you understand how to do it on paper, there's no point looking at the code.

    Trust me, you have a "oh wow" moment coming up.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    2,246
    Hint: Think of how a mechanical counter works, to see if this sparks any ideas. Also do some research on nested loops, if you're not familiar with how they work.

  6. #6
    Registered User
    Join Date
    Sep 2012
    Posts
    45
    Quote Originally Posted by Matticus View Post
    Hint: Think of how a mechanical counter works, to see if this sparks any ideas. Also do some research on nested loops, if you're not familiar with how they work.
    Thank you Salem,after an hour of trying your suggestion,I finally got it.Here is the code.
    Code:
    #include<stdio.h>
    main()
    {
     int lnum[5][5];
     int i,j,lnsf;
     for(i=0;i<=4;i++)
     {
      printf("\n");
      for(j=0;j<=4;j++)
      {
       printf("Enter the number");
       scanf("%d",&lnum[i][j]);
      }
     }
     for(i=0;i<=4;i++)
     {
      printf("\n");
      for(j=0;j<=4;j++)
      {
       printf("%d",lnum[i][j]);
       if(lnum[i][j]>lnum[i][j-1])
        lnsf=lnum[i][j];
        lnum[i][j]=lnsf;
            
      }
      
     }
    printf("the largest number is %d",lnsf);
    getch();
    }
    C enlightened

  7. #7
    Registered User
    Join Date
    Jun 2011
    Posts
    2,246
    Oops, it appears you already had nested loops. Somehow I missed that.

    One problem: On line 21, when 'i' and 'j' are both zero, [j-1] goes outside the bounds of your array.

    You can simplify this by initializing your "lnsf" variable to a really low number (like -1 if you're only expecting positive input), and comparing the current array element with that variable - instead of comparing the current array element with a previous array element.

  8. #8
    Registered User
    Join Date
    Sep 2012
    Posts
    45
    Quote Originally Posted by Matticus View Post
    Oops, it appears you already had nested loops. Somehow I missed that.

    One problem: On line 21, when 'i' and 'j' are both zero, [j-1] goes outside the bounds of your array.

    You can simplify this by initializing your "lnsf" variable to a really low number (like -1 if you're only expecting positive input), and comparing the current array element with that variable - instead of comparing the current array element with a previous array element.
    Thank you Matticus,yes [j-1] does go outside the bounds and your suggestion is better.
    Here is the better code.
    Code:
    #include<stdio.h>
    main()
    {
     int lnum[5][5];
     int i,j,lnsf=1;
     for(i=0;i<=4;i++)
     {
      printf("\n");
      for(j=0;j<=4;j++)
      {
       printf("Enter the number");
       scanf("%d",&lnum[i][j]);
      }
     }
     for(i=0;i<=4;i++)
     {
      printf("\n");
      for(j=0;j<=4;j++)
      {
       printf("%d",lnum[i][j]);
       if(lnum[i][j]>lnsf)
        lnsf=lnum[i][j];
      }
      
     }
    printf("the largest number is %d",lnsf);
    getch();
    }
    C enlightened

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 10
    Last Post: 02-01-2013, 07:13 PM
  2. Replies: 3
    Last Post: 01-30-2013, 08:30 AM
  3. Replies: 14
    Last Post: 01-24-2013, 09:19 PM
  4. Find largest and second largest number (help)
    By Arkon in forum C++ Programming
    Replies: 6
    Last Post: 01-20-2006, 10:21 PM
  5. how do u find 2nd largest number??
    By juancardenas in forum C Programming
    Replies: 8
    Last Post: 02-14-2003, 07:28 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21