Академический Документы
Профессиональный Документы
Культура Документы
Dry -Run
How does the programmer know whether his/her program is correct? S/He should be able to anticipate the results. What if the results are wrong? How does s/he know which lines are wrong? He should be able to anticipate what is happening at different lines and then check with what the computer is actually giving. The programmer should thus dry-run his program.
When a program executes, there is processing of data values. These cause the values of variables to change. During a dry-run the programmer should be able to follow all the changes in data values.
Eg. - Consider the following program 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. #include <stdio.h> int main() { int x,y,z; x=15; y=2*x+5; z=x+y; y = z + 2*y; x = 2*x; y= 2*x + y; z = z + y; printf (The final values are %d %d %d\n,x,y,z); return 1; }
Line No. 5 6 7 8 9 10 11
x 15 15 15 15 30 30 30
z ? ? 50 50 50 50 230
Line No. 4 5 6 7 8 9 10
a ? 30 30 30 45 45 45
c ? ? 60 60 60 60 176
Type Casting
C type casting is used to tell the C compiler to treat a variable as of a different type in a specific context. The compiler will automatically change one type of data into another if it makes sense. For instance, if you assign an integer value to a floating-point variable, the compiler will insert code to convert the int to a float. Casting allows you to make this type conversion explicit, or to force it when it wouldnt normally happen.
7
Type Casting
To perform explicit type casting, put the desired type including modifiers (like unsigned) inside parentheses to the left of the variable or constant you want to cast.
Eg. Y= (float) x; Z = (float) x / y; Casting is particularly useful when we want to divide an integer x by another integer y, and we want the result to include the fractional part. With the declarations
float z; int x,y;
given that the values of x and y are respectively 12 and 5; z = x/y will yield 2 as the value of z, whereas z = (float) x/ y will yield 2.4.
Type Casting
/*Explicit casting from int to char*/
Another equivalent code: int x; X=97; printf(%c , (char)x);
Conditional Statements
In the programs discussed so far all statements are executed once and only once.
This is unrealistic in practice.
11
14
condition
{ }
Variable or literal or expression Eg. of conditions (x > y) (x<=z) (y==5) (z!=0) ((x+y) <10)) Conditions evaluate to true or false
Relational Operator
{ }
Variable or literal or expression
15
16
#include <stdio.h> int main() { float salary, bonus; int years; printf("Input Salary: "); scanf("%f", &salary); printf("Input No. of years of service: "); scanf("%d", &years); bonus = 0.5*salary; if (years>=10) bonus = bonus +3000; printf("Total Bonus is %.2f\n", bonus); }
17
20
Pitfalls
Using = in place of == What is the difference between these two? if (toss = = 7) printf(You win the bet.); if (toss = 7) printf(You win the bet.);
21
22
#include <stdio.h> int main() { float salary, bonus; int years; printf("Input Salary: "); scanf("%f", &salary); printf("Input No. of years of service: "); scanf("%d", &years); if (years>=10) bonus = salary; else bonus = 0.5*salary; printf(" Bonus is %.2f\n", bonus); return 1; }
24
25
if .else examples
Exercise - A company pays a travel grant of Rs 5000 and a
children allowance of Rs 3000 per child to all its employees earning above Rs 25,000, whereas other employees receive a travelling allowance of Rs 3000 and a children allowance of Rs 2500 per child. Write a program to input all required information for an employee and display the total income obtainable.
27
Pseudo codes
input Salary input no-of-children if (income > 25000) travel-grant=5000 child-allow = no-of-children x 3000 else travel-grant=3000 child-allow = no-of-children x 2500 endif income= salary+travel-grant + child-allow output income
28
#include <stdio.h> int main() { float salary, income,travel_grant,child_allow; int children; printf("Input Salary: "); scanf("%f",&salary); printf("Input No. of children: "); scanf("%d", &children); if (salary>25000) { travel_grant=5000; child_allow=children*3000; }
else { travel_grant=3000; child_allow=children*2500; } income = salary+ travel_grant + child_allow; printf("Total income is %.2f\n",income); return 1; }
29
Nested if Statements
A conditional structure can be enclosed in another conditional structure if (condition 1) { statements; if (condition 2); { statements; } statements; } . ..
30
Exercise
Students taking a given module are given chocolates according to the marks they obtain in the exams and their gender. Female students obtain a weight of chocolate (in grams) equivalent to 1.5 times their marks if they score 50 and above. Otherwise they obtain a weight equivalent to their marks. Male students obtain an amount equivalent to 1.25 times their marks irrespective of the marks. Write a program to input the marks of a student and the gender and output the weight of chocolate that he/she should obtain.
31
if (gender == 'f') if (marks >= 50) weight=1.5 *marks; else weight= marks; printf("Marks? "); else scanf("%f",&marks); weight = 1.25* marks; printf("Gender: (m/f): "); printf("Weight of Chocolate scanf("%c",&gender); obtained is %.2f\n",weight); getchar(); return 1; }
32
33
Eg. consider a program that reads an integer value from 0 to 5 and outputs the value in words.
#include <stdio.h> int main() { int value ; printf("Enter value: "); scanf("%d",&value); if (value == 0) printf("zero"); else if (value == 1) printf("one\n"); else if (value == 2) printf("two\n"); else if (value == 3) printf("three\n"); else if (value == 4) printf("four\n"); else if (value == 5) printf("five\n"); else printf("Wrong value\n"); return 1; }
34
Using Switch
#include <stdio.h> int main() { int value ; printf("Enter value: "); scanf("%d",&value); switch (value) { case 0: { printf("zero"); break;} case 1: { printf("one\n"); break; } case 2: { printf("two\n"); break; }
case 3: { printf("three\n"); break; } case 4: { printf("four\n"); break; } case 5: { printf("five\n"); break; } default: { printf("Wrong value\n"); break; } } return 1; }
35
Exercise: A bank has 4 types of accounts with annual interest rates as follows:
Savings 8% Savings with Cheque Book 5% Current 4% Fixed deposits 10%
Write a program that allows the input of the current balance of an account and the type of account and displays the total interest obtainable. Make use of the switch statement. Note : You have to choose the type of input you wish to use for account type.
36
#include <stdio.h> int main() { int account_type ; float balance, interest; printf("balance:"); scanf("%f",&balance); printf("Choose an Account Type\n"); printf("1: Savings \n"); printf("2: Savings with Cheque Book \n"); printf("3: Current \n"); printf("4: Fixed Deposits\n"); printf("Input account type: "); scanf("%d",&account_type);
switch (account_type) { case 1: {interest=0.08*balance; break;} case 2: {interest=0.05*balance; break;} case 3: {interest=0.04*balance; break;} case 4: {interest=0.1*balance; break;} default: { interest=-1; break;} } balance=balance+interest; if(interest==-1) printf("wrong type"); else printf("balance is %7.2f", balance); return 1; }
37
40
41
Pseudo codes
input Salary input no-of-children input age child-allow=0 if (income > 25000) and (age>=40) travel-grant=5000 child-allow = no-of-children x 3000 else travel-grant=3000 if (no-of-children >= 1) child-allow = 2500 endif endif income= salary+travel-grant + child-allow output income
42