Professional Documents
Culture Documents
Slide 2 PDF
Slide 2 PDF
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
The length of the string is the total number of elements of the array.
char variable_name[n]
where
3 Ghalib University
Cont.
The last character of every string variable is a null character.
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.
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>
int main()
{ char str1[15],str2[15];
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.
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
The first indexed value represents the total number of rows and the
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.
array.
dimensional array.
10 Ghalib University
Example 1
Write a program to print out data from the elements of a table.
#include<iostream>
int main()
int abc[3][2];
for(int row=0;row<3;row++){
for(int col=0;col<2;col++){
cin>>abc[row][col];
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>
int main()
{ int abc[2][2], m;
for(int row=0;row<2;row++){
for(int col=0;col<2;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];
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
For example. to assign values to a table abc[2][3] that has two rows and three
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
14 Ghalib University
Any question…?????????????
15 Ghalib University