Arsel Doe
Arsel Doe

Reputation: 21

Why this function is not showing the correct result as output?

The output of this function everytime is:

The row with minimal sum is 1 whose sum is 0.

But I want it to calculate the sum of each row and then display the min row with its sum. It displays the correct result if I use this code as a program, and not inside the function(as in this case).

void findRowWithMinimalSum(int row, int col, int A[row][col]){
    int i,j,sum,minSum,position;

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

  for(i=0; i<row; i++){
    for(j=0; j<col; j++)
        printf("%d", A[i][j]);
    printf("\n");
  }

   for(i=0; i<row; i++){
    sum=0;
    for(j=0; j<col; j++){
        sum = sum + A[i][j];
    }
    if(sum<minSum){
        minSum = sum;
        position = i;
    }
   }
   printf("\nThe row with minimal sum is %d whose sum is %d", position+1, minSum);
  }

int main(){
 int row, column, m[50][50];

 //function call
 findRowWithMinimalSum(3,3,m);
  return 0;
 }

Upvotes: 0

Views: 88

Answers (3)

Thetchina Moorthy
Thetchina Moorthy

Reputation: 1

void findRowWithMinimalSum(int row, int col, int A[50][50])
{
  int i,j,sum=0,minSum=0,position,f=0;

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

  for(i=0; i<row; i++){
  for(j=0; j<col; j++)
printf("%d ", A[i][j]);
  printf("\n");
 }

  for(i=0; i<row; i++){
  sum=0;
 for(j=0; j<col; j++){
sum = sum + A[i][j];
    }
    if(f==0)
    {
    minSum=sum;
 f++;
  }
 if(sum<minSum){
minSum = sum;
position = i;
    }
 }
 printf("\nThe row with minimal sum is %d whose sum is %d", position+1, minSum);
 }

int main(){
 int row, column, m[50][50];

//function call
indRowWithMinimalSum(3,3,m);
return 0;
}

Upvotes: 0

dgnuff
dgnuff

Reputation: 3557

Look carefully at minSum. You don't explicitly initialize it before using it in this test:

if (sum < minSum)

Therefore, if you're running a debug build and it gets initialized to zero, it'll never change unless sum is < 0.

Note that referencing an uninitialized value is undefined behavior, something that should be avoided at all costs.

  1. Try this entering all numbers as negative.

  2. Try setting it to INT_MAX and try again. Note that this is the correct solution to the problem.

As another editorial remark, it's probably not a good idea to modify the dimensions of the array. As declared in main() m is 50 by 50. But you're effectively viewing it as 3 by 3 in findRowWithMinimalSum(). That's not a good habit to get into, I can pretty much guarantee that doing so will cause problems for you at some point in the future.

Finally get yourself a debugger. This would have jumped right off the screen at you if you'd single stepped through findRowWithMinimalSum() watching how the variables change as the program progresses.

Upvotes: 4

elouassif
elouassif

Reputation: 318

this will work

#include<stdio.h>

void findRowWithMinimalSum(int row, int col, int A[row][col]){
    int i,j,sum,minSum,position;

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

  for(i=0; i<row; i++){
    for(j=0; j<col; j++)
        printf("%d", A[i][j]);
    printf("\n");
  }

   for(i=0; i<row; i++){
    sum=0;
    for(j=0; j<col; j++){
        sum = sum + A[i][j];
    }


    if(i==0){
        minSum = sum;
        position = i;
    }
    else if(sum<minSum){
        minSum = sum;
        position = i;
    }
   }
   printf("\nThe row with minimal sum is %d whose sum is %d", position+1, minSum);
  }

int main(){
 int row, column, m[50][50];

 //function call
 findRowWithMinimalSum(3,3,m);
  return 0;
 }

Upvotes: 0

Related Questions