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

More Related Content

What's hot (20)

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

Viewers also liked (8)

PPT
Arrays Class presentation
Neveen Reda
 
PDF
Lecture17 arrays.ppt
eShikshak
 
PPSX
C Programming : Arrays
Gagan Deep
 
PPT
Array Presentation (EngineerBaBu.com)
EngineerBabu
 
PPTX
Arrays In C++
Awais Alam
 
PPTX
Array in C
Kamal Acharya
 
PPT
Arrays
archikabhatia
 
PPTX
Array in c language
home
 
Arrays Class presentation
Neveen Reda
 
Lecture17 arrays.ppt
eShikshak
 
C Programming : Arrays
Gagan Deep
 
Array Presentation (EngineerBaBu.com)
EngineerBabu
 
Arrays In C++
Awais Alam
 
Array in C
Kamal Acharya
 
Array in c language
home
 
Ad

Similar to Array notes (20)

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

More from Hitesh Wagle (20)

ODP
Zinkprinter
Hitesh Wagle
 
PPT
48695528 the-sulphur-system
Hitesh Wagle
 
PDF
Fundamentals of data structures ellis horowitz & sartaj sahni
Hitesh Wagle
 
PDF
Diode logic crkts
Hitesh Wagle
 
PPT
Applicationof datastructures
Hitesh Wagle
 
PDF
Oops index
Hitesh Wagle
 
DOC
Google search tips
Hitesh Wagle
 
PDF
Diode logic crkts
Hitesh Wagle
 
PDF
Computer
Hitesh Wagle
 
PPT
Applicationof datastructures
Hitesh Wagle
 
PDF
Green chem 2
Hitesh Wagle
 
PDF
Convergence tests
Hitesh Wagle
 
PDF
Lecture notes on infinite sequences and series
Hitesh Wagle
 
PDF
Switkes01200543268
Hitesh Wagle
 
PDF
Cryptoghraphy
Hitesh Wagle
 
PDF
Quote i2 cns_cnr_25064966
Hitesh Wagle
 
PDF
Pointers
Hitesh Wagle
 
PDF
Notes
Hitesh Wagle
 
DOC
Inheritance
Hitesh Wagle
 
Zinkprinter
Hitesh Wagle
 
48695528 the-sulphur-system
Hitesh Wagle
 
Fundamentals of data structures ellis horowitz & sartaj sahni
Hitesh Wagle
 
Diode logic crkts
Hitesh Wagle
 
Applicationof datastructures
Hitesh Wagle
 
Oops index
Hitesh Wagle
 
Google search tips
Hitesh Wagle
 
Diode logic crkts
Hitesh Wagle
 
Computer
Hitesh Wagle
 
Applicationof datastructures
Hitesh Wagle
 
Green chem 2
Hitesh Wagle
 
Convergence tests
Hitesh Wagle
 
Lecture notes on infinite sequences and series
Hitesh Wagle
 
Switkes01200543268
Hitesh Wagle
 
Cryptoghraphy
Hitesh Wagle
 
Quote i2 cns_cnr_25064966
Hitesh Wagle
 
Pointers
Hitesh Wagle
 
Inheritance
Hitesh Wagle
 

Recently uploaded (20)

PDF
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
PDF
The Rise of AI and IoT in Mobile App Tech.pdf
IMG Global Infotech
 
PPTX
Seamless Tech Experiences Showcasing Cross-Platform App Design.pptx
presentifyai
 
PPTX
COMPARISON OF RASTER ANALYSIS TOOLS OF QGIS AND ARCGIS
Sharanya Sarkar
 
PPTX
"Autonomy of LLM Agents: Current State and Future Prospects", Oles` Petriv
Fwdays
 
PDF
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
PDF
Go Concurrency Real-World Patterns, Pitfalls, and Playground Battles.pdf
Emily Achieng
 
PPTX
The Project Compass - GDG on Campus MSIT
dscmsitkol
 
PDF
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
PDF
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
PDF
The 2025 InfraRed Report - Redpoint Ventures
Razin Mustafiz
 
PPTX
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
PDF
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 
PDF
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
PDF
Mastering Financial Management in Direct Selling
Epixel MLM Software
 
PDF
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 
PDF
CIFDAQ Market Wrap for the week of 4th July 2025
CIFDAQ
 
PDF
How do you fast track Agentic automation use cases discovery?
DianaGray10
 
PDF
"AI Transformation: Directions and Challenges", Pavlo Shaternik
Fwdays
 
PPTX
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
The Rise of AI and IoT in Mobile App Tech.pdf
IMG Global Infotech
 
Seamless Tech Experiences Showcasing Cross-Platform App Design.pptx
presentifyai
 
COMPARISON OF RASTER ANALYSIS TOOLS OF QGIS AND ARCGIS
Sharanya Sarkar
 
"Autonomy of LLM Agents: Current State and Future Prospects", Oles` Petriv
Fwdays
 
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
Go Concurrency Real-World Patterns, Pitfalls, and Playground Battles.pdf
Emily Achieng
 
The Project Compass - GDG on Campus MSIT
dscmsitkol
 
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
The 2025 InfraRed Report - Redpoint Ventures
Razin Mustafiz
 
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
Mastering Financial Management in Direct Selling
Epixel MLM Software
 
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 
CIFDAQ Market Wrap for the week of 4th July 2025
CIFDAQ
 
How do you fast track Agentic automation use cases discovery?
DianaGray10
 
"AI Transformation: Directions and Challenges", Pavlo Shaternik
Fwdays
 
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 

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