You are on page 1of 44

Array Declaration

age[0]

• For example : age[1]

age[2]

int age [ 10 ] ; age[3]


age[4] .72
age[5] .94
Array Properties
age[6]
• Contiguous Area in Memory age[7]
• Homogenous Data age[8]

age[9]
Initializing Array
There are three ways to initialize an array:
• int age[3];
• age[0]=32;
• age[1]=34;
• age[2]=40;
• Declaration and Initialization
• int age [ 10 ] = { 0,0,0,0,0,0,0,0,0,0} ;
• int age [ 10 ] = { 0 } ;
• int age [ ] = {10,15,30,20,34} ;
Example# 1
• int main(){
• int age[5];
• for(int i=0;i<5;i++){
• cout<<"Please enter your age:";
• cin>>age[i];
• }
• for(int i=0;i<5;i++){
• cout<<"You Entered:"<<age[i]<<endl;
• }
• return 0;
• }
Copying Arrays
To copy from array “ a ” to array “ b ” :
•b[0]=a[0];
•b[1]=a[1];

•b[2]=a[2];
•b[3]=a[3];
• ……… ………
•b[5]=a[5];
Example
• int main(){
• int age[]={56,23,45,57,98};
• int c[5];
• for(int i=0;i<5;i++){
• c[i]=age[i];
• }
• cout<<"Elements of c are now:";
• for(int i=0;i<5;i++){
• cout<<c[i]<<" ";
• }
• return 0;
• }
Problem

Declare an array of size ten of int. Input the


marks of ten students. Calculate average of
these numbers. Output the average
• #include<iostream>
• using namespace std; Example
• int main(){
• const int ArraySize=10;
• int numbers[ArraySize];
• int add=0;
• for(int i=0;i<ArraySize;i++){
• cout<<"Plz Enter ur Number:";
• cin>>numbers[i];
• add+=numbers[i]; //Exactly Equal to add=add+numbers[i]
• }
• cout<<"\nAverage is:"<<(add/10);
• return 0;
• }
Compound Statements
• Statement like c=c+3; can be abbreviated as:
c+=3;
• In short a statement of type:
• variable=variable operator expression; can be
written as:
• variable operator=expression;
• Operators can be: +,-,%,*,/
• #include<iomanip>
• int main(){ Example
• const int arraySize=10;
• int n[arraySize]={19,3,15,11,9,13,5,17,11,5};
• cout<<"Elements"<<setw(13)<<"Value"<<setw(17)<<"
Histogram"<<endl;
• for(int i=0;i<arraySize;i++)
{ cout<<setw(7)<<i<<setw(13)<<n[i]<<setw(9);
• for(int j=0;j<n[i];j++)
• cout<<"*";
• cout<<endl;
• }
• return 0;
Searching the Array
• Important area in computer science
• The process of finding particular element
in an array is called searching
• The technique we use for searching and
element from the array is called linear
search
• The element to be searched is also
sometimes called key
Linear Search
-23 97 18 21 5 -86 64 0 -37

Linear search means looking at


each element of the array, in
turn, until you find the target
value.
Linear Search
-23 97 18 21 5 -86 64 0 -37

element
Searching for -86.
Linear Search
-23 97 18 21 5 -86 64 0 -37

element
Searching for -86.
Linear Search
-23 97 18 21 5 -86 64 0 -37

element
Searching for -86.
Linear Search
-23 97 18 21 5 -86 64 0 -37

element
Searching for -86.
Linear Search
-23 97 18 21 5 -86 64 0 -37

element
Searching for -86.
Linear Search
-23 97 18 21 5 -86 64 0 -37

element
Searching for -86: FOUND
Search Problems Variations
• Search questions can be posed in
many ways such as:
1. Search whether an element exists in
as array or not… Display Elements
exists otherwise Not Exists
2. Count frequency of an element if it
exists in an array
3. Search multiple elements


#include<iostream>
int main(){
Linear Search
• int Record[arraySize]={19,3,15,11,9,13,5,17,11,5};
• int i,n,found=0;
• cout<<"Enter the element you want to search:";
• cin>>n;
• for(i=0;i<arraySize;i++){
• if(n==Record[i]){
• found=1;
• break; }
• }
• if(found==1)
• cout<<"Element is found at location Record["<<i<<"]";
• else
• cout<<"Element not found in record";
• return 0;
Exercise
• Input 10 element integer array
from user
• Display smallest element of the
array
Character Arrays
Character Arrays
• char string1[5]={‘f’,’i’,’r’,’s’,’t’};
• char string1[]={‘f’,’i’,’r’,’s’,’t’};
• Character array can be initialized as:
char string[]=“first”;
• This string contains 5 character and a special
character NULL character
• This character is ‘\0’
Character Arrays
• This character is appended in the end of string by
compiler
• We can store character by character in this kind of
array like
• char string1[]={‘f’,’i’,’r’,’s’,’t’,’\0’};
• We can use cin for input directly for character array
• For output of string cout work and display char
array up to Null character


int main(){
char c[100],d[]={'T','h','i','s',' ','i','s','\0'};
(Example)
• cout<<"Plz Enter c string:";
• cin>>c;
• cout<<"\nd is: "<<d;
• cout<<"\nc is: "<<c;
• cout<<"\nc from Loop: ";
• for(int i=0;c[i]!='\0';i++){
• cout<<c[i];
• }
• cout<<"\nd from Loop: ";
• for(int i=0;d[i]!='\0';i++)
• cout<<d[i];
• return 0;
(Example)
• int main(){
• char c[]="Two roads diverged\n in a yellow wood,";
• cout<<c;
• int i=0;
• while(c[i]!='\0'){
• if(c[i]=='d')
• c[i]='e';
• i++;
• }
• return 0;
• }
Problem
• Declare an array of characters. The
size of the array is 100.Input a string
from the user and return it with more
spaces (tab Spaces) on the location
where space already exists.
• #include<stdio.h>
Problem
• int main(){
• char a[100];
• cout<<"Plz Enter A String:";
• gets(a);
• cout<<"\nThe text with more spaces:\n";
• for(int i=0;a[i]!='\0';i++){
• if(a[i]==' ')
• a[i]='\t';
• }
• cout<<a;
• return 0;
• }
Exercise
• Input your name and display it in
reverse order
• Count the actual elements of any
character array


#include<iostream>
#include<conio>
Exercise
• #include<stdio>
• void main(){
• char a[100];
• gets(a);
• int count=0;
• for(int i=0;a[i]!='\0';i++,count++);
• for(int i=count-1;i>=0;i--)
• cout<<a[i];
• return 0;
• }
Passing Arrays To
Functions
Passing Arrays to Functions

• We should mention the size of the array


when pass to the function
• Arrays are passed by reference
• Only Name of Array is passed to
function
• int MyArray[5]
MyArray
• #include<iostream>
• void f(int[],int,int);
• int main(){ Passing Array To Function
• int number[10]={0},a=15;
• f(number,10,a);
• for(int i=0;i<10;i++)
• cout<<number[i];
• cout<<a;
• return 0;
• }

• void f(int x[],int arraySize,int a){


• a=100;
• for(int i=0;i<arraySize;i++)
• x[i]=i;
Passing Element of Array To Function
• #include<conio>
• #include<iostream>
• void check(int);
• int main(){
• int a[5]={5,4,6,7,9};
• cout<<"Third element is::"<<a[2];
• check(a[2]);
• cout<<"\nThird element after call:"<<a[2];
• getch();
• return 0;
• }
• void check(int x){
• x=35;
Question Explaination
Assignment
• #include<iostream>
• void menu();
• void input(int[]);
• void output(int[]);
• void large(int[]);
• void small(int[]);
• void sortA(int[]);
• void sortD(int[]);
• void search(int[]);
• const int Size=10;
• int main(){
• int main(){
• int x[Size],choice;
• while(1){
• clrscr();
• menu();
• cin>>choice;
• switch(choice){
• case 1: input(x);break;
• case 2: output(x);getch();break;
• ……..
• case 8: return 0;break;
• default: cout<<"\n\n\t\tYou have Entered Wrong Choice";
• }
• }
• }

You might also like