Module 1
Module 1
Problem Inputs
Quantity of apples purchased (in pounds)
Cost per pound of apples (in dollars per pound)
Problem Output
Total cost of apples (in dollars)
Once you know the problem inputs and outputs, develop a list of formulas that specify relationships
between them.
The general formula is :
Total cost = Unit cost* Number of units
The above computes the total cost of any item purchased. Substituting the variables for our
particular problem yields the formula
Total cost of apples = Cost per pound * Pounds of apples
DVN Siva Kumar, Asst Prof, CSE Dept, GITAM Hyd
Problem Abstraction
Def(Abstract): This process of modeling a problem by extracting the
essential variables and their relationships is called abstraction
Top-down Design:
• It is the design process that involves breaking a problem into its major
subproblems and then solving the subproblems
• In top-down design (also called divide and conquer ), we first list the major steps
or subproblems, that need to be solved. Then we solve the original problem by
solving each of its subproblems.
DVN Siva Kumar, Asst Prof, CSE Dept, GITAM Hyd
3. Design (Cont…)
Algorithm for solving any programmable problem involves:
1. Get the data.
2. Perform the computations.
3. Display the results.
5. Testing:
• Testing and verifying the program requires testing the completed program to verify that
it works as desired.
• Here, don’t rely on just one test case.
• Run the program several times using different sets of data to make sure that it works
correctly for every situation provided for in the algorithm.
Note: We use the first five steps of the software development method to solve any
programming problems
ALGORITHM
1. Get the distance in miles.
2. Convert the distance to kilometers.
3. Display the distance in kilometers.
The refinement of step 2 is numbered as step 2.1 and is indented under step 2.
Desk check:
• Let’s desk check the algorithm before going further.
- If step 1 gets a distance of 10.0 miles
- step 2.1 would convert it to 1.609 *10.00 = 16.09 kilometers.
- This correct result would be displayed by step 3.
4: Implementation:
To implement the solution, we must write the algorithm as a C program.
• To do this, you must first tell the C compiler about the problem data
requirements—that is, what variables we are using for input and what variables
for output
• Next, convert each algorithm step (including refinement steps) into one or more
C programming statements.
Input
Output
Flowchart for an
algorithm that reads the
value of two variables
from the keyboard and
then in reverse order
Module call
statement (AVRG(ave,
a,b,c)
Note: The above one is condition with two possible outcomes. This is called Two-way
DVNselection.
Siva Kumar, Asst Prof, CSE Dept, GITAM Hyd
Multi-way Selection
Note: Loops are useful when we want to execute a set of statements repeatedly until some
condition becomes true.
Documentation Section
Link Section
Definition Section
Global Declaration Section
main() //Main Function Section
{
Declaration part
Executable part
}
Subprogram Section
Function 1
Function 2
(User defined functions)
-
-
Function n DVN Siva Kumar, Asst Prof, CSE Dept, GITAM Hyd
Basic Structure of a C program (Cont…)
Section Description
1. Documentation Section Consists of the description of the program, programmer's name, and creation date.
These are generally written in the form of comments.
Comments are two types:
- Single line comment, e.g., // comment text
- Multiline comment: /*
Line 1 text
Line 2 text
Line 3 text
/*
2. Link Section All header files are included in this section which contains different functions from the
libraries. It provides instructions to the compiler to link functions from the system
library. A copy of these header files is inserted into your code before compilation.
3. Definition Section Includes preprocessor directives, which contains symbolic constants.
E.g.: #define allows us to use constants in our code. It replaces all the constants with its
value in the code. Example: #define PI 3.14
4. Global Declarations Section It includes declaration of global variables, function declarations (to be used subprogram
section, static global variables, and user-defined functions.
- Global variables declared can be used (more than one function), i.e., throughout the
program. DVN Siva Kumar, Asst Prof, CSE Dept, GITAM Hyd
Basic Structure of a C program (Cont…)
Section Description
5. main() function section For every C program, the execution starts from the main() function. It is mandatory to
include a main() function in every C program.
6. Subprogram section Includes all user-defined functions (functions the user provides). They can contain the
inbuilt functions, and the function definitions declared in the Global Declaration
section. These are called in the main() function.
Output
Preprocessor
Expanded Code
Compiler
Assembly code
Assembler
Other object files Object code Libraries
Linker
executable code
(In Ubuntu, a.out is called as the executable file)
Note: List of variables need to be used in printf() only if format string specifiers are used.
DVN Siva Kumar, Asst Prof, CSE Dept, GITAM Hyd
scanf() function
• It is used to read data entered by a user from keyboard.
• #include<stido.h> must be included in a c program that uses scanf()
• The general form of scanf() statement is as follows:
scanf(“format string”, list of addresses of variables);
Format string in scanf can contain:
%f for printing real values
%d for printing integer values
%c for printing character values. It can contain other format specifiers as well as
per the type of variables.
Note: No escape sequences to be used in scanf
Alphabets Constants
Instructions
Digits Variables
Or Program
Special Symbols Keywords
Statements
Digits : 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
Constants
Our focus
for now is
on
Primary Constants Secondary Constants
Output
Execution:
./CharRange
Execution:
./Sample
Execution:
./Sample4
#include<stdio.h>
int main()
{
char ch='A';
ch=ch+2;
printf("The value of a character after adding 2 is %c\n",ch);
printf("The character calue in integer value form is %d\n",ch);
return 0;
}
Output
Execution:
./CharDemo
Execution: Output
./Lab1
Execution:
./Lab2
Test Case2:
Input:100
Output:37.777779
c = a+b;
printf("a+b = %d \n",c);
c = a-b;
printf("a-b = %d \n",c);
c = a*b;
printf("a*b = %d \n",c);
c = a/b;
printf("a/b = %d \n",c);
c = a%b;
printf("Remainder when a divided by b = %d \n",c);
return 0;
}
DVN Siva Kumar, Asst Prof, CSE Dept, GITAM Hyd
Real Arithmetic
• An arithmetic operation involving real operands is called real
arithmetic.
Example of Real arithmetic expression:
x=6.0/7.0
The x value would be 0.857143
• A real operand may assume values either in decimal or exponential
notation.
• Since floating point values are rounded to the number of singinificant
digits permissible, the final value is an approximation of the correct
result.
Operator Meaning
&& Logical And
|| Logical Or
! Logical Not
Non-Zero Non-Zero 1 1
Non-Zero 0 0 1
0 Non-Zero 0 1
0 0 0 0
return 0;
DVN Siva Kumar, Asst Prof, CSE Dept, GITAM Hyd
}
Expressions and Theirs outputs
Expression Output
-14%3 -2
14%-3 -2
15/10.0 1.5
15/10 1
Suppose a=2, b=3, c=1, 0
Then the value of (a>b) && (a>c)
Suppose a=2, b=3, c=1, 1
Then the value of (a<b) && (c<a)
• Shorthand Notation: C also supports shorthand assignment operators that is shown below.
v op=exp;// This is known as shorthand operator.
The above is equivalent to
v= v op exp;
here, v is a variable, exp is an expression, and op is an arithmetic operator.
Note: Shorthand notation can be used with all arithmetic operators.
• A prefix operator first adds 1 to the operand and then the result is assigned to the variable on
left.
• For example:
v=5;
y=++v;
Here, the value of y would be 6 and v would be 6.
• A postfix operator first assigns the value to the variable on left and then increments the
operand. Example is provided below.
v=5;
y=v++;
Here, the value of y would be 5 but the value of v would be 6 (due to post increment).
return 0;
}
<< Shift left. Here, this operator left shifts the bits of b<<1
the first operand, the second operand decides the It produces 18
number of places to shift.
>> Shift right. Here, this operator right shifts the bits of b>>1
the first operand, the second operand decides the It produces 4
number of places to shift.
return 0;
} DVN Siva Kumar, Asst Prof, CSE Dept, GITAM Hyd
8. Special Operators
• C supports some of the special operators. Two of them provided
below.
i. comma operator (,)
ii. sizeof() operator
Test case 2:
Input: 10 -20
Output: 10
Test Case 1:
Input: A
Output: a
Test Case 2:
Input: E
Output: e
Test Case 1:
Inputs of a and b before swapping are: 5 6
Output of a and b after swapping are: 6 5
Test Case 2:
Inputs of a and b before swapping are: 19 20
Output of a and b after swapping are: 20 19
Test Case 1:
Inputs of a and b before swapping are: 5 6
Output of a and b after swapping are: 6 5
Test Case 2:
Inputs of a and b before swapping are: 19 20
Output of a and b after swapping are: 20 19
Test case 2:
Input: 14 8
Output: 6
Example 2:
int i=pow(5,2), returns the value 52 ,i.e., 25
Note:When integer is used to assign the result of math function, then the
result (decimal part) will be truncated to integer value.
DVN Siva Kumar, Asst Prof, CSE Dept, GITAM Hyd
Functions of math.h header file
Function Meaning Examples
ceil(x) It rounds up the given value and returns the i. ceil(1.6) returns 2.0
smallest integer value greater than or equal ii. ceil(1.2) returns 2.0
to x.
floor(x) It rounds down the given value and returns floor(1.6) returns 1.0
the largest integer value less than or equal floor(1.2) returns 1.0
to x
fabs(x) It returns the absolute value of x. fabs(1234) returns 1234.0
fabs(-4) returns 4.0
log(x) It returns the natural logarithm (base-e log(2.7) returns 0.993252
logarithm) of x.
Note:#include<math.h> must be included in your C program when you the above
functions in your program DVN Siva Kumar, Asst Prof, CSE Dept, GITAM Hyd
Functions of math.h header file
Function Meaning Examples
log10(x) It returns the common logarithm (base-10 log10(10000) returns 4.0
logarithm) of x.
fmod(x) It returns the remainder of x divided by y. fmod(8.2,5.7) returns
2.500000
sqrt(x) It returns the square root of x Sqrt(225) returns 15
Sqrt(300.0) returns
17.320508
pow(x,y) It returns x raised to the power of y, i.e., 𝑥 𝑦 pow(8,3) returns 24.0
Pow(3.05, 1.98) returns
9.097324
// Time
double time;
printf("Enter principal amount");
scanf("%lf",&principal);
printf("Enter rate of interest\n");
scanf("%lf",&rate);
printf("Enter time in years");
scanf("%lf",&time);
double si=(principal*time*rate)/100;// simnple interst
printf("The simple interest is %lf\n",si);
bool x = true;
char y = ‘X’;
int i = 123;
Long double d = 1234.5;
y = x; // value of y is 1 (ASCII)
i = y; // value of i is 88
d = x; // value of d is 1.0
d = i; // value of d is 1234.0
Output:
float f=a/b;
printf("%f",f);
Output:
1
float f=(float)a/b;
printf("%f",f);
Output:
1.666667
int main()
{
double x = 1.2;
Output:
sum =2
// Explicit conversion from double to int
int sum = (int)x + 1;
return 0;
}
Testcase1:
Input:
Enter value of a number:10
Enter kth bit value: 4
Output:
1
Test case:
Input
Enter First Angle: 60
Enter Second Angle: 80
Output
Third Angle: 40