I am stuck with "2/5", Need help or advice

• 09-24-2011
dapping
I am stuck with "2/5", Need help or advice
Hello guys, I am new in here,

Need help in C program, basically, I would like trying input number such as "2/5", "1/2","3/4"..etc not 0.5 , 0.25, 0.333 something like that on the screen and it will be automatically calculated by my program.
I am stuck and need help, has somebody ever done a program something like that? it's like to input number in Matlab.

Regards,:redface:
• 09-24-2011
You tried this with doubles yet?

Some numbers will be approximations, but they're close.
• 09-24-2011
Salem
You mean sometimes type in "1/2" and other times type in "0.5", and the program treats them as being the same thing?

Is it restricted to simple fractions, or do you want a full expression evaluator there?
• 09-24-2011
dapping
Quote:

You tried this with doubles yet?

Some numbers will be approximations, but they're close.

Thanks for your reply, :redface:, but definitely, I don't understand what you meant.
The idea is : I could input my number with "/" and directly calculated.

Okay, actually I'm building a program for gaussian elimination. And User can input by fraction number, not round number...

Maybe, I should share to you guys my gaussian program. I just want to input my number by fraction number such as "1/2", "1/3", "1/4"...etc, and on the screen will automatically show the input become "0,50", "0,33", "0,25"...My question is "How to create it happens?", Thanks for your help guys....Actually my matrix has used double.

Code:

```#include <stdio.h> #include <math.h> #define NMAX 100 int main() {     int m,n; //rows and columns     int i,j,k; //help character     double A[NMAX][NMAX]; //matrix input     double x[NMAX][1];    //matrix solved     double b[NMAX][1];    //matrix output     /**********************/     /*  [A][x]=[b]      */     /**********************/     double pivot; //pivot element /*Input First****************************************************************************************/         printf("This is a Gaussian Elimination program \nthat can only solve linier problem\n[A][x]=[b] ");     printf("Enter Input Matrix [A] Size Dimension (m,n) :\n");     printf("Amount of Rows:\n");     scanf ("%d",&m);     printf("Amount of Colums:\n");     scanf ("%d",&n);         if (m>100 || n>100)     {         printf("Matrix Size A is more than 100.\n");         getch();         exit(1);     }         //Enter Input for A matrix     printf("Enter Input Number for [A] matrix:\n");         for (i=0;i<m;i++)     {         printf("new row:\n");                       for (j=0;j<n;j++)         {             scanf("%lf",&A[i][j]);  //Input number                              }     }         //Let to display the matrix input A     printf("\nYour [A] Matrix:\n");     for (i=0;i<m;i++)     {         for (j=0;j<n;j++)         {             printf(" %.4f", A[i][j]);         }         printf("\n");     }         //Enter input for b Matrix     printf("\nEnter Input Number for [b] matrix:\n");     printf("only one column:\n");     for (i=0;i<m;i++)     {         scanf("%lf",&b[i][0]);  //Input number                          }         //Let to display the matrix output b     printf("\nYour [b] Matrix:\n");     for (i=0;i<m;i++)     {         printf(" %.4f", b[i][0]);         printf("\n");     } /*********************************************Input Finished**************************/ /*********************************************Start to Elimination********************/ /*Gaussian will be applied************************************************************/ for (j=0;j<n;j++) {     for (i=j;i<m;i++)     {         pivot=A[i+1][j]/A[j][j];         b[i+1][0]=b[i+1][0]-(pivot*b[j][0]); //Each element of b         for(k=j;k<n;k++)         {             A[i+1][k]=A[i+1][k]-(pivot*A[j][k]); //Each element of A         }        } }     //Show result matrix after elemination     printf("\nAfter Elimination*************\n");     printf("Your [A] Matrix:\n");     for (i=0;i<m;i++)     {         for (j=0;j<n;j++)         {             printf(" %.4f", A[i][j]);         }         printf("\n");     }     printf("\nYour [b] Matrix:\n");     for (i=0;i<m;i++)     {         printf("%.4f", b[i][0]);         printf("\n");     } /********************************************End Elimination**************************/ /*Show the result*********************************************************************/ x[m-1][0]=b[m-1][0]/A[m-1][n-1]; //x(m,n) result for (i=m-2;i>=0;i--) {     x[i][0]=b[i][0];         for (j=m-1;j>=i+1;j--)     {         x[i][0]=x[i][0]-(x[j][0]*A[i][j]);        }     x[i][0]=x[i][0]/A[i][i]; }     //Show result matrix after elemination     printf("\nAfter Substitution*************\n");     printf("Your [X] Matrix:\n");     for (i=0;i<m;i++)     {         printf("%.4f", x[i][0]);         printf("\n");     } /********************************************End Show*********************************/            getch();     return 0;     }```
I programmed it by Dev-C++, This program definitely succeeded, but I just want to change the input by fraction number. And now I stuck with my problem.

Regards,
• 09-24-2011
dapping
Quote:

Originally Posted by Salem
You mean sometimes type in "1/2" and other times type in "0.5", and the program treats them as being the same thing?

Is it restricted to simple fractions, or do you want a full expression evaluator there?

Yeah, I just want to input my number with "1/2" and it will show automatically become "0.5". Obviously, If I input the number with "0.5" , I will not ask in here...:)
• 09-24-2011
AndrewHunter
scanf is formatted input. That means if you want to have the user input "number/number" your could do something like
scanf("%d/%d", &num1, &num2). That would input two numbers seperated by a '/'.
• 09-24-2011
dapping
Quote:

Originally Posted by AndrewHunter
scanf is formatted input. That means if you want to have the user input "number/number" your could do something like
scanf("%d/%d", &num1, &num2). That would input two numbers seperated by a '/'.

Well, thank for your help, I don't know if scanf can do like that. I've tried yours, but Now I cannot input only one number...:) Solved!
• 09-24-2011
AndrewHunter
Quote:

Originally Posted by dapping
Well, thank for your help, I don't know if scanf can do like that.

You are right, I have no idea what I am talking about.

Quote:

Originally Posted by dapping
I've tried yours, but Now I cannot input only one number...:) Solved!

The easiest way to do multiple input options is to use an fgets / sscanf pair. However since I am clueless I will defer to your expertise on the subject.
• 09-24-2011
iMalc
You'll need to read it in as a string then, and from than you can try to sscanf into a %d/%d format and if that does not return that two arguments were filled then try scanning if into a float instead using %f.
Do you follow that, or do you want an example?