Thread: Condition statements help

  1. #1
    Registered User
    Join Date
    Sep 2013
    Posts
    5

    Condition statements help

    Hello, this is my homework:
    Write a program that prompts the user for an observed resistivity of an unknown material (as nano-ohm metres) and identies the substance if the observed resistivity is within
    3% of the known resistivity of any of the materials in Table
    1. If the input value lies outside the 3% range, output Unknown substance.
    Table 1

    Copper 16.78.
    Aluminum 26.50
    Beryllium 35.6

    Potassium 72.0
    Iron 96.10

    And here is my code:
    Code:
    /* Duy Nguyen
    CSE155h
    ID:02104267*/
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    void main(int argc, char *argv[]){
    double res;    
    printf("Enter the observed resistivity:\n");
    scanf("%lf", &res);
    // Identify
        if (res <= 16.78*0.03+16.78 &&
            res >= 16.78*0.03-16.78)
            printf("The material is  Copper\n");
        else if (res <= 26.5*0.03+26.5 &&
                res >= 26.5*0.03-26.5)
                printf("The material is  Aluminium\n");
        else if (res <= 35.6*0.03+35.6 &&
                res >= 35.6*0.03-35.6)
                printf("The material is  Beryllium\n");
        else if (res <= 72.0*0.03+72.0 &&
                res >= 72.0*0.03-72.0)
                printf("The material is  Potassium\n");
        else if (res <= 96.1*0.03+96.1 &&
                res >= 96.1*0.03-96.1)
                printf("The material is  Iron\n");
            else 
                printf("Unknown substance\n");
    return 0;
    }


    Most of the time, it works fine. However, there is a problem when I enter the value lower than the resistivity of a certain substance. It'll just output that substance's name. For example if I enter 20. It'll will say that the substance is Aluminium(which is not correct). If I enter 100 It'll say unknown, but if I enter 1 I'll say Copper.. Help




  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    if you think this values are incorrect - you should really change your ranges
    Code:
    Coper [-16.276600,17.283400]
    Aluminium [-25.705000,27.295000]
    Beryllium [-34.532000,36.668000]
    Potassium [-69.840000,74.160000]
    Iron [-93.217000,98.983000]
    Code:
    #include <stdio.h>
    
    int main(void){
    
        printf ("Coper [%f,%f]\n", 16.78*0.03-16.78,16.78*0.03+16.78);
        printf ("Aluminium [%f,%f]\n", 26.5*0.03-26.5,26.5*0.03+26.5);
        printf ("Beryllium [%f,%f]\n", 35.6*0.03-35.6,35.6*0.03+35.6);
        printf ("Potassium [%f,%f]\n", 72.0*0.03-72.0,72.0*0.03+72.0);
        printf ("Iron [%f,%f]\n", 96.1*0.03-96.1,96.1*0.03+96.1);
        return 0;
    }
    PS. FAQ > main() / void main() / int main() / int main(void) / int main(int argc, char *argv[]) - Cprogramming.com
    Last edited by vart; 09-19-2013 at 10:00 PM.
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Sep 2013
    Posts
    5
    I'm sorry, but you are not getting my question. My code should check if the value the user enter is with in the range of 3% or not, and then output the name of that substance according to the table

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    your table is wrong - you need to fix it
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by huuduy216
    I'm sorry, but you are not getting my question. My code should check if the value the user enter is with in the range of 3% or not, and then output the name of that substance according to the table
    vart pointed out that the ranges that you are using to check are wrong. For example, the range you are using for copper is: [-16.276600,17.283400]. This means that 0 is in the range, but obviously 0 is not within 3% of 16.78. The correct range should be: [16.78 * 0.97, 16.78 * 1.03].
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Sep 2013
    Posts
    5
    Okay, I see thanks. I probably need more sleep haha

  7. #7
    Registered User loserone+_+'s Avatar
    Join Date
    Dec 2012
    Location
    Indonesia
    Posts
    112
    why the calculation for cooper bla bla bla in if parameter ? why you didnt make a variable that hold the value of the material ?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Execution of statements in a loop condition
    By 127.0.0.1 in forum C Programming
    Replies: 4
    Last Post: 05-10-2011, 06:12 AM
  2. if condition
    By jgtech in forum C Programming
    Replies: 1
    Last Post: 04-12-2011, 05:25 AM
  3. If condition
    By rits in forum C Programming
    Replies: 3
    Last Post: 09-02-2009, 05:54 AM
  4. c++ condition use? how u use it? help
    By mikeasianlee in forum C++ Programming
    Replies: 4
    Last Post: 12-06-2004, 09:44 PM
  5. if statements using strings as the condition
    By trang in forum C Programming
    Replies: 7
    Last Post: 12-13-2003, 05:20 PM