Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
3 views

Array Part 1

array programming c++

Uploaded by

Ahmad Ishtiaq
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Array Part 1

array programming c++

Uploaded by

Ahmad Ishtiaq
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 25

Arrays

Arrays

▶ C/C++ language provides a capability that enables the user to design a set of
similar data types, called array.
▶ For understanding the arrays properly, let us consider the following program:

main()
{
int x;
x = 5;
x = 10;
cout<<x;
}
Arrays

▶ For example, suppose we wish to arrange the percentage marks obtained


by 100 students in ascending order. In such a case we have two options
to store these marks in memory:
■Construct 100 variables to store percentage marks obtained by 100
different students, i.e. each variable containing one student’s marks.
■Construct one variable (called array or subscripted variable) capable
of storing or holding all the hundred values.
Arrays

▶ An array is a collective name given to a group of ‘similar quantities.


OR
■A group of Consecutive memory locations having the same name and
same data type.
Advantages/Uses of Arrays

 Array can store many values with single name


 Arrays are used to process many values easily and
quickly
 The values stored in an array can be sorted easily.
 A search process can be applied on an arrays easily.
Types of Arrays
▶ Usually, the array of characters is called a ‘string’,
▶ whereas an array of ints or floats is called simply an array.
▶ Remember that all elements of any given array must be of the
same type.
▶ i.e. we cannot have an array of 10 numbers, of which 5 are
ints and 5 are floats.
Array Declaration
▶ Like other variables, an array needs to be declared so that the compiler
will know what kind of an array and how large an array we want.
 To declare an array, you need to specify its element type (data type) and
size using the following syntax:
elementType arrayName[SIZE];
■ For-example int marks[30];
■ The bracket ( [ ] ) tells the compiler that we are dealing with an array.
■int specifies the type of the variable, just as it does with ordinary variables
and the word marks specifies the name of the variable.
■The [30] however is new. The number 30 tells how many elements of the
type int will be in our array.
Accessing Array’s Elements

▶ After array declaration, how individual elements in the array can be referred.
■This is done with subscript, the number in the brackets following the array
name.
■Array indices are 0-based.
■ This number specifies the element’s position in the array.
■All the array elements are numbered, starting with 0. Thus, marks[2] is not the
second element of the array, but the third.
How to initialize?

▶ So far we have used arrays that did not have any values in them to begin with.
▶ We managed to store values at run time,
■ Let us now see how to initialize an array while declaring it.
■ Following are a few examples that demonstrate this.
▶ int marks[4]= {1,2};
• C++ allows you to initialize a part of the array. For example, the above statement assigns
values 1, 2 to the first two elements of the array named marks. The other two elements will
be set to zero.

Note: below syntax gives an error


int marks[];
marks[0]=1;
marks[1]=11;
marks[2]=33;
marks[3]=4;

Note that if an array is declared, but not initialized, all its elements will contain
“garbage,” like all other local variables.
Entering data to an
Array
▶ Here is the section of code that places data into an
array:

for (i=0;i<=5;i++)
{
cout<<“Enter marks \n”;
cin>>marks[i];
}
Reading Data from an
array
int main() marks[0]=2
{ marks[1]=3
int i, marks[10]={2,3,4,7,6,5,4,0,9,5}; marks[2]=4
for(i=0;i<10;i++) marks[3]=7
{ marks[4]=6
cout<<"marks["<<i<<"]="<<marks[i]<<"\n"; marks[5]=5
marks[6]=4
} marks[7]=0
} marks[8]=9
marks[9]=5
Arrays in
memory
▶ Consider the following array declaration: int arr[8];
■16 bytes get immediately reserved in memory, 2 bytes each for the 8
integers.
■ Since the array is not being initialized, all eight values present in it
would be garbage values.
■All the array elements would always be present in contiguous
memory locations.
Task # 1

Write a program which stores the marks of 5 students in an array and then
print all the marks. The marks of students are 75, 85, 80, 95, 90
Task # 2

Write a program which reads a set of number from keyboard and then find out
smallest element. Use array for storing elements.
Two-Dimensional Array

 Two-dimensional array can be considered as a table that consists of rows


and columns.
 The two-dimensional array is also called a matrix.
 An array of array is also called as 2D array.
 Each element in 2D array is referred with the help of two indexes.
 Syntax
 Data_Type Identifier[Rows][Cols];
Two-Dimensional Array

 In C++, you can create an array of an array known as multi-dimensional array.


For example:

 int arr[4][3];

 Here, arr is a two-dimensional array. It can hold a maximum of 12 elements.


 We can think this array as table with 4 rows and each row has 3 columns.
Two-Dimensional Array
Declaring and Initializing Two Dimensional
Arrays
• int arr[4][4];
• Visualized as:

• arr[0][0] = 21;
Declaring and Initializing Two Dimensional
Arrays
• int arr[4][4];
• Visualized as:

• arr[0][0] = 21;
• arr[1][2] = 36;
Declaring and Initializing Two Dimensional
Arrays
• int arr[4][4];
• Visualized as:

• arr[0][0] = 21;
• arr[1][2] = 36;
Initializing and Accessing Two-Dimensional
Array
int arr[2][3] = { 1,2,3,4,5,6};
• Better way
 int arr[2][3] = {{9,2,3},{6,9,11}};

cout<<arr[0][0];
 This statement will display 9
cout<<arr[1][2];
 This statement will display 11

You might also like