SlideShare a Scribd company logo
1 of 8
Download to read offline
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

More Related Content

What's hot

What's hot (20)

C++ practical
C++ practicalC++ practical
C++ practical
 
Pointer
PointerPointer
Pointer
 
The Ring programming language version 1.5.2 book - Part 37 of 181
The Ring programming language version 1.5.2 book - Part 37 of 181The Ring programming language version 1.5.2 book - Part 37 of 181
The Ring programming language version 1.5.2 book - Part 37 of 181
 
Applied Design Patterns - A Compiler Case Study
Applied Design Patterns - A Compiler Case Study Applied Design Patterns - A Compiler Case Study
Applied Design Patterns - A Compiler Case Study
 
Let the type system be your friend
Let the type system be your friendLet the type system be your friend
Let the type system be your friend
 
The Ring programming language version 1.2 book - Part 20 of 84
The Ring programming language version 1.2 book - Part 20 of 84The Ring programming language version 1.2 book - Part 20 of 84
The Ring programming language version 1.2 book - Part 20 of 84
 
Hidden Gems in Swift
Hidden Gems in SwiftHidden Gems in Swift
Hidden Gems in Swift
 
The Ring programming language version 1.5.3 book - Part 38 of 184
The Ring programming language version 1.5.3 book - Part 38 of 184The Ring programming language version 1.5.3 book - Part 38 of 184
The Ring programming language version 1.5.3 book - Part 38 of 184
 
The Ring programming language version 1.6 book - Part 40 of 189
The Ring programming language version 1.6 book - Part 40 of 189The Ring programming language version 1.6 book - Part 40 of 189
The Ring programming language version 1.6 book - Part 40 of 189
 
ماترێکس به‌ کوردی ئارام
ماترێکس به‌ کوردی ئارامماترێکس به‌ کوردی ئارام
ماترێکس به‌ کوردی ئارام
 
The Ring programming language version 1.5.4 book - Part 38 of 185
The Ring programming language version 1.5.4 book - Part 38 of 185The Ring programming language version 1.5.4 book - Part 38 of 185
The Ring programming language version 1.5.4 book - Part 38 of 185
 
Collection v3
Collection v3Collection v3
Collection v3
 
Promise: async programming hero
Promise: async programming heroPromise: async programming hero
Promise: async programming hero
 
The Ring programming language version 1.5.2 book - Part 30 of 181
The Ring programming language version 1.5.2 book - Part 30 of 181The Ring programming language version 1.5.2 book - Part 30 of 181
The Ring programming language version 1.5.2 book - Part 30 of 181
 
Pytables
PytablesPytables
Pytables
 
Developer Experience i TypeScript. Najbardziej ikoniczne duo
Developer Experience i TypeScript. Najbardziej ikoniczne duoDeveloper Experience i TypeScript. Najbardziej ikoniczne duo
Developer Experience i TypeScript. Najbardziej ikoniczne duo
 
The Ring programming language version 1.10 book - Part 48 of 212
The Ring programming language version 1.10 book - Part 48 of 212The Ring programming language version 1.10 book - Part 48 of 212
The Ring programming language version 1.10 book - Part 48 of 212
 
Chapter 7 functions (c)
Chapter 7 functions (c)Chapter 7 functions (c)
Chapter 7 functions (c)
 
C++ Expression Templateを使って式をコンパイル時に微分
C++ Expression Templateを使って式をコンパイル時に微分C++ Expression Templateを使って式をコンパイル時に微分
C++ Expression Templateを使って式をコンパイル時に微分
 
The Ring programming language version 1.5 book - Part 6 of 31
The Ring programming language version 1.5 book - Part 6 of 31The Ring programming language version 1.5 book - Part 6 of 31
The Ring programming language version 1.5 book - Part 6 of 31
 

Viewers also liked (20)

апкс 2011 05_verilog
апкс 2011 05_verilogапкс 2011 05_verilog
апкс 2011 05_verilog
 
Writing 2
Writing 2Writing 2
Writing 2
 
8085 microprocessor
8085 microprocessor8085 microprocessor
8085 microprocessor
 
Bhogve eni-bhul
Bhogve eni-bhulBhogve eni-bhul
Bhogve eni-bhul
 
WORLD WIDE WEB
WORLD WIDE WEBWORLD WIDE WEB
WORLD WIDE WEB
 
Test
TestTest
Test
 
מבחן אופי
מבחן אופימבחן אופי
מבחן אופי
 
Estructuras dinámicas de datos
Estructuras dinámicas de datosEstructuras dinámicas de datos
Estructuras dinámicas de datos
 
Personel
PersonelPersonel
Personel
 
Words power
Words powerWords power
Words power
 
Pila Abstracta
Pila AbstractaPila Abstracta
Pila Abstracta
 
Narrative for 091
Narrative for 091Narrative for 091
Narrative for 091
 
Cask excerpt for narrative task
Cask excerpt for narrative taskCask excerpt for narrative task
Cask excerpt for narrative task
 
Rph mari
Rph mariRph mari
Rph mari
 
Utp sirn_s2_rna 2014-2
 Utp sirn_s2_rna 2014-2 Utp sirn_s2_rna 2014-2
Utp sirn_s2_rna 2014-2
 
שיעורים באסטרטגיה ארגונית
שיעורים באסטרטגיה ארגוניתשיעורים באסטרטגיה ארגונית
שיעורים באסטרטגיה ארגונית
 
Ajlaa tailbarlakhiin a.hol
Ajlaa tailbarlakhiin a.holAjlaa tailbarlakhiin a.hol
Ajlaa tailbarlakhiin a.hol
 
Al2ed chapter2
Al2ed chapter2Al2ed chapter2
Al2ed chapter2
 
Karm nu-vignan
Karm nu-vignanKarm nu-vignan
Karm nu-vignan
 
Task3c web banner thing
Task3c web banner thingTask3c web banner thing
Task3c web banner thing
 

Similar to Notes

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
 
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
 
basic programs in C++
basic programs in C++ basic programs in C++
basic programs in C++
Arun Nair
 

Similar to Notes (20)

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
 
Computer science-2010-cbse-question-paper
Computer science-2010-cbse-question-paperComputer science-2010-cbse-question-paper
Computer science-2010-cbse-question-paper
 
C Prog - Pointers
C Prog - PointersC Prog - Pointers
C Prog - Pointers
 
662305 10
662305 10662305 10
662305 10
 
Ejercicios de programacion
Ejercicios de programacionEjercicios de programacion
Ejercicios de programacion
 
Computer_Practicals-file.doc.pdf
Computer_Practicals-file.doc.pdfComputer_Practicals-file.doc.pdf
Computer_Practicals-file.doc.pdf
 
Programa.eje
Programa.ejePrograma.eje
Programa.eje
 
Daapracticals 111105084852-phpapp02
Daapracticals 111105084852-phpapp02Daapracticals 111105084852-phpapp02
Daapracticals 111105084852-phpapp02
 
public class TrequeT extends AbstractListT { .pdf
  public class TrequeT extends AbstractListT {  .pdf  public class TrequeT extends AbstractListT {  .pdf
public class TrequeT extends AbstractListT { .pdf
 
Introduction to Groovy
Introduction to GroovyIntroduction to Groovy
Introduction to Groovy
 
Cpp c++ 1
Cpp c++ 1Cpp c++ 1
Cpp c++ 1
 
Cbse question-paper-computer-science-2009
Cbse question-paper-computer-science-2009Cbse question-paper-computer-science-2009
Cbse question-paper-computer-science-2009
 
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
 
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
 
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
 
Pnno
PnnoPnno
Pnno
 
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
 
Computer Science Practical Science C++ with SQL commands
Computer Science Practical Science C++ with SQL commandsComputer Science Practical Science C++ with SQL commands
Computer Science Practical Science C++ with SQL commands
 
basic programs in C++
basic programs in C++ basic programs in C++
basic programs in C++
 
DAA Lab File C Programs
DAA Lab File C ProgramsDAA Lab File C Programs
DAA Lab File C Programs
 

More from 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
 

More from Hitesh Wagle (20)

Zinkprinter
ZinkprinterZinkprinter
Zinkprinter
 
48695528 the-sulphur-system
48695528 the-sulphur-system48695528 the-sulphur-system
48695528 the-sulphur-system
 
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
 
Diode logic crkts
Diode logic crktsDiode logic crkts
Diode logic crkts
 
Applicationof datastructures
Applicationof datastructuresApplicationof datastructures
Applicationof datastructures
 
Oops index
Oops indexOops index
Oops index
 
Google search tips
Google search tipsGoogle search tips
Google search tips
 
Diode logic crkts
Diode logic crktsDiode logic crkts
Diode logic crkts
 
Computer
ComputerComputer
Computer
 
Applicationof datastructures
Applicationof datastructuresApplicationof datastructures
Applicationof datastructures
 
Green chem 2
Green chem 2Green chem 2
Green chem 2
 
Convergence tests
Convergence testsConvergence tests
Convergence tests
 
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
 
Switkes01200543268
Switkes01200543268Switkes01200543268
Switkes01200543268
 
Cryptoghraphy
CryptoghraphyCryptoghraphy
Cryptoghraphy
 
Quote i2 cns_cnr_25064966
Quote i2 cns_cnr_25064966Quote i2 cns_cnr_25064966
Quote i2 cns_cnr_25064966
 
Pointers
PointersPointers
Pointers
 
P1
P1P1
P1
 
Inheritance
InheritanceInheritance
Inheritance
 
Function notes 2
Function notes 2Function notes 2
Function notes 2
 

Recently uploaded

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...
TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...
TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...
TrustArc
 

Recently uploaded (20)

Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
Modernizing Legacy Systems Using Ballerina
Modernizing Legacy Systems Using BallerinaModernizing Legacy Systems Using Ballerina
Modernizing Legacy Systems Using Ballerina
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering Developers
 
Simplifying Mobile A11y Presentation.pptx
Simplifying Mobile A11y Presentation.pptxSimplifying Mobile A11y Presentation.pptx
Simplifying Mobile A11y Presentation.pptx
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
 
Decarbonising Commercial Real Estate: The Role of Operational Performance
Decarbonising Commercial Real Estate: The Role of Operational PerformanceDecarbonising Commercial Real Estate: The Role of Operational Performance
Decarbonising Commercial Real Estate: The Role of Operational Performance
 
WSO2 Micro Integrator for Enterprise Integration in a Decentralized, Microser...
WSO2 Micro Integrator for Enterprise Integration in a Decentralized, Microser...WSO2 Micro Integrator for Enterprise Integration in a Decentralized, Microser...
WSO2 Micro Integrator for Enterprise Integration in a Decentralized, Microser...
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
TEST BANK For Principles of Anatomy and Physiology, 16th Edition by Gerard J....
TEST BANK For Principles of Anatomy and Physiology, 16th Edition by Gerard J....TEST BANK For Principles of Anatomy and Physiology, 16th Edition by Gerard J....
TEST BANK For Principles of Anatomy and Physiology, 16th Edition by Gerard J....
 
TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...
TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...
TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...
 
Navigating Identity and Access Management in the Modern Enterprise
Navigating Identity and Access Management in the Modern EnterpriseNavigating Identity and Access Management in the Modern Enterprise
Navigating Identity and Access Management in the Modern Enterprise
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 

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