Professional Documents
Culture Documents
age[0]
age[2]
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
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