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

This is a discussion on I am stuck with "2/5", Need help or advice within the C Programming forums, part of the General Programming Boards category; Hello guys, I am new in here, Need help in C program, basically, I would like trying input number such ...

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    4

    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,

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You tried this with doubles yet?

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

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,834
    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?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Sep 2011
    Posts
    4
    Quote Originally Posted by Adak View Post
    You tried this with doubles yet?

    Some numbers will be approximations, but they're close.
    Thanks for your reply, , 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,

  5. #5
    Registered User
    Join Date
    Sep 2011
    Posts
    4
    Quote Originally Posted by Salem View Post
    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...

  6. #6
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    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 '/'.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  7. #7
    Registered User
    Join Date
    Sep 2011
    Posts
    4
    Quote Originally Posted by AndrewHunter View Post
    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!

  8. #8
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Quote Originally Posted by dapping View Post
    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 View Post
    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.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  9. #9
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    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?
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 08-11-2008, 01:50 AM
  2. Replies: 46
    Last Post: 08-24-2007, 05:52 PM
  3. Replies: 7
    Last Post: 08-28-2003, 11:15 PM
  4. "itoa"-"_itoa" , "inp"-"_inp", Why some functions have "
    By L.O.K. in forum Windows Programming
    Replies: 5
    Last Post: 12-08-2002, 08:25 AM
  5. "CWnd"-"HWnd","CBitmap"-"HBitmap"...., What is mean by "
    By L.O.K. in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2002, 07:59 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21