Unit 2
Unit 2
UNIT II
Conditional Control -Statements :Simple if, if...else -
Conditional Statements : else if and nested if - Conditional
Statements : Switch case - Un-conditional Control Statements :
break, continue, goto - Looping Control Statements:for, while,
do..while - Looping Control Statements: nested for, nested
while - Introduction to Arrays -One Dimensional (1D) Array
Declaration and initialization - Accessing, Indexing and
operations with 1D Arrays
SR
M
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
UNIT II
Array Programs – 1D - Initializing and Accessing 2D Array,
Array Programs – 2D - Pointer and address-of operators
-Pointer Declaration and dereferencing, Void Pointers, Null
pointers, Pointer based Array manipulation
SR
MIENCE AND TECHNOLOGY,
INSTITUTE OF SC
CHENNAI.
2. 1 Control Statements
❑ Also called as Conditional Statement
❑ Decides order of execution based on conditions
❑ Helps repeat a group of statements
❑ Modifies control flow of program
❑ Decision Making
❑ Branching
SR
MF SCIENCE AND
INSTITUTE O
TECHNOLOGY,
CHENNAI.
#include<stdio.h>
int main( )
{
int number;
printf(“Enter the Number: ”);
scanf(“%d, &number);
if(number%2==0)
{
printf(“The Number is Even”);
}
else
{
printf(“The Number is Odd”);
}
return 0;
Output 1
Enter the Number : 10341
The number is Odd
Output 2
Enter the Number : 10342
The number is Even
SR
MF SCIENCE AND
INSTITUTE O
TECHNOLOGY,
CHENNAI.
#include <stdio.h>
void main( )
char username;
int password;
printf("Username:");
scanf("%c",&username);
printf("Password:");
scanf("%d",&password);
if(username=='a')
{
if(password==12345)
{
printf("Login successful");
}
else
{
printf("Password is incorrect, Try
again.");
}
}
else
{
printf("Username is incorrect, Try again.");
}
return 0;
}
Output 1 Username: a
Password:
12345 Login Successful
Output 2 Username: a
Password:
54321
Output 3 Username: b
Password:
Username is incorrect
SR
MCE AND TECHNOLOGY,
INSTITUTE OF SCIEN
CHENNAI.
switch(variable or expression)
{
case constant 1:
statements;
break;
….
case constant N;
statements;
break;
default:
statements;
}
/* Program for Switch Case*/
{
int a, b, choice;
printf(“\nEnter Two Numbers:”);
scanf(“%d%d”, &a,&b);
printf(“\n Enter 1 for Addition”);
printf(“\n Enter 2 for Subtraction”);
CHENNAI.
#include<stdio.h>
int main( )
{
int square, i, n, fact = 1,choice;
printf(“\n Enter Any Number: ”);
scanf(“%d”, &n);
printf(“ 1. Square \n”);
printf(“ 2. Factorial\n”);
printf(“ 3. Find Odd or Even \n”);
printf(“ 4. Exit \n”);
printf(“ Enter your Choice”);
scanf("%d", &choice);
switch (choice)
{
case 1:
square = n * n;
printf(“TheSquare of the Given number is
%d\n”,square);
break;
case 2:
for(i=1;i<=n;i++)
{
fact = fact * i;
}
2. 2 Looping Statements
❑ Loop – A segment of the program that is executed repeatedly
until a condition is satisfied
#include<stdio.h>
int main( )
{
int a, b, c, sum;
printf(“\n Enter the Three Numbers: ”);
scanf(“%d%d%d”, &a,&b,&c);
sum = a+b+c;
printf(“The sum of 3 Numbers is %d”,
sum); return 0;
}
Output
Enter the Three Numbers: 10 20 30
#include<stdio.h>
int main( )
{
int i=1,n, sum=0;
printf(“\n Enter the value for n: ”);
scanf(“%d”, &n);
while (i<=n)
{
sum = sum + i;
i++;
}
printf(“The sum of n Numbers is: %d”,
sum); return 0;
}
Output
Enter the value for n: 5
The sum of n Numbers is: 15
SR
MF SCIENCE AND
INSTITUTE O
TECHNOLOGY,
CHENNAI.
variable; do
{
Statements;
increment / Decrement loop
counter variable;
}
while (condition)
SR
MF SCIENCE AND
INSTITUTE O
TECHNOLOGY,
CHENNAI.
#include<stdio.h>
int main( )
{
int i, n, sum=0;
printf(“\n Enter the value for n: ”);
scanf(“%d”, &n);
for (i =1; i<=n; i++)
{
sum = sum + i;
}
printf(“The sum of n Numbers is: %d”, sum);
return 0;
}
Output
Enter the value for n: 5
The sum of n Numbers is: 15
SR
M
INSTITUTE OF SC IENCE AND TECHNOLOGY,
CHENNAI.
{
for(j=’A’;j<=row;j++)
{
printf(“%c”, j);
}
printf(“\n);
}
return 0;
}
SR
M
INSTITUTE OF SC IENCE AND TECHNOLOGY,
CHENNAI.
A
AB
ABC
ABCD
SR
M
INSTITUTE OF SC IENCE AND TECHNOLOGY,
CHENNAI.
{
for(j=1;j<=5;j++)
{
printf(“%d”,j);
}
printf(“\n”);
}
return 0;
}
SR
M
INSTITUTE OF SC IENCE AND TECHNOLOGY,
CHENNAI.
12345
12345
12345
12345
12345
SR
M
INSTITUTE OF SC IENCE AND TECHNOLOGY,
CHENNAI.
2. 3 Arrays
❑ Definition
An array is definedas finite ordered collection of
homogenous data, stored in contiguous memory locations.
2. 3 Arrays Contd...
❑ Need for Arrays
❑ Used to represent a list of numbers / names
❑ Used to represent tabular data in 2, 3 or more dimensions
❑ Important Data Structure in any programming language
❑ Definition
❑ Collection of elements of similar data types
❑ Each element is located in separate memory locations
❑ Each Array element share a common name
SR
MCE AND TECHNOLOGY,
INSTITUTE OF SCIEN
CHENNAI.
2. 3 Arrays Contd...
❑ Characteristics of Arrays
❑ All elements in the arrays share a common name
❑ Elements distinguished by index number
❑Index (or) element number of an array plays vital role for calling
each element
2. 3 Arrays Contd...
❑ Storage space for array depends on its data type and size
Total bytes = sizeof (Data type) x Size of Array
❑ Example
int a [5];
Total bytes = sizeof (int) x 5 = 2 x 5 = 10 bytes
SR
MF SCIENCE AND
INSTITUTE O
TECHNOLOGY,
CHENNAI.
2. 3 Arrays Contd...
a) Array Declaration
❑ Syntax
Datatype arrayname [size/subscript];
❑ Data Type: int, float, double, char, structure, union
❑ Array Name: Name given to the Array variable
❑ Size / Subscript: Number of values an Array can hold
❑ Exampleisnt numbers[5]; float marks[50];
char name[20]; double a[i];
SR
MF SCIENCE AND
INSTITUTE O
TECHNOLOGY,
CHENNAI.
2. 3 Arrays Contd...
❑ Illustration
int a[n];
SR
MF SCIENCE AND
INSTITUTE O
TECHNOLOGY,
CHENNAI.
2. 3 Arrays Contd...
❑ Static Array: Array size (range) declared in the program
❑ Dynamic Array: Array size given during execution
2. 3 Arrays Contd...
b) Array Initialization
❑ Initialization: Assigning values to array elements
❑ Values specified in curly braces separated by
commas
❑ Examples
int a[ 5] = {1, 2, 3, 4, 5};
float b[3] = { 40.5, 59.0, 98.5};
char name[6] = ” SRMIST”;
❑ Array element index start from 0
SR
MCE AND TECHNOLOGY,
INSTITUTE OF SCIEN
CHENNAI.
2. 3 Arrays Contd...
❑ Array elements are called by array names followed by the
element numbers
2. 3 Arrays Contd...
c) Getting Input for Arrays
❑ Use for loops to get input in arrays
❑ Use for loops with regard to the Array’s dimension
❑ Input for One Dimensional Arrays – 1 for
loop for(i = 0; i < 5; i++)
{
scanf(“%d”, &a[i]);
}
SR
MF SCIENCE AND
INSTITUTE O
TECHNOLOGY,
CHENNAI.
2. 3 Arrays Contd...
❑ Input for Two Dimensional Arrays – 2 for loops
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
scanf(“%d”,&a[i][j]);
}
}
SR
MF SCIENCE AND
INSTITUTE O
TECHNOLOGY,
CHENNAI.
2. 3 Arrays Contd...
d) Printing Output in Arrays
❑ Use for loops to print array output
❑ Use for loops with regard to the Array’s dimension
❑ Printing One Dimensional Array Output – 1 for
loop for(i=0;i<5;i++)
{
printf(“%d”,a[i]);
}
SR
MF SCIENCE AND
INSTITUTE O
TECHNOLOGY,
CHENNAI.
2. 3 Arrays Contd...
❑ Printing Two Dimensional Array Output – 2 for loops
for(i = 0; i < 5; i++)
{
for(j=0; j < 5; j++)
{
printff(“%d”, a[i][j]);
}
}
/* Program 1 : Array Declaration & Initialization*/
#include<stdio.h>
int main( )
{
int i, arr[5];
arr[0] = 10;
arr[1] = 20;
arr[2] = 30;
arr[3] = 40;
arr[4] = 50;
for(i=0; i<5;i++)
{
printf(“%d\n”, arr[i]);
}
return 0;
Output
10
20
30
40
50
/* Program 2 : Array Declaration & Initialization*/
#include<stdio.h>
int main( )
{
int i, arr[5] = {10, 20, 30, 40, 50};
for(i=0; i<=4; i++)
{
printf(“%d\n”, arr[i]);
}
return 0;
}
Output
10
20
30
40
50
/* Program 3 : Array Declaration & Initialization*/
#include<stdio.h>
int main( )
{
int i, n, arr[5];
scanf(“%d”, &n);
printf(“Enter the Elements of Array\n”);
for(i=0; i<=4; i++)
{
scanf(“%d”, &a[i]);
}
printf(“The Elements of the Array are”\n”);
for(i=0; i<n; i++)
{
printf(“%d”, a[i]);
}
return 0;
}
Output
Enter the Elements of the
Array 10 20 30 40 50
The Elements of the Array
are 10 20 30 40 50
SR
MF SCIENCE AND
INSTITUTE O
TECHNOLOGY,
CHENNAI.
2. 3 Arrays Contd...
e) Classification of Arrays
i. One-Dimensional Array
ii. Two-Dimensional Array
iii. Multi-Dimensional Array
SR
MF SCIENCE AND
INSTITUTE O
TECHNOLOGY,
CHENNAI.
2. 3 Arrays Contd...
i. One Dimensional Array
❑ Data stored under a single variable using one subscript
❑ 1-D Array Declaration – Syntax
datatype arrayname [size/subscript];
❑ Example: int a [5];
❑ 1-D Array initialization – Syntax
datatype arrayname [size] = { list of values};
Example: int a [5] = { 10, 20, 30, 40, 50};
/* Program 1 : One Dimensional Array*/
a
#include<stdio.h> a [1400]
int main ( ) [0]
22
{ a
[1]
34
int a[10], n, i, sum; 12
a
clrscr( ); 64
[2]
printf(“Enter the Number of a
[3]
Elements\n”); scanf(“%d”, &n);
a
for(i = 0; i < n; i++) [4]
{ a
[5] i
scanf(“%d”, & a [i]);
5a 0
} [6]
sum = 0; a su
[7] m0
for(i = 0; i < n; i++)
a
/* Program 1 : One Dimensional Array*/
{ a
sum = sum + a[i]; a [1400]
[0]
} 22
a
printf(“The Sum is: %d”, sum); [1]
34
12
return 0; a
[2] 64
} a
[3]
Output a
Enter the Number of Elements [4]
5 a
40 22 34 12 64 [5] i
The Sum is 182 5a 4
[6]
a Sum
[7]
182
a
/* Program 2 : 1-D Array for
Sorting*/
#include<stdio.h>
int main( )
{
int i, j, temp, n, a[10];
printf(“Enter the Number of Elements:”);
scanf(“%d”, &n);
printf(“Enter the Elements to be Sorted\n”);
for(i=0; i<n; i++)
{
scanf(“%d\n”, &a[i]);
}
for(i=0; i<n; i++)
{
for(j=i+1; j<n; j++)
{
if(a[i] >a[j])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
printf(“The Sorted Elements are:
\n”); for(i=0; i<n; i++)
{
printf(“%d\n”, a[i]);
}
return 0;
}
Output
Enter the Number of Elements:5
Enter the Elements to be Sorted
25
12
45
68
7
The Sorted Elements are:
7
12
25
45
68
SR
MIENCE AND TECHNOLOGY,
INSTITUTE OF SC
CHENNAI.
int arr[4][3]={{1,2,3},{2,3,4},{3,4,5},{4,5,6}};
/* Program 1 : 2-D Array */
#include<stdio.h>
int main(){
int i=0,j=0;
int arr[4][3]={{1,2,3},{2,3,4},{3,4,5},{4,5,6}};
//traversing 2D array
for(i=0;i<4;i++){
for(j=0;j<3;j++){
}
Output
arr[0][0] = 1
arr[0][1] = 2
arr[0][2] = 3
arr[1][0] = 2
arr[1][1] = 3
arr[1][2] = 4
arr[2][0] = 3
arr[2][1] = 4
arr[2][2] = 5
arr[3][0] = 4
arr[3][1] = 5
arr[3][2] = 6
/* Program 2 : 2-D Array Storing elements in a matrix and printing it*/
#include <stdio.h>
void main ()
{
int arr[3][3],i,j;
for (i=0;i<3;i++)
{
for (j=0;j<3;j++)
{
printf("Enter a[%d][%d]: ",i,j);
scanf("%d",&arr[i][j]);
}
}
printf("\n printing the elements ....\n");
for(i=0;i<3;i++)
{
printf("\n");
for (j=0;j<3;j++)
{
printf("%d\t",arr[i][j]);
}
}
}
Output
Enter a[0][0]: 56
Enter a[0][1]: 10
Enter a[0][2]: 30
Enter a[1][0]: 34
Enter a[1][1]: 21
Enter a[1][2]: 34
Enter a[2][0]: 45
Enter a[2][1]: 56
Enter a[2][2]: 78
#include<stdio.h> Output:
int main() Enter elements: 1
{ 2
int data[5],i; 3
printf(“Enter elements: ”); 5
for(i=0;i<5;++i) 4
scanf(“%d”, &data[i]);
printf(“you entered: \n”); You entered: 1
for(i=0; i<5; i++) 2
printf(“%d”, *(data + i)); 3
return 0; 5
} 4
SR
MIENCE AND TECHNOLOGY,
INSTITUTE OF SC
CHENNAI.
Like normal variables, pointer variables must be declared before using them.
General syntax for declaring pointer variable is:
2. 6 - Dereferencing of Pointer
❑ The operator * used in front of the name of the pointer variable is known as
pointer or dereferencing or indirection operator. After valid referencing
of pointer variable, *pointer_variable gives the value of the variable pointed
by pointer variable and this is known as dereferencing of pointer.
2. 7 – Void Pointers
❑ A void pointer is a pointer that has no associated data type with it. A void
pointer can hold address of any type and can be typecasted to any type.
#include<stdio.h>
int main()
{
int a[2] = {1, 2};
void *ptr = &a;
ptr = ptr + sizeof(int);
printf("%d", *(int *)ptr);
return 0;
}
Output: 2
SR
MIENCE AND TECHNOLOGY,
INSTITUTE OF SC
CHENNAI.
2. 8 – Null Pointers
The null pointer is a constant with a value of zero defined in several
standard libraries.
#include<stdio.h>
return 0;
}
SR
MIENCE AND TECHNOLOGY,
INSTITUTE OF SC
CHENNAI.
2. 8 – Null Pointers
When the above code is compiled and executed, it produces the following
result:
int arr[5] = { 1, 2, 3, 4, 5 };
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
Output:
123
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
The above code creates a string and stores its address in the
pointer variable str. The pointer str now points to the first
character of the string "Hello".
char *str;
str = "hello";
The content of the string can be printed using printf() and
puts()
printf("%s", str);
puts(str);