Docsity

Arrays in Computer Fundamentals & Programming: Declaration, Initialization, and Access, Exercises of Computer Fundamentals

Toggle sidebar
bg1
DCIS‐PIEAS,[email protected]
Lab‐08
ComputerFundamental&Programming
Arrays
Anarrayisasequenceofobjectsallhavingsamedatatypes,storedatcontiguousmemorylocations.
Theobjectsinanarrayarecalled‘elements’ofthearray,arrayelementsareaccessedusinganindexstartingfrom
0(inC/C++)
DeclaringArray
typearrayName[size];
Examples
intA[10]; //This will create an array of 10 integers
intA[10],B[10];//This will create two arrays of 10 integers each
floatA[10]; //This will create an array of 10 floats
charA[10]; //This will create an array of 10 floats
Exceptions
Anarraycanalsobedeclaredusingavariable(Thevariableshouldmustbedeclaredandinitializedbeforeusing
witharray)
intsize=10;
intA[size];
InitializingArray
Arrayscanbeinitializedinmanyways
INITIALIZATION‐1(justlikevariables)
intA[5];
A[0]=1; A[1]=2; A[2]=3; A[3]=4; A[4]=5;
INITIALIZATION‐2(usingloops)
intA[5];
for(intn=0;n<5;n++)
A[n]=n+1;
INITIALIZATION‐3(usingInitializerlist)
intA[5]={1,2,3,4,5];
intA[5]={1,2,3}; //restoftheelementswillbeinitializedto0
AccessingandDisplayingthevalueofArrayElements
Arrayelementscanbeaccessedandusedbymeansofelementsindexes
intA[5]={2,4,6,8,10};
cout<<A[0]<<A[1]<<A[2]<<A[3]<<A[4];
OR
for(intn=0;n<5;n++)
cout<<A[n];
Docsity.com
pf3

Partial preview of the text

Download Arrays in Computer Fundamentals & Programming: Declaration, Initialization, and Access and more Exercises Computer Fundamentals in PDF only on Docsity!

DCIS‐PIEAS, [email protected]

Lab‐ 08

Computer Fundamental & Programming

Arrays An array is a sequence of objects all having same data types, stored at contiguous memory locations. The objects in an array are called ‘elements’ of the array, array elements are accessed using an index starting from 0 (in C/C++) Declaring Array type arrayName [ size ]; Examples int A[10]; //This will create an array of 10 integers int A[10],B[10]; //This will create two arrays of 10 integers each float A[10]; //This will create an array of 10 floats char A[10]; //This will create an array of 10 floats Exceptions An array can also be declared using a variable (The variable should must be declared and initialized before using with array) int size=10; int A[size]; Initializing Array Arrays can be initialized in many ways INITIALIZATION‐ 1 (just like variables) int A[5]; A[0]=1; A[1]=2; A[2]=3; A[3]=4; A[4]=5; INITIALIZATION‐ 2 (using loops) int A[5]; for(int n=0;n<5;n++) A[n]=n+1; INITIALIZATION‐ 3 (using Initializer list) int A[5]={1,2,3,4,5]; int A[5]={1,2,3}; //rest of the elements will be initialized to 0 Accessing and Displaying the value of Array Elements Array elements can be accessed and used by means of elements indexes int A[5]={2,4,6,8,10}; cout<<A[0]<<A[1]<< A[2]<< A[3]<< A[4]; OR for(int n=0; n<5;n++) cout<<A[n];

DCIS‐PIEAS, [email protected]

Try following programs

Program‐ 01

The following program declares an array of 5 integers, initializes the array using an initializer list (complete) and displays the values stored in array elements #include<stdio.h> #include<conio.h> #include<iostream.h> main(){ int A[5]={1,2,3,4,5}; int index; for(index=0;index<5;index++) cout<<A[index]<<endl; }

Program‐ 02

The following program declares an array of 5 integers, initializes the array using an initializer list(incomplete) and displays the values stored in array elements #include<stdio.h> #include<conio.h> #include<iostream.h> main(){ int A[5]={1,2,3,4,5}; int index; for(index=0;index<5;index++) cout<<A[index]<<endl; }

Program‐ 03

The following program declares two arrays of 5 integers each, initializes the arrays using initializer lists, and displays the sum of their values #include<stdio.h> #include<conio.h> #include<iostream.h> main(){ int A[5]={1,2,3,4,5}; int B[5]={6,7,8,9,10}; int index; //displaying elements of Array A for(index=0;index<5;index++) cout<<”A [“<<index+1<<”] = ”<<A[index]<<endl; //displaying elements of Array B for(index=0;index<5;index++) cout<<”B [“<<index+1<<”] = ”<<B[index]<<endl; cout<<”Displaying sum of elements of two arrays”<<endl; for(index=0; index<5;index++) cout<<A[index]+B[index]<<endl }

Program‐ 04

The following program declares an array of 5 elements, asks user to input the value of 5 integers that are to be stored in the array, and displays the values #include<stdio.h> #include<conio.h> #include<iostream.h> main(){ int A[5]; int index;