C o n f I d e n t I a l

Course : MCA
Semester : II
Subject Code : MC 0066A
Subject Name : OOPS Using C++
Unit Number : 3
Unit Title : Arrays and Strings in C++
Lecture Number : 1
Lecture Title : Introduction & Arrays
HOME NEXT
C o n f I d e n t I a l
Introduction & Arrays
Objectives:
- To understand simple arrays
- To understand multidimensional arrays
- To understand strings and string related library functions in C++
HOME NEXT PREVIOUS
Unit-3 Arrays and Strings in C++
- Introduction to Arrays
- Example Program to store and retrieve data from the array
- Example Program to sort an array
Introduction & Arrays
NEXT PREVIOUS
Unit-3 Arrays and Strings in C++
- Arrays are used when you want to store related data of same type
together with the same name so that you can access them easily
- Arrays can be defined as data structures that allow to store group of
data of same data type
- You can retrieve data stored in the array in any order
- Example: int a[10];
Introduction & Arrays
HOME NEXT PREVIOUS
Unit-3 Arrays and Strings in C++
- Example Program to store and retrieve data from the array:
// max.cpp
#include <iostream.h>
void main()
{
int a[5];
for (int i=0; i<5; i++)
cin>>a[i];
for (int i=0; i<5; i++)
cout<<a[i];
} Contd.
Introduction & Arrays
HOME NEXT PREVIOUS
Unit-3 Arrays and Strings in C++
#un the above program and the enter the values 5, 10, 12, 15, 18. The
values are stored and retrieved in the below order
Array Index Value
a[0] 5
a[1] 10
a[2] 12
a[3] 15
a[4] 18
Introduction & Arrays
HOME NEXT PREVIOUS
Unit-3 Arrays and Strings in C++
- Example Program to sort an array
// sort.cpp
#include <iostream.h>
void main()
{ int a[10], temp; // array declaration
for(int i=0; i<10; i++)
{ cin>>a[i]; } // enter 10 values one by one
cout<<"Array after sorting is ";
for(int j=0; j<9; j++) // outer loop starts
for(i=0; i<9; i++) // inner loop starts
{ if (a[i]>a[i+1]) // compare consecutive values
{ temp=a[i]; // if first value is greater than
second then store that value in temp
Contd.
Introduction & Arrays
HOME NEXT PREVIOUS
Unit-3 Arrays and Strings in C++
a[i]=a[i+1] // store second value in the place of first
a[i+1]=temp; // store the temp (first) value in second
// the above procedure is called swapping
} // inner loop ends
} // outer loop ends
// by now all the elements have sorted
for(i=0; i<10; i++)
{
cout<<a[i]<<endl; // print the sorted elements
}
}
Introduction & Arrays
HOME PREVIOUS

90/ -7.943 77.77. &$   % .3974/:.3/ 2:9/203843.77.9438 3  !#' 431/039.7 1:3.8 %4 :3/0789.3/ 89738 .8 %4 :3/0789.08 W W W %4 :3/0789.9.3/ 8250 .8 -0. .3/ 8973 70.

 !#' &$ % .250!747.2948479.943 77.8.250!747.174290. W .8 W .3/$97383 3974/:.77.3.9439477.&39 77.77.9.29489470.0/.3/70970.8 W 3974/:.

3 -0 /0130/ .20 950 940907 9 90 8.9.9.39 94 89470 70. ( !#' &$   % .9 4: .3 . 950 W 4: .250 39 .9 .70 :80/ 03 4: .20 84 9. 89470/ 3 90 ..943 77.90/ /.8 /.8 ..088 902 0.3/$97383 3974/:.77.9..8 .4 94 89470 74:5 41 /.0 /.9:708 9.8.. 897:. 41 8.20 3.9.8 W 77..8 W 77.3 70970. 3 .3 47/07 W .9.20 /. 41 8.&39 77.

3/70970.9.77.8 W .3/$97383 3974/:. .943 77.0/.250!747.29489470.&39 77.174290.8.

.

( < 439/ !#' &$   % .( 147 39  .55 3.3  39. .( 147 39  .2  .3.4/2.:/048970.4:9.2.

:08 .8..-4.:0      !#' &$   % .2 ..70 89470/ .8 #:3 90 .943 77.3/ 90 03907 90 . ( .3/0 .( .3/ 70970.( .( '.( .3/$97383 3974/:.0 5747.&39 77.:08      %0 .0/ 3 90 -04 47/07 77.

250!747.3/$97383 3974/:.&39 77.8.77.2948479. .943 77.3.8 W .

.

(.3.(< 147 39  147   1 . ( 9025.2  .3 39.55 3. ( 9025 147 39   .4/2.( 439/ .8479 .:/048970.

.

.:08430-430 .03907 .

.

4:90744589.798 .

.

330744589.798 .

.

.:9.0..425.4380.:08 .70.

.

9.43/903894709.3 80.4:9 77.:039025 .9079.1907847938  ....:0870.11789.

.

77.943 !#' &$   % ./0...7.

 ( .8 . (9025 .(.&39 77.943 77.8.3/$97383 3974/:.

.

:03905.8947080...43/.0411789 .

.

43/ .89470909025 1789 .:0380..

.

553 < < .0/:708.-4..0/8.90.0574.

.

330744503/8 .

.

4:90744503/8 .

.

.084790/ 147     .(03/ < < .900020398.4:9.-34.

.

57399084790/0020398 !#' &$   .

Sign up to vote on this title
UsefulNot useful

Master Your Semester with Scribd & The New York Times

Special offer for students: Only $4.99/month.

Master Your Semester with a Special Offer from Scribd & The New York Times

Cancel anytime.