# Compiled without error but Result varies!!

• 02-12-2008
RahulDhanpat
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(); }```
• 02-12-2008
matsp
So, think about what the condition is for grade 6, and how the code flows in general.

--
Mats
• 02-12-2008
RahulDhanpat
Quote:

Originally Posted by matsp
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. :(
• 02-12-2008
matsp
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
• 02-12-2008
RahulDhanpat
Quote:

Originally Posted by matsp
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.....:)
• 02-12-2008
xuftugulus
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.
• 02-12-2008
matsp
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
• 02-12-2008
manav
rahul, have you solved the convert to roman numbers problem from the same book?
• 02-12-2008
RahulDhanpat
Quote:

Originally Posted by manav
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..
• 02-12-2008
RahulDhanpat
Quote:

Originally Posted by xuftugulus
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.
• 02-12-2008
Abda92
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...