Cpps Unit-3 Arrays Array:: Declaration: Syntax: Data Type Array - Name (Size of The Array)
Cpps Unit-3 Arrays Array:: Declaration: Syntax: Data Type Array - Name (Size of The Array)
Pramod TC
Assistant Professor
Dept. of CSE
CPPS SIT Tumkur
[email protected]
Unit-3 Arrays
Array:
An array is a collection of similar data items.
An array is a variable that can store multiple values.
An array is defined as collection of homogenous data (means data must be of similar data
type), stored in contiguous memory locations.
Types of arrays:
1. One dimensional array
2. Two dimensional array
Declaration:
Initialization:
Ex:
int mark[5] = {19, 10, 8, 17, 9};
double balance[5] = {1000.0, 2.0, 3.4, 7.0, 50.0};
Initialization without size: If size is not specified, complier will calculate the
size of the array based on the number of initial values
Array index
Array Index
always start
with zero (0) Values
Advantages of Array:
For any reason a user wishes to store multiple values of similar type then the Array can be
used and utilized efficiently.
Saves time and memory
Easier debugging
More compact in memory usage
Accessing an element is very easy by using the index number.
The search process can be applied to an array easily.
2D Array is used to represent matrices.
Arrays allocate memory in contiguous memory locations for all its elements. Hence
there is no chance of extra memory being allocated in case of arrays
Example:
In order to read and display 6 variables of same data type, we require six variables. In case of
1000 elements, we require 1000 variables, which take more time for declaring, initializing
and printing.
#include<Stdio.h>
int main()
{
int n1,n2,n3,n4,n5,n6;
printf(“enter the values of n1, n2, n3, n4, n5 and n6”);
scanf(“%d%d%d%d%d%d”, &n1, &n2, &n3, &n4, &n5, &n6);
printf(“Elements are n1=%d n2=%d n3=%d n4=%d n5=%d n6=%d”, n1, n2, n3, n4, n5, n6);
return 0;
}
However, with arrays we require only one array (one variable). It saves time , memory space
and execute faster.
#include<stdio.h>
int main()
{
int i;
int arr[6] = {10,20,30,40,50,60};
for (i=0;i<6;i++)
{
printf(“value of arr [%d] is %d \n”, i, arr[i]);
}
}
#include <stdio.h>
#include<stdlib.h>
int main()
{
int a[6]={10,20,30,40,50,60};
printf("%d %d %d",a[0], a[2], a[5]);
return 0;
}
Program to read elements from the key board and to display using array
#include<Stdio.h>
int main()
{
int a[10], i, n;
#include <stdio.h>
int main()
{
int a[10], i, n, sum=0;
#include<stdio.h>
int main()
{
int a[20], n, i, max=0;
printf("\n Enter the number of elements for 1-D array : ");
scanf("%d“, &n);
for(i=0; i <n; i++)
{
scanf("%d", &a[i]);
}
for(i=0;i<n;i++)
{
if(max<a[i])
max=a[i];
}
Searching
A method for finding an element within a list
Linear search (sequential search)
Binary search
Linear Search: It sequentially checks each element of the array until a match is found or
the whole array has been searched.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a[100],key,n,i,j;
for(i=0;i<n;i++)
{
if(key==a[i])
{
printf("\nElement %d found at %d
position\n",key, i+1);
exit(0);
}
}
Binary Search: The basic requirement is the elements of the array should have been sorted
alphabetically or numerically.
Mid= (low+high)/2
Compare x with the middle element. If x matches with middle element, we return the
mid index.
Else If x is greater than the mid element, then x can only lie in right half subarray
after the mid element. So we recur for right half.
int a[100],key,num,i,j,low,mid,high,temp;
low = 0;
high = num -1;
else
low = mid +1;
}
Sorting
Sorting is a technique of rearranging the elements in any particular order.
Sorting methods
Bubble sort
Selection sort
Bubble sort
Bubble sort is an algorithm that compares the adjacent elements and swaps their positions if
they are not in the intended order. The order can be ascending or descending.
for(i=0;i<n;i++)
{
for(j=0;j<n-i-1;j++)
{
Bubble sort
if(a[j] > a[j+1])
{ algorithm
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
Example: {4, 9, 5, 1, 0}
for(i=0;i<n;i++)
{
min=i;
for(j=i+1;j<n;j++)
{
if(a[j]<a[min]) Selection sort
min=j; algorithm
}
temp=a[i];
a[i]=a[min];
a[min]=temp;
}
printf("sorted array\n");
for(i=0;i<n;i++)
{
printf("%d\n",a[i]);
}
return 0;
}
Two Dimensional Arrays
Used when data is in tabular format (rows and columns)
The 2D array is organized as matrices which can be represented as the collection of
rows and columns.
Applications: matrix multiplication, addition, transpose , tic-tac-toe game etc.
Declaration:
Initialization:
Syntax: data type array_name [row size][col size]={ var1, var2, ……varn};
Ex:
int a[2][4] = {
{10, 20, 30, 40} ,
{45, 55, 65, 75} ,
};
00 01 02 03
1 2 3 4 Array element
10 11 12 13
5 6 7 8
20 21 22 22
9 10 11 12
Example: To read and display two dimensional array elements
#include<stdio.h>
int main ()
{
int a[3][2], i, j;
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{ Matrix addition
c[i][j]=a[i][j]+b[i][j];
}
}
#include<stdio.h>
int main()
{
int a[100][100], i,j,r1 ,c1;
printf("Enter the size of the matrix \n");
scanf("%d%d", &r1, &c1);
if(c1!=r2){
printf("Matrix mutiplication is not possible");
exit(0);
}
for(i=0;i<r1;i++)
{
for(j=0;j<c2j++)
{
c[i][j]=0;
for(k=0;k<c1;k++)
{
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
}
Trace of a matrix
Trace of a n x n square matrix is sum of diagonal elements.
#include<stdio.h>
int main()
{
int a[100][100], b[100][100], i,j,r1 ,c1, sum=0;
printf("Enter the size of the matrix \n");
scanf("%d%d", &r1, &c1);
if(r1!=c1)
{ Square matrix
printf(“not a square matrix”); or not
exit(0);
}
for(i=0;i<r1;i++)
{
sum=sum+a[i][i];
}
#include<stdio.h>
#include<math.h>
int main()
{
int a[100][100], b[100][100], i,j,r1 ,c1,
sum=0,norm;
printf("Enter the size of the matrix \n");
scanf("%d%d", &r1, &c1);
if(r1!=c1)
{
printf(“not a square matrix”);
exit(0);
}
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
sum=sum+a[i][j]*a[i][j];
}
}
norm=sqrt(sum);
#include<stdio.h>
#include<math.h>
int main()
{
int a[100][100], i,j,r1 ,c1, flag=0;
printf("Enter the size of the matrix \n");
scanf("%d%d", &r1, &c1);
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
if(i==j && a[i][j]!=1)
{
flag=1;
break;
}
else if(i!=j && a[i][j]!=0)
{
flag=1;
break;
}
}
If(flag==0)
printf(“identity matrix”);
else
printf(“not a identity matrix”);
}
return 0;
}
Symmetric matrix or not
#include<stdio.h>
void main()
{
int a[100][100], b[100][100], i,j,r1 ,c1;
printf("Enter the size of the matrix \n");
scanf("%d%d", &r1, &c1);
Or
Explain the initialization of an array at compile time and run time.
Or
How are arrays declared and initialized? Explain with an example.
3 As a programmer, when do you think of using an array? Give an example to justify 5
your answer. ***
Or
What is the necessity of one-dimensional array?
4 Define two-dimensional array. Explain memory layout of a two dimensional array. List out 5
the applications/use of 2-D array? ***
5 Explain the initialization of a two-dimensional array. Give the general syntax of declaring a 5
two dimensional array. *****
Or
How do you initialize a two dimensional array at compile time and at run time?
Or
Discuss the various methods to initialize 2-dimensional array
6 Write C program to read a matrix of size m x n and print its transpose. Also find the sum 8
of all the elements in the matrix. ****
Or
Write a C program to find the transpose of a matrix.
7 Define sorting. Develop a C program to sort ‘n’ array elements in ascending order using 6
bubble sort technique. *****
Or
Write a program in ‘C’ that uses a function to sort an array of integers
8 What is searching? Discuss any two searching techniques used in array searching.
9 Develop a C program to search for an element in an array using linear search technique. 7
Or ***
Develop a C program to search whether item x is present in a set of M items
10 Develop a C program to display the first ‘n’ terms of Fibonacci numbers using arrays. 5
11 Mention the advantage and disadvantage of binary search over linear search. Write a C 6
program to read ‘n’ integers in an ascending order and search a ‘key’ using binary search ****
technique.
12 Write a C program to multiply two matrices of order MXN and PXQ respectively 8
13 Write a C program to read n integers and sort them in descending order using bubble sort 6
technique and display the sorted array with suitable message. **
14 Develop a C program to read 2-D array and compute i) sum of odd numbers ii) sum of 6
even numbers iii) average of all numbers. **
15 Write a program that reads an array A of n-elements, generates two arrays B and C which 8
contain odd elements and even elements respectively. Program also finds the sum and 6
average of all the elements.
16 Write a C program to accept 60 students name and marks scored in C Programming 6
Course. Also, display the name and marks of the students who have scored more than 80. **
17 Write a C program to find redundant elements from a given 1 dimensional array. Count 6
the number of times the particular element occurred. **
18 Write a program to check whether the elements in an array are unique or not. 6
19 Write a program that reads a matrix of order m x n and then determines whether the 10
given **
matrix is a i) Square matrix ii) Symmetric matrix iii) Identity matrix
20 Write a C Program to find the norm of a matrix. The norm is defined as the square root of 6
the sum of squares of all elements in the matrix.
21 Develop a program that reads a matrix of order M x N and fills the matrix by following 6
i. 1’s in the principal diagonal.
ii. 0’s in the remaining positions.