0% found this document useful (0 votes)
8 views31 pages

Module-3 POP Study Material

The document covers various programming concepts in C, including the dangling else problem, expression evaluation, and basic C programs for patterns, Fibonacci series, counting odd/even numbers, factorial calculation, and binary search. It also explains arrays, their properties, advantages, disadvantages, and how to declare, initialize, and access one-dimensional and two-dimensional arrays. Additionally, it discusses the scope of variables in C programming.

Uploaded by

Ebbali Pavithra
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
8 views31 pages

Module-3 POP Study Material

The document covers various programming concepts in C, including the dangling else problem, expression evaluation, and basic C programs for patterns, Fibonacci series, counting odd/even numbers, factorial calculation, and binary search. It also explains arrays, their properties, advantages, disadvantages, and how to declare, initialize, and access one-dimensional and two-dimensional arrays. Additionally, it discusses the scope of variables in C programming.

Uploaded by

Ebbali Pavithra
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 31

MODULE-3: FUNCTIONS AND ARRAYS

Question with answers:


1. With the help of an example. Explain the dangling else problem.
It explains the association of single else statement in a nested if statement.
In nested if statements, when a single “else clause” occurs, the situation happens to be dangling else!
One danger of not writing braces is the so-called dangling else problem. The dangling else problem
results when a programmer believes (or forgets) the code indentation and loses track of the if statement
that goes with the else statement. Unless braces are used to designate the code blocks, the else statement
will be associated with the last if statement used.
Example:

if (condition)
if (condition)
if (condition)
else
printf("dangling else!\n");

In such situations, else clause belongs to the closest if statement which is incomplete that is the
innermost if statement!
we can make else clause belong to desired if statement by enclosing all if statements in block outer to
which if statement to associate the else clause.
Example:

if (condition) {
if (condition)
if (condition)
} else
printf("else associates with the outermost if statement!\n");

2. Evaluate the following expressions (with steps)


i) a = x*2+y/5-z*y
ii) b = ++x*(y-3)/2-z++*y; where x=3, y=5 and z=7
i) a = x*2+y/5-z*y
a=3*2+5/5-7*5
a=6+5/5-7*5
a=6+1-7*5
a=6+1-35
a=7-35
a=-28
3. Develop a C program to print the following pattern.
A
AB
ABC
ABCD
#include<stdio.h>
int main()
{
char i,j;
for(i=65;i<=70;i++)
{
printf(“\n”);
for(j=65;j<=i;j++)
printf(“%c”,j);
}
return 0;
}

4. Develop a C program to print Fibonacci series using loops and functions.


Fibonacci Series program using loops:
#include<stdio.h>
int main()
{
int n1=0,n2=1,n3,i,number;
printf("Enter the number of elements:");
scanf("%d",&number);
printf("%d %d",n1,n2);
for(i=2;i<number;++i)
{
n3=n1+n2;
printf(" %d",n3);
n1=n2;
n2=n3;
}
return 0;
}
Fibonacci Series program using functions:
#include<stdio.h>
void fibonacciSeries(int range)
{
int a=0, b=1, c;
while (a<=range)
{
printf("%d\t", a);
c = a+b;
a = b;
b = c;
}
}
int main()
{
int range;
printf("Enter range: ");
scanf("%d", &range);
printf("The fibonacci series is: \n");
fibonacciSeries(range);
return 0;
}

5. Develop a C program to count the number of odd and even numbers using for loop.

#include<stdio.h>
int main()
{
int i,n,even=0,odd=0;
printf("\nEnter the Ending value:");
scanf("%d",&n);
printf("\nEven numbers:");
for(i=0;i<=n;i++)
{
if(i%2==0)
{
printf("\n%d",i);
even++;
}
}
printf("\nOdd numbers:");
for(i=1;i<=n;i++)
{
if(i%2==1)
{
printf("\n%d",i);
odd++;
}
}
printf("\nTotal even numbers:%d",even);
printf("\nTotal odd numbers:%d",odd);
return 0;
}
6. Develop a C program to find factorial of given number with and without recursion

Factorial of given number with recursion:


#include<stdio.h>
int fact(int);
int main()
{
int x,n;
printf(" Enter the Number to Find Factorial :");
scanf("%d",&n);
x=fact(n);
printf(" Factorial of %d is %d",n,x);
return 0;
}
int fact(int n)
{
if(n==0)
return(1);
return(n*fact(n-1));
}

Factorial of given number without recursion:


#include <stdio.h>
#include <stdlib.h>
int main()
{
int n, i;
int factorial = 1;

printf("Enter a number to find factorial: ");


scanf("%d",&n);

if (n < 0)
printf("Error! Please enter any positive integer number");
else
{
for(i=1; i<=n; ++i)
{
factorial *= i;
}
printf("Factorial of Number %d = %d", n, factorial);
}
getch();
}

7. Develop C program to perform binary search.


#include <stdio.h>
int main()
{
int i, low, high, mid, n, key, array[100];
printf("Enter number of elementsn");
scanf("%d",&n);
printf("Enter %d integersn", n);
for(i = 0; i < n; i++)
scanf("%d",&array[i]);
printf("Enter value to findn");
scanf("%d", &key);
low = 0;
high = n - 1;
mid = (low+high)/2;
while (low <= high) {
if(array[mid] < key)
low = mid + 1;
else if (array[mid] == key) {
printf("%d found at location %d.n", key, mid+1);
break;
}
else
high = mid - 1;
mid = (low + high)/2;
}
if(low > high)
printf("Not found! %d isn't present in the list.n", key);
return 0;
}

ARRAYS:
An array is defined as the collection of similar type of data items stored at contiguous memory
locations.
Arrays are used to store multiple values in a single variable, instead of declaring separate
variables for each value.
Arrays are the derived data type in C programming language which can store the primitive type
of data such as int, char, double, float, etc.
It also has the capability to store the collection of derived data types, such as pointers, structure,
etc. The array is the simplest data structure where each data element can be randomly accessed
by using its index number.

C array is beneficial if you have to store similar elements. For example, if we want to store the
marks of a student in 6 subjects, then we don't need to define different variables for the marks
in the different subject. Instead of that, we can define an array which can store the marks in
each subject at the contiguous memory locations.

By using the array, we can access the elements easily. Only a few lines of code are required to
access the elements of the array.
Properties of Array:
The array contains the following properties.
• Each element of an array is of same data type and carries the same size, i.e., int = 4
bytes.
• Elements of the array are stored at contiguous memory locations where the first element
is stored at the smallest memory location.
• Elements of the array can be randomly accessed since we can calculate the address of
each element of the array with the given base address and the size of the data element.
Advantage of C Array
1) Code Optimization: Less code to the access the data.
2) Ease of traversing: By using the for loop, we can retrieve the elements of an array easily.
3) Ease of sorting: To sort the elements of the array, we need a few lines of code only.
4) Random Access: We can access any element randomly using the array.

Disadvantage of C Array
1) Fixed Size: Whatever size, we define at the time of declaration of the array, we can't
exceed the limit. So, it doesn't grow the size dynamically like LinkedList which we will
learn later.
Declaration of C Array
We can declare an array in the C language in the following way.

data_type array_name[array_size];
Example: int marks[5];

Initialization of C Array:
The simplest way to initialize an array is by using the index of each element.
We can initialize each element of the array by using the index.
Consider the following example.

marks[0]=80;//initialization of array
marks[1]=60;
marks[2]=70;
marks[3]=85;
marks[4]=75;
#include<stdio.h>
int main(){
int i=0;
int marks[5];//declaration of array
marks[0]=80;//initialization of array
marks[1]=60;
marks[2]=70;
marks[3]=85;
marks[4]=75;
//traversal of array
for(i=0;i<5;i++){
printf("%d \n",marks[i]);
}//end of for loop
return 0;
}

Access the Elements of an Array:


To access an array element, refer to its index number.
Array indexes start with 0:
[0] is the first element. [1] is the second element, etc.
This statement accesses the value of the first element [0] in myNumbers:

int myNumbers[] = {25, 50, 75, 100};


printf("%d", myNumbers[0]);
// Outputs 25
Change an Array Element
To change the value of a specific element, refer to the index number:
Example
int myNumbers[] = {25, 50, 75, 100};
myNumbers[0] = 33;

printf("%d", myNumbers[0]);
// Now outputs 33 instead of 25

C Array: Declaration with Initialization:


We can initialize the C array at the time of declaration. Let's see the code.

int marks[5]={20,30,40,50,60};
In such case, there is no requirement to define the size. So it may also be written as
the following code.
int marks[ ]={20,30,40,50,60};
Let's see the C program to declare and initialize the array in C.
#include<stdio.h>
int main(){
int i=0;
int marks[5]={20,30,40,50,60};//declaration and initialization of array
//traversal of array
for(i=0;i<5;i++){
printf("%d \n",marks[i]);
}
return 0;
}

Multidimensional Arrays:

If we want to store data as a tabular form, like a table with rows and columns, we need
to get familiar with multidimensional arrays.

A multidimensional array is basically an array of arrays.

Arrays can have any number of dimensions. Here, we will introduce two-dimensional
arrays (2D).

Two-Dimensional Arrays:

A 2D array is also known as a matrix (a table of rows and columns).

To create a 2D array of integers, take a look at the following example:

int matrix[2][3] = { {1, 4, 2}, {3, 6, 8} };


The first dimension represents the number of rows [2], while the second dimension
represents the number of columns [3]. The values are placed in row-order, and can be
visualized like this:

Access the Elements of a 2D Array:

To access an element of a two-dimensional array, we must specify the index number


of both the row and column.

This statement accesses the value of the element in the first row (0) and third column
(2) of the matrix array.

Example

int matrix[2][3] = { {1, 4, 2}, {3, 6, 8} };

printf("%d", matrix[0][2]); // Outputs 2

Change Elements in a 2D Array:


To change the value of an element, refer to the index number of the element in each of
the dimensions:
The following example will change the value of the element in the first row (0) and first
column (0):
Example:
int matrix[2][3] = { {1, 4, 2}, {3, 6, 8} };
matrix[0][0] = 9;
printf("%d", matrix[0][0]); // Now outputs 9 instead of 1

SCOPE OF VARIABLES

You might also like