Help with flawed algorithm?

This is a discussion on Help with flawed algorithm? within the C Programming forums, part of the General Programming Boards category; Originally Posted by qny If the user types "<SPACE> <4> <2> <SPACE> <ENTER>" this will not consume the <ENTER>. It ...

  1. #16
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by qny View Post
    If the user types "<SPACE> <4> <2> <SPACE> <ENTER>" this will not consume the <ENTER>.
    It presumes the user is not a drunken sod, at that time - true.

  2. #17
    Registered User
    Join Date
    Dec 2012
    Posts
    23
    Lol actually, Adak, it worked! I Had to tweak it a bit. But now I found another bug, prolly the last one! The code below is the mathematical algorithm the matrix does... The only problem is with division - if I enter numbers to divide - any numbers - it will tell me it failed (failed = the sum is NOT an integer!)

    Code:
     switch (matrix[i][j])
             {
                case '+': matrix[i+1][j] = (matrix[i-1][j]) + (matrix[i+1][j]);
                          break;
                case '-': matrix[i+1][j] = (matrix[i-1][j]) - (matrix[i+1][j]);
                          break;
                case '*': matrix[i+1][j] = (matrix[i-1][j]) * (matrix[i+1][j]);
                          break;
                case '/':  if ((matrix[i+1][j]) ==0 )
                          {
                              printf("There is a division by zero");
                              return 1;
                          }
                          if (((matrix[i-1][j] / matrix[i+1][j])%2)  != 0)
                          {
                              left=1;
                          }
    
    
                          if (left == 1 )
                          {
                              printf("There is a non-integer division");
                              return 1;
                          }
    
    
                          matrix[i+1][j] = (matrix[i-1][j]) / (matrix[i+1][j]);
                          break;
             }
    I tried a few variations but none worked...

    The %2 thingy doesn't work for some reason.... and it enters the "Non Integer division" all the time! Even if : 1/1-1

    Suggestions?

  3. #18
    Registered User
    Join Date
    Dec 2012
    Posts
    23
    Probably because both matrix[i-1][j] and matrix[i+1][j] are Integers. Any way around that, tho? To save only the division's output? I tried putting it into float but it won't work...

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 01-20-2011, 09:58 PM
  2. Replies: 3
    Last Post: 06-23-2010, 06:50 AM
  3. algorithm
    By osheghi in forum C++ Programming
    Replies: 0
    Last Post: 05-01-2009, 10:07 AM
  4. O(n) algorithm
    By wu_weidong in forum C Programming
    Replies: 16
    Last Post: 10-05-2005, 05:52 PM
  5. my grandfather's chess algorithm can beat your grandfather's chess algorithm...
    By doubleanti in forum A Brief History of Cprogramming.com
    Replies: 22
    Last Post: 08-17-2001, 06:52 PM

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