I am new to stackoverflow as am new to programming, yet am not really a ‘professional and enthusiast programmer’. Enthusiast maybe but not professional…
In a part of some beginner code of mine i have a two dimensional array diff[i][j], where the value is zero wherever i==j. I am trying to get the smallest value in each row but not the zero value…
the part of the code (under construction) that searches the smallest of the first row is:
i=1;
double smallest;
for ( j=1 ; j<=n ; j++ )
{
smallest = diff[i][j];
if ( j!=i && diff[i][j] < smallest )
smallest = diff[i][j];
}
printf("\n %lf\n", smallest);
however, the result is always the biggest number not the smallest. Anyone knows why??
P.S. I’d be thankful for any suggestion or comment of dealing with stackoverflow.com and the way i asked my question, since am new here… thank you in advance…
EDIT
after the answers below, i decided to make the i=1 a special case and make two separate functions for both cases… however, when i try to assign j to other variable i failed… in the previous code:
if (j!=i && diff[i][j]<smallest) {smallest=diff[i][j]; d=j}
declared d previously and everything… when i print d it prints a random number >maybe the memory location content… tried for debugging to assign an initial value – with the declaration – and when printing it came out the initial value… the point is i want d to hold the column where the smallest value is… how can i acheive that??
You never initialize smallest
EDIT:
You also seem to have
{ smallest= diff[i][j]; .. }in your code that overrides the value of smallest each iteration. I removed it in my answer.