SlideShare a Scribd company logo
By : Rakesh Kumar                                                                  D.A.V. Centenary Public School


                                                    Array
Array is a collection of similar data types sharing a common name and one element can be distinguished using their
index.
Syntax to declare Array
        datatype identifier[size];
Example
        int x[10];
        float f[20]
The size of array can not be variable, it is recommended as a integer constant values without any type of positive or
negative sign.

When an array is declared using it’s syntax. The compiler creates the defined number of blocks in continuation. Each
block is assigned a unique index number which start from 0(Zero).
Example
       x[0]                      x[1]                   x[2]                     x[3]                    x[4]
          10                      20                      30                     40                       50


Initialization Method
                                                                 Declaration and
Method-I                                                         initialization simultaneouly
                int x[5] = {10,20,30,40,50 };
Method –II                                           Only when declaration + initialization simultaneously
                                                     takesh place
              int x[] = { 10,20,30,40,50 };
Method –III
               int x[5];
               x[0] =10;
               x[1]=20;
               x[2]=30;
               x[3]=40;
               x[4]=50;
Method –IV
               int x[5];
               cin>>x[0]>>x[1]>>x[2]>>x[3]>>x[4];
Method V
               int x[5];
               For(i=0;I,5;i++)
                         cin>>x[i];


Some Sample Question and Their Solution

Write a program in C++ to read an array of integer of         Write a program in C++ to read an array of integer
size 10 . Also display the same on the screen.                of size 10 and give an increment of 20 to each element.
                                                              Also display this modified list on the screen
#include<iostream>                                            #include<iostream>
#include<iomanip>                                             #include<iomanip>
#include<conio.h>                                             #include<conio.h>
using namespace std;                                          using namespace std;
int main()                                                    int main()
{                                                             {
  int x[10],i;                                                  int x[10],i;
  for(i=0;i<10;i++)                                             // input phase
    {                                                           for(i=0;i<10;i++)
         cout<<"Enter x["<<i<<"] element                          {
:";                                                                    cout<<"Enter       x["<<i<<"] element
         cin>>x[i];                                           :";
    }                                                                  cin>>x[i];
  cout<<"n Entered array :";                                     }

                                                          8
By : Rakesh Kumar                                                             D.A.V. Centenary Public School

    for(i=0;i<10;i++)                                          // processing phase
      cout<<setw(6)<<x[i];                                      for(i=0;i<10;i++)
    getch();                                                        x[i] = x[i]+20;
    return 0;
}                                                              cout<<"n Entered array :";
                                                               for(i=0;i<10;i++)
                                                                 cout<<setw(6)<<x[i];
                                                               getch();
                                                               return 0;
                                                           }


Write a program in C++ to read an array of integer Write a program in C++ to read an array of integer
of size 10 and find out the sum of even element and of size 10 and swap it’s first element with the last half
the sum of odd element. Also display this entered of it’s element and display the result on the screen.
array and sums on the screen.
#include<iostream>                                     #include<iostream>
#include<iomanip>                                      #include<iomanip>
#include<conio.h>                                      #include<conio.h>
using namespace std;                                   using namespace std;
int main()                                             int main()
{                                                      {
  int x[10],i,seven,sodd;                                int x[10],i,temp,j;
  // input phase                                         // input phase
  for(i=0;i<10;i++)                                      for(i=0;i<10;i++)
    {                                                      {
         cout<<"Enter x["<<i<<"] element                     cout<<"Enter x["<<i<<"] element :";
:";                                                           cin>>x[i];
         cin>>x[i];                                        }
    }                                                    // processing phase
  // processing phase
  seven=sodd=0;                                           for(i=0,j=9;i<5;i++,j--)
   for(i=0;i<10;i++)                                          {
      if(x[i]%2==0)                                                temp = x[i];
              seven +=x[i];                                          x[i] = x[j];
      else                                                           x[j] = temp;
              sodd += x[i];                                     }
  // output phase                                      // output phase
  cout<<"n Entered array :";                            cout<<"n Swaped array :";
  for(i=0;i<10;i++)                                      for(i=0;i<10;i++)
    cout<<setw(6)<<x[i];                                   cout<<setw(6)<<x[i];
  cout<<"n Sum of Even Element :"<<seven;               getch();
  cout<<"n Sum of Odd element :"<<sodd;                 return 0;
  getch();                                             }
  return 0;
}



                                        Bubble Sort ( Ascending Order)
 #include<iostream>
 #include<iomanip>
 #include<conio.h>
 using namespace std;
 // function to read an array from the keyboard
 void input(int x[],int n)
  {
    for(int i=0;i<n;i++)
      {
          cout<<"Enter "<<i<<" element :";
          cin>>x[i];
          }
    return;
  }




                                                       8
By : Rakesh Kumar                                       D.A.V. Centenary Public School

// function to display an arry on the screen
void output(int x[],int n)
  {
      for(int i=0;i<n;i++)
         cout<<setw(6)<<x[i];
    return;
}

// function to sort an array using bubble sort method
void bubble_sort(int x[],int n)
{
    int i,j,temp;
    for(i=0;i<n-1;i++)
       for(j=0;j<n-1-i;j++)
         {
                if(x[j]>x[j+1])
                  {
                      temp      =   x[j];
                      x[j]      =   x[j+1];
                      x[j+1]    =   temp;
                  }
           }
        return;
}
int main()
  {
     int x[10];
     input(x,10);
     bubble_sort(x,10);
     cout<<"n Sorted Array :";
     output(x,10);
     getch();
     return 0;
}


                                      Selection Sort
#include<iostream>
#include<iomanip>
#include<conio.h>
using namespace std;
// function to read an array from the keyboard
void input(int x[],int n)
 {
   for(int i=0;i<n;i++)
     {
         cout<<"Enter "<<i<<" element :";
         cin>>x[i];
         }
   return;
 }

// function to display an arry on the screen
void output(int x[],int n)
  {
      for(int i=0;i<n;i++)
         cout<<setw(6)<<x[i];
    return;
}

// function to sort an array using selection sort
void selection_sort(int x[],int n)
{
  int i,j,pos,low, temp;
  for(i=0;i<n-1;i++)
    {
         pos = i;


                                            8
By : Rakesh Kumar                                      D.A.V. Centenary Public School

          low = x[i];
     for(j=i+1;j<n-1;j++)
        {
               if(low>x[j])
                 {
                     low = x[j];
                     pos=j;
                 }
          }
          temp     = x[i];
          x[i]        =     x[pos];
          x[pos] = temp;
      }
}


int main()
  {
    int x[10];
    input(x,10);
    selection_sort(x,10);
    cout<<"n Sorted Array :";
    output(x,10);
    getch();
    return 0;
}


                                      INSERTION SORT
#include<iostream>
#include<iomanip>
#include<conio.h>
using namespace std;
// function to read an array from the keyboard
void input(int x[],int n)
 {
   for(int i=0;i<n;i++)
     {
         cout<<"Enter "<<i<<" element :";
         cin>>x[i];
         }
   return;
 }

// function to display an arry on the screen
void output(int x[],int n)
  {
      for(int i=0;i<n;i++)
         cout<<setw(6)<<x[i];
    return;
}

// function to sort an array using insertion sort
void insertion_sort(int x[],int n)
{
  int i,j,temp;
  for(i=1;i<n;i++)
    {
         temp = x[i];
         j = i-1 ;
       while(temp<x[j] && j>=0)
           {
                   x[j+1] = x[j];
                   j = j-1;
             }
       x[j+1]= temp;
    }


                                            8
By : Rakesh Kumar                                      D.A.V. Centenary Public School

    return;
}


int main()
  {
    int x[10];
    input(x,10);
    insertion_sort(x,10);
    cout<<"n Sorted Array :";
    output(x,10);
    getch();
    return 0;
}


                                       Contatenation
#include<iostream>
#include<iomanip>
#include<conio.h>
using namespace std;
// function to read an array from the keyboard
void input(int x[],int n)
 {
   for(int i=0;i<n;i++)
     {
         cout<<"Enter "<<i<<" element :";
         cin>>x[i];
         }
   return;
 }

// function to display an arry on the screen
void output(int x[],int n)
  {
      for(int i=0;i<n;i++)
         cout<<setw(6)<<x[i];
    return;
}

// search an element using binary search
void concate(int x[],int m,int y[],int n, int z[])
{
  for(int i=0;i<m;i++)
     z[i] = x[i];
  for(int i=0;i<n;i++)
     z[m+i] = y[i];
  return;
}

int main()
 {
   int x[5],y[10],z[15];
   cout<<"n ARRAY Xn";
   input(x,5);
   cout<<"n ARRAY Yn";
   input(y,10);
   concate(x,5,y,10,z);
   system("cls");
   cout<<"n Array A : ";
   output(x,5);
   cout<<"n Array B : ";
   output(y,10);
   cout<<"n Concatenated Array : ";
   output(z,15);
   getch();
   return 0;}


                                             8
By : Rakesh Kumar                                     D.A.V. Centenary Public School


                                      Merging
#include<iostream>
#include<iomanip>
#include<conio.h>
using namespace std;
// function to read an array from the keyboard
void input(int x[],int n)
 {
   for(int i=0;i<n;i++)
     {
         cout<<"Enter "<<i<<" element :";
         cin>>x[i];
         }
   return;
 }

// function to display an arry on the screen
void output(int x[],int n)
  {
      for(int i=0;i<n;i++)
         cout<<setw(6)<<x[i];
    return;
}

// merge two array to produce third array
void concate(int a[],int m,int b[],int n, int c[])
{
  int i,j,k;
  i=j=k=0;
  while(i<m && j<n )
   if(a[i]<b[j])
     c[k++] = a[i++];
   else
     c[k++] = b[j++];

     while(i<m)
       c[k++]= a[i++];
     while(j<n)
       c[k++]= b[j++];
    return;
}

int main()
 {
   int x[5],y[10],z[15];
   cout<<"n ARRAY Xn";
   input(x,5);
   cout<<"n ARRAY Yn";
   input(y,10);
   concate(x,5,y,10,z);
   system("cls");
   cout<<"n Array A : ";
   output(x,5);
   cout<<"n Array B : ";
   output(y,10);
   cout<<"n Merged Array : ";
   output(z,15);
   getch();
   return 0;}


                                      Linear Search
#include<iostream>
#include<iomanip>
#include<conio.h>
using namespace std;


                                           8
By : Rakesh Kumar                                             D.A.V. Centenary Public School

// function to read an array from the keyboard
void input(int x[],int n)
 {
   for(int i=0;i<n;i++)
     {
         cout<<"Enter "<<i<<" element :";
         cin>>x[i];
         }
   return;
 }

// function to display an arry on the screen
void output(int x[],int n)
  {
      for(int i=0;i<n;i++)
         cout<<setw(6)<<x[i];
    return;
}

// search an element using linear search
int linear_search(int x[],int n,int data)
{
  int i,found =0;
  for(i=0;i<n;i++)
    {
      if(x[i]==data)
          found =1;
      }
  return found;
}

int main()
  {
    int x[10],data;
    input(x,10);
    cout<<"n Enter element to search :";
    cin>>data;
    int res = linear_search(x,10,data);
    cout<<"n Entered Array :";
    output(x,10);
    if(res ==1)
       cout<<"n Given data available in given array ";
    else
       cout<<"n Given data not available in given array ";
    getch();
    return 0;
}

                                      Binary Search
#include<iostream>
#include<iomanip>
#include<conio.h>
using namespace std;
// function to read an array from the keyboard
void input(int x[],int n)
 {
   for(int i=0;i<n;i++)
     {
         cout<<"Enter "<<i<<" element :";
         cin>>x[i];
         }
   return;
 }

// function to display an arry on the screen
void output(int x[],int n)
 {


                                            8
By : Rakesh Kumar                                             D.A.V. Centenary Public School

      for(int i=0;i<n;i++)
         cout<<setw(6)<<x[i];
    return;
}

// search an element using binary search
int binary_search(int x[],int n,int data)
{
  int first,last,mid ,found =0;
  first =0;
  last = n-1;
  while(first<=last && found ==0)
   {
        mid = (first+last)/2;
        if(x[mid] == data)
              found =1;
        else
          if(x[mid]>data)
                last = mid-1;
             else
                first = mid+1;
   }
  return found;
}

int main()
  {
    int x[10],data;
    input(x,10);
    cout<<"n Enter element to search :";
    cin>>data;
    int res = binary_search(x,10,data);
    cout<<"n Entered Array :";
    output(x,10);
    if(res ==1)
       cout<<"n Given data available in given array ";
    else
       cout<<"n Given data not available in given array ";
    getch();
    return 0;
}




                                            8
Ad

More Related Content

What's hot (20)

Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...
ssuserd6b1fd
 
FP 201 - Unit 3 Part 2
FP 201 - Unit 3 Part 2FP 201 - Unit 3 Part 2
FP 201 - Unit 3 Part 2
rohassanie
 
Chapter 7 functions (c)
Chapter 7 functions (c)Chapter 7 functions (c)
Chapter 7 functions (c)
hhliu
 
Arrry structure Stacks in data structure
Arrry structure Stacks  in data structureArrry structure Stacks  in data structure
Arrry structure Stacks in data structure
lodhran-hayat
 
Data structure lab manual
Data structure lab manualData structure lab manual
Data structure lab manual
nikshaikh786
 
Unit 3
Unit 3 Unit 3
Unit 3
GOWSIKRAJAP
 
Pointers
PointersPointers
Pointers
sanya6900
 
Library functions in c++
Library functions in c++Library functions in c++
Library functions in c++
Neeru Mittal
 
2 BytesC++ course_2014_c9_ pointers and dynamic arrays
2 BytesC++ course_2014_c9_ pointers and dynamic arrays 2 BytesC++ course_2014_c9_ pointers and dynamic arrays
2 BytesC++ course_2014_c9_ pointers and dynamic arrays
kinan keshkeh
 
Lecture#8 introduction to array with examples c++
Lecture#8 introduction to array with examples c++Lecture#8 introduction to array with examples c++
Lecture#8 introduction to array with examples c++
NUST Stuff
 
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 5 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 5 of 5 by...Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 5 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 5 of 5 by...
ssuserd6b1fd
 
Dynamic Objects,Pointer to function,Array & Pointer,Character String Processing
Dynamic Objects,Pointer to function,Array & Pointer,Character String ProcessingDynamic Objects,Pointer to function,Array & Pointer,Character String Processing
Dynamic Objects,Pointer to function,Array & Pointer,Character String Processing
Meghaj Mallick
 
INTRODUCTION TO FUNCTIONS IN PYTHON
INTRODUCTION TO FUNCTIONS IN PYTHONINTRODUCTION TO FUNCTIONS IN PYTHON
INTRODUCTION TO FUNCTIONS IN PYTHON
vikram mahendra
 
VTU Data Structures Lab Manual
VTU Data Structures Lab ManualVTU Data Structures Lab Manual
VTU Data Structures Lab Manual
Nithin Kumar,VVCE, Mysuru
 
FP 201 Unit 2 - Part 3
FP 201 Unit 2 - Part 3FP 201 Unit 2 - Part 3
FP 201 Unit 2 - Part 3
rohassanie
 
Ch7 C++
Ch7 C++Ch7 C++
Ch7 C++
Venkateswarlu Vuggam
 
Java cheatsheet
Java cheatsheetJava cheatsheet
Java cheatsheet
Anass SABANI
 
54602399 c-examples-51-to-108-programe-ee01083101
54602399 c-examples-51-to-108-programe-ee0108310154602399 c-examples-51-to-108-programe-ee01083101
54602399 c-examples-51-to-108-programe-ee01083101
premrings
 
C aptitude scribd
C aptitude scribdC aptitude scribd
C aptitude scribd
Amit Kapoor
 
Data Structure Project File
Data Structure Project FileData Structure Project File
Data Structure Project File
Deyvessh kumar
 
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...
ssuserd6b1fd
 
FP 201 - Unit 3 Part 2
FP 201 - Unit 3 Part 2FP 201 - Unit 3 Part 2
FP 201 - Unit 3 Part 2
rohassanie
 
Chapter 7 functions (c)
Chapter 7 functions (c)Chapter 7 functions (c)
Chapter 7 functions (c)
hhliu
 
Arrry structure Stacks in data structure
Arrry structure Stacks  in data structureArrry structure Stacks  in data structure
Arrry structure Stacks in data structure
lodhran-hayat
 
Data structure lab manual
Data structure lab manualData structure lab manual
Data structure lab manual
nikshaikh786
 
Library functions in c++
Library functions in c++Library functions in c++
Library functions in c++
Neeru Mittal
 
2 BytesC++ course_2014_c9_ pointers and dynamic arrays
2 BytesC++ course_2014_c9_ pointers and dynamic arrays 2 BytesC++ course_2014_c9_ pointers and dynamic arrays
2 BytesC++ course_2014_c9_ pointers and dynamic arrays
kinan keshkeh
 
Lecture#8 introduction to array with examples c++
Lecture#8 introduction to array with examples c++Lecture#8 introduction to array with examples c++
Lecture#8 introduction to array with examples c++
NUST Stuff
 
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 5 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 5 of 5 by...Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 5 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 5 of 5 by...
ssuserd6b1fd
 
Dynamic Objects,Pointer to function,Array & Pointer,Character String Processing
Dynamic Objects,Pointer to function,Array & Pointer,Character String ProcessingDynamic Objects,Pointer to function,Array & Pointer,Character String Processing
Dynamic Objects,Pointer to function,Array & Pointer,Character String Processing
Meghaj Mallick
 
INTRODUCTION TO FUNCTIONS IN PYTHON
INTRODUCTION TO FUNCTIONS IN PYTHONINTRODUCTION TO FUNCTIONS IN PYTHON
INTRODUCTION TO FUNCTIONS IN PYTHON
vikram mahendra
 
FP 201 Unit 2 - Part 3
FP 201 Unit 2 - Part 3FP 201 Unit 2 - Part 3
FP 201 Unit 2 - Part 3
rohassanie
 
54602399 c-examples-51-to-108-programe-ee01083101
54602399 c-examples-51-to-108-programe-ee0108310154602399 c-examples-51-to-108-programe-ee01083101
54602399 c-examples-51-to-108-programe-ee01083101
premrings
 
C aptitude scribd
C aptitude scribdC aptitude scribd
C aptitude scribd
Amit Kapoor
 
Data Structure Project File
Data Structure Project FileData Structure Project File
Data Structure Project File
Deyvessh kumar
 

Viewers also liked (8)

Arrays Class presentation
Arrays Class presentationArrays Class presentation
Arrays Class presentation
Neveen Reda
 
Lecture17 arrays.ppt
Lecture17 arrays.pptLecture17 arrays.ppt
Lecture17 arrays.ppt
eShikshak
 
C Programming : Arrays
C Programming : ArraysC Programming : Arrays
C Programming : Arrays
Gagan Deep
 
Array Presentation (EngineerBaBu.com)
Array Presentation (EngineerBaBu.com)Array Presentation (EngineerBaBu.com)
Array Presentation (EngineerBaBu.com)
EngineerBabu
 
Arrays In C++
Arrays In C++Arrays In C++
Arrays In C++
Awais Alam
 
Array in C
Array in CArray in C
Array in C
Kamal Acharya
 
Arrays
ArraysArrays
Arrays
archikabhatia
 
Array in c language
Array in c languageArray in c language
Array in c language
home
 
Arrays Class presentation
Arrays Class presentationArrays Class presentation
Arrays Class presentation
Neveen Reda
 
Lecture17 arrays.ppt
Lecture17 arrays.pptLecture17 arrays.ppt
Lecture17 arrays.ppt
eShikshak
 
C Programming : Arrays
C Programming : ArraysC Programming : Arrays
C Programming : Arrays
Gagan Deep
 
Array Presentation (EngineerBaBu.com)
Array Presentation (EngineerBaBu.com)Array Presentation (EngineerBaBu.com)
Array Presentation (EngineerBaBu.com)
EngineerBabu
 
Array in c language
Array in c languageArray in c language
Array in c language
home
 
Ad

Similar to Array notes (20)

Oops lab manual2
Oops lab manual2Oops lab manual2
Oops lab manual2
Mouna Guru
 
C++ practical
C++ practicalC++ practical
C++ practical
Rahul juneja
 
Go vs C++ - CppRussia 2019 Piter BoF
Go vs C++ - CppRussia 2019 Piter BoFGo vs C++ - CppRussia 2019 Piter BoF
Go vs C++ - CppRussia 2019 Piter BoF
Timur Safin
 
Computer science-2010-cbse-question-paper
Computer science-2010-cbse-question-paperComputer science-2010-cbse-question-paper
Computer science-2010-cbse-question-paper
Deepak Singh
 
C Prog - Pointers
C Prog - PointersC Prog - Pointers
C Prog - Pointers
vinay arora
 
662305 10
662305 10662305 10
662305 10
Nitigan Nakjuatong
 
Ejercicios de programacion
Ejercicios de programacionEjercicios de programacion
Ejercicios de programacion
Jeff Tu Pechito
 
Computer_Practicals-file.doc.pdf
Computer_Practicals-file.doc.pdfComputer_Practicals-file.doc.pdf
Computer_Practicals-file.doc.pdf
HIMANSUKUMAR12
 
Programa.eje
Programa.ejePrograma.eje
Programa.eje
guapi387
 
Daapracticals 111105084852-phpapp02
Daapracticals 111105084852-phpapp02Daapracticals 111105084852-phpapp02
Daapracticals 111105084852-phpapp02
Er Ritu Aggarwal
 
public class TrequeT extends AbstractListT { .pdf
  public class TrequeT extends AbstractListT {  .pdf  public class TrequeT extends AbstractListT {  .pdf
public class TrequeT extends AbstractListT { .pdf
info30292
 
Introduction to Groovy
Introduction to GroovyIntroduction to Groovy
Introduction to Groovy
André Faria Gomes
 
Cpp c++ 1
Cpp c++ 1Cpp c++ 1
Cpp c++ 1
Sltnalt Cosmology
 
Cbse question-paper-computer-science-2009
Cbse question-paper-computer-science-2009Cbse question-paper-computer-science-2009
Cbse question-paper-computer-science-2009
Deepak Singh
 
code (1) thông tin nhập môn cntt hdsd.docx
code (1) thông tin nhập môn cntt hdsd.docxcode (1) thông tin nhập môn cntt hdsd.docx
code (1) thông tin nhập môn cntt hdsd.docx
minhthucuteo2003
 
CBSE Question Paper Computer Science with C++ 2011
CBSE Question Paper Computer Science with C++ 2011CBSE Question Paper Computer Science with C++ 2011
CBSE Question Paper Computer Science with C++ 2011
Deepak Singh
 
CBSE Class XII Comp sc practical file
CBSE Class XII Comp sc practical fileCBSE Class XII Comp sc practical file
CBSE Class XII Comp sc practical file
Pranav Ghildiyal
 
C++ Nested loops, matrix and fuctions.pdf
C++ Nested loops, matrix and fuctions.pdfC++ Nested loops, matrix and fuctions.pdf
C++ Nested loops, matrix and fuctions.pdf
yamew16788
 
Pnno
PnnoPnno
Pnno
shristichaudhary4
 
Implement a function in c++ which takes in a vector of integers and .pdf
Implement a function in c++ which takes in a vector of integers and .pdfImplement a function in c++ which takes in a vector of integers and .pdf
Implement a function in c++ which takes in a vector of integers and .pdf
feelingspaldi
 
Oops lab manual2
Oops lab manual2Oops lab manual2
Oops lab manual2
Mouna Guru
 
Go vs C++ - CppRussia 2019 Piter BoF
Go vs C++ - CppRussia 2019 Piter BoFGo vs C++ - CppRussia 2019 Piter BoF
Go vs C++ - CppRussia 2019 Piter BoF
Timur Safin
 
Computer science-2010-cbse-question-paper
Computer science-2010-cbse-question-paperComputer science-2010-cbse-question-paper
Computer science-2010-cbse-question-paper
Deepak Singh
 
C Prog - Pointers
C Prog - PointersC Prog - Pointers
C Prog - Pointers
vinay arora
 
Ejercicios de programacion
Ejercicios de programacionEjercicios de programacion
Ejercicios de programacion
Jeff Tu Pechito
 
Computer_Practicals-file.doc.pdf
Computer_Practicals-file.doc.pdfComputer_Practicals-file.doc.pdf
Computer_Practicals-file.doc.pdf
HIMANSUKUMAR12
 
Programa.eje
Programa.ejePrograma.eje
Programa.eje
guapi387
 
Daapracticals 111105084852-phpapp02
Daapracticals 111105084852-phpapp02Daapracticals 111105084852-phpapp02
Daapracticals 111105084852-phpapp02
Er Ritu Aggarwal
 
public class TrequeT extends AbstractListT { .pdf
  public class TrequeT extends AbstractListT {  .pdf  public class TrequeT extends AbstractListT {  .pdf
public class TrequeT extends AbstractListT { .pdf
info30292
 
Cbse question-paper-computer-science-2009
Cbse question-paper-computer-science-2009Cbse question-paper-computer-science-2009
Cbse question-paper-computer-science-2009
Deepak Singh
 
code (1) thông tin nhập môn cntt hdsd.docx
code (1) thông tin nhập môn cntt hdsd.docxcode (1) thông tin nhập môn cntt hdsd.docx
code (1) thông tin nhập môn cntt hdsd.docx
minhthucuteo2003
 
CBSE Question Paper Computer Science with C++ 2011
CBSE Question Paper Computer Science with C++ 2011CBSE Question Paper Computer Science with C++ 2011
CBSE Question Paper Computer Science with C++ 2011
Deepak Singh
 
CBSE Class XII Comp sc practical file
CBSE Class XII Comp sc practical fileCBSE Class XII Comp sc practical file
CBSE Class XII Comp sc practical file
Pranav Ghildiyal
 
C++ Nested loops, matrix and fuctions.pdf
C++ Nested loops, matrix and fuctions.pdfC++ Nested loops, matrix and fuctions.pdf
C++ Nested loops, matrix and fuctions.pdf
yamew16788
 
Implement a function in c++ which takes in a vector of integers and .pdf
Implement a function in c++ which takes in a vector of integers and .pdfImplement a function in c++ which takes in a vector of integers and .pdf
Implement a function in c++ which takes in a vector of integers and .pdf
feelingspaldi
 
Ad

More from Hitesh Wagle (20)

Zinkprinter
ZinkprinterZinkprinter
Zinkprinter
Hitesh Wagle
 
48695528 the-sulphur-system
48695528 the-sulphur-system48695528 the-sulphur-system
48695528 the-sulphur-system
Hitesh Wagle
 
Fundamentals of data structures ellis horowitz & sartaj sahni
Fundamentals of data structures   ellis horowitz & sartaj sahniFundamentals of data structures   ellis horowitz & sartaj sahni
Fundamentals of data structures ellis horowitz & sartaj sahni
Hitesh Wagle
 
Diode logic crkts
Diode logic crktsDiode logic crkts
Diode logic crkts
Hitesh Wagle
 
Applicationof datastructures
Applicationof datastructuresApplicationof datastructures
Applicationof datastructures
Hitesh Wagle
 
Oops index
Oops indexOops index
Oops index
Hitesh Wagle
 
Google search tips
Google search tipsGoogle search tips
Google search tips
Hitesh Wagle
 
Diode logic crkts
Diode logic crktsDiode logic crkts
Diode logic crkts
Hitesh Wagle
 
Applicationof datastructures
Applicationof datastructuresApplicationof datastructures
Applicationof datastructures
Hitesh Wagle
 
Green chem 2
Green chem 2Green chem 2
Green chem 2
Hitesh Wagle
 
Lecture notes on infinite sequences and series
Lecture notes on infinite sequences and seriesLecture notes on infinite sequences and series
Lecture notes on infinite sequences and series
Hitesh Wagle
 
Switkes01200543268
Switkes01200543268Switkes01200543268
Switkes01200543268
Hitesh Wagle
 
Cryptoghraphy
CryptoghraphyCryptoghraphy
Cryptoghraphy
Hitesh Wagle
 
Quote i2 cns_cnr_25064966
Quote i2 cns_cnr_25064966Quote i2 cns_cnr_25064966
Quote i2 cns_cnr_25064966
Hitesh Wagle
 
Pointers
PointersPointers
Pointers
Hitesh Wagle
 
P1
P1P1
P1
Hitesh Wagle
 
Notes
NotesNotes
Notes
Hitesh Wagle
 
48695528 the-sulphur-system
48695528 the-sulphur-system48695528 the-sulphur-system
48695528 the-sulphur-system
Hitesh Wagle
 
Fundamentals of data structures ellis horowitz & sartaj sahni
Fundamentals of data structures   ellis horowitz & sartaj sahniFundamentals of data structures   ellis horowitz & sartaj sahni
Fundamentals of data structures ellis horowitz & sartaj sahni
Hitesh Wagle
 
Applicationof datastructures
Applicationof datastructuresApplicationof datastructures
Applicationof datastructures
Hitesh Wagle
 
Google search tips
Google search tipsGoogle search tips
Google search tips
Hitesh Wagle
 
Applicationof datastructures
Applicationof datastructuresApplicationof datastructures
Applicationof datastructures
Hitesh Wagle
 
Lecture notes on infinite sequences and series
Lecture notes on infinite sequences and seriesLecture notes on infinite sequences and series
Lecture notes on infinite sequences and series
Hitesh Wagle
 
Switkes01200543268
Switkes01200543268Switkes01200543268
Switkes01200543268
Hitesh Wagle
 
Quote i2 cns_cnr_25064966
Quote i2 cns_cnr_25064966Quote i2 cns_cnr_25064966
Quote i2 cns_cnr_25064966
Hitesh Wagle
 

Recently uploaded (20)

Q1 2025 Dropbox Earnings and Investor Presentation
Q1 2025 Dropbox Earnings and Investor PresentationQ1 2025 Dropbox Earnings and Investor Presentation
Q1 2025 Dropbox Earnings and Investor Presentation
Dropbox
 
AI and Data Privacy in 2025: Global Trends
AI and Data Privacy in 2025: Global TrendsAI and Data Privacy in 2025: Global Trends
AI and Data Privacy in 2025: Global Trends
InData Labs
 
Build 3D Animated Safety Induction - Tech EHS
Build 3D Animated Safety Induction - Tech EHSBuild 3D Animated Safety Induction - Tech EHS
Build 3D Animated Safety Induction - Tech EHS
TECH EHS Solution
 
Hybridize Functions: A Tool for Automatically Refactoring Imperative Deep Lea...
Hybridize Functions: A Tool for Automatically Refactoring Imperative Deep Lea...Hybridize Functions: A Tool for Automatically Refactoring Imperative Deep Lea...
Hybridize Functions: A Tool for Automatically Refactoring Imperative Deep Lea...
Raffi Khatchadourian
 
Hybridize Functions: A Tool for Automatically Refactoring Imperative Deep Lea...
Hybridize Functions: A Tool for Automatically Refactoring Imperative Deep Lea...Hybridize Functions: A Tool for Automatically Refactoring Imperative Deep Lea...
Hybridize Functions: A Tool for Automatically Refactoring Imperative Deep Lea...
Raffi Khatchadourian
 
TrsLabs - AI Agents for All - Chatbots to Multi-Agents Systems
TrsLabs - AI Agents for All - Chatbots to Multi-Agents SystemsTrsLabs - AI Agents for All - Chatbots to Multi-Agents Systems
TrsLabs - AI Agents for All - Chatbots to Multi-Agents Systems
Trs Labs
 
AsyncAPI v3 : Streamlining Event-Driven API Design
AsyncAPI v3 : Streamlining Event-Driven API DesignAsyncAPI v3 : Streamlining Event-Driven API Design
AsyncAPI v3 : Streamlining Event-Driven API Design
leonid54
 
Are Cloud PBX Providers in India Reliable for Small Businesses (1).pdf
Are Cloud PBX Providers in India Reliable for Small Businesses (1).pdfAre Cloud PBX Providers in India Reliable for Small Businesses (1).pdf
Are Cloud PBX Providers in India Reliable for Small Businesses (1).pdf
Telecoms Supermarket
 
Build Your Own Copilot & Agents For Devs
Build Your Own Copilot & Agents For DevsBuild Your Own Copilot & Agents For Devs
Build Your Own Copilot & Agents For Devs
Brian McKeiver
 
AI 3-in-1: Agents, RAG, and Local Models - Brent Laster
AI 3-in-1: Agents, RAG, and Local Models - Brent LasterAI 3-in-1: Agents, RAG, and Local Models - Brent Laster
AI 3-in-1: Agents, RAG, and Local Models - Brent Laster
All Things Open
 
Viam product demo_ Deploying and scaling AI with hardware.pdf
Viam product demo_ Deploying and scaling AI with hardware.pdfViam product demo_ Deploying and scaling AI with hardware.pdf
Viam product demo_ Deploying and scaling AI with hardware.pdf
camilalamoratta
 
Challenges in Migrating Imperative Deep Learning Programs to Graph Execution:...
Challenges in Migrating Imperative Deep Learning Programs to Graph Execution:...Challenges in Migrating Imperative Deep Learning Programs to Graph Execution:...
Challenges in Migrating Imperative Deep Learning Programs to Graph Execution:...
Raffi Khatchadourian
 
TrsLabs - Fintech Product & Business Consulting
TrsLabs - Fintech Product & Business ConsultingTrsLabs - Fintech Product & Business Consulting
TrsLabs - Fintech Product & Business Consulting
Trs Labs
 
How to Install & Activate ListGrabber - eGrabber
How to Install & Activate ListGrabber - eGrabberHow to Install & Activate ListGrabber - eGrabber
How to Install & Activate ListGrabber - eGrabber
eGrabber
 
Cybersecurity Identity and Access Solutions using Azure AD
Cybersecurity Identity and Access Solutions using Azure ADCybersecurity Identity and Access Solutions using Azure AD
Cybersecurity Identity and Access Solutions using Azure AD
VICTOR MAESTRE RAMIREZ
 
Webinar - Top 5 Backup Mistakes MSPs and Businesses Make .pptx
Webinar - Top 5 Backup Mistakes MSPs and Businesses Make   .pptxWebinar - Top 5 Backup Mistakes MSPs and Businesses Make   .pptx
Webinar - Top 5 Backup Mistakes MSPs and Businesses Make .pptx
MSP360
 
HCL Nomad Web – Best Practices and Managing Multiuser Environments
HCL Nomad Web – Best Practices and Managing Multiuser EnvironmentsHCL Nomad Web – Best Practices and Managing Multiuser Environments
HCL Nomad Web – Best Practices and Managing Multiuser Environments
panagenda
 
Linux Professional Institute LPIC-1 Exam.pdf
Linux Professional Institute LPIC-1 Exam.pdfLinux Professional Institute LPIC-1 Exam.pdf
Linux Professional Institute LPIC-1 Exam.pdf
RHCSA Guru
 
Connect and Protect: Networks and Network Security
Connect and Protect: Networks and Network SecurityConnect and Protect: Networks and Network Security
Connect and Protect: Networks and Network Security
VICTOR MAESTRE RAMIREZ
 
Canadian book publishing: Insights from the latest salary survey - Tech Forum...
Canadian book publishing: Insights from the latest salary survey - Tech Forum...Canadian book publishing: Insights from the latest salary survey - Tech Forum...
Canadian book publishing: Insights from the latest salary survey - Tech Forum...
BookNet Canada
 
Q1 2025 Dropbox Earnings and Investor Presentation
Q1 2025 Dropbox Earnings and Investor PresentationQ1 2025 Dropbox Earnings and Investor Presentation
Q1 2025 Dropbox Earnings and Investor Presentation
Dropbox
 
AI and Data Privacy in 2025: Global Trends
AI and Data Privacy in 2025: Global TrendsAI and Data Privacy in 2025: Global Trends
AI and Data Privacy in 2025: Global Trends
InData Labs
 
Build 3D Animated Safety Induction - Tech EHS
Build 3D Animated Safety Induction - Tech EHSBuild 3D Animated Safety Induction - Tech EHS
Build 3D Animated Safety Induction - Tech EHS
TECH EHS Solution
 
Hybridize Functions: A Tool for Automatically Refactoring Imperative Deep Lea...
Hybridize Functions: A Tool for Automatically Refactoring Imperative Deep Lea...Hybridize Functions: A Tool for Automatically Refactoring Imperative Deep Lea...
Hybridize Functions: A Tool for Automatically Refactoring Imperative Deep Lea...
Raffi Khatchadourian
 
Hybridize Functions: A Tool for Automatically Refactoring Imperative Deep Lea...
Hybridize Functions: A Tool for Automatically Refactoring Imperative Deep Lea...Hybridize Functions: A Tool for Automatically Refactoring Imperative Deep Lea...
Hybridize Functions: A Tool for Automatically Refactoring Imperative Deep Lea...
Raffi Khatchadourian
 
TrsLabs - AI Agents for All - Chatbots to Multi-Agents Systems
TrsLabs - AI Agents for All - Chatbots to Multi-Agents SystemsTrsLabs - AI Agents for All - Chatbots to Multi-Agents Systems
TrsLabs - AI Agents for All - Chatbots to Multi-Agents Systems
Trs Labs
 
AsyncAPI v3 : Streamlining Event-Driven API Design
AsyncAPI v3 : Streamlining Event-Driven API DesignAsyncAPI v3 : Streamlining Event-Driven API Design
AsyncAPI v3 : Streamlining Event-Driven API Design
leonid54
 
Are Cloud PBX Providers in India Reliable for Small Businesses (1).pdf
Are Cloud PBX Providers in India Reliable for Small Businesses (1).pdfAre Cloud PBX Providers in India Reliable for Small Businesses (1).pdf
Are Cloud PBX Providers in India Reliable for Small Businesses (1).pdf
Telecoms Supermarket
 
Build Your Own Copilot & Agents For Devs
Build Your Own Copilot & Agents For DevsBuild Your Own Copilot & Agents For Devs
Build Your Own Copilot & Agents For Devs
Brian McKeiver
 
AI 3-in-1: Agents, RAG, and Local Models - Brent Laster
AI 3-in-1: Agents, RAG, and Local Models - Brent LasterAI 3-in-1: Agents, RAG, and Local Models - Brent Laster
AI 3-in-1: Agents, RAG, and Local Models - Brent Laster
All Things Open
 
Viam product demo_ Deploying and scaling AI with hardware.pdf
Viam product demo_ Deploying and scaling AI with hardware.pdfViam product demo_ Deploying and scaling AI with hardware.pdf
Viam product demo_ Deploying and scaling AI with hardware.pdf
camilalamoratta
 
Challenges in Migrating Imperative Deep Learning Programs to Graph Execution:...
Challenges in Migrating Imperative Deep Learning Programs to Graph Execution:...Challenges in Migrating Imperative Deep Learning Programs to Graph Execution:...
Challenges in Migrating Imperative Deep Learning Programs to Graph Execution:...
Raffi Khatchadourian
 
TrsLabs - Fintech Product & Business Consulting
TrsLabs - Fintech Product & Business ConsultingTrsLabs - Fintech Product & Business Consulting
TrsLabs - Fintech Product & Business Consulting
Trs Labs
 
How to Install & Activate ListGrabber - eGrabber
How to Install & Activate ListGrabber - eGrabberHow to Install & Activate ListGrabber - eGrabber
How to Install & Activate ListGrabber - eGrabber
eGrabber
 
Cybersecurity Identity and Access Solutions using Azure AD
Cybersecurity Identity and Access Solutions using Azure ADCybersecurity Identity and Access Solutions using Azure AD
Cybersecurity Identity and Access Solutions using Azure AD
VICTOR MAESTRE RAMIREZ
 
Webinar - Top 5 Backup Mistakes MSPs and Businesses Make .pptx
Webinar - Top 5 Backup Mistakes MSPs and Businesses Make   .pptxWebinar - Top 5 Backup Mistakes MSPs and Businesses Make   .pptx
Webinar - Top 5 Backup Mistakes MSPs and Businesses Make .pptx
MSP360
 
HCL Nomad Web – Best Practices and Managing Multiuser Environments
HCL Nomad Web – Best Practices and Managing Multiuser EnvironmentsHCL Nomad Web – Best Practices and Managing Multiuser Environments
HCL Nomad Web – Best Practices and Managing Multiuser Environments
panagenda
 
Linux Professional Institute LPIC-1 Exam.pdf
Linux Professional Institute LPIC-1 Exam.pdfLinux Professional Institute LPIC-1 Exam.pdf
Linux Professional Institute LPIC-1 Exam.pdf
RHCSA Guru
 
Connect and Protect: Networks and Network Security
Connect and Protect: Networks and Network SecurityConnect and Protect: Networks and Network Security
Connect and Protect: Networks and Network Security
VICTOR MAESTRE RAMIREZ
 
Canadian book publishing: Insights from the latest salary survey - Tech Forum...
Canadian book publishing: Insights from the latest salary survey - Tech Forum...Canadian book publishing: Insights from the latest salary survey - Tech Forum...
Canadian book publishing: Insights from the latest salary survey - Tech Forum...
BookNet Canada
 

Array notes

  • 1. By : Rakesh Kumar D.A.V. Centenary Public School Array Array is a collection of similar data types sharing a common name and one element can be distinguished using their index. Syntax to declare Array datatype identifier[size]; Example int x[10]; float f[20] The size of array can not be variable, it is recommended as a integer constant values without any type of positive or negative sign. When an array is declared using it’s syntax. The compiler creates the defined number of blocks in continuation. Each block is assigned a unique index number which start from 0(Zero). Example x[0] x[1] x[2] x[3] x[4] 10 20 30 40 50 Initialization Method Declaration and Method-I initialization simultaneouly int x[5] = {10,20,30,40,50 }; Method –II Only when declaration + initialization simultaneously takesh place int x[] = { 10,20,30,40,50 }; Method –III int x[5]; x[0] =10; x[1]=20; x[2]=30; x[3]=40; x[4]=50; Method –IV int x[5]; cin>>x[0]>>x[1]>>x[2]>>x[3]>>x[4]; Method V int x[5]; For(i=0;I,5;i++) cin>>x[i]; Some Sample Question and Their Solution Write a program in C++ to read an array of integer of Write a program in C++ to read an array of integer size 10 . Also display the same on the screen. of size 10 and give an increment of 20 to each element. Also display this modified list on the screen #include<iostream> #include<iostream> #include<iomanip> #include<iomanip> #include<conio.h> #include<conio.h> using namespace std; using namespace std; int main() int main() { { int x[10],i; int x[10],i; for(i=0;i<10;i++) // input phase { for(i=0;i<10;i++) cout<<"Enter x["<<i<<"] element { :"; cout<<"Enter x["<<i<<"] element cin>>x[i]; :"; } cin>>x[i]; cout<<"n Entered array :"; } 8
  • 2. By : Rakesh Kumar D.A.V. Centenary Public School for(i=0;i<10;i++) // processing phase cout<<setw(6)<<x[i]; for(i=0;i<10;i++) getch(); x[i] = x[i]+20; return 0; } cout<<"n Entered array :"; for(i=0;i<10;i++) cout<<setw(6)<<x[i]; getch(); return 0; } Write a program in C++ to read an array of integer Write a program in C++ to read an array of integer of size 10 and find out the sum of even element and of size 10 and swap it’s first element with the last half the sum of odd element. Also display this entered of it’s element and display the result on the screen. array and sums on the screen. #include<iostream> #include<iostream> #include<iomanip> #include<iomanip> #include<conio.h> #include<conio.h> using namespace std; using namespace std; int main() int main() { { int x[10],i,seven,sodd; int x[10],i,temp,j; // input phase // input phase for(i=0;i<10;i++) for(i=0;i<10;i++) { { cout<<"Enter x["<<i<<"] element cout<<"Enter x["<<i<<"] element :"; :"; cin>>x[i]; cin>>x[i]; } } // processing phase // processing phase seven=sodd=0; for(i=0,j=9;i<5;i++,j--) for(i=0;i<10;i++) { if(x[i]%2==0) temp = x[i]; seven +=x[i]; x[i] = x[j]; else x[j] = temp; sodd += x[i]; } // output phase // output phase cout<<"n Entered array :"; cout<<"n Swaped array :"; for(i=0;i<10;i++) for(i=0;i<10;i++) cout<<setw(6)<<x[i]; cout<<setw(6)<<x[i]; cout<<"n Sum of Even Element :"<<seven; getch(); cout<<"n Sum of Odd element :"<<sodd; return 0; getch(); } return 0; } Bubble Sort ( Ascending Order) #include<iostream> #include<iomanip> #include<conio.h> using namespace std; // function to read an array from the keyboard void input(int x[],int n) { for(int i=0;i<n;i++) { cout<<"Enter "<<i<<" element :"; cin>>x[i]; } return; } 8
  • 3. By : Rakesh Kumar D.A.V. Centenary Public School // function to display an arry on the screen void output(int x[],int n) { for(int i=0;i<n;i++) cout<<setw(6)<<x[i]; return; } // function to sort an array using bubble sort method void bubble_sort(int x[],int n) { int i,j,temp; for(i=0;i<n-1;i++) for(j=0;j<n-1-i;j++) { if(x[j]>x[j+1]) { temp = x[j]; x[j] = x[j+1]; x[j+1] = temp; } } return; } int main() { int x[10]; input(x,10); bubble_sort(x,10); cout<<"n Sorted Array :"; output(x,10); getch(); return 0; } Selection Sort #include<iostream> #include<iomanip> #include<conio.h> using namespace std; // function to read an array from the keyboard void input(int x[],int n) { for(int i=0;i<n;i++) { cout<<"Enter "<<i<<" element :"; cin>>x[i]; } return; } // function to display an arry on the screen void output(int x[],int n) { for(int i=0;i<n;i++) cout<<setw(6)<<x[i]; return; } // function to sort an array using selection sort void selection_sort(int x[],int n) { int i,j,pos,low, temp; for(i=0;i<n-1;i++) { pos = i; 8
  • 4. By : Rakesh Kumar D.A.V. Centenary Public School low = x[i]; for(j=i+1;j<n-1;j++) { if(low>x[j]) { low = x[j]; pos=j; } } temp = x[i]; x[i] = x[pos]; x[pos] = temp; } } int main() { int x[10]; input(x,10); selection_sort(x,10); cout<<"n Sorted Array :"; output(x,10); getch(); return 0; } INSERTION SORT #include<iostream> #include<iomanip> #include<conio.h> using namespace std; // function to read an array from the keyboard void input(int x[],int n) { for(int i=0;i<n;i++) { cout<<"Enter "<<i<<" element :"; cin>>x[i]; } return; } // function to display an arry on the screen void output(int x[],int n) { for(int i=0;i<n;i++) cout<<setw(6)<<x[i]; return; } // function to sort an array using insertion sort void insertion_sort(int x[],int n) { int i,j,temp; for(i=1;i<n;i++) { temp = x[i]; j = i-1 ; while(temp<x[j] && j>=0) { x[j+1] = x[j]; j = j-1; } x[j+1]= temp; } 8
  • 5. By : Rakesh Kumar D.A.V. Centenary Public School return; } int main() { int x[10]; input(x,10); insertion_sort(x,10); cout<<"n Sorted Array :"; output(x,10); getch(); return 0; } Contatenation #include<iostream> #include<iomanip> #include<conio.h> using namespace std; // function to read an array from the keyboard void input(int x[],int n) { for(int i=0;i<n;i++) { cout<<"Enter "<<i<<" element :"; cin>>x[i]; } return; } // function to display an arry on the screen void output(int x[],int n) { for(int i=0;i<n;i++) cout<<setw(6)<<x[i]; return; } // search an element using binary search void concate(int x[],int m,int y[],int n, int z[]) { for(int i=0;i<m;i++) z[i] = x[i]; for(int i=0;i<n;i++) z[m+i] = y[i]; return; } int main() { int x[5],y[10],z[15]; cout<<"n ARRAY Xn"; input(x,5); cout<<"n ARRAY Yn"; input(y,10); concate(x,5,y,10,z); system("cls"); cout<<"n Array A : "; output(x,5); cout<<"n Array B : "; output(y,10); cout<<"n Concatenated Array : "; output(z,15); getch(); return 0;} 8
  • 6. By : Rakesh Kumar D.A.V. Centenary Public School Merging #include<iostream> #include<iomanip> #include<conio.h> using namespace std; // function to read an array from the keyboard void input(int x[],int n) { for(int i=0;i<n;i++) { cout<<"Enter "<<i<<" element :"; cin>>x[i]; } return; } // function to display an arry on the screen void output(int x[],int n) { for(int i=0;i<n;i++) cout<<setw(6)<<x[i]; return; } // merge two array to produce third array void concate(int a[],int m,int b[],int n, int c[]) { int i,j,k; i=j=k=0; while(i<m && j<n ) if(a[i]<b[j]) c[k++] = a[i++]; else c[k++] = b[j++]; while(i<m) c[k++]= a[i++]; while(j<n) c[k++]= b[j++]; return; } int main() { int x[5],y[10],z[15]; cout<<"n ARRAY Xn"; input(x,5); cout<<"n ARRAY Yn"; input(y,10); concate(x,5,y,10,z); system("cls"); cout<<"n Array A : "; output(x,5); cout<<"n Array B : "; output(y,10); cout<<"n Merged Array : "; output(z,15); getch(); return 0;} Linear Search #include<iostream> #include<iomanip> #include<conio.h> using namespace std; 8
  • 7. By : Rakesh Kumar D.A.V. Centenary Public School // function to read an array from the keyboard void input(int x[],int n) { for(int i=0;i<n;i++) { cout<<"Enter "<<i<<" element :"; cin>>x[i]; } return; } // function to display an arry on the screen void output(int x[],int n) { for(int i=0;i<n;i++) cout<<setw(6)<<x[i]; return; } // search an element using linear search int linear_search(int x[],int n,int data) { int i,found =0; for(i=0;i<n;i++) { if(x[i]==data) found =1; } return found; } int main() { int x[10],data; input(x,10); cout<<"n Enter element to search :"; cin>>data; int res = linear_search(x,10,data); cout<<"n Entered Array :"; output(x,10); if(res ==1) cout<<"n Given data available in given array "; else cout<<"n Given data not available in given array "; getch(); return 0; } Binary Search #include<iostream> #include<iomanip> #include<conio.h> using namespace std; // function to read an array from the keyboard void input(int x[],int n) { for(int i=0;i<n;i++) { cout<<"Enter "<<i<<" element :"; cin>>x[i]; } return; } // function to display an arry on the screen void output(int x[],int n) { 8
  • 8. By : Rakesh Kumar D.A.V. Centenary Public School for(int i=0;i<n;i++) cout<<setw(6)<<x[i]; return; } // search an element using binary search int binary_search(int x[],int n,int data) { int first,last,mid ,found =0; first =0; last = n-1; while(first<=last && found ==0) { mid = (first+last)/2; if(x[mid] == data) found =1; else if(x[mid]>data) last = mid-1; else first = mid+1; } return found; } int main() { int x[10],data; input(x,10); cout<<"n Enter element to search :"; cin>>data; int res = binary_search(x,10,data); cout<<"n Entered Array :"; output(x,10); if(res ==1) cout<<"n Given data available in given array "; else cout<<"n Given data not available in given array "; getch(); return 0; } 8