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

An introduction to arrays, their declaration, initialization, and access in c/c++ programming. It includes examples of declaring and initializing arrays of different data types and sizes, as well as accessing and displaying their elements. The document also provides incomplete and complete code examples for initializing arrays using loops and initializer lists.

Typology: Exercises

2011/2012

Uploaded on 07/04/2012

waniaa
waniaa ๐Ÿ‡ต๐Ÿ‡ฐ

7 documents

1 / 3

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
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];๎˜ƒ
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;