Click here to Skip to main content
14,975,219 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
In a given А=[aij]m×n matrix entered through the keyboard, find the smallest elements in a row and then the biggest one from them (biggest one from the smallest ones). Stuck on this for awhile now, I am noob at c programming btw. I apologize if this seems as an absurdly dumb question, but that's my limit. Would appreciate any answer. Cheers!

What I have tried:

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

int main(int argc, char *argv[])
{
int a[10][10],maxr[10],maxs[10],i,j,n;
printf("enter rows for the matrix\n");
scanf("%d",&n);
printf("input the elements of the matrix\n");
for(i=0;i++)
{
printf("a[%d][%d]:",i,j);
scanf("%d",&a[j]);
}
Posted
Updated 6-Dec-17 2:29am
v2

Start collecting inputs:
C
#include <stdio.h>
#include <stdlib.h>
#define N 10

int main()
{
  int m[N][N];
  int n,r,c;

  // get matrix dimension
  printf("please enter the number of the rows\n");
  if ( scanf("%d",&n) != 1 || n < 1 || n > N )
  {
    exit(-1); // TODO: better error handling here
  }

  // collect matrix items
  for (r = 0; r < n; ++r)
    for (c = 0; c < n; ++c)
    {
      printf("please enter item[%d][%d]\n", r,c);
      if ( scanf("%d", &m[r][c] ) != 1)
      {
        exit(-1); // TODO: better error handling here
      }
    }

  // show the matrix items
  for (r = 0; r < n; ++r)
  {
    for (c = 0; c < n; ++c)
      printf("%5d", m[r][c]);
    printf("\n");
  }
}
Please note, you don't need arrays to store the candidates to your final result. Just store current minimum of current row and the current maximum (of the minima).
   
Things you should do:
1 - separate the problems: input and analysis
2 - Allocate the matrix storage dynamically (size of a[][]), similarly for the other arrays. what if it's 20x3? your code will (probably) crash.
3 - Think simply: you compare two items in a row and keep the smaller, then compare the next one to the current smaller of the first two and keep the smaller of them, etc. etc. etc. until you'd done all the rows.
4 - apply modified (3) to find the largest of the saved smallest values.

Testing your code: create a matrix in code [no scanf()] and use that to test. This eliminates a dependence on input method AND you can easily repeat tries with tedious keyboard input.


   
Comments
CPallini 6-Dec-17 9:27am
   
"Testing your code: create a matrix in code [no scanf()] and use that to test. This eliminates a dependence on input method AND you can easily repeat tries with tedious keyboard input."
Well, you may feed the program redirecting a text file to the stdin.
W Balboos, GHB 6-Dec-17 9:41am
   
Yup - but this one eliminates the input functionality (I put the idea of splitting up into separate problems/tasks). That being said, there's always a number of ways to solve the same problem.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900