Solved

find max value after some calculation

Posted on 2006-10-30
14
223 Views
Last Modified: 2012-05-05



#include <stdio.h>


float matrix [3][3]={{0.5,0.25,0.25},
                     {0.375,0.125,0.375},
                     {0.125,0.675,0.375}};

main (){
    int i,j,k;

     for (i=0; i<3; i++){
          printf("\n");
         for (j=0; j<3; j++) {
             printf("\n");
            for (k=0; k<3; k++){
              printf("Prob(%d%d%d)=%.5f\n",i,j,k,matrix[i][j]*matrix[j][k]);   ------------>? how to find max value
           
            }//end third for
                       
        }// end second for
    }// end first for


    system("pause");
}
0
Comment
Question by:rmtogether
  • 7
  • 7
14 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 17838648

#include <stdio.h>
#include <float.h>


float matrix [3][3]={{0.5,0.25,0.25},
                     {0.375,0.125,0.375},
                     {0.125,0.675,0.375}};

main (){
    int i,j,k;
    float max = -FLT_MAX;

     for (i=0; i<3; i++){
          printf("\n");
         for (j=0; j<3; j++) {
             printf("\n");
            for (k=0; k<3; k++){
   
               if( max < matrix[i][j]*matrix[j][k] ){ max = matrix[i][j]*matrix[j][k]; }
              printf("Prob(%d%d%d)=%.5f\n",i,j,k,matrix[i][j]*matrix[j][k]);   ------------>? how to find max valu
           
            }//end third for
                       
        }// end second for
    }// end first for


    system("pause");
}
0
 

Author Comment

by:rmtogether
ID: 17838719
thanks,

what is "-FLT_MAX" means?
0
 
LVL 84

Expert Comment

by:ozo
ID: 17838780
It is the smallest floating point number
0
 

Author Comment

by:rmtogether
ID: 17838850
thanks,

Can I ask you the last question? Since the max value is happen in more than one pleace, can I print a list of them?

for example,
in this program, the max value  is 0.25313 and happen in Prob (121), prob (210), prob(221). I would like have an end summary to show something like

   printf("max prob is: %.5f\n",max);
   printf("they are.....")---------------------------------? list of item has max value





#include <stdio.h>
#include <float.h>


float matrix [3][3]={{0.5,0.25,0.25},
                     {0.375,0.125,0.375},
                     {0.125,0.675,0.375}};

main (){
    int i,j,k;
    float max = -FLT_MAX;

     for (i=0; i<3; i++){
          printf("\n");
         for (j=0; j<3; j++) {
             printf("\n");
            for (k=0; k<3; k++){
   
               if( max < matrix[i][j]*matrix[j][k] ) {max = matrix[i][j]*matrix[j][k];
               }
                 printf("Prob(%d%d%d)=%.5f\n",i,j,k,matrix[i][j]*matrix[j][k]);  
           
            }//end third for
                       
        }// end second for
    }// end first for
   
   printf("\n");
   printf("max prob is: %.5f\n",max);
   printf("they are.....")---------------------------------? list of item has max value

    system("pause");
}
0
 
LVL 84

Expert Comment

by:ozo
ID: 17839038
It's generally not safe to rely on floating point to be the same when they happen in more than one place but in this case, all the values in your matrix are dyadic rationals, which can be exactly represented in floating point numbers when FLT_RADIX==2

      
   printf("max prob is: %.5f\n",max);
   printf("they are.....")
     for (i=0; i<3; i++){
         for (j=0; j<3; j++) {
            for (k=0; k<3; k++){
   
               if( max == matrix[i][j]*matrix[j][k] ){  printf("Prob(%d%d%d)\n",i,j,k);  }
           
            }//end third for
                       
        }// end second for
    }// end first for
   
0
 

Author Comment

by:rmtogether
ID: 17839127
hi, ozo

I put the code like below, but seems not work.. could you help me about it?

#include <stdio.h>
#include <float.h>


float matrix [3][3]={{0.5,0.25,0.25},
                     {0.375,0.125,0.375},
                     {0.125,0.675,0.375}};

main (){
    int i,j,k;
    float max = -FLT_MAX;

     for (i=0; i<3; i++){
          printf("\n");
         for (j=0; j<3; j++) {
             printf("\n");
            for (k=0; k<3; k++){
   
               if( max < matrix[i][j]*matrix[j][k] ) {max = matrix[i][j]*matrix[j][k];
               }
                 printf("Prob(%d%d%d)=%.5f\n",i,j,k,matrix[i][j]*matrix[j][k]);  
           
            }//end third for
                       
        }// end second for
    }// end first for
   
   
   printf("max prob is: %.5f\n",max);
   printf("they are.....");
     for (i=0; i<3; i++){
         for (j=0; j<3; j++) {
            for (k=0; k<3; k++){
   
               if( max == matrix[i][j]*matrix[j][k] ){  printf("Prob(%d%d%d)\n",i,j,k);  }
           
            }//end third for
                       
        }// end second for
    }// end first for

    system("pause");
}
0
 
LVL 84

Expert Comment

by:ozo
ID: 17840317
What does not seem to work?
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

Author Comment

by:rmtogether
ID: 17840928
there is  nothing showing after "they are...."
0
 

Author Comment

by:rmtogether
ID: 17840951

I don't see anything printed from this line

   if( max == matrix[i][j]*matrix[j][k] ){  printf("Prob(%d%d%d)\n",i,j,k);  }
0
 

Author Comment

by:rmtogether
ID: 17840965
it works after I change  float max = -FLT_MAX; to

double max = -FLT_MAX;
0
 
LVL 84

Expert Comment

by:ozo
ID: 17841090
This looks like an instance of the dangers of comparing floating point numbers for equality.
As I look more carefully at
float matrix [3][3]={{0.5,0.25,0.25},
                     {0.375,0.125,0.375},
                     {0.125,0.675,0.375}};
I see that you have 0.675 which is not represented exactly in floating point numbers with FLT_RADIX==2
instead of 0.625 which could have been represented exactly, so there could be round off errors that keep the comparison from being exact.

one way to handle it would be to use exact integer arithmetic with
long matrix [3][3]={{500,250,250},
                     {375,125,375},
                     {125,675,375}};
or to have some error tolerance
like
epsilon=0.0001;
 if( fabs(max - matrix[i][j]*matrix[j][k]) < epsilon ){  printf("Prob(%d%d%d)\n",i,j,k);  }


0
 

Author Comment

by:rmtogether
ID: 17841210
thanks,

(1) what does FLT_RADIX==2 means?
(2) if I change matrix to long and make
     printf to printf("Prob(%d%d%d)=%f\n",i,j,k,matrix[i][j]*matrix[j][k]*0.000001), do I still have round off problem?
0
 
LVL 84

Expert Comment

by:ozo
ID: 17841292
A floating-point number (x) is defined by the following model:

x =s * b^e *
p
&#931; f[k]*b^&#8722;k , emin &#8804; e &#8804; emax
k=1

s sign (±1)
b base or radix of exponent representation (an integer > 1)
e exponent (an integer between a minimum emin and a maximum
p precision (the number of base-b digits in the significand)
f[k] nonnegative integers less than b (the significand digits)

FLT_RADIX represents b
0
 
LVL 84

Accepted Solution

by:
ozo earned 500 total points
ID: 17841322
if you change matrix to long, matrix[i][j]*matrix[j][k] will not have a round off problem (although there could be an overflow problem)
matrix[i][j]*matrix[j][k]*0.000001 could have a round off problem, since 0.000001 might really be
0.000000999999997475242707878351 or 0.000000999999999999999954748112


0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now