0% found this document useful (0 votes)
4 views

lecture 11

The document provides an overview of arrays in programming, particularly in C++. It explains the concept of arrays as collections of similar data types, their declaration, access methods, and the importance of bounds checking. Additionally, it covers the use of one-dimensional and two-dimensional arrays with examples of programs that utilize these structures.

Uploaded by

yasinbilal234
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

lecture 11

The document provides an overview of arrays in programming, particularly in C++. It explains the concept of arrays as collections of similar data types, their declaration, access methods, and the importance of bounds checking. Additionally, it covers the use of one-dimensional and two-dimensional arrays with examples of programs that utilize these structures.

Uploaded by

yasinbilal234
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 18

Programming Fundamental

1
Array
• Offers a simple way of grouping like variables for easy access
• It is a group of elements having same data type
• An array is a collective name given to a group of ‘similar quantities’
• Arrays in C++ share a few common attributes
• Variables in an array share the same name
• Variables in an array share the same data type
• Individual variables in an array are called elements
• Elements in an array are accessed with an index number

2
Cont.

Cout<<“x= ”<<x;

• Ordinary variables are capable of holding only one value at a time


• There are situations in which we would want to store more than
one value at a time in a single variable

3
Cont.
• For example, suppose we want 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:
• Declare 100 variables to store percentage marks obtained by 100
different students, i.e. each variable containing marks of single student
int m1, m2, m3 ……… m100;
• Declare one variable (called array or subscripted variable) capable of
storing or holding all the hundred values

4
Array declaration

int marks[10];

type Array name size


Like any other variable, arrays occupy memory space
marks [10] Index of elements in array

0 1 2 3 4 5 6 7 8 9

500 504 508 512 516 520 524 528 532 530
Memory address of array elements 5
How to access array elements
int x; int marks[10];
x= 2; marks[0] = 2;
Cout<<x; marks[1] = 3;
Cout<<x; Cout<<marks[2])
Cout<<“marks [2] = ”<< marks[2])
x 43
2 Output
695 x = 43 Output
marks [2] = 16
0 1 2 3 4 5 6 7 8 9
2 3 16
500 504 508 512 516 520 524 528 532 530
6
Points to remember
• Array is a collection of elements having same data type
• Memory allocate to array elements are continuous
• Array size must be mentioned in array declaration ( int marks
[10]; )
• Array index always starts with 0
• In 10 elements array, index of first elements is 0 and index of
last element is 9
• Array element can be access using array index

7
A Simple Program Using Array
• Write a program that take 10 integer from user and then display
those integers

Write a program

8
Marks program

Cout<<“Enter Marks ”<<endl;


Cin>>marks[i];

Cout<<“Average is = ”<<avg<<endl;
Go to program
9
Marks program

Go to program
10
Array initialization

11
Bounds Checking
• In C++ there is no check to see if the subscript used for an array exceeds the size of
the array
• Data entered with a subscript exceeding the array size will simply be placed in
memory outside the array

0 1 2 3 4 5 6 7 8 9

500 504 508 512 516 520 524 528 532 530

• To see to it that we do not reach beyond the array size is entirely the programmer’s
botheration and not the compiler’s
12
Passing Array Elements to a Function
• Array elements can be passed to a function by calling the function by
value
• In the call by value we pass values of array elements to the function

13
Value of array element pass to a function
0 1 2 3 4 5 6
55 65 75 56 78 78 90
500 504 508 512 516 520 524

ii ==406
2
3
1
5
7
display(marks[6])
display(marks[5])
display(marks[2])
display(marks[3])
display(marks[4])
display(marks[o])
display(marks[1])
display(90)
display(78)
display(75)
display(56)
Program output display(55)
display(65)
Cout<<m;
55
65
75
56
78
78
90 14
Two Dimensional Arrays
• It is also possible for arrays to have two or more dimensions
• For example you want to input roll no and mark of 4 students.
• Since we know 1D array, so for this problem we can have two
1D array
– One for roll number
– Second for marks
• One 2D array can be used to store these values

15
• #include<iostream>

• using namespace std;

• main()
• {
• int first[10][10];
• Int m,n;

• cout << "Enter the number of rows and columns of matrix ";
• cin >> m >> n;
• cout << "Enter the elements of matrix\n";
• for ( c = 0 ; c < m ; c++ )
• for ( d = 0 ; d < n ; d++ )
• cin >> first[c][d];

16
Initializing a 2-Dimensional Array

17
Example program
• Write a program that adds two 4 x 4 matrices.

3 5 6 1 4 2 -7 0 7 7 -1 1
5 3 2 9 5 7 3 1 10 10 5 10
1 0 -3 4 + 6 3 -1 0 = 7 3 -4 4
7 2 3 -9 8 9 -2 3 15 11 1 -6

Write a program

18

You might also like