Chapter 5 Array & Strings PDF
Chapter 5 Array & Strings PDF
Chapter # 05
Array:
An array is collection of same type of elements stored in contiguous memory locations represented
by single variable name.
Each call represents an element of the array.
The number within the square brackets in array is called index.
Index is used to access specific element of the array.
It is always an integer value.
First element of the array always has index 0.
The last index of an array is one less than the size of the array.
It is always an integer value.
Types: It has two types.
o One Dimensional Array (List)
o Two Dimensional Array (Matrix or Table)
Example: marks (variable name)
45
67
50
79
58
36
Advantages of Array:
It is used to represent multiple values of same type by single name.
Values are stored in contiguous memory location.
It reduces the program size.
It provides easy way to handle list, table or matrix.
It makes computer programing task simple and easy.
Question#2:
#include <iostream>
using namespace std;
#include <conio.h>
int main()
{
int n[10],i;
for(i=1;i<=10;i++)
{
cout<<"Enter a number:";
cin>>n[i];
}
cout<<"Numbers in Reverse order:"<<endl;
for(i=10;i>=1;i--)
{
cout<<n[i]<<" ";
}
getche();
}
Question#3:
#include <iostream>
using namespace std;
#include <conio.h>
int main()
{
int n[10],min,index;
for(int i=1;i<=10;i++)
{
cout<<"Enter a number:";
cin>>n[i];
}
min=n[1];
index=1;
for(int i=1;i<=10;i++)
{
if(n[i]<min)
{
min=n[i];
index=i;
}
}
cout<<"Smallest Value="<<min<<endl;
Chapter 5: Arrays & Strings Grade XII
Lecturer Qurratulain
cout<<"Index="<<index<<endl;
getche();
}
Question#4:
#include <iostream>
using namespace std;
#include <conio.h>
int main()
{
int i,counter=0,
arr[15]={4,8,5,1,3,5,0,12,5,7,3,15,8,4,11};
for(i=0;i<15;i++)
{
if(arr[i]==5)
counter=counter+1;
}
cout<<"Number 5 appears "<<counter<<" times.";
getche();
}
Question#5:
#include <iostream>
using namespace std;
#include <conio.h>
int main()
{
int i,j,sum=0,a[3][2]={{6,3},{7,8},{4,5}};
for(i=0;i<3;i++)
{
for(j=0;j<2;j++)
{
cout<<a[i][j]<<" ";
sum=sum+a[i][j];
}
cout<<endl;
}
cout<<"Sum="<<sum;
getche();
}
Question#6:
#include <iostream>
using namespace std;
Chapter 5: Arrays & Strings Grade XII
Lecturer Qurratulain
#include <conio.h>
int main()
{
Int i,j,sum=0,
arr[3][4]={{4,18,-16,11},{-5,10,-2,12},{15,-3,17,18}};
for(i=0;i<3;i++)
for(j=0;j<4;j++)
{
if(arr[i][j]>0)
sum=sum+arr[i][j];
}
cout<<"Sum of positive numbers is "<<sum;
getche();
}
Question#7:
#include <iostream>
using namespace std;
#include <conio.h>
int main()
{
int i,j,a[2][4]={{14,8,11,10},{15,12,20,3}};
int b[2][4]={{2,3,4,7},{6,7,8,9}};
int sum[2][4];
for(i=0;i<2;i++)
{
for(j=0;j<4;j++)
sum[i][j]=a[i][j]+b[i][j];
}
for(i=0;i<2;i++)
{
for(j=0;j<4;j++)
cout<<sum[i][j]<<" ";
cout<<endl;
}
getche();
}
Question#8:
#include <iostream>
using namespace std;
#include <conio.h>
int main()
Chapter 5: Arrays & Strings Grade XII
Lecturer Qurratulain
{
int i,j,r,c,sum=0,a[10][10];
String Functions:
String functions used string.h header file.
cin.get() Function:
It is an input function to get data from keyboard.
It reads a string that may contain blank spaces.
Syntax: cin.get(stringname,stringsize);
Example: cin.get(name,20);
strcpy() Function:
It copies contents of a string variable or string constant to another string variable.
Syntax: strcpy(string2, string1);
Example: char str1[10]=”Hello”,str2[10],str3[10];
strcpy(str2, str1);
strcpy(str3, “World”);
strcat() Function:
It is used for concatenation or joining of two strings.
Syntax: strcat(string1, string2);
Example: char str1[10]= “HOME”,str2[10] = ”WORK”;
strcat(string1, string2);
strlen() Function:
It is used to return the length (number of characters) of a string.
Syntax: strlen(string);
Example: char string[10] = “COMPUTER”;
strlen(string);
strcmp() Function:
It compares two strings and return an integer value.
The comparison is based on ASCII codes of characters.
Syntax: strcmp(string1,string2);
Chapter 5: Arrays & Strings Grade XII
Lecturer Qurratulain
o Return 0, if both strings are equal.
o Return 1, if first string is greater than second.
o Return -1, if first string is less than second.
getche();
}
Question#10: if((strlen(string1)<strlen(string2))&&(strlen(string
#include <string.h> 1)<strlen(string3)))
#include <iostream> cout<<string1<<" is smallest.";
using namespace std;
#include <conio.h> else
int main() if((strlen(string2)<strlen(string1))&&(strlen(string
{ 2)<strlen(string3)))
char string1[20],string2[20]; cout<<string2<<" is smallest.";
cout<<"Enter a string1:"<<endl;
cin>>string1; else
cout<<"Enter a string2:"<<endl; cout<<string3<<"is smallest.";
cin>>string2;
cout<<"Length of string1:" getche();
<<strlen(string1)<<endl; }
cout<<"Length of string2:"
<<strlen(string2)<<endl;
strcat(string1,string2);
cout<<"You typed:"<<string1<<endl;
getche();
}