Click here to Skip to main content
14,979,837 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
The problem is as follows - Write a program in C to input m x n matrix and print it . Also take row number and column number as an user input and find the maximum number in respective row and column .

I can successfully take input and print the Matrix . the problem occurs in taking row and column input from user and finding the maximum number in that row and column .

I searched everywhere but could only find maximum number in whole matrix or maximum number in each row and column . Hopefully someone can clear this doubt of mine . This is my first question on CodeProject. I tried to read all the rules. Sorry if I missed something .

What I have tried:

C++
#include <stdio.h>

int main()
{
    int matrix[100][100],max1[100],max2[100];
    int i,j,m,n,o,p;

    printf("Enter number of Rows :");
    scanf("%d",&m);
    printf("Enter number of Columns :");
    scanf("%d",&n);

    printf("\nEnter matrix elements :\n");
    for(i=0;i< m;i++)
    {
        for(j=0;j<n;j++)
        {
            scanf("%d",&matrix[i][j]);
        }
    }

    printf("\nMatrix is :\n");
    for(i=0;i< m;i++)
    {
        for(j=0;j<n;j++)
        {
            printf("%d\t",matrix[i][j]);
        }
        printf("\n");
    }
    printf("Enter Row number :");
    scanf("%d",&i);
    for(i=i;i<i+1;i++);
    {
        max1[i]=matrix[i][j];
        for(j=0;j<n;j++);
        {
            if(matrix[i][j]>max1[i]);
            {
                max1[i]=matrix[i][j];
            }
        }
    }
    printf("Enter Column number :");
    scanf("%d",&j);
    for(i=0;i<m;i++);
    {
        max2[j]=matrix[i][j];
        for(j=j;j<j+1;j++);
        {
            if(matrix[i][j]>max2[j]);
            {
                max2[j]=matrix[i][j];
            }
        }
    }
    printf("%d",max1);
    printf("%d",max2);

    return 0;
}
Posted
Updated 26-Apr-20 7:07am
v2

Quote:
I searched everywhere but could only find maximum number in whole matrix or maximum number in each row and column

Development isn't about searching the internet and giving up when you can't find exactly what you need: if it was, nothing new could even be produced and that would be very silly.

Instead, it's about thinking: read the task carefully, think about what is required, break it down into smaller bits and see if you can do them. If you can do a smaller step, do. If you can't break it down into smaller bits and try to do them. Repeat as necessary.

This is your homework, not ours: and it doesn't help anyone in the long run for us to do it for you - especially you! If you can't do this - pretty trivial - task for yourself, then the next task (which will be more complicated because your tutor assumes you understood this one) is going to be even further beyond you. And when you finally get to the exams, and can't get help at all ... well, you know what happens then.

So stop, throw the code you found away, and think about the task: it's not really at all complicated and if you think about how you would do it manually, that should tell you how to do it on a computer.
Give it try: see how far you can get.
   
Comments
Aeyan Ashraf 26-Apr-20 10:45am
   
The code I have written , I have written by myself . It is not copied .
To find the max of a row you might do something like this:
C++
#include <limits.h>  // to get INT_MIN

    /* ... get user input, etc ... */ 

int rowmax = INT_MIN;  // Smallest possible value for an int
for(col = 0; col < n_cols; ++col)
{
    if(array[row][col] > rowmax)
        rowmax = array[row][col];
}
printf("max value for row %d is %d\n", row, rowmax);


similarly for a column.
   
Comments
Aeyan Ashraf 26-Apr-20 12:25pm
   
Thank you so much for your time . But only stdio.h file can be used .
a few mistakes:
- Language mistakes
C++
printf("Enter Row number :");
scanf("%d",&i);
for(i=i;i<i+1;i++); // the semicolon here prevent looping by saying loop is empty
{
    max1[i]=matrix[i][j];
    for(j=0;j<n;j++); // the semicolon here prevent looping by saying loop is empty
    {
        if(matrix[i][j]>max1[i]); // the semicolon here prevent if from working
        {
            max1[i]=matrix[i][j];
        }
    }
}

- Logic mistake
C++
printf("Enter Row number :");
scanf("%d",&i);
for(i=i;i<i+1;i++) // No need
{ // No need
    max1[i]=matrix[i][j];
    for(j=0;j<n;j++)
    {
        if(matrix[i][j]>max1[i])
        {
            max1[i]=matrix[i][j];
        }
    }
} // No need


Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
   
Comments
[no name] 26-Apr-20 10:35am
   
+5 (btw I don't expect a response)
Patrice T 26-Apr-20 10:55am
   
Thank you
Aeyan Ashraf 26-Apr-20 12:32pm
   
Thank you So much for your time . I totally forgot the syntax for for loop I don't know how. After quite a struggle . I could get the code to work and all . Just the answer isn't coming correct . I changed max1 and max2 from array to int quantities and used the following code . I don't know why the answer is not coming correct. I will try and use the debugger . You have been a big help so I'm accepting this as an answer.

printf("Enter Row number :");
scanf("%d",&i);


max1=matrix[0][0];
for(j=0;j<n;j++)
{
="" if(matrix[i][j]="">max1)
{
max1=matrix[i][j];
}
}

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