320 likes | 448 Views
Lecture# 15 Programming Concepts. Arrays. They are special kind of data type They are like data structures in which identical data types are stored In C++each array has: Name Data type Size They occupy continuous area of memory. Array Declaration.
E N D
Lecture# 15 Programming Concepts
Arrays • They are special kind of data type • They are like data structures in which identical data types are stored • In C++each array has: • Name • Data type • Size • They occupy continuous area of memory
Array Declaration • arrayType arrayName[numberOfElements ]; • For example : intage [ 10 ] ;
Array Declaration • arrayType arrayName[numberOfElements ]; • For example : intage [ 10 ] ; age[0] age[1] age[2] age[3] age[4] .72 age[5] .94 age[6] age[7] age[8] age[9]
Array Declaration • More than one array can be declared on a line: • int age [10] , height [10] , names [20] ; • Mix declaration of variables with: declaration of arrays • int i , j , age [10] ;
0 0 32 0 1 1 34 0 2 2 0 3 3 0 4 4 0 .72 .72 5 5 0 .94 .94 0 6 6 7 7 0 40 8 8 0 9 9 0 Initializing Array There are several ways to initialize an array: • int age[10]; age[0]=32;age[1]=34;age[7]=40; • int age [ 10 ] = { 0,0,0,0,0,0,0,0,0,0} ; • int age [ 10 ] = { 0 } ; • int age [ ] = {10,15,30,20,34} ;
Initializing Array Perhaps the most common way is the use of loops for initialization: • int c[10]; • for(int i=0;i<10;i++) • c[i]=0; • for(int i=0;i<10;i++) • cin>>c[i];
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; • } • getch(); • 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 [ 10 ] = a [ 10 ] ;
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]<<" "; • } • getch(); • return 0; • }
Problem Declare an array of ten integers.Input the numbers of ten students. Calculate average of these numbers.Output the average
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); • getch(); • return 0; • }
Compound Statements • Statement like c=c+3; can be abbreviated using addition assignment operator += as: c+=3; • In short an statement of type: • variable=variable operator expression;can be written as: • variable operator=expression; • Operators can be: +,-,%,*,/
#include<iomanip> • int main(){ • 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; • } • getch(); • return 0; • }
Search • 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
#include<conio> • #include<iostream> • int main(){ • const int arraySize=10; • int Record[arraySize]={19,3,15,11,9,13,5,17,11,5}; • cout<<"Enter the element you want to search:"; • int i,n,found=0; • 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"; • getch(); • return 0; • }
Character Arrays • Character array can be initialized as: char string[]=“first”; • This string contains 5 character and a special 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 • 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'}; • 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]; • getch(); • return 0; • }
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++; • } • getch(); • 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 on the location where spaces already exists.
#include<stdio.h> • 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; • getch(); • return 0; • }
Sorting • Arrangement of data in an array is called sorting • We can sort data in ascending or descending order • Lot of techniques used for sorting purpose • Technique we use for sorting in called “bubble sort”
Bubble Sort • Array: 4 3 5 2 1 • Ascending order: 1 2 3 4 5 • Descending order: 5 4 3 2 1
Bubble Sort • Array: 2 1 3 4 5 • Pass 1: 2 3 4 5 1 • Pass 2: 3 4 5 2 1 • Pass 3: 4 5 3 2 1 • Pass 4: 4 5 3 2 1
#include<conio> • #include<iostream> • int main(){ • const int Size=10; • int number[Size]; • cout<<"Enter 10 Numbers:"; • for(int i=0;i<Size;i++) • cin>>number[i]; • cout<<"\nYou Have Entered:"; • for(int i=0;i<Size;i++) • cout<<number[i]<<" ";
for(int i=0;i<Size-1;i++){ • for(int j=0;j<Size-1;j++){ • if(number[j]<number[j+1]){ • int temp=number[j]; • number[j]=number[j+1]; • number[j+1]=temp; • } • } • } • cout<<"\nSorted Aray: \n\n\n"; • for(int i=0;i<Size;i++) • cout<<number[i]<<" "; • getch(); • return 0; • }