Upload
ksi12345
View
215
Download
0
Embed Size (px)
Citation preview
8/14/2019 Search Number
1/17
Arrays
An array is a collection of several data items of the same data type. It consists of
several contiguous memory locations storing data. It removes the cumbersome task
of defining separate variables for each data item. A single variable can be used to
store one or more data items. For example, if you want to store ages of 10 students
of a class, you can easily declare an array of integer type of size 10 instead of
declaring 10 variables of integer type each storing age of a student. The general
form of the single dimension array is:-
General format:
type variable_name [size];
The type is the base type of the array and which is the type of each of the element
of the array. The size is the no of the elements in the array. The variable_name is
the name of the identifier holding the array. For example,
int age [10];
The age is an array of type integer whose size is 10. It can hold 10 data values. The specific
element in the array can be accessed using an index. An index is the offset from the first element
of the array. For example, first element has an index of 0 while the third element has an index of
2. The integer age has elements from age[0] to age[9]. Here is a program which illustrates the
working of arrays.
#include
void main ()
{
int age[10];
int i,sum=0, avg=0;
int max,min;
Programming II C++- Prepared by Syed Ibrahim Page 1
8/14/2019 Search Number
2/17
for(i=0;i
8/14/2019 Search Number
3/17
cout
8/14/2019 Search Number
4/17
will initialize all the elements of the array age to be zero. If you want some of the
elements to be initialized with specific value and rest of the values to be zero then
here is an example.
int age[5]={12,13};
The elements age[0] and age[1] will be initialized to 12 and 13 respectively and rest
of the elements will be initialized to zero.
Null Terminated Strings
The most common use of one dimensional array is for character strings. The null
terminated string is a character array with a null character at the end. The
characters form the string with a null character indicating the termination of the
string. A null terminated string can be declared as
char name[10];
it will hold 10 characters with a null at the end. The size of the array can be more
than the length of the array. The array can be initialized as
char name[10]={'f','a','t','h','m',a};
The first 6 elements are initialized and rest elements are null characters. Here is a
program which calculates the length of the string.
// program which calculates the length of the string
#include
void main()
{
char name[15];
int i=0;
cout name;
Programming II C++- Prepared by Syed Ibrahim Page 4
http://en.wikipedia.org/wiki/Null_characterhttp://en.wikipedia.org/wiki/Null_character8/14/2019 Search Number
5/17
while(name[i]!='\0')
{
i++;
}
cout
8/14/2019 Search Number
6/17
int flag = 0; // set flag to off
for(int i=0; i
8/14/2019 Search Number
7/17
Introduction to Two-Dimensional
Arrays
Objectives:
To be able to declare a two-dimensional array.
To be able to perform fundamental operations on a two-dimensionalarray.
To be able to view a two-dimensional array as an array of arrays.
Two-dimensional arrays, the most common multidimensional arrays, are
used to store information that we normally represent in table form. Two-
dimensional arrays, like one-dimensional arrays, are homogeneous. This
means that all of the data in a two-dimensional array is of the same type.
Examples of applications involving two-dimensional arrays include:
a seating plan for a room (organized by rows and columns), a monthly budget (organized by category and month), and a grade book where rows might correspond to individual students and
columns to student scores.
The two dimensional array can be declared as
int age[2][5]; this array has two rows and 5 columns.
Two-Dimensional Array Initialization
We can declare and initialize an array A as follows:
//declaration
Programming II C++- Prepared by Syed Ibrahim Page 7
8/14/2019 Search Number
8/17
int A[3][4] = {{8, 2, 6, 5}, //row 0
{6, 3, 1 ,0}, //row 1
{8, 7, 9, 6}}; //row 2
Memory for the array may be visualized as:
Example-I
#include
void main ()
{
int age[2][5]= { {12,13,14,15,15}, { 12,16,17,13,12}};
int i,j;
int sum=0,avg=0; for(i=0;i
8/14/2019 Search Number
9/17
}
}
The result of the program is: -
Example-II
#include
#include
main()
{
clrscr();
char week[7][9] =
{Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday};
clrscr();
cout
8/14/2019 Search Number
10/17
No. of Rows =7 (0 -6) No.of.Columns = 9(0-8)
0 1 2 3 4 5 6 7 8
Example-III
#include
int main()
{
int age [] = {16, 2, 77, 40, 12};
char name[5][5]={"jane","fred","mary","henry","john"};
int i;
int total_age = 0, average_age = 0;
for ( i=0 ; i
8/14/2019 Search Number
11/17
average_age = total_age/5;
cout
8/14/2019 Search Number
12/17
char yourname[10] ;
cout>yourname;
flag = NOTFOUND ;
for ( i = 0 ; i
8/14/2019 Search Number
13/17
#include
#include
main()
{
clrscr();
int temp[3][4] = {{26, 34, 22, 17},{24, 32, 19, 13},{28, 38, 25, 20}};
int highest = 0;
for (int i = 0; i < 3; ++i)
for (int j = 0; j < 4; ++j)
{
if (temp[i][j] > highest)
highest = temp[i][j];
}
cout
8/14/2019 Search Number
14/17
7 8 9
#include
int main()
{
int array[ 3 ][ 3 ] = { { 1, 2, 3 },{ 4, 5, 6 } ,{ 7, 8, 9 } };
cout
8/14/2019 Search Number
15/17
cout
8/14/2019 Search Number
16/17
void main()
{
clrscr();
int pos,i,j;
j=0;
char s[20],s1[20];
couts;
pos=strlen(s);
for(i=pos-1;i>=0;i--)
{
s1[j]=s[i];
j+=1;
}
s1[j]='\0';
cout
8/14/2019 Search Number
17/17
Programming II C++- Prepared by Syed Ibrahim Page 17