You are on page 1of 17

ARRAY

. Array

Array

Array

Array

Array

Array
Array

Datatype

Array [ Array];
VariableName[size of array]

Array
Array

int a[10], b[4][2];


float x[5], y[3][3];

Array a, b, x, y

Array int a

Array a a[0], a[1], a[2],......,a[9]

int Array a
int

Array int b

b[0][0]
b[1][0]
b[2][0]
b[3][0]

b[0][1]
b[1][1]
b[2][1]
b[3][1]

b b[1][1]
int Array b
int

Array float x x[0],

x[1],x[4] x[1]

float

Array float y

y[0][0]
y[1][0]
y[2][0]

y[0][1]
y[1][1]
y[2][1]

y[0][2]
y[1][2]
y[2][2]

y y[1][1] float Array y


float

. Array

Array

. Array

Ex1: Read
Array x Keyboard

#include<iostream>
#include<conio.h>
using namespace std;
float Add (float a, float b){

cout<<"Addition

== ";

return (a+b);
}
int sub (int a, int b){
cout<<"Subtraction == ";
return (a-b);
}
int mul (float a, float b){
cout<<"multiplication== ";
return (a*b);
}
float div(float a, float b){
cout<<"Divide

== ";

return (a/b);
}
int main (){
float arr[2];
cout<<"GROUP NAME : ARRAY"<<endl;
cout<<" DATE : Mornning"<<endl;
cout<<"Marjoring : COMPUTER SCIENCE "<<endl;
cout<<"\t\t\t_____________________________"<<endl;
cout<<"INPUT FIRST = "; cin>>arr[0];
cout<<"INPUT SECOUND= "; cin>>arr[1];
cout<<Add(arr[0], arr[1])<<endl;
cout<<sub(arr[0], arr[1])<<endl;
cout<<mul(arr[0], arr[1])<<endl;
cout<<div(arr[0], arr[1])<<endl;

getch();
return 0;

Ex2: Access data x :


#include<iostream>
//#include<iomanip.h>
#define max n 7;
using namespace std;
void main(){
char ch[n]={'H','O','P','S','E','S','T'},ch1;
int i;
cout<<"what is the replacing letter?";cin>>ch1;
for(i=0;i<n;i++)
if(ch1==ch[i])ch[i]=ch1;
return 0;
}

. Array
Array Create Value Declaration :
int x[3] = { 7, 4, 5 } ;
Declaration,
Array
x[0] = 7; x[1] = 4; x[2] = 5 ;

. Array Characters :
Declaration
char text1 [5] = { D, C, f, s, p };
char text2 [5] = DCfsp;
char text3 [ ] = DCfsp;
text[0] = D; text[1] = C; text[2] = f; text[3] = s; text[4] = p;

Ex3: Convert string


# include <stdio.h>
# include <conio.h>
# include <ctype.h>
# define SIZE 80
main ( )
{
char text [SIZE];
int count;
for (count = 0; count <= SIZE 1; count ++ )
text[count] = getchar ( );
for (count = 0; count < SIZE; count ++ )
putchar(toupper(text[count]));
getch();
return 0;

. Array
short array

array

Algorithm

array

Ex1: array
#include<iostream>
#include<conio.h>
#include<windows.h>
using namespace std ;
int main(){
system("color 3A");

int b[100];
int i, j, n, temp;
cout<<"\t\tNAME : SANG SOPHEAP"<<endl;
cout<<"\t\tID : 32429"<<endl;
cout<<"\t\tMAJOR: INFORMATION TECHNOLOGY"<<endl;
cout<<"Number array in the array ?"<<endl;
cin>>n;
cout<<"Enter the element :"<<endl;
for(i=0;i<=n-1;++i){
cin>>b[i];}
cout<<"Content of the array is "<<endl;
for(i=0;i<=n-1;++i){
cout<<b[i]<<"\t";}
cout<<endl;
for(i=0;i<=n-1;++i){
for(j=0;j<=n-1;++j)
if(b[i]>b[j]){
temp=b[i];
b[i]=b[j];
b[j]=temp;}}
cout<<"Content of the array (short big to small form) is "<<endl;
for(i=0;i<=n-1;++i){
cout<<b[i]<<"\t";
}
cout<<endl;
getch();
}

Ex2. Array
#include<iostream>
#include<conio.h>
#include<windows.h>
using namespace std ;
int main(){
system("color 4A");
int b[100];
int i, j, n, temp;
cout<<"\t\tNAME : SANG SOPHEAP"<<endl;
cout<<"\t\tID : 32429"<<endl;
cout<<"\t\tMAJOR: INFORMATION TECHNOLOGY"<<endl;
cout<<"Number array in the array ?"<<endl;
cin>>n;
cout<<"Enter the element :"<<endl;
for(i=0;i<=n-1;++i){
cin>>b[i];}
cout<<"Content of the array is "<<endl;
for(i=0;i<=n-1;++i){
cout<<b[i]<<"\t";}
cout<<endl;
for(i=0;i<=n-1;++i){
for(j=0;j<=n-1;++j)
if(b[i]<b[j]){
temp=b[i];
b[i]=b[j];
b[j]=temp;}}

cout<<"Content of the array (short) is "<<endl;


for(i=0;i<=n-1;++i){
cout<<b[i]<<"\t";
}
cout<<endl;
getch();
}

. Array (Multidimensional Array) :



C / C ++ ()

"

int [5] [6]" 5 ,


6 : int [12] [5] [6] "

dUcrUbxageRkam

x[0,0]

x[0,1]

x[0,2]

x[0,3]

x[0,4]

x[1,0]

x[1,1]

x[1,2]

x[1,3]

x[1,4]

x[2,0]

x[2,1]

x[2,2]

x[2,3]

x[2,4]

0
1
2

{} braces

{} braces

,

. (Averaging Array Elements)

SALES

series

( )

#include <iostream>
using namespace std;
int main(){
const int SIZE = 6; //size of array

double sales[SIZE]; //array of 6 variables


cout << Enter widget sales for 6 days\n;
for(int j=0; j<SIZE; j++) //put figures in array
cin >> sales[j];
double total = 0;
for(j=0; j<SIZE; j++) //read figures from array

total += sales[j]; //to find total


double average = total / SIZE; // find average
cout << Average = << average << endl;
return 0;
}
Heres some sample interaction with SALES:
Enter widget sales for 6 days
352.64
867.70
781.32
867.35
746.21
189.45
Average = 634.11

(Initializing Arrays)
.


, DAYS , 12 days_per_month

#include <iostream>
using namespace std;
int main(){
int month, day, total_days;
int days_per_month[12] = { 31, 28, 31, 30, 31, 30,
31, 31, 30, 31, 30, 31 };
cout << \nEnter month (1 to 12): ; //get date
cin >> month;
cout << Enter day (1 to 31): ;

10

cin >> day;


total_days = day; //separate days
for(int j=0; j<month-1; j++) //add days each month
total_days += days_per_month[j];
cout << Total days from start of year is: << total_days
<< endl;
return 0;
}
The program calculates the number of days from the beginning of the year to a date specified
by the user. (Beware: It doesnt work for leap years.) Heres some sample interaction:
Enter month (1 to 12): 3
Enter day (1 to 31): 11
Total days from start of year is: 70

. (Array Fundamentals)

#include <iostream>
using namespace std;
int main(){
int age[4];
for(int j=0; j<4; j++) //get 4 ages{
cout << Enter an age: ;
cin >> age[j]; //access array element
}
for(j=0; j<4; j++) //display 4 ages

11

cout << You entered << age[j] << endl;


return 0;
}
Heres a sample interaction with the program:
Enter an age: 44
Enter an age: 16
Enter an age: 23
Enter an age: 68
You entered 44
You entered 16
You entered 23
You entered 68
The first for loop gets the ages from the user and places them in the array, while the
secondreads them from the array and displays them.

.
(Initializing Multidimensional Arrays)
,


SALEMON


SALEINIT

#include <iostream>
#include <iomanip>
using namespace std;
const int DISTRICTS = 4; //array dimensions
const int MONTHS = 3;
int main(){
int d, m;
//initialize array elements

12

double sales[DISTRICTS][MONTHS]
= { { 1432.07, 234.50, 654.01 },
{ 322.00, 13838.32, 17589.88 },
{ 9328.34, 934.00, 4492.30 },
{ 12838.29, 2332.63, 32.93 } };
cout << \n\n;
cout << Month\n;
cout << 1 2 3;
for(d=0; d<DISTRICTS; d++)
{
cout <<\nDistrict << d+1;
for(m=0; m<MONTHS; m++)
cout << setw(10) << setiosflags(ios::fixed)
<< setiosflags(ios::showpoint) << setprecision(2)
<< sales[d][m]; //access array element
}
cout << endl;
return 0;
}
Out put
{ 1432.07, 234.50, 654.01 }

. Arrays of Structures
structures

structures
#include <iostream>
using namespace std;
const int SIZE = 4; //number of parts in array

13

////////////////////////////////////////////////////////////////
struct part //specify a structure
{
int modelnumber; //ID number of widget
int partnumber; //ID number of widget part
float cost; //cost of part
};
////////////////////////////////////////////////////////////////
int main()
{
int n;
part apart[SIZE]; //define array of structures
for(n=0; n<SIZE; n++) //get values for all members
{
cout << endl;
cout << Enter model number: ;
cin >> apart[n].modelnumber; //get model number
cout << Enter part number: ;
cin >> apart[n].partnumber; //get part number
cout << Enter cost: ;
cin >> apart[n].cost; //get cost
}
cout << endl;
for(n=0; n<SIZE; n++) //show values for all members
{
cout << Model << apart[n].modelnumber;
cout << Part << apart[n].partnumber;

14

cout << Cost << apart[n].cost << endl;


}
return 0;
}
The user types in the model number, part number, and cost of a part. The program records this
data in a structure. The program asks for the data for four different parts, and stores it in the
four elements of the apart array. It then displays the information. Heres some sample input:
Enter model number: 44
Enter part number: 4954
Enter cost: 133.45
Enter model number: 44
Enter part number: 8431
Enter cost: 97.59
Enter model number: 77
Enter part number: 9343
Enter cost: 109.99
Enter model number: 77
Enter part number: 4297
Enter cost: 3456.55
Model 44 Part 4954 Cost 133.45
Model 44 Part 8431 Cost 97.59
Model 77 Part 9343 Cost 109.99
Model 77 Part 4297 Cost 3456.55

. Pointer Linking with Array :

cMnucEdlRtUvykcitTukdak;kg]TahrN_xagelIKW eRkayBI Call Function Sort_Array,


statement Edl print lTplmkelI Screen sitenAxageRkA Function Sort_Array
kg main
Function) EtenAEt print RtUv . eyIgdwgfa parameter EdlbBaneGay Function Sort_Array Ca Value
parameter . tamBitcg; print RtUv eyIgRtUvEtsresr statement Edl print lTplenaHenAkg
-

15

Function Sort_Array

Etmg .

enaHKWedaysarEt Array Ca Address, )annyfa pointer point eTAFatutMbUgrbs; Array . karyl;BI


Gtnyrbs; Array kg C TamTaeGayeyIg RtUvmanKMnitmUldanGMBI Pointer .
eyIgeRbIbNasBaa Operator pointer nig Address dUcxageRkam
* p CaTinnyEdlpkkg Address p, niyaymYyrebobeTot p Ca memory pk address rbs; *p bCa
Pointer point eTA Variable pktMl *p .
&x Ca Address rbs; Variable x .
pointer Ca Variable pk Address, pktMleT . ]bmaeyIg Declaration variable x, p nig assignment
dUcxageRkam
int x
= 15;
/* x Ca Variable mantMl = 15 */
int *p;
/* Declaration TinnyCacMnYnKt;man address KW p */
p = &x ;
/* p TTYltMl address rbs; x */
dMbUg pointer p Value eT/ EteRkayeBlman assignment p = &x enaH p nig point eTA x .
eBlenaH Pointer Ca Variable pk tMl Address n memory rbs; x . ehIy p xngkman
address pal;rbs;xn EdrKW &p . Et address rbs; p eyIgminykcitTukdak;eT/
EteyIgKiteTAdl;Gtny memory KW address p .
cUrBinitrUbxageRkam . cUrykcitTukdak;Gtny F0F4, KWCatMlsresreRkamrag Hexa, smmUl
nig 2 bytes . GkmincaM)ac;xVl;KitfatMlenaHb:uNaeTkgelxeKaldb; . cMeBaH variable p enaH
F0F4 CatMl rbs; p . cMeBaH x, vaCa address KW &x .
Code :
#include<iostream>
#include<conio.h>
#include<windows.h>
using namespace std;
int main(){
int numbers[5];
int *p;
p=numbers; *p=10;
cout<<"fisrt elemenbt :"<<p<<endl;
p++; *p=20;

16

p=&numbers[2]; *p=30;
p=numbers+3;
*p=40;
p=numbers; *(p+4)=50;
for(int n=0; n<5; n++){
if(n<4) cout<<numbers[n]<<",";
else cout<<numbers[n]<<".";
}
getch();
return 0;
}

17

You might also like