Thread: Compiled without error but Result varies!!

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    83

    Compiled without error but Result varies!!

    Question:-
    A Certain grade of steel is graded according in the following condition.
    (1) Hardness must be greater then 50.
    (2) Tesile strength must be greater then 5600.
    (3) Carbon content must be less then 0.7


    The grades are as follows
    Grade is 10 if all above conditions are met.
    Grade is 9 when 1 and 2 condition are met.
    Grade is 8 when 2 and 3 conditions are met.
    Grade is 7 when 1 and 3 conditions are met.
    Grade is 6 if only one condition met.
    Grade is 5 if none are met.

    I m getting wrong output for Grade 10 and 6.Both are over lapping.
    when all the condition are met it shd show Grade 10 but output is Both,, "Grade 10 and Grade 6. infact it shd be only Grade 10........

    here is the code i have used for it..
    CHECK MY CODE AND PLEASE GUIDE ME...

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <conio.h>
    #include <ctype.h>
    main()
    {
    clrscr();
    int h,t;
    float c;
    printf("Enter The Hardness , Tensile Strength And Carbon Content");
    scanf("%d%d%f",&h,&t,&c);
    /* For Grade 10 */
    if((h>50 && c<0.7 && t>5600))
       printf("The Grade is 10");
    /* For Grade 9 */
    if((h>50 && c<0.7 && t<5600))
       printf("The Grade is 9");
    /* For Grade 8*/
    if((h<50 && c<0.7 && t>5600))
       printf("The Grade is 8");
    /* For Grade 7 */
    if((h>50 && c>0.7 && t>5600))
       printf("The Grade is 7");
    /* For Grade 6 */
    if((h>50) ||(c<0.7)||(t>5600))
       printf("The Grade is 6");
    
    else
    printf(" The Grade is 5");
    
    getch();
    }

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So, think about what the condition is for grade 6, and how the code flows in general.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    83
    Quote Originally Posted by matsp View Post
    So, think about what the condition is for grade 6, and how the code flows in general.

    --
    Mats

    I have tried alot ... but i changed the pgm in different ways..but it was worst.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Right, whilst experimenting is sometimes a good way to learn, it is also a waste of time to just "change this to see what happens". To solve your problem, you have to understand the flow of the code - and yes, it's broken as it was posted above, and by the sounds of it, you haven't fixed it.

    Take a pen, and step through each line of the code, and make a note of what happens when you get there, if it's executed, what the result becomes from that, etc, etc.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Apr 2006
    Posts
    83
    Quote Originally Posted by matsp View Post
    Right, whilst experimenting is sometimes a good way to learn, it is also a waste of time to just "change this to see what happens". To solve your problem, you have to understand the flow of the code - and yes, it's broken as it was posted above, and by the sounds of it, you haven't fixed it.

    Take a pen, and step through each line of the code, and make a note of what happens when you get there, if it's executed, what the result becomes from that, etc, etc.

    --
    Mats
    It looks a nice suggestion....Started working over this.....

  6. #6
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355
    When you have conditions that are used to find a value, in your case grade, then it is best to use a variable and store the value as the program runs. Here some of the conditions make the program print again, more outputs than one because you don't stop the outputs in a controlled way.

    Besides the program is difficult to read because the printf() everywhere is covering the programs purpose, which is to calculate a number, then print it.

    I give you the solution for studying. You are quite a few hours working now, and probably tired. Sometimes things are understood better when we leave them for a while to relax, and focus on what we were learning.

    matsp is right about the pen and paper thing. Make a plan, before writing code. Use only the things you have learned well and understand. This program introduces nested if's.
    Imagine trying all the combinations of the three conditions you stated. There are 3 combinations which are NOT mutually exclusive. Try to figure out which they are.
    And since they are NOT mutually exclusive, they cannot be tested on the same level of nesting.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <conio.h>
    #include <ctype.h>
    main()
    {
    clrscr();
    int h,t;
    float c;
    int grade;
    printf("Enter The Hardness , Tensile Strength And Carbon Content");
    scanf("&#37;d%d%f",&h,&t,&c);
    if(h>50)
    {
        if(c<0.7)
        {
            if(t>5600)
                grade = 10;  /* All three hold here */
            else
                grade = 9;   
        } else
        {
            if(t>5600)
                grade = 7;
            else
                grade = 6;   /* h>50 was only satisfied */
        }
    } else
    {
        if(c<0.7)
        {
            if(t>5600)
                grade = 8;
            else
                grade = 6;   /*  c<0.7 was only satisfied */
        } else
            if(t>5600)
                grade = 6;  /*  t>5600 was only satisfied */
            else
                grade = 5;   /* None was satisfied  */
    }
    
    printf(" The Grade is %d",grade);
    
    getch();
    }
    Note that for three conditions there are eight combinations. For four there would be sixteen. Sometimes, it is possible to significantly reduce the number of conditions to be tested to reach a correct answer. But that demands understanding how to perform reduction on boolean algebra. At such cases it is best to break a problem to smaller bits, rather than trying to solve it all at once.
    Last edited by xuftugulus; 02-12-2008 at 06:20 AM.

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, I was trying to get Rahul to understand this by experience, rather than by posting a ready-made concept. By the way, your solution is a good one.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Banned
    Join Date
    Nov 2007
    Posts
    678
    rahul, have you solved the convert to roman numbers problem from the same book?

  9. #9
    Registered User
    Join Date
    Apr 2006
    Posts
    83
    Quote Originally Posted by manav View Post
    rahul, have you solved the convert to roman numbers problem from the same book?
    I have not encountered the problem till now... in which chapter it comes.. i just completed 2 chapters and going to learn Looping now..

  10. #10
    Registered User
    Join Date
    Apr 2006
    Posts
    83
    Quote Originally Posted by xuftugulus View Post
    When you have conditions that are used to find a value, in your case grade, then it is best to use a variable and store the value as the program runs. Here some of the conditions make the program print again, more outputs than one because you don't stop the outputs in a controlled way.

    Besides the program is difficult to read because the printf() everywhere is covering the programs purpose, which is to calculate a number, then print it.

    I give you the solution for studying. You are quite a few hours working now, and probably tired. Sometimes things are understood better when we leave them for a while to relax, and focus on what we were learning.

    matsp is right about the pen and paper thing. Make a plan, before writing code. Use only the things you have learned well and understand. This program introduces nested if's.
    Imagine trying all the combinations of the three conditions you stated. There are 3 combinations which are NOT mutually exclusive. Try to figure out which they are.
    And since they are NOT mutually exclusive, they cannot be tested on the same level of nesting.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <conio.h>
    #include <ctype.h>
    main()
    {
    clrscr();
    int h,t;
    float c;
    int grade;
    printf("Enter The Hardness , Tensile Strength And Carbon Content");
    scanf("%d%d%f",&h,&t,&c);
    if(h>50)
    {
        if(c<0.7)
        {
            if(t>5600)
                grade = 10;  /* All three hold here */
            else
                grade = 9;   
        } else
        {
            if(t>5600)
                grade = 7;
            else
                grade = 6;   /* h>50 was only satisfied */
        }
    } else
    {
        if(c<0.7)
        {
            if(t>5600)
                grade = 8;
            else
                grade = 6;   /*  c<0.7 was only satisfied */
        } else
            if(t>5600)
                grade = 6;  /*  t>5600 was only satisfied */
            else
                grade = 5;   /* None was satisfied  */
    }
    
    printf(" The Grade is %d",grade);
    
    getch();
    }
    Note that for three conditions there are eight combinations. For four there would be sixteen. Sometimes, it is possible to significantly reduce the number of conditions to be tested to reach a correct answer. But that demands understanding how to perform reduction on boolean algebra. At such cases it is best to break a problem to smaller bits, rather than trying to solve it all at once.

    Very nice and easy to get!!!!!! i m also trying to solve it the way i started ..as per book dmands " we can not use nest if else .....

    But i learnt alot from your pgm.

  11. #11
    Registered User
    Join Date
    Sep 2006
    Posts
    230
    Since you need one and only one of the grades to be output why don't you use if... else if... else.
    e.g.
    Code:
    /* For Grade 7 */
    if((h>50 && c>0.7 && t>5600))
       printf("The Grade is 7");
    /* For Grade 6 */
    else if((h>50) ||(c<0.7)||(t>5600))
       printf("The Grade is 6");
    
    else
    printf(" The Grade is 5");
    Please note that you'll have to change the first one above in the actual program.

    But didn't you find the problem with your old version yet?
    look at it like this. If we have three logical expressions, and they will all return a value of true (that is, 1 in C). Then which one of these will work? or will both work?
    Code:
    int a=1, b=1, c=1;      // The values the expressions will return
    
    if (a && b && c) printf("All the expressions are true");
    if (a || b || c) printf("At least one of the expressions are true");
    So? Which one of these will be printed (if not both)???

    Sorry if I made it confusing...
    I might not be a pro, but I'm usually right

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inserting a swf file in a windows application
    By face_master in forum Windows Programming
    Replies: 12
    Last Post: 05-03-2009, 11:29 AM
  2. Replies: 1
    Last Post: 03-12-2008, 12:10 AM
  3. Output problems with structures
    By Gkitty in forum C Programming
    Replies: 1
    Last Post: 12-16-2002, 05:27 AM
  4. reverse a singly linked list
    By ivandn in forum C Programming
    Replies: 15
    Last Post: 12-18-2001, 05:33 PM
  5. VC 6.0 compiled error
    By alan4100 in forum Windows Programming
    Replies: 4
    Last Post: 09-17-2001, 03:10 PM