Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

Grade XII

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.

Declaring One Dimensional Array:


 It means specifying the data_type and size of an array.
 It allows compiler to decide how many locations should be reserve in memory.
 It has two dimensions i.e. vertical represent rows and horizontal represent column.
 Syntax: data_type array_name[array_size];
 Example: int marks[6];
float weight[8];

Initialization of One Dimensional Array:


 It means assigning values to the elements of list.
 An array can be initialized in declaration statement.
 When initializing and array it is not necessary to mention an array size as compiler can find iout by
counting values in the bracket.
 Syntax: data_type array_name[array_size]={values};
Chapter 5: Arrays & Strings Grade XII
Lecturer Qurratulain
 Example: int marks[6]={12,14,13,16,18,19};
Declaring Two Dimensional Array:
 It means specifying the data_type and size of row and column in an array.
 It allows compiler to decide how many locations should be reserve in memory.
 Syntax: data_type array_name[row_size][column_size];
 Example: int marks[3][4];
float weight[6][4];

Initialization of Two Dimensional Array:


 It means assigning values to the elements of table.
 An array can be initialized in declaration statement.
 Syntax: data_type array_name[row_size][column_size]={values};
 Example: int marks[2][3]={{12,14,13},{16,18,19}};

Accessing and Filling Two Dimensional Array:


 Data can be accessed in table by specifying the index of row and column.
 Syntax: array_name[row][column]=value;
 Example: arr[3][4]=15;

The sizeof() Function:


 It provides the number of bytes occupied to store values for data type or variable within the
parenthesis.
 It is used to determine the amount of storage reserved for int, float, char etc.
 Syntax: sizeof(data_type/variable);
 Example: sizeof(int), sizeof(arr), size(a)

Difference between One and Two Dimensional Array:


One Dimensional Array Two Dimensional Array
It store element in the form of list. It store element in the form of table.
Elements can be accessed by specifying single Elements can be accessed by specifying row and column
index along with array name. index along with array name.
It is also known as list or linear array. It is also known as table or matrix.
Syntax:data_type array_name[array_size]; Syntax: data_type array_name[row_size][column_size];

Chapter 5: Arrays & Strings Grade XII


Lecturer Qurratulain
Programs in Chapter
Program # 3:
Program # 1: #include <iostream>
#include <iostream> using namespace std;
using namespace std; #include <conio.h>
#include <conio.h> int main()
int main() {
{ int i, n, marks[50],p=0;
int n[6], sum=0; cout<<"Enter no of students(max 50):";
float avg; cin>>n;
for(int i=1;i<=6;i++) for(i=1;i<=n;i++)
{ {
cout<<"Enter a number:"; cout<<"Enter your marks:";
cin >>n[i]; cin>>marks[i];
sum=sum+n[i]; if(marks[i]>=33)
} p=p+1;
avg=sum/6; }
cout<<"Sum="<<sum<<endl; cout<<"No of passed students:"<<p;
cout<<"Average="<<avg; getche();
getche(); }
}
Program # 4:
Program # 2: #include <iostream>
#include <iostream> using namespace std;
using namespace std; #include <conio.h>
#include <conio.h> int main()
int main() {
{ cout<<"Data_Type "<<"Bytes"<<endl;
int i,n[10],max; cout<<"int="<<sizeof(int)<<endl
for(i=1;i<=10;i++) <<"float="<<sizeof(float)<<endl
{ <<"double="<<sizeof(double)<<endl
cout<<"Enter a number:"; <<"char="<<sizeof(char)<<endl;
cin >>n[i]; getche();
} }
max=n[1];
for(i=1;i<=10;i++) Program # 5:
{ #include <iostream>
if(n[i]>max) using namespace std;
max=n[i]; #include <conio.h>
} int main()
cout<<"Biggest number="<<max; {
getche(); int i,j, sum=0;
} int n[3][4]={{30,20,55,206},
{78,81,25,90},{3,48,67,104}};
Chapter 5: Arrays & Strings Grade XII
Lecturer Qurratulain
for(i=0;i<3;i++)
{
for(j=0;j<4;j++) Program # 7:
{ #include <iostream>
cout<<"Enter a number:"; using namespace std;
cin >>n[i][j]; #include <conio.h>
sum=sum+n[i][j]; int main()
} {
} int n[3][4],i,j,max;
cout<<"Sum="<<sum<<endl; for(i=1;i<=3;i++)
getche(); {
} for(j=1;j<=4;j++)
Program # 6: {
#include <iomanip.h> cout<<"Enter a number:"<<endl;
#include <iostream> cin>>n[i][j];
using namespace std; }
#include <conio.h> }
int main() max=n[1][1];
{ for(i=1;i<=3;i++)
int i,j,k[3][4]={{1,2,3,4},{5,6,7,8},{9,10,11,12}}; {
for(i=0;i<3;i++) for(j=1;j<=4;j++)
{ {
for(j=0;j<4;j++) if(n[i][j]>max)
cout<<setw(5)<<k[i][j]*2; max=n[i][j];
cout<<endl; }
} }
getche(); cout<<"Biggest Number:"<<max;
} getche();
}

Chapter 5: Arrays & Strings Grade XII


Lecturer Qurratulain
Programs in Exercise

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];

cout<<"Enter no of rows(max 10):"<<endl;


cin>>r;
cout<<"Enter no of columns(max 10):"<<endl;
cin>>c;
for(i=0;i<r;i++)
for(j=0;j<c;j++)
{
cout<<"Enter a no:";
cin>>a[i][j];
}
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
Sum=sum+a[i][j];
cout<<"Sum of row "<<(i+1)<<" ="<< sum<<endl;
sum=0;
}
getche();
}

Chapter 5: Arrays & Strings Grade XII


Lecturer Qurratulain
Strings
String:
 It is a sequence of characters enclosed in double quotes.
 It may consist of alphabetic characters, digits and special symbols.
 It is stored in a one dimensional array of char data type. Each element of character string
holds one character.
 All strings end with a special character, known as null character and it is represented by
‘\0’. The null character is automatically appended at the end of string.
 Syntax: char stringname[stringsize];
 Example: char weekday[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.

Difference between cin Statement and cin.get Function:


cin Statement cin.get() Function
It is an input statement to get data from keyboard. It is an input function to get data from keyboard.
It reads a string, character, integer etc. It reads only string that may contain blank spaces.
It considers blank space in string as terminating It considers blank space in string as normal character.
character.
It reads more than one variable in single statement. It reads only one variable in single statement.
Header File: iostream.h Header File: string.h
Syntax: cin>>variable; Syntax: cin.get(stringname,stringsize);

Difference between strcpy() and strcmp() Function:


strcpy() Function strcmp() Function
It copies contents of a string variable or string It compares two strings and return an integer value
constant to another string variable. based on the result of comparison.
Syntax: strcpy(string2, string1); Syntax: strcmp(string1, string2);
Example Example

Difference between strlen() and strcat() Function:


strlen()Function strcat() Function
It is used to return the length (number of It is used for concatenation or joining of two strings.
characters) of a string.

Syntax: strlen(string); Syntax: strcat(string1, string2);


Example Example

Chapter 5: Arrays & Strings Grade XII


Lecturer Qurratulain
Programs in Chapter
Program # 1: Program # 4:
#include <string.h> #include <string.h>
#include <iostream> #include <iostream>
using namespace std; using namespace std;
#include <conio.h> #include <conio.h>
int main() int main()
{ {
char str[50]; char string1[10],string2[10];
cout<<"Enter a string:"<<endl; strcpy(string1,"HOME");
cin>>str; strcpy(string2,"WORK");
cout<<"Your typed:"<<str<<endl; strcat(string1,string2);
getche(); cout<<"String1="<<string1<<endl;
} cout<<"String2="<<string2<<endl;
getche();
Program # 2: }
#include <string.h>
#include <iostream> Program # 5:
using namespace std; #include <string.h>
#include <conio.h> #include <iostream>
int main() using namespace std;
{ #include <conio.h>
char str[50]; int main()
cout<<"Enter a string:"<<endl; {
cin.get(str,50); char city1[10]="LAHORE",
cout<<"Your typed:"<<str<<endl; city2[10]="ISLAMABAD",
getche(); city3[10]="KARACHI";
} cout<<"Characters in CITY1:"
<<strlen(city1)<<endl;
Program # 3: cout<<"Characters in CITY2:"
#include <string.h> <<strlen(city2)<<endl;
#include <iostream> cout<<"Characters in CITY3:"
using namespace std; <<strlen(city3)<<endl;
#include <conio.h>
int main() getche();
{ }
char string1[10]="ISLAMABAD",
string2[10],string3[10];
strcpy(string2,string1);
cout<<"String2="<<string2<<endl;
strcpy(string3,"PAKISTAN");
cout<<"String3="<<string3<<endl;
getche();
}
Chapter 5: Arrays & Strings Grade XII
Lecturer Qurratulain
Program # 6:
#include <string.h>
#include <iostream>
using namespace std;
#include <conio.h>
int main()
{
char string1[10]="MANGO",string2[10]="MANGO",
string3[10]="POTATO",string4[10]="ORANGE";
int x,y,z;
x=strcmp(string1,string2);
y=strcmp(string3,string1);
z=strcmp(string1,string4);
cout<<"String 1 is equal to string1.So x="<<x<<endl;
cout<<"String 3 is greater than string1.So y="<<y<<endl;
cout<<"String 1 is less than string4.So z="<<z<<endl;

getche();
}

Chapter 5: Arrays & Strings Grade XII


Lecturer Qurratulain
Programs in Exercise
Question#9:
#include <string.h> Question#10:
#include <iostream> # include <string.h>
using namespace std; #include <iostream>
#include <conio.h> using namespace std;
int main() #include <conio.h>
{ int main()
char str1[20],str2[20]; {
cout<<"Enter string:"<<endl; char string1[20], string2[20], string3[20];
cin.get(str1,20); cout<<"Enter FIRST string:"<<endl;
strcpy(str2,str1); cin >> string1;
cout<<"String 1:"<<str1<<endl; cout<<"Enter SECOND string:"<<endl;
cout<<"String 2:"<<str2<<endl; cin>>string2;
getche(); cout<<"Enter THIRD string:"<<endl;
} cin>>string3;

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();
}

Chapter 5: Arrays & Strings Grade XII


Lecturer Qurratulain

You might also like