You are on page 1of 15

ISLAMIC REPUBLIC OF AFGHANISTAN

MINISTRY OF HIGHER EDUCATION


GHALIB UNIVERSITY
COMPUTER SCEINE FACULTY
2TH SEMESTER
Advance Programming Structure
LECTURER : Zabihullah Rahmani

1 Ghalib University
Table of Contents
1. String variables
2. Multi-Dimensional Arrays

2 Ghalib University
String variables
 String variables are declared in the same way as character type

variables are declared.

 In fact a string is an array of character type.

 The length of the string is the total number of elements of the array.

 The general syntax to declare a string type variable is:

 char variable_name[n]

 where

 variable_name It specifies the name of the character type variable.


 n It specifies the total number of characters of the string
variable.
 For example str[5],name[15],city[10]

3 Ghalib University
Cont.
 The last character of every string variable is a null character.

 The null character is represented by ‘\0’.Thus when the information is

entered into a string variable, the last character of the variable is always
null character(‘\0’).Thus if a string variable is declare 5 characters, it
can store only 4 characters and the fifth character is the null character.

 Initializing String Variables

 The data in the string variables can be assigned at the time of

declaration.

 For example:
 char myword[] = { 'H', 'e', 'l', 'l', 'o', '\0' };
 char myword[] = "Hello";

4 Ghalib University
Example 1
Write a program in C++ to initialize a string with the word “Afghanistan” and then print it
on screen.
#include <iostream>
using namespace std;
int main()
{
char str[12]="Afghanistan";

cout<<str<<endl;

return 0;
}

5 Ghalib University
Example 2
Write a program to copy one string to another string. Input the string into the first string
variable and then copy this string to the second string variable by copying characters one
by one.
#include<iostream>

using namespace std;

int main()

{ char str1[15],str2[15];

cout<<"Enter any string?";

cin>>str1;

int i=0;

while(str1[i]!='\0'){

str2[i]=str1[i];

i=i+1;

} str2[i]='\0';

cout<<str2;

return 0;

6 Ghalib University
Multi-Dimensional Arrays
 A two-dimensional array is an array of more than one array.

 For example, an array of two one-dimensional arrays is called two-

dimensional array. It is also known as table or matrix.

 A two-dimensional array consists of columns and rows.

 Each element of the two-dimensional array is referenced by its index


values or subscripts.

 The index value of a two-dimensional array consists of two subscripts.

one subscript represents the row number and the second subscript
represents the column number.

7 Ghalib University
Declaration of Two Dimensional Arrays
 A two-dimensional array is declared by giving two indexed values

enclosed in square brackets.

 The first indexed value represents the total number of rows and the

second represents the total number of columns.


 The syntax to declare a two-dimensional array is:
 type array_name [r][c] ;
 where
 type represents the data type of array
 array_name represents the name of the two-dimensional array.
 r represents the total number of rows of table
 c represents the total number of columns of the table

8 Ghalib University
Cont.
 For example
 int marks[3][2]

Column 0 Column 1
Row 0 marks[0][0] marks[0][1]
Row 1 marks[1][0] marks[1][1]
Row 2 marks[2][0] marks[2][1]

9 Ghalib University
Accessing Data in two-Dimensional Arrays
 Data is entered into individual elements of a two-dimensional array.

 To enter data, the element is referenced by its index or subscript value.

 Similarly, data is retrieved from an array from individual elements of the

array.

 Usually, nested loops are used to access elements of the two-

dimensional array.

10 Ghalib University
Example 1
Write a program to print out data from the elements of a table.
#include<iostream>

using namespace std;

int main()

int abc[3][2];

cout<<"Input data into table "<<endl;

for(int row=0;row<3;row++){

for(int col=0;col<2;col++){

cout<<"Enter value in row = "<<row<<" Column = "<<col<<" ";

cin>>abc[row][col];

}}cout<<"Printing data from table "<<endl;

for(int row=0;row<3;row++){

for(int col=0;col<2;col++){

cout<<abc[row][col]<<"\t";

}cout<<endl;

} return 0;}

11 Ghalib University
Example 2
Write a program to input integer type data into a table having 2 rows and 2 columns. Find
out the maximum number entered in the table and print it out on the screen.
#include<iostream>

using namespace std;

int main()

{ int abc[2][2], m;

cout<<"Input data into table "<<endl;

for(int row=0;row<2;row++){

for(int col=0;col<2;col++){

cout<<"Enter value in row = "<<row<<" Column = "<<col<<" ";

cin>>abc[row][col];

}} m=abc[0][0];

for(int row=0;row<2;row++){

for(int col=0;col<2;col++){

if(m<abc[row][col])

m=abc[row][col];

}}cout<<"The maximum value is = "<<m<<endl;

return 0;}
12 Ghalib University By Zabihullah Rahmani
Initializing Tables
 Assigning values to the elements of a table at the time of its declaration is called

initializing of the table.

 For example. to assign values to a table abc[2][3] that has two rows and three

columns, values are assigned row-wise.

 The declaration and initialization in the above table will be:

 int abc[2][3]={{16,32,2},{10,12,16}};

13 Ghalib University
Initializing Character Type Tables
 The values in a table of “char” type is also assigned in the same way as in int, float or

double type tables.


 char st[4][4]={{'a','x','y'},
{'p','e','t'},
{'p','a','k'},
{'3','1','6'}};

14 Ghalib University
Any question…?????????????

15 Ghalib University

You might also like