-2

So the problem is:" Create a program, that inputs the elements(integers) of a matrix a[n][m] and finds the element with the smallest value amongst the the elements with the biggest value of every column."

So the way I understand it, I have to go through every column and find the biggest number in each, so I will end up with 4 numbers. From these 4 numbers I have to print out the smallest.

int main()
{

    int n,m;
    int A[n][m];
    int B[m];
    int max;
    int min;
    int i,j;

    printf("Enter a number for n: ");
    scanf("%d",&n);
    printf("Enter a number for m: ");
    scanf("%d", &m);


    for(i=1; i<=n;i++)
    {
        for(j=1; j<=m;j++)
        {
            printf("Enter a number [%d][%d]: ", i, j);
            scanf("%d", &A[i][j]);
        }
    }

    for(j=1; j<=m; j++)
    {
        max=A[j][1];
        for( i=1; i<=n; i++)
        {
            if(max<A[j][i])
          {
            max=A[j][i];
            B[j]=max;
          }
        }
    }
    min=B[1];

    for(i=1; i<=m; i++)
    {
        if(min>B[i])
        {
            min=B[i];
            printf("%d ", B[i]);
        }
    }
    printf("Min is: %d\n", min);

    return 0;
}

My question is where would the error be? It must be a logical one. I apologize for not asking my question as clearly as most of you in this forum, but it's my first time asking here. Please if you answer, explain it like you would to a beginner. Thanks!

Martin James
  • 24,453
  • 3
  • 36
  • 60
Perplexed
  • 11
  • 3
  • 1
    Please specify your question. You have none right now. – Jelle Jan 13 '21 at 09:01
  • Read [*Modern C*](https://modernc.gforge.inria.fr/) and see [this C reference](https://en.cppreference.com/w/c). With a modern compiler (e.g. [GCC](http://gcc.gnu.org/) invoked as `gcc -Wall -Wextra -g`) your program should not even compile, because `#include ` is missing. Once you compile your program *without warnings* use your debugger, e.g. [GDB](https://www.gnu.org/software/gdb/) to understand its behabior – Basile Starynkevitch Jan 13 '21 at 09:04
  • See also [this answer](https://stackoverflow.com/a/41410503/841108) to a similar question – Basile Starynkevitch Jan 13 '21 at 09:18
  • It’s worth noting that in C indexes go from 0 to n-1 and JIT 1-n. Secondly you’re going to have to use malloc to create memory space for the arrays if they aren’t known at compile time. – AlBlue Jan 13 '21 at 09:21
  • You should not edit your post after an answer has been provided, edit that invalidates this answer. – Damien Jan 13 '21 at 10:03

1 Answers1

2

C programs are generally executed from top to bottom. Therefore you cannot do this:

int n,m;
int A[n][m];

Because n and m has not been given any values. To give them values further down doesn't mean that the line int A[n][m]; will somehow get re-executed.

Lundin
  • 195,001
  • 40
  • 254
  • 396