KEMBAR78
array ppt of c programing language for exam | PPTX
ARRAYS
• An arrays is defined as collection of
homogenous (similar) data which is stored in
contiguous memory locations or(continuous
memory addresses) because arrays is a linear
data structure .
• Arrays can be single(1-D) or multi
dimensional(2-D).
1st
element last element
Address of an array
10 2 5 11 72
for int 1000 1002 1004 1006 1008
1st
element last element
10.5 2.68 58.7 11.6 72.9
for float 1000 1004 1008 10012 10016
1st
element last element
Calculating the length of an array
Length = upper_bound –lower_bound+1
Eg. 4-0+1= 5
Declaring Arrays
data-type variable-name[size];
Examples:-
char a[5]; /* char type value array */
float a[9]; /* float type value array */
int a[10]; /* int type value array */
a[0],a[1]…. Are known as index
The index of an array starts from 0 to size-1 means first
element of any array will be stored at a[0] address
and the last element will be at a[size - 1].
Initialization of Array
• After declaration it is necessary to initialize an
array. Otherwise, it will contain garbage value(
random value). An array can be initialized at
either compile time or at runtime. No validity
check for array index not available in c.
Compile time Initialization :- It means that
providing the value to an array in the code,
during creation of it.
Syntax:-
data-type arrayname[size] = { list of values };
• int a[4] = { 67, 87, 56, 77 };
• float a[5] = { 23.4, 6.8, 5.5 };
• int marks[4] = { 67, 87, 56, 77, 59 }; // Compile time error .
• If the values are more than the declared array size than
the compiler will give an error.
Example:-
1. #include<stdio.h>
2. void main()
3. {
4. int i;
5. int a[] = {1, 3, 4};
6. for(i = 0 ; i < 3 ; i++)
7. {
8. printf("%dn",a[i]);
9. }
10. }
• #include<stdio.h>
• int main(){
• int arr[5] = {1,-1,3};
• printf("Array elements are:");
• for(int i = 0; i < 5; i++)
• printf("%d ", arr[i]);
• return 0;
• }
Rest two place will be initialized with 0. if
int arr[5] ={} if this will leave blank , compiler will
give the error.
• #include<stdio.h>
• int main(){
• float arr[3] = {1.5,-
1.10,3.8};
• printf("Array elements
are:");
• for(int i = 0; i < 3; i++)
• printf("%f ", arr[i]);
• return 0;
• }
• #include<stdio.h>
• int main(){
• char arr[3] = {'a','b','c'};
• printf("Array elements
are:n");
• for(int i = 0; i < 3; i++)
• printf("%cn", arr[i]);
• return 0;
• }
Runtime Array initialization
• An array can also be initialized at runtime
using scanf() function. Means user will provide
the values. This approach is usually used for
initializing large arrays.
Example:-
1. scanf("%d", &a[1]); // means index 0 but
position first.
2. for(int i = 0; i < 10; i++)
scanf("%d", &a[i]);
3. for(int i = 0; i < 10; i+=2)
scanf("%d", &a[i]);
Accessing Array elements
Accessing an array elements or printing
Example:-
for(int i = 0; i < 10; i++)
printf("%d", a[i]);
To access and print elements at specified index
printf("%d", a[0]);
printf("%d", a[5]);
NOTE:- if we try to access elements, more than the size
of an array or less than 0,then it won’t show an error
but will produce the wrong output (garbage value).
WAP to calculate sum of elements of array.
• #include<stdio.h>
• int main(){
• int arr[5];
• printf("Enter array elements:"");
• for(int i = 0; i < 5; i++)
• scanf("%d", &arr[i]);
• printf("Array elements are:"");
• for(int i = 0; i < 5; i++)
• printf("%d ", arr[i]);
• int sum = 0;
• for(int i = 0; i < 5; i++)
• sum += arr[i];
• printf("Sum =%d", sum);
• }
WAP to copy date from one array to other
• #include <stdio.h>
• int main()
• {
• int a[5] = {1,2,2,4,5}, a2[5];
• for(int i = 0; i < 5; i++)
• a2[i] = a[i];
• for(int i = 0; i < 5; i++)
• printf("%d ", a2[i]);
• return 0;
• }
• #include<stdio.h>
• int main(){
• int a[10];
• printf("Array elements are:n");
• for(int i = 0; i < 10; i++)
• {
• if (i<4)
• a[i]= 1;
• else if(i>=4&&i<7)
• a[i]= 2;
• else
• a[i]= 0;
• printf("%dn", a[i]);
• }
• return 0;
• }
• Program to insert an elements at specific place in array.
• #include <stdio.h>
• int main()
• {
• int a[10],i,in,e;
• printf("enter elements in array n");
• for(i=0;i<5;i++)
• {
• scanf("%d",&a[i]);
• printf("%dt",a[i]);
• }
• printf("nenter elements and indexn");
• scanf("%dn%d",&e,&in);
• for(i=5;i>=in; i--)
• {
• a[i+1]=a[i];
• }
• a[in]=e;
• for(i=0;i<6;i++)
• {
• printf("%dt",a[i]);
• }
• return 0;
• }
Program to delete an elements from an array.
• #include <stdio.h>
• int main()
• {
• int a[5],i,in;
• printf("enter elements in array n");
• for(i=0;i<5;i++)
• scanf("%d",&a[i]);
• printf("enter index from where you want to delete elementsn");
• scanf("%d",&in);
• for(i=in;i<5;i++)
• a[i]=a[i+1];
• printf("after deletionn");
• for(i=0;i<4;i++)
• printf("%d",a[i]);
• return 0;
• }
Examples
• Program to reverse elements of an array.
#include <stdio.h>
int main()
{
int a[5],i;
printf("enter elements in array n");
for(i=0;i<5;i++)
scanf("%d",&a[i]);
printf("elements of arrayn");
for(i=4;i>=0;i--)
printf("%d",a[i]);
return 0;
}
Merging of two arrays
• #include <stdio.h>
• int main()
• {
• int i,in=0;
• int a[3]={2,3,4}, b[3]={5,6,7}, c[6];
• for(i=0;i<3;i++)
• {
• c[in]= a[i];
• in++;
• }
• for(i=0;i<3;i++)
• {
• c[in]= b[i];
• in++;
•
• }
• for(i=0;i<6;i++)
• {
• printf("%d",c[i]);
• }
• return 0;
• }
Find the largest & smallest element in an array
• #include <stdio.h>
• int main() {
• int n;
• int a[5];
• for (int i = 0; i < 5; i++)
• {
• scanf("%d", & a[i]);
• }
• for (int i = 0; i < 5; i++) {
• if (a[0] < a[i]) {
• a[0] = a[i];
• }
• }
• printf("Largest element = %d", a[0]);
• return 0;
• }
• #include <stdio.h>
• int main() {
• int n;
• int a[5];
• for (int i = 0; i < 5; i++)
• {
• scanf("%d", & a[i]);
• }
• for (int i = 0; i < 5; i++) {
• if (a[0] > a[i]) {
• a[0] = a[i];
• }
• }
• printf("smallest element = %d", a[0]);
• return 0;
• }
Two dimensional Arrays
• It is also known as multidimensional array. It
contains a row index and a column index. Both
the row's and column's index begins with 0.
• Just like a one-dimensional array, Compile
time and runtime initialization of two
dimensional array is possible.
Declaration:-
data-type array-name[row-size][column-size]
Example:-
int a[3][4];
Compile-time initialization
Examples:-
int a[2][2] = {1, 2, 3, 4}; // {{1, 2},{3, 4}}
Int a[2][3] = {1, 2, 3, 4}; // {{1, 2, 3},{4}}
int a[2][4] = {1, 2, 3, 4}; // {{1,2,3,4}}
First of all the values are stored in first row, and then if there is
any extra value, it goes to next row.
Example:-
int a[][3] = {
{0,0,0},
{1,1,1}
}; // no any values is assigned to row in array. It means that
initialization of any number of rows. But must specify number
of columns, else it will give a compile time error.
Runtime initialization
WAP to show the Runtime initialization
• #include<stdio.h>
• void main()
• {
• int arr[3][4];
• int i, j, ;
• printf("Enter array elements:n");
• for(i = 0; i < 3;i++)
• {
• for(j = 0; j < 4; j++)
• {
• scanf("%d", &arr[i][j]);
• }
• }
• for(i = 0; i < 3; i++)
• {
• for(j = 0; j < 4; j++)
• {
• printf("%d", arr[i][j]);
• }
• }
• }
We can also initialize 2D arrays like this
int a[2][4] = { {0, 1, 2, 3} , {4, 5, 6, 7} };
or
int a[2][4] = {0,1,2,3,4,5,6,7};
Both are equivalent
Int [2][4] ={0} ;// all initialized with zero
int [3][4]= {1,1,1,1,2,2,2,2}
firstly 1 1 1 1
2 2 2 2
This row will be initialized by 0
Calculate the sum of all the elements
• #include<stdio.h>
• int main()
• {
• int arr[2][3];
• int i, j,sum =0;
• printf("Enter array elements:n");
• for(i = 0; i < 2;i++)
• {
• for(j = 0; j < 3; j++)
• {
• scanf("%d", &arr[i][j]);
• }
• }
• for(i = 0; i < 2; i++)
• {
• for(j = 0; j < 3; j++)
• {
• printf("%dt", arr[i][j]);
• sum= sum+arr[i][j];
•
• }
• printf("n");
• }
• printf("n%d", sum);
• }
• #include<stdio.h>
• int main()
• {
• int arr[2][3];
• int i, j,sum =0;
• printf("Enter array elements:n");
• for(i = 0; i < 2;i++)
• {
• for(j = 0; j < 3; j++)
• {
• scanf("%d", &arr[i][j]);
• }
• }
• for(i = 0; i < 2; i++)
• {
• for(j = 0; j < 3; j++)
• {
• printf("%dt", arr[i][j]);
• sum= sum+arr[i][j];
•
• }
• printf("n");
• printf("n%dn", sum);
• }
•
//Printing two matrix
• #include<stdio.h>
• int main()
• {
• int a[2][3],b[2][3];
• int i, j;
• printf("Enter array elements:n");
• for(i = 0; i < 2;i++)
• {
• for(j = 0; j < 3; j++)
• {
• scanf("%d", &a[i][j]);
• }
• }
• for(i = 0; i < 2;i++)
• {
• for(j = 0; j < 3; j++)
• {
• scanf("%d", &b[i][j]);
• }
• }
• printf("1st matrixn");
• for(i = 0; i < 2; i++)
• {
• for(j = 0; j < 3; j++)
• {
• printf("%dt", a[i][j]);
• }
• printf("n");
• }
• printf("2nd matrixn");
• for(i = 0; i < 2; i++)
• {
• for(j = 0; j < 3; j++)
• {
• printf("%dt", b[i][j]);
• }
• printf("n");
• }
• }
•
Addition of two matrix
• #include<stdio.h>
• int main()
• {
• int a[2][3],b[2][3],c[2][3];
• int i, j;
• printf("Enter array elements:n");
• for(i = 0; i < 2;i++)
• {
• for(j = 0; j < 3; j++)
• {
• scanf("%d", &a[i][j]);
• }
• }
• for(i = 0; i < 2;i++)
• {
• for(j = 0; j < 3; j++)
• {
• scanf("%d", &b[i][j]);
• }
• }
• printf("1st matrixn");
• for(i = 0; i < 2; i++)
• {
• for(j = 0; j < 3; j++)
• {
• printf("%dt", a[i][j]);
• }
• printf("n");
• }
• printf("2nd matrixn");
• for(i = 0; i < 2; i++)
• {
• for(j = 0; j < 3; j++)
• {
• printf("%dt", b[i][j]);
• }
• printf("n");
• }
• printf("the addition of two matrix isn");
• for(i = 0; i < 2; i++)
• {
• for(j = 0; j < 3; j++)
• {
• c[i][j]= a[i][j]+b[i][j];
• printf("%dt", c[i][j]);
• }
• printf("n");
• }
•
• }
Linear search
A linear search, also known as a sequential search, is a
method of finding an element within a list. It checks
each element of the list sequentially until a match is
found or the whole list has been searched. It works on
both sorted and unsorted list of elements.
Algorithm:-
1. Begin with the leftmost element of array[] and one by
one compare searching element with each element.
2. If searching element matches with an element then
return the index.
3. If searching element does not match with any of the
elements then return -1.
#include <stdio.h>
int main()
{
int a[50], b, i, n;
printf("Enter number of elements in array:n");
scanf("%d", &n);
printf("Enter %d elements:n", n);
for (i = 0; i < n; i++)
scanf("%d", &a[i]);
printf("Enter a number to search:n");
scanf("%d", &b);
for (i = 0; i < n; i++)
{
if (a[i] == b)
{
printf("Element %d found at index %d.n", b, i);
break;
}
}
if (i == n)
printf("Element %d not found.n", b);
return 0;
}
Binary search
• A Binary Search is a sorting algorithm, that is
used to search an element in a sorted array.
• A binary search technique works only on a
sorted array, so an array must be sorted to
apply binary search on the array.
• It is a searching technique that is better then
the liner search technique as the number of
iterations decreases in the binary search.
• #include <stdio.h>
• int main()
• {
• int i, first, last, middle, n, search,
array[50];
• printf("Enter number of
elementsn");
• scanf("%d", &n);
• printf("Enter %d integersn", n);
• for (i = 0; ci< n; i++)
• scanf("%d", &array[i]);
• printf("Enter value to findn");
• scanf("%d", &search);
• first = 0;
• last = n - 1;
• middle = (first+last)/2;
• while (first <= last) {
• if (array[middle] < search)
• first = middle + 1;
• else if (array[middle] ==
search) {
• printf("%d found at location
%d.n", search, middle+1);
• break;
• }
• else
• last = middle - 1;
• middle = (first + last)/2;
• }
• if (first > last)
• printf("Not found! %d isn't
present in the list.n", search);
• return 0;
• }
• #include <iostream>
• using namespace std;
• int main()
• {
• int i,j,s,a[10];
•
• for(i=0;i<4;i++)
• {
• cin>>a[i];
• }
• for(i=0;i<4-1;i++)
• {
• for(j=0;j<4-1;j++)
• {
• if(a[j]>a[j+1])
•
• {
• s=a[j];
• a[j]=a[j+1];
• a[j+1]=s;
• }
• }
• }
• cout<<"the sorted array
is"<<endl;
• for(i=0;i<8;i++)
• {
• cout<<a[i]<<endl;
• }
•
• return 0;
• } //bubble sort
Insertion sort
• #include <iostream>
• using namespace std;
• int main()
• {
• int i,j,s,a[10];
•
• for(i=0;i<4;i++)
• {
• cin>>a[i];
• }
• for(i=1;i<4;i++)
• {
• s=a[i];
• j=i-1;
• while(j>=0&&a[j]>s)
• {
• a[j+1]=a[j];
• j--;
• }
• a[j+1]=s;
• }
• cout<<"the sorted array
is"<<endl;
• for(i=0;i<4;i++)
• {
• cout<<a[i]<<endl;
• }
•
• return 0;
• }

array ppt of c programing language for exam

  • 1.
    ARRAYS • An arraysis defined as collection of homogenous (similar) data which is stored in contiguous memory locations or(continuous memory addresses) because arrays is a linear data structure . • Arrays can be single(1-D) or multi dimensional(2-D). 1st element last element
  • 2.
    Address of anarray 10 2 5 11 72 for int 1000 1002 1004 1006 1008 1st element last element 10.5 2.68 58.7 11.6 72.9 for float 1000 1004 1008 10012 10016 1st element last element Calculating the length of an array Length = upper_bound –lower_bound+1 Eg. 4-0+1= 5
  • 3.
    Declaring Arrays data-type variable-name[size]; Examples:- chara[5]; /* char type value array */ float a[9]; /* float type value array */ int a[10]; /* int type value array */ a[0],a[1]…. Are known as index The index of an array starts from 0 to size-1 means first element of any array will be stored at a[0] address and the last element will be at a[size - 1].
  • 4.
    Initialization of Array •After declaration it is necessary to initialize an array. Otherwise, it will contain garbage value( random value). An array can be initialized at either compile time or at runtime. No validity check for array index not available in c. Compile time Initialization :- It means that providing the value to an array in the code, during creation of it. Syntax:- data-type arrayname[size] = { list of values };
  • 5.
    • int a[4]= { 67, 87, 56, 77 }; • float a[5] = { 23.4, 6.8, 5.5 }; • int marks[4] = { 67, 87, 56, 77, 59 }; // Compile time error . • If the values are more than the declared array size than the compiler will give an error. Example:- 1. #include<stdio.h> 2. void main() 3. { 4. int i; 5. int a[] = {1, 3, 4}; 6. for(i = 0 ; i < 3 ; i++) 7. { 8. printf("%dn",a[i]); 9. } 10. }
  • 6.
    • #include<stdio.h> • intmain(){ • int arr[5] = {1,-1,3}; • printf("Array elements are:"); • for(int i = 0; i < 5; i++) • printf("%d ", arr[i]); • return 0; • } Rest two place will be initialized with 0. if int arr[5] ={} if this will leave blank , compiler will give the error.
  • 7.
    • #include<stdio.h> • intmain(){ • float arr[3] = {1.5,- 1.10,3.8}; • printf("Array elements are:"); • for(int i = 0; i < 3; i++) • printf("%f ", arr[i]); • return 0; • } • #include<stdio.h> • int main(){ • char arr[3] = {'a','b','c'}; • printf("Array elements are:n"); • for(int i = 0; i < 3; i++) • printf("%cn", arr[i]); • return 0; • }
  • 8.
    Runtime Array initialization •An array can also be initialized at runtime using scanf() function. Means user will provide the values. This approach is usually used for initializing large arrays. Example:- 1. scanf("%d", &a[1]); // means index 0 but position first. 2. for(int i = 0; i < 10; i++) scanf("%d", &a[i]); 3. for(int i = 0; i < 10; i+=2) scanf("%d", &a[i]);
  • 9.
    Accessing Array elements Accessingan array elements or printing Example:- for(int i = 0; i < 10; i++) printf("%d", a[i]); To access and print elements at specified index printf("%d", a[0]); printf("%d", a[5]); NOTE:- if we try to access elements, more than the size of an array or less than 0,then it won’t show an error but will produce the wrong output (garbage value).
  • 10.
    WAP to calculatesum of elements of array. • #include<stdio.h> • int main(){ • int arr[5]; • printf("Enter array elements:""); • for(int i = 0; i < 5; i++) • scanf("%d", &arr[i]); • printf("Array elements are:""); • for(int i = 0; i < 5; i++) • printf("%d ", arr[i]); • int sum = 0; • for(int i = 0; i < 5; i++) • sum += arr[i]; • printf("Sum =%d", sum); • }
  • 11.
    WAP to copydate from one array to other • #include <stdio.h> • int main() • { • int a[5] = {1,2,2,4,5}, a2[5]; • for(int i = 0; i < 5; i++) • a2[i] = a[i]; • for(int i = 0; i < 5; i++) • printf("%d ", a2[i]); • return 0; • }
  • 12.
    • #include<stdio.h> • intmain(){ • int a[10]; • printf("Array elements are:n"); • for(int i = 0; i < 10; i++) • { • if (i<4) • a[i]= 1; • else if(i>=4&&i<7) • a[i]= 2; • else • a[i]= 0; • printf("%dn", a[i]); • } • return 0; • }
  • 13.
    • Program toinsert an elements at specific place in array. • #include <stdio.h> • int main() • { • int a[10],i,in,e; • printf("enter elements in array n"); • for(i=0;i<5;i++) • { • scanf("%d",&a[i]); • printf("%dt",a[i]); • } • printf("nenter elements and indexn"); • scanf("%dn%d",&e,&in); • for(i=5;i>=in; i--) • { • a[i+1]=a[i]; • } • a[in]=e; • for(i=0;i<6;i++) • { • printf("%dt",a[i]); • } • return 0; • }
  • 14.
    Program to deletean elements from an array. • #include <stdio.h> • int main() • { • int a[5],i,in; • printf("enter elements in array n"); • for(i=0;i<5;i++) • scanf("%d",&a[i]); • printf("enter index from where you want to delete elementsn"); • scanf("%d",&in); • for(i=in;i<5;i++) • a[i]=a[i+1]; • printf("after deletionn"); • for(i=0;i<4;i++) • printf("%d",a[i]); • return 0; • }
  • 15.
    Examples • Program toreverse elements of an array. #include <stdio.h> int main() { int a[5],i; printf("enter elements in array n"); for(i=0;i<5;i++) scanf("%d",&a[i]); printf("elements of arrayn"); for(i=4;i>=0;i--) printf("%d",a[i]); return 0; }
  • 16.
    Merging of twoarrays • #include <stdio.h> • int main() • { • int i,in=0; • int a[3]={2,3,4}, b[3]={5,6,7}, c[6]; • for(i=0;i<3;i++) • { • c[in]= a[i]; • in++; • } • for(i=0;i<3;i++) • { • c[in]= b[i]; • in++; • • } • for(i=0;i<6;i++) • { • printf("%d",c[i]); • } • return 0; • }
  • 17.
    Find the largest& smallest element in an array • #include <stdio.h> • int main() { • int n; • int a[5]; • for (int i = 0; i < 5; i++) • { • scanf("%d", & a[i]); • } • for (int i = 0; i < 5; i++) { • if (a[0] < a[i]) { • a[0] = a[i]; • } • } • printf("Largest element = %d", a[0]); • return 0; • } • #include <stdio.h> • int main() { • int n; • int a[5]; • for (int i = 0; i < 5; i++) • { • scanf("%d", & a[i]); • } • for (int i = 0; i < 5; i++) { • if (a[0] > a[i]) { • a[0] = a[i]; • } • } • printf("smallest element = %d", a[0]); • return 0; • }
  • 18.
    Two dimensional Arrays •It is also known as multidimensional array. It contains a row index and a column index. Both the row's and column's index begins with 0. • Just like a one-dimensional array, Compile time and runtime initialization of two dimensional array is possible. Declaration:- data-type array-name[row-size][column-size] Example:- int a[3][4];
  • 20.
    Compile-time initialization Examples:- int a[2][2]= {1, 2, 3, 4}; // {{1, 2},{3, 4}} Int a[2][3] = {1, 2, 3, 4}; // {{1, 2, 3},{4}} int a[2][4] = {1, 2, 3, 4}; // {{1,2,3,4}} First of all the values are stored in first row, and then if there is any extra value, it goes to next row. Example:- int a[][3] = { {0,0,0}, {1,1,1} }; // no any values is assigned to row in array. It means that initialization of any number of rows. But must specify number of columns, else it will give a compile time error.
  • 21.
    Runtime initialization WAP toshow the Runtime initialization • #include<stdio.h> • void main() • { • int arr[3][4]; • int i, j, ; • printf("Enter array elements:n"); • for(i = 0; i < 3;i++) • { • for(j = 0; j < 4; j++) • { • scanf("%d", &arr[i][j]); • } • } • for(i = 0; i < 3; i++) • { • for(j = 0; j < 4; j++) • { • printf("%d", arr[i][j]); • } • } • }
  • 22.
    We can alsoinitialize 2D arrays like this int a[2][4] = { {0, 1, 2, 3} , {4, 5, 6, 7} }; or int a[2][4] = {0,1,2,3,4,5,6,7}; Both are equivalent Int [2][4] ={0} ;// all initialized with zero int [3][4]= {1,1,1,1,2,2,2,2} firstly 1 1 1 1 2 2 2 2 This row will be initialized by 0
  • 23.
    Calculate the sumof all the elements • #include<stdio.h> • int main() • { • int arr[2][3]; • int i, j,sum =0; • printf("Enter array elements:n"); • for(i = 0; i < 2;i++) • { • for(j = 0; j < 3; j++) • { • scanf("%d", &arr[i][j]); • } • } • for(i = 0; i < 2; i++) • { • for(j = 0; j < 3; j++) • { • printf("%dt", arr[i][j]); • sum= sum+arr[i][j]; • • } • printf("n"); • } • printf("n%d", sum); • } • #include<stdio.h> • int main() • { • int arr[2][3]; • int i, j,sum =0; • printf("Enter array elements:n"); • for(i = 0; i < 2;i++) • { • for(j = 0; j < 3; j++) • { • scanf("%d", &arr[i][j]); • } • } • for(i = 0; i < 2; i++) • { • for(j = 0; j < 3; j++) • { • printf("%dt", arr[i][j]); • sum= sum+arr[i][j]; • • } • printf("n"); • printf("n%dn", sum); • } •
  • 24.
    //Printing two matrix •#include<stdio.h> • int main() • { • int a[2][3],b[2][3]; • int i, j; • printf("Enter array elements:n"); • for(i = 0; i < 2;i++) • { • for(j = 0; j < 3; j++) • { • scanf("%d", &a[i][j]); • } • } • for(i = 0; i < 2;i++) • { • for(j = 0; j < 3; j++) • { • scanf("%d", &b[i][j]); • } • } • printf("1st matrixn"); • for(i = 0; i < 2; i++) • { • for(j = 0; j < 3; j++) • { • printf("%dt", a[i][j]); • } • printf("n"); • } • printf("2nd matrixn"); • for(i = 0; i < 2; i++) • { • for(j = 0; j < 3; j++) • { • printf("%dt", b[i][j]); • } • printf("n"); • } • } •
  • 25.
    Addition of twomatrix • #include<stdio.h> • int main() • { • int a[2][3],b[2][3],c[2][3]; • int i, j; • printf("Enter array elements:n"); • for(i = 0; i < 2;i++) • { • for(j = 0; j < 3; j++) • { • scanf("%d", &a[i][j]); • } • } • for(i = 0; i < 2;i++) • { • for(j = 0; j < 3; j++) • { • scanf("%d", &b[i][j]); • } • } • printf("1st matrixn"); • for(i = 0; i < 2; i++) • { • for(j = 0; j < 3; j++) • { • printf("%dt", a[i][j]); • } • printf("n"); • } • printf("2nd matrixn"); • for(i = 0; i < 2; i++) • { • for(j = 0; j < 3; j++) • { • printf("%dt", b[i][j]); • } • printf("n"); • } • printf("the addition of two matrix isn"); • for(i = 0; i < 2; i++) • { • for(j = 0; j < 3; j++) • { • c[i][j]= a[i][j]+b[i][j]; • printf("%dt", c[i][j]); • } • printf("n"); • } • • }
  • 26.
    Linear search A linearsearch, also known as a sequential search, is a method of finding an element within a list. It checks each element of the list sequentially until a match is found or the whole list has been searched. It works on both sorted and unsorted list of elements. Algorithm:- 1. Begin with the leftmost element of array[] and one by one compare searching element with each element. 2. If searching element matches with an element then return the index. 3. If searching element does not match with any of the elements then return -1.
  • 27.
    #include <stdio.h> int main() { inta[50], b, i, n; printf("Enter number of elements in array:n"); scanf("%d", &n); printf("Enter %d elements:n", n); for (i = 0; i < n; i++) scanf("%d", &a[i]); printf("Enter a number to search:n"); scanf("%d", &b); for (i = 0; i < n; i++) { if (a[i] == b) { printf("Element %d found at index %d.n", b, i); break; } } if (i == n) printf("Element %d not found.n", b); return 0; }
  • 28.
    Binary search • ABinary Search is a sorting algorithm, that is used to search an element in a sorted array. • A binary search technique works only on a sorted array, so an array must be sorted to apply binary search on the array. • It is a searching technique that is better then the liner search technique as the number of iterations decreases in the binary search.
  • 29.
    • #include <stdio.h> •int main() • { • int i, first, last, middle, n, search, array[50]; • printf("Enter number of elementsn"); • scanf("%d", &n); • printf("Enter %d integersn", n); • for (i = 0; ci< n; i++) • scanf("%d", &array[i]); • printf("Enter value to findn"); • scanf("%d", &search); • first = 0; • last = n - 1; • middle = (first+last)/2; • while (first <= last) { • if (array[middle] < search) • first = middle + 1; • else if (array[middle] == search) { • printf("%d found at location %d.n", search, middle+1); • break; • } • else • last = middle - 1; • middle = (first + last)/2; • } • if (first > last) • printf("Not found! %d isn't present in the list.n", search); • return 0; • }
  • 30.
    • #include <iostream> •using namespace std; • int main() • { • int i,j,s,a[10]; • • for(i=0;i<4;i++) • { • cin>>a[i]; • } • for(i=0;i<4-1;i++) • { • for(j=0;j<4-1;j++) • { • if(a[j]>a[j+1]) • • { • s=a[j]; • a[j]=a[j+1]; • a[j+1]=s; • } • } • } • cout<<"the sorted array is"<<endl; • for(i=0;i<8;i++) • { • cout<<a[i]<<endl; • } • • return 0; • } //bubble sort
  • 31.
    Insertion sort • #include<iostream> • using namespace std; • int main() • { • int i,j,s,a[10]; • • for(i=0;i<4;i++) • { • cin>>a[i]; • } • for(i=1;i<4;i++) • { • s=a[i]; • j=i-1; • while(j>=0&&a[j]>s) • { • a[j+1]=a[j]; • j--; • } • a[j+1]=s; • } • cout<<"the sorted array is"<<endl; • for(i=0;i<4;i++) • { • cout<<a[i]<<endl; • } • • return 0; • }