this is a program to enter the elements of a square matrix and find the smallest element from each row and subtract the minimum value from all the values in the respective rows.........take the input as the following values
999 54 48 92 24
54 999 32 61 35
48 32 999 45 23
92 61 45 999 67
24 34 23 67 999
so the output should be.......
975 30 24 68 0
22 967 0 29 3
25 9 976 22 0
47 16 0 954 22
1 11 0 44 976
but i'm getting output as.......
975 30 24 68 0
30 975 8 37 11
25 9 976 22 0
69 38 22 976 44
1 11 0 44 976
pls temme wat is da error in ma program........

#include<stdio.h>
#include<conio.h>
main()
{
int n,M[10][10],min=3267;
printf("\nEnter the order of square matrix:");
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
printf("\nEnter the elements of the row %d:",i);
for(int j=1;j<=n;j++)
{
scanf("%d",&M[i][j]);
}
}
for(i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
printf("\t %d",M[i][j]);
}
printf("\n");
}
printf("\nNow subtract each element from every row from their respective minimum  value\n");
getch();
for(i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if(M[i][j]<min)
min=M[i][j];
}
for( j=1;j<=n;j++)
M[i][j]=M[i][j]-min;
}
for(i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
printf("\t %d",M[i][j]);
}
printf("\n");
}
}

Recommended Answers

All 7 Replies

I down voted you just because of the crazy/childish title you gave this thead. Otherwise what can I say except that the code you posted is horribly formatted.

On positive note -- you used code tags correctly on your very first post :) I would probably have up reped you for that but wont because of that horrid title.

The program is using the last row's minimum, instead of the current row's minimum:

Original data:
999 54 48 92 24
54 999 32 61 35

but i'm getting output as.......
975 30 24 68 0  
>> 30 975 8 37 11 
Minimum here should be 32, but your program is using 24 from the first row, instead, .
(54 - 24 == 30), should be (54 - 32 == 22).

If you'll format your code so your subordinate lines of code, are indented 2 - 6 spaces, it will make your code MUCH easier to study, and bugs like this will be 10X easier to solve.

And please refrain from the text-speak. It sounds more retard than cool.

@adak:no....u r wrong.........have a look at the 3rd row over dere it's using 23 as min correctly.....how is dat so.......pls give da corrected code also....

we can start by 'correcting' the indentation, courtesy of astyle:

#include<stdio.h>

int main()
{
    int i, j;
    int n,M[10][10],min=3267;

    /* read in the values of the matrix */
    printf("\nEnter the order of square matrix:");
    scanf("%d",&n);

    for(i=1; i<=n; i++)
    {
        printf("\nEnter the elements of the row %d:",i);
        for(j=1; j<=n; j++)
        {
            scanf("%d",&M[i][j]);
        }
    }

    /* print out the matrix as confirmation */
    for(i=1; i<=n; i++)
    {
        for(j=1; j<=n; j++)
        {
            printf("\t %d",M[i][j]);
        }
        printf("\n");
    }

    printf("\nNow subtract each element from every row from their respective minimum  value\n");

    /* find the minimum value for each row and subtract it from the row elements */
    for(i=1; i<=n; i++)
    {
        min = M[i][0];      /* reset the minimum value to the first element of the row */
        for(j=1; j<=n; j++)
        {
            if(M[i][j]<min)
                min=M[i][j];
        }
        for(j=1; j<=n; j++)
            M[i][j]=M[i][j]-min;
    }

    /* print the resulting matrix */
    for(i=1; i<=n; i++)
    {
        for(j=1; j<=n; j++)
        {
            printf("\t %d",M[i][j]);
        }
        printf("\n");
    }
}

And I have to concur about the SMS crap - if you persist in it, I doubt anyone will bother reading your posts, never mind answering them.

Finally, I think you'll find that Adak was right: I added the line

min = M[i][0];      /* reset the minimum value to the first element of the row */

and the program now runs correctly. HTH.

in c u cannot initialize i and j inside for loop

@adak:no....u r wrong.........have a look at the 3rd row over dere it's using 23 as min correctly.....how is dat so.......pls give da corrected code also....

Instead of arguing with me, and asking for the corrected code, please work on your program, and learn more about C. I'm trying to guide you, not do your homework. It's about you learning to program in C, not about how many assignments somebody will do for you, on the net.

Note that I never said that every row has the wrong (previous) row's minimum value. I said the second row had the previous rows minimum value.

I expect *you* to step up and find out why that is. You'll learn something more about C in the process, and troubleshooting code, as well. You *WILL* have to debug lots of code in programming, so it really helps to get good at it, early on.

You received an "absolute gift" of a fixed program, today. You should thank him, and understand that kind of help won't be available, every day.

Your code is currect. but a simple currection as said by Schoil-R-LEA.
Modified code is follow. It should Work.

#include<stdio.h>
main()
{
int i,j,a,n,M[10][10],min=3267;
printf("\nEnter the order of square matrix:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("\nEnter the elements of the row %d:",i);
for( j=1;j<=n;j++)
{
scanf("%d",&M[j]);
}
}
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
printf("\t %d",M[j]);
}
printf("\n");
}
printf("\nNow subtract each element from every row from their respective minimum value\n");
getch();
for(i=1;i<=n;i++)
{
min = M[1];
for( j=1;j<=n;j++)
{
if(M[j]<min)
min=M[j];
}
for( j=1;j<=n;j++)
M[j]=M[j]-min;
}
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
printf("\t %d",M[j]);
}
printf("\n");
}
}

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.