CSEG1003 Programming for Problem Solving
CSEG 1001
Computer Programming
Instructor
Dhiviya Rose J . Asst. Prof. Senior Scale
School of Computer Science and Engineering | UPES
CSEG1003 Programming for Problem
Solving
Road Map
• Generations of Computers and Languages
• Organization of Computers-Online Lecture
• Number Systems Conversion
• Logical Analysis and Thinking
Introduction to
Computers
• Structure of C Program & Compilation and Linking Process
• Variables and Datatypes
• Managing Input and Output statements and Operators
• Decision and Looping Statements
C Programming
Basics
• Creation and Usages
• 1D and 2 D arrys
• String Functions
• Matrix operations
Arrays and
Strings
• Declaration and Definitions of Functions
• Passing Arguments
• Recursion
• Pointers & Pointer Arithmetic
Functions and
Pointers
• Need of Structure and Unions
• Declaration and Definition
• Storage classes
• Preprocessor Directives
Structures and
Unions
CSEG1003 Programming for Problem
Solving
Online Lecture
• Problem Formulation
• Problem Solving
• Introduction to C
CSEG1003 Programming for Problem
Solving
LECTURE #8
STRUCTURE OF C PROGRAM ..COMPILATIONAND
LINKING
Instructor
Dhiviya Rose J . Asst. Prof. Senior Scale
School of Computer Science and Engineering | UPES
CSEG1003 Programming for Problem
Solving
CSEG1003 Programming for Problem Solving
CSEG1003 Programming for Problem Solving
Structured Blocks -> Functions
• Solves a large problem
• divides the problem into smaller
modules called functions
• each have particular responsibility.
• The program which solves the
entire problem is a collection of
such functions.
• Types
• Built-in Functions (Predefined)
• E.g. printf(), scanf()
• User-defined Functions
CSEG1003 Programming for Problem
Solving
Example Program 1
#include<stdio.h>
void main()
{
printf("Hello");
printf("this is my function");
}
9Instructor: Dhiviya Rose J , AP-Sr. Scale | CIT
Example Program 2
#include<stdio.h> PREPROCESSOR DIRECTIVE
void myline(); FUNCTION DECLARATION
void main()
{
printf("Hello");
myline(); FUNCTION CALL
}
void myline()
{
printf("this is my function"); FUNCTION DEFINITION
}
10Instructor: Dhiviya Rose J , AP-Sr. Scale | CIT
Functions Example 3
CSEG1003 Programming for Problem
Solving
OUTPUT ?????
Function Call ????
Function definition???
Function Declaration????
Main Function
CSEG1003 Programming for Problem
Solving
• When system is executed it calls the main function
• Entry point of any program
• Can have void main(){……}
• where it return type is void
• Or can be int main(){……… return 0;}
• where it returns an integer.
Steps in C compilation
CSEG1003 Programming for Problem Solving
Files in a C program
CSEG1003 Programming for Problem
Solving
Binary Format
a.out ./ <obj file>
Structure
of
C
Program
CSEG1003 Programming for Problem
Solving
CSEG1003 Programming for Problem Solving
Documentation
• Comments
• provide clarity to the C source code and allows others to better
understand
• helps in debugging the code.
• Two types
• Single Line //
• Multiline /* any text */
// This is my first Program
/* Written for Engineering students of Petroleum University for the batch
2018-1019 students */
#include <stdio.h>
void main()
{
printf(“Hello”);
}
CSEG1003 Programming for Problem
Solving
Preprocessing
• First stage of compilation
• Lines starting with a # character are interpreted by the
preprocessor as preprocessor commands.
• This language is used to reduce repetition in source code
• Print the result of the preprocessing stage, pass the -E
option to gcc:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
CSEG1003 Programming for Problem
Solving
CSEG1003 Programming for Problem
Solving
LECTURE #9
VARIABLES, CONSTANTS, DATATYPESAND
OPERATORS
Instructor
Dhiviya Rose J . Asst. Prof. Senior Scale
School of Computer Science and Engineering | UPES
CSEG1003 Programming for Problem
Solving
Knowledge Checks
#include <stdio.h>
void solveMeFirst( )
{
int num1,num2;
scanf("%d %d",&num1,&num2);
int sum;
sum = num1+num2;
printf("%d",sum);
}
void main()
{
________________Fill me
}
CSEG1003 Programming for Problem
Solving
Knowledge Check
• C Program Specification
• 3 user defined functions
• F1 -> prints “I am F1”
• F2 -> prints “I am F2”
• F3 -> prints “I am F3”
• Required Output
I am F2
I am F3
I am F1
I am F2
CSEG1003 Programming for Problem
Solving
Keywords
• C has a set of 32 reserved words often known as
keywords.
• All keywords are basically a sequence of characters
that have a fixed meaning.
• By convention all keywords must be written in
lowercase (small) letters.
• Example: for, while, do-while, auto break, case, char,
continue, do, double, else, enum, extern, float, goto,
if, int, long, register, return, short, signed, sizeof,
static, struct, switch, typedef, union, unsigned, void,
volatile
CSEG1003 Programming for Problem
Solving
Identifiers
• Identifiers are names given to program elements such as
variables, arrays and functions.
• Rules for forming identifier name
• it cannot include any special characters or punctuation marks (like #,
$, ^, ?, ., etc) except the underscore"_".
• There cannot be two successive underscores
• Keywords cannot be used as identifiers
• The names are case sensitive. So, example, “FIRST” is different from
“first” and “First”.
• It must begin with an alphabet or an underscore.
• It can be of any reasonable length. Though it should not contain more
than 31 characters.
• Example: roll_number, marks, name, SAP_ID, COURSE,
SEM
CSEG1003 Programming for Problem
Solving
Variables
• Meaningful name given to the data storage location in
computer memory.
• C language supports two basic kinds of variables.
• Numeric variables can be used to store either integer values or
floating point values.
• While an integer value is a whole numbers without a fraction part or
decimal point, a floating point number, can have a decimal point in
them.
• Numeric values may also be associated with modifiers like short, long,
signed and unsigned.
• By default, C automatically a numeric variable signed..
• Character variables can include any letter from the alphabet or from
the ASCII chart and numbers 0 – 9 that are put between single
quotes.
CSEG1003 Programming for Problem
Solving
Data & Variable
CSEG1003 Programming for Problem
Solving
Knowledge Check - Output????
CSEG1003 Programming for Problem
Solving
Knowledge Check - Output????
CSEG1003 Programming for Problem
Solving
DataType in C language
CSEG1003 Programming for Problem
Solving
Variable Declaration / Definition
• Declaring/Definition a Variable
• Declaration announces the data type of a variable and allocates
appropriate memory location.
• Each variable used must be declared.
• A form of a declaration statement is
data-type var1, var2,…;
Examples
int sum = 0;
char t1 = ‘a’;
float epsilon = 1.44;
CSEG1003 Programming for Problem
Solving
Possible ways of Variable Declaration
CSEG1003 Programming for Problem
Solving
CSEG1003 Programming for Problem Solving
Know your datatype char….
CSEG1003 Programming for Problem
Solving
Range of Datatypes in C
CSEG1003 Programming for Problem Solving
Global and Local Variables
• Global Variables
• These variables are
declared outside all
functions.
• Life time of a global
variable is the entire
execution period of the
program.
• Can be accessed by any
function defined below the
declaration, in a file.
• Local Variables
• These variables are
declared inside some
functions.
• Life time of a local
variable is the entire
execution period of the
function in which it is
defined.
• Cannot be accessed by any
other function.
• In general variables
declared inside a block are
accessible only in that
block.
CSEG1003 Programming for Problem Solving
Example
CSEG1003 Programming for Problem Solving
Constants
• Constants are identifiers whose value does not
change.
• To declare a constant, precede the normal variable
declaration with const keyword and assign it a value.
For example,
const float pi = 3.14;
• Another way to designate a constant is to use the pre-
processor command define.
#define PI 3.14159
CSEG1003 Programming for Problem
Solving
Operators in C
• C language supports a lot of operators to be used in
expressions. Includes
• Arithmetic operators
• Relational Operators
• Equality Operators
• Logical Operators
• Unary Operators
• Conditional Operators
• Bitwise Operators
• Assignment operators
• Comma Operator
• Sizeof Operator
CSEG1003 Programming for Problem
Solving
Arithmetic and Relational
CSEG1003 Programming for Problem
Solving
Logical and Unary
• Logical operators
• Logical AND (&&)
• Logical OR (||)
• Logical NOT (!).
• As in case of arithmetic expressions, the logical
expressions are evaluated from left to right.
• Unary operators
• act on single operands.
• Three unary operators
• unary minus(-)
• Increment(++)
• Decrement operators(--)
CSEG1003 Programming for Problem
Solving
Conditional Operator
• (?:) is just like an if .. else statement
• Syntax of the conditional operator is
exp1 ? exp2 : exp3
• Eg.
large = ( a > b) ? a : b
• Conditional operator is also known as ternary
operator as it is neither a unary nor a binary operator;
it takes three operands.
CSEG1003 Programming for Problem
Solving
Bitwise and Bitwise Shift Operators
• Bitwise operators perform operations at bit level.
• bitwise AND operator (&)
• bitwise OR operator (|)
• bitwise NOT (~)
• bitwise XOR operator (^)
• In bitwise shift operations, the digits are moved, or shifted,
to the left or right.
• Left Shift unsigned (<<)
int x = 11000101;
x << 2 = 00010100
• Right arithmetic shift (>>)
int x = 11000101;
x >> 2 = 00110001
CSEG1003 Programming for Problem
Solving
CSEG1003 Programming for Problem Solving
Assignment, Comma Operator
CSEG1003 Programming for Problem
Solving
equal sign (=) is the fundamental assignment operator, C also supports
other assignment operators that provide shorthand ways to represent
common variable assignments.
Comma separated operands when chained together are evaluated in left-
to-right sequence with the right-most value yielding the result of the
expression.
Sizeof operator
• sizeof is a unary operator used to calculate the sizes
of data types.
• sizeof(char) returns 1, that is the size of a character
data type.
int a = 10;
int result;
result = sizeof(a);
then result = 2,
CSEG1003 Programming for Problem
Solving
CSEG1003 Programming for Problem
Solving
LECTURE #10
INPUTAND OUTPUT STATEMENTS
Instructor
Dhiviya Rose J . Asst. Prof. Senior Scale
School of Computer Science and Engineering | UPES
CSEG1003 Programming for Problem
Solving
OUTPUT STATEMENT - printf()
• printf() function
• Output to Standard Output
• Prototype definition available in stdio.h
CSEG1003 Programming for Problem Solving
• Case 1: printing only text
Flowchart code Output
printf(“Hello”); Hello
• Case 2: printing only value of a variable
printf(“%d” , area); 120
• Case 3: printing text and a value
printf(“The value of area is %d”,area); The value of area is 120
Print Hello
Print area
OUTPUT STATEMENT - printf()
CSEG1003 Programming for Problem
Solving
INPUT STATEMENT – scanf()
• Case 1: getting 1 input value
scanf( “%d”, &r);
• Case 2: getting 2 input value
scanf( “%d %d ”, &l, &b);
• Case 3: getting 3 input value
scanf( “%d %d %d ”, &a, &b ,&c);
CSEG1003 Programming for Problem
Solving
• scanf() function
• Input from Standard Input
• Prototype definition available in stdio.h
• Format Specifier(%d)
• Address-of Operator (&)
• Provides the memory address of the input variable were the input value is to
be stored
CSEG1003 Programming for Problem Solving
CSEG1003 Programming for Problem Solving
CSEG1003 Programming for Problem Solving
DECISION STRUCTURES IN C
CSEG1003 Programming for Problem
Solving
List of Decision Making Structures in C
•If structure
•If Else structure
•Nested If structure
•Switch Structures
•Conditional Operator/
Terniary operator
CSEG1003 Programming for Problem
Solving
Converting a if block to C program
CSEG1003 Programming for Problem
Solving
if(condition)
{
//true statements
}
Check if entered number is positive
CSEG1003 Programming for Problem
Solving
Converting a if-else block to C program
CSEG1003 Programming for Problem
Solving
if(condition)
{
//true statements
}
else
{
//false statements
}
Example: If Else Statement
CSEG1003 Programming for Problem
Solving
Knowledge Checks – Implement – If… Else
CSEG1003 Programming for Problem
Solving
Nested IF
CSEG1003 Programming for Problem
Solving
if(condition 1)
{
//true statements
}
else if(condition 2)
{
//statements
}
else if(condition 3)
{
//statements
}
else
{
//statements
}
Example
CSEG1003 Programming for Problem
Solving
switch …case decision Implementation
CSEG1003 Programming for Problem
Solving
Switch Case
• Keywords used switch, case, default, break
• An value is passed as the input
• Switch(value)
• Evaluates with an value in each case
• Case 1:
• Case ‘A’:
• Default:
• Break is used to get exit from switch block
• Break;
CSEG1003 Programming for Problem
Solving
Convert Switch to C program
CSEG1003 Programming for Problem
Solving
Switch case Vs Nested IF
CSEG1003 Programming for Problem
Solving
Knowledge Check
CSEG1003 Programming for Problem
Solving
Knowledge Checks
CSEG1003 Programming for Problem
Solving
EXPERIMENT NO – 4
Control Statements in C Language
List of lab works:
1. Write a program to accept 3 numbers and find the greatest of them, using
if…….else statements.
2. Write a program to find the biggest of 3 numbers using conditional operator/ternary
operator?
3. Write a program to check whether the roots of a quadratic equation are real or
imaginary?
4. Program to find the average of students marks, if average<50 then result is ‘FAIL’
otherwise print the grade as pass /first class/distinction.
5. A book and stationary store decides to give its customers 10% discount on a
purchase greater than 10,000/-. The program should accept the quantity
purchased the price of the items and then calculate the amount payable. Further
based on the total amount, appropriate discount should be given and final payable
amount should be displayed.
6. Write a program to accept a number and display “Sunday/Monday/Tuesday…..”
Based on the number. (hint: if 1 is input then “Sunday”, if 2 is input then
“Monday”…..) using switch case.
7. Read the minutes from the keyboard and find out the no. of hours, mins , days ?
(Ex 1210 mins are displayed as 0 days,20 hrs,10 min)
CSEG1003 Programming for Problem
Solving
CSEG1003 Programming for Problem
Solving
LECTURE #13
LOOP STRUCTURES IN C
Instructor
Dhiviya Rose J . Asst. Prof. Senior Scale
School of Computer Science and Engineering | UPES
CSEG1003 Programming for Problem
Solving
Introduction - Loop Statements
• Helps in executing a statement in flowchart repeatedly
• Loop statements – block of statement executed more
than one time
• How many times the loop statements are executed is
managed by a counter variable
• Counter Variable Initialization
• Eg. C=0
• Counter Variable Increment/Decrement
• Eg. C++, c=c+1,c=c+3
• Counter Variable Condition Check
• Eg. C<3
CSEG1003 Programming for Problem
Solving
List of Loop Structures in C
• While
• Do….While
• For
CSEG1003 Programming for Problem
Solving
While
• Entry condition check loop
CSEG1003 Programming for Problem
Solving
CSEG1003 Programming for Problem
Solving
Example – While Program
CSEG1003 Programming for Problem
Solving
Do … While Loop
• Exit check Loop
• At least executes the
statement once
if the condition is false
CSEG1003 Programming for Problem
Solving
At least executes once - if condition fails
CSEG1003 Programming for Problem
Solving
CSEG1003 Programming for Problem
Solving
For
CSEG1003 Programming for Problem
Solving
Example
CSEG1003 Programming for Problem
Solving
CSEG1003 Programming for Problem
Solving
Continue statement
CSEG1003 Programming for Problem
Solving
Knowledge Check
CSEG1003 Programming for Problem
Solving
EXPERIMENT NO – 5
Loop Statements in C Language
List of lab works:
1. Write a program to print half pyramid using *.
2. Write a C program to print all natural numbers from 1-n
using while loop.
3. Write a C program to find the sum of all even numbers
between 1 to n using do-while loop.
4. Write a C program to print multiplication table of any
number using for loop.
CSEG1003 Programming for Problem
Solving
CSEG1003 Programming for Problem
Solving
KNOWLEDGE CHECKS
Input – Output Statements
CSEG1003 Programming for Problem
Solving
1. I = scanf("%d%d",&a,&b); value of I will be
2. I = sizeof(float); the value of I will be
3. i=printf("%d",20000); then i=?
4. i=printf("upes"); then i= ?
5. i=scanf("%d",&a); then i=?
CSEG1003 Programming for Problem
Solving
6. i=printf("%d",20000); then i=?
7. printf("%d",printf("%d",345)); the output of the
statement will be ?
8. printf(“%d”,’A’);
9. printf("%o",12); the output of the statement will be ?
10. printf("%d",2&2); the output of the statement will
be ?
11. printf("%.2f",78.34567);
CSEG1003 Programming for Problem
Solving
KNOWLEDGE CHECKS
Conditional and Loop Statements
CSEG1003 Programming for Problem
Solving
Question 1
#include <stdio.h>
int main()
{
int var=100;
{
int var=200;
printf("%d...",var);
}
printf("%d",var);
return 0;
}
Question 2
int main()
{
int n,i=0;
while(scanf("%d",&n)==1)
{
printf("ENDn");
break;
}
return 0;
}
Question 3
int main()
{
int n=10,i=0;
while(1)
{
printf("ENDn"); n++;
}
return 0;
}
Question 4
int a=2;
switch(a)
{
case 1:
printf(“one ”);
case 2:
printf(“two ”);
case 3:
printf(“three ”);
default:
printf(“other ”);
}
Question 5
void anyFun(int a)
{
if(a==0)
return;
printf("Value is not Zero.");
}
void main()
{
anyFun(0);
}
Question 6
#include <stdio.h>
void fun(int a,int b)
{
a+=10;
b+=10;
}
int main()
{
int val1=10;
int val2=10;
printf("nValue before function calling : %d, %d",val1,val2);
fun(val1,val2);
printf("nValue after function calling : %d, %d",val1,val2);
return 0;
}
Question 7
#include <stdio.h>
void fun1(void)
{
void fun2(void); /*another function declaration*/
printf("nI am fun1");
fun2();
}
int main()
{
fun1();
return 0;
}
void fun2(void)
{
printf("nI am fun2");
}
Question 8
for(i=1;i<=2;i++)
{
for(j=1;j<=10;j++)
{
printf("%d ",j);
}
printf("n");
}
CSEG1003 Programming for Problem
Solving

Programming for Problem Solving Unit 2

  • 1.
    CSEG1003 Programming forProblem Solving
  • 2.
    CSEG 1001 Computer Programming Instructor DhiviyaRose J . Asst. Prof. Senior Scale School of Computer Science and Engineering | UPES CSEG1003 Programming for Problem Solving
  • 3.
    Road Map • Generationsof Computers and Languages • Organization of Computers-Online Lecture • Number Systems Conversion • Logical Analysis and Thinking Introduction to Computers • Structure of C Program & Compilation and Linking Process • Variables and Datatypes • Managing Input and Output statements and Operators • Decision and Looping Statements C Programming Basics • Creation and Usages • 1D and 2 D arrys • String Functions • Matrix operations Arrays and Strings • Declaration and Definitions of Functions • Passing Arguments • Recursion • Pointers & Pointer Arithmetic Functions and Pointers • Need of Structure and Unions • Declaration and Definition • Storage classes • Preprocessor Directives Structures and Unions CSEG1003 Programming for Problem Solving
  • 4.
    Online Lecture • ProblemFormulation • Problem Solving • Introduction to C CSEG1003 Programming for Problem Solving
  • 5.
    LECTURE #8 STRUCTURE OFC PROGRAM ..COMPILATIONAND LINKING Instructor Dhiviya Rose J . Asst. Prof. Senior Scale School of Computer Science and Engineering | UPES CSEG1003 Programming for Problem Solving
  • 6.
    CSEG1003 Programming forProblem Solving
  • 7.
    CSEG1003 Programming forProblem Solving
  • 8.
    Structured Blocks ->Functions • Solves a large problem • divides the problem into smaller modules called functions • each have particular responsibility. • The program which solves the entire problem is a collection of such functions. • Types • Built-in Functions (Predefined) • E.g. printf(), scanf() • User-defined Functions CSEG1003 Programming for Problem Solving
  • 9.
    Example Program 1 #include<stdio.h> voidmain() { printf("Hello"); printf("this is my function"); } 9Instructor: Dhiviya Rose J , AP-Sr. Scale | CIT
  • 10.
    Example Program 2 #include<stdio.h>PREPROCESSOR DIRECTIVE void myline(); FUNCTION DECLARATION void main() { printf("Hello"); myline(); FUNCTION CALL } void myline() { printf("this is my function"); FUNCTION DEFINITION } 10Instructor: Dhiviya Rose J , AP-Sr. Scale | CIT
  • 11.
    Functions Example 3 CSEG1003Programming for Problem Solving OUTPUT ????? Function Call ???? Function definition??? Function Declaration????
  • 12.
    Main Function CSEG1003 Programmingfor Problem Solving • When system is executed it calls the main function • Entry point of any program • Can have void main(){……} • where it return type is void • Or can be int main(){……… return 0;} • where it returns an integer.
  • 13.
    Steps in Ccompilation CSEG1003 Programming for Problem Solving
  • 14.
    Files in aC program CSEG1003 Programming for Problem Solving Binary Format a.out ./ <obj file>
  • 15.
  • 16.
    CSEG1003 Programming forProblem Solving
  • 17.
    Documentation • Comments • provideclarity to the C source code and allows others to better understand • helps in debugging the code. • Two types • Single Line // • Multiline /* any text */ // This is my first Program /* Written for Engineering students of Petroleum University for the batch 2018-1019 students */ #include <stdio.h> void main() { printf(“Hello”); } CSEG1003 Programming for Problem Solving
  • 18.
    Preprocessing • First stageof compilation • Lines starting with a # character are interpreted by the preprocessor as preprocessor commands. • This language is used to reduce repetition in source code • Print the result of the preprocessing stage, pass the -E option to gcc: #include <stdio.h> #include <stdlib.h> #include <string.h> CSEG1003 Programming for Problem Solving
  • 19.
    CSEG1003 Programming forProblem Solving
  • 20.
    LECTURE #9 VARIABLES, CONSTANTS,DATATYPESAND OPERATORS Instructor Dhiviya Rose J . Asst. Prof. Senior Scale School of Computer Science and Engineering | UPES CSEG1003 Programming for Problem Solving
  • 21.
    Knowledge Checks #include <stdio.h> voidsolveMeFirst( ) { int num1,num2; scanf("%d %d",&num1,&num2); int sum; sum = num1+num2; printf("%d",sum); } void main() { ________________Fill me } CSEG1003 Programming for Problem Solving
  • 22.
    Knowledge Check • CProgram Specification • 3 user defined functions • F1 -> prints “I am F1” • F2 -> prints “I am F2” • F3 -> prints “I am F3” • Required Output I am F2 I am F3 I am F1 I am F2 CSEG1003 Programming for Problem Solving
  • 23.
    Keywords • C hasa set of 32 reserved words often known as keywords. • All keywords are basically a sequence of characters that have a fixed meaning. • By convention all keywords must be written in lowercase (small) letters. • Example: for, while, do-while, auto break, case, char, continue, do, double, else, enum, extern, float, goto, if, int, long, register, return, short, signed, sizeof, static, struct, switch, typedef, union, unsigned, void, volatile CSEG1003 Programming for Problem Solving
  • 24.
    Identifiers • Identifiers arenames given to program elements such as variables, arrays and functions. • Rules for forming identifier name • it cannot include any special characters or punctuation marks (like #, $, ^, ?, ., etc) except the underscore"_". • There cannot be two successive underscores • Keywords cannot be used as identifiers • The names are case sensitive. So, example, “FIRST” is different from “first” and “First”. • It must begin with an alphabet or an underscore. • It can be of any reasonable length. Though it should not contain more than 31 characters. • Example: roll_number, marks, name, SAP_ID, COURSE, SEM CSEG1003 Programming for Problem Solving
  • 25.
    Variables • Meaningful namegiven to the data storage location in computer memory. • C language supports two basic kinds of variables. • Numeric variables can be used to store either integer values or floating point values. • While an integer value is a whole numbers without a fraction part or decimal point, a floating point number, can have a decimal point in them. • Numeric values may also be associated with modifiers like short, long, signed and unsigned. • By default, C automatically a numeric variable signed.. • Character variables can include any letter from the alphabet or from the ASCII chart and numbers 0 – 9 that are put between single quotes. CSEG1003 Programming for Problem Solving
  • 26.
    Data & Variable CSEG1003Programming for Problem Solving
  • 27.
    Knowledge Check -Output???? CSEG1003 Programming for Problem Solving
  • 28.
    Knowledge Check -Output???? CSEG1003 Programming for Problem Solving
  • 29.
    DataType in Clanguage CSEG1003 Programming for Problem Solving
  • 30.
    Variable Declaration /Definition • Declaring/Definition a Variable • Declaration announces the data type of a variable and allocates appropriate memory location. • Each variable used must be declared. • A form of a declaration statement is data-type var1, var2,…; Examples int sum = 0; char t1 = ‘a’; float epsilon = 1.44; CSEG1003 Programming for Problem Solving
  • 31.
    Possible ways ofVariable Declaration CSEG1003 Programming for Problem Solving
  • 32.
    CSEG1003 Programming forProblem Solving
  • 33.
    Know your datatypechar…. CSEG1003 Programming for Problem Solving
  • 34.
    Range of Datatypesin C CSEG1003 Programming for Problem Solving
  • 35.
    Global and LocalVariables • Global Variables • These variables are declared outside all functions. • Life time of a global variable is the entire execution period of the program. • Can be accessed by any function defined below the declaration, in a file. • Local Variables • These variables are declared inside some functions. • Life time of a local variable is the entire execution period of the function in which it is defined. • Cannot be accessed by any other function. • In general variables declared inside a block are accessible only in that block. CSEG1003 Programming for Problem Solving
  • 36.
  • 37.
    Constants • Constants areidentifiers whose value does not change. • To declare a constant, precede the normal variable declaration with const keyword and assign it a value. For example, const float pi = 3.14; • Another way to designate a constant is to use the pre- processor command define. #define PI 3.14159 CSEG1003 Programming for Problem Solving
  • 38.
    Operators in C •C language supports a lot of operators to be used in expressions. Includes • Arithmetic operators • Relational Operators • Equality Operators • Logical Operators • Unary Operators • Conditional Operators • Bitwise Operators • Assignment operators • Comma Operator • Sizeof Operator CSEG1003 Programming for Problem Solving
  • 39.
    Arithmetic and Relational CSEG1003Programming for Problem Solving
  • 40.
    Logical and Unary •Logical operators • Logical AND (&&) • Logical OR (||) • Logical NOT (!). • As in case of arithmetic expressions, the logical expressions are evaluated from left to right. • Unary operators • act on single operands. • Three unary operators • unary minus(-) • Increment(++) • Decrement operators(--) CSEG1003 Programming for Problem Solving
  • 41.
    Conditional Operator • (?:)is just like an if .. else statement • Syntax of the conditional operator is exp1 ? exp2 : exp3 • Eg. large = ( a > b) ? a : b • Conditional operator is also known as ternary operator as it is neither a unary nor a binary operator; it takes three operands. CSEG1003 Programming for Problem Solving
  • 42.
    Bitwise and BitwiseShift Operators • Bitwise operators perform operations at bit level. • bitwise AND operator (&) • bitwise OR operator (|) • bitwise NOT (~) • bitwise XOR operator (^) • In bitwise shift operations, the digits are moved, or shifted, to the left or right. • Left Shift unsigned (<<) int x = 11000101; x << 2 = 00010100 • Right arithmetic shift (>>) int x = 11000101; x >> 2 = 00110001 CSEG1003 Programming for Problem Solving
  • 43.
    CSEG1003 Programming forProblem Solving
  • 44.
    Assignment, Comma Operator CSEG1003Programming for Problem Solving equal sign (=) is the fundamental assignment operator, C also supports other assignment operators that provide shorthand ways to represent common variable assignments. Comma separated operands when chained together are evaluated in left- to-right sequence with the right-most value yielding the result of the expression.
  • 45.
    Sizeof operator • sizeofis a unary operator used to calculate the sizes of data types. • sizeof(char) returns 1, that is the size of a character data type. int a = 10; int result; result = sizeof(a); then result = 2, CSEG1003 Programming for Problem Solving
  • 46.
    CSEG1003 Programming forProblem Solving
  • 47.
    LECTURE #10 INPUTAND OUTPUTSTATEMENTS Instructor Dhiviya Rose J . Asst. Prof. Senior Scale School of Computer Science and Engineering | UPES CSEG1003 Programming for Problem Solving
  • 48.
    OUTPUT STATEMENT -printf() • printf() function • Output to Standard Output • Prototype definition available in stdio.h CSEG1003 Programming for Problem Solving
  • 49.
    • Case 1:printing only text Flowchart code Output printf(“Hello”); Hello • Case 2: printing only value of a variable printf(“%d” , area); 120 • Case 3: printing text and a value printf(“The value of area is %d”,area); The value of area is 120 Print Hello Print area OUTPUT STATEMENT - printf() CSEG1003 Programming for Problem Solving
  • 50.
    INPUT STATEMENT –scanf() • Case 1: getting 1 input value scanf( “%d”, &r); • Case 2: getting 2 input value scanf( “%d %d ”, &l, &b); • Case 3: getting 3 input value scanf( “%d %d %d ”, &a, &b ,&c); CSEG1003 Programming for Problem Solving
  • 51.
    • scanf() function •Input from Standard Input • Prototype definition available in stdio.h • Format Specifier(%d) • Address-of Operator (&) • Provides the memory address of the input variable were the input value is to be stored CSEG1003 Programming for Problem Solving
  • 52.
    CSEG1003 Programming forProblem Solving
  • 53.
    CSEG1003 Programming forProblem Solving
  • 54.
    DECISION STRUCTURES INC CSEG1003 Programming for Problem Solving
  • 55.
    List of DecisionMaking Structures in C •If structure •If Else structure •Nested If structure •Switch Structures •Conditional Operator/ Terniary operator CSEG1003 Programming for Problem Solving
  • 56.
    Converting a ifblock to C program CSEG1003 Programming for Problem Solving if(condition) { //true statements }
  • 57.
    Check if enterednumber is positive CSEG1003 Programming for Problem Solving
  • 58.
    Converting a if-elseblock to C program CSEG1003 Programming for Problem Solving if(condition) { //true statements } else { //false statements }
  • 59.
    Example: If ElseStatement CSEG1003 Programming for Problem Solving
  • 60.
    Knowledge Checks –Implement – If… Else CSEG1003 Programming for Problem Solving
  • 61.
    Nested IF CSEG1003 Programmingfor Problem Solving if(condition 1) { //true statements } else if(condition 2) { //statements } else if(condition 3) { //statements } else { //statements }
  • 62.
  • 63.
    switch …case decisionImplementation CSEG1003 Programming for Problem Solving
  • 64.
    Switch Case • Keywordsused switch, case, default, break • An value is passed as the input • Switch(value) • Evaluates with an value in each case • Case 1: • Case ‘A’: • Default: • Break is used to get exit from switch block • Break; CSEG1003 Programming for Problem Solving
  • 65.
    Convert Switch toC program CSEG1003 Programming for Problem Solving
  • 66.
    Switch case VsNested IF CSEG1003 Programming for Problem Solving
  • 67.
  • 68.
  • 69.
    EXPERIMENT NO –4 Control Statements in C Language List of lab works: 1. Write a program to accept 3 numbers and find the greatest of them, using if…….else statements. 2. Write a program to find the biggest of 3 numbers using conditional operator/ternary operator? 3. Write a program to check whether the roots of a quadratic equation are real or imaginary? 4. Program to find the average of students marks, if average<50 then result is ‘FAIL’ otherwise print the grade as pass /first class/distinction. 5. A book and stationary store decides to give its customers 10% discount on a purchase greater than 10,000/-. The program should accept the quantity purchased the price of the items and then calculate the amount payable. Further based on the total amount, appropriate discount should be given and final payable amount should be displayed. 6. Write a program to accept a number and display “Sunday/Monday/Tuesday…..” Based on the number. (hint: if 1 is input then “Sunday”, if 2 is input then “Monday”…..) using switch case. 7. Read the minutes from the keyboard and find out the no. of hours, mins , days ? (Ex 1210 mins are displayed as 0 days,20 hrs,10 min) CSEG1003 Programming for Problem Solving
  • 70.
    CSEG1003 Programming forProblem Solving
  • 71.
    LECTURE #13 LOOP STRUCTURESIN C Instructor Dhiviya Rose J . Asst. Prof. Senior Scale School of Computer Science and Engineering | UPES CSEG1003 Programming for Problem Solving
  • 72.
    Introduction - LoopStatements • Helps in executing a statement in flowchart repeatedly • Loop statements – block of statement executed more than one time • How many times the loop statements are executed is managed by a counter variable • Counter Variable Initialization • Eg. C=0 • Counter Variable Increment/Decrement • Eg. C++, c=c+1,c=c+3 • Counter Variable Condition Check • Eg. C<3 CSEG1003 Programming for Problem Solving
  • 73.
    List of LoopStructures in C • While • Do….While • For CSEG1003 Programming for Problem Solving
  • 74.
    While • Entry conditioncheck loop CSEG1003 Programming for Problem Solving
  • 75.
    CSEG1003 Programming forProblem Solving
  • 76.
    Example – WhileProgram CSEG1003 Programming for Problem Solving
  • 77.
    Do … WhileLoop • Exit check Loop • At least executes the statement once if the condition is false CSEG1003 Programming for Problem Solving
  • 78.
    At least executesonce - if condition fails CSEG1003 Programming for Problem Solving
  • 79.
    CSEG1003 Programming forProblem Solving
  • 80.
  • 81.
  • 82.
    CSEG1003 Programming forProblem Solving
  • 83.
  • 84.
  • 85.
    EXPERIMENT NO –5 Loop Statements in C Language List of lab works: 1. Write a program to print half pyramid using *. 2. Write a C program to print all natural numbers from 1-n using while loop. 3. Write a C program to find the sum of all even numbers between 1 to n using do-while loop. 4. Write a C program to print multiplication table of any number using for loop. CSEG1003 Programming for Problem Solving
  • 86.
    CSEG1003 Programming forProblem Solving
  • 87.
    KNOWLEDGE CHECKS Input –Output Statements CSEG1003 Programming for Problem Solving
  • 88.
    1. I =scanf("%d%d",&a,&b); value of I will be 2. I = sizeof(float); the value of I will be 3. i=printf("%d",20000); then i=? 4. i=printf("upes"); then i= ? 5. i=scanf("%d",&a); then i=? CSEG1003 Programming for Problem Solving
  • 89.
    6. i=printf("%d",20000); theni=? 7. printf("%d",printf("%d",345)); the output of the statement will be ? 8. printf(“%d”,’A’); 9. printf("%o",12); the output of the statement will be ? 10. printf("%d",2&2); the output of the statement will be ? 11. printf("%.2f",78.34567); CSEG1003 Programming for Problem Solving
  • 90.
    KNOWLEDGE CHECKS Conditional andLoop Statements CSEG1003 Programming for Problem Solving
  • 91.
    Question 1 #include <stdio.h> intmain() { int var=100; { int var=200; printf("%d...",var); } printf("%d",var); return 0; }
  • 92.
    Question 2 int main() { intn,i=0; while(scanf("%d",&n)==1) { printf("ENDn"); break; } return 0; }
  • 93.
    Question 3 int main() { intn=10,i=0; while(1) { printf("ENDn"); n++; } return 0; }
  • 94.
    Question 4 int a=2; switch(a) { case1: printf(“one ”); case 2: printf(“two ”); case 3: printf(“three ”); default: printf(“other ”); }
  • 95.
    Question 5 void anyFun(inta) { if(a==0) return; printf("Value is not Zero."); } void main() { anyFun(0); }
  • 96.
    Question 6 #include <stdio.h> voidfun(int a,int b) { a+=10; b+=10; } int main() { int val1=10; int val2=10; printf("nValue before function calling : %d, %d",val1,val2); fun(val1,val2); printf("nValue after function calling : %d, %d",val1,val2); return 0; }
  • 97.
    Question 7 #include <stdio.h> voidfun1(void) { void fun2(void); /*another function declaration*/ printf("nI am fun1"); fun2(); } int main() { fun1(); return 0; } void fun2(void) { printf("nI am fun2"); }
  • 98.
  • 99.
    CSEG1003 Programming forProblem Solving