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

Programming With C++ - Lecture7

This document discusses arrays in C++. It begins by defining arrays as sequential data storage and explaining that arrays allow storing multiple elements of the same data type. It then covers: 1) One-dimensional arrays, explaining their syntax and how to access elements using subscripts. 2) How to directly access elements and use for loops to sequentially access all elements. 3) Arithmetic operations can be performed on array elements. 4) Multidimensional arrays which allow arrays of arrays, creating 2D and 3D arrays to store data. 5) Examples are given of declaring and using one-dimensional and 2D arrays in C++ programs. Practice problems are suggested to work with matrices

Uploaded by

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

Programming With C++ - Lecture7

This document discusses arrays in C++. It begins by defining arrays as sequential data storage and explaining that arrays allow storing multiple elements of the same data type. It then covers: 1) One-dimensional arrays, explaining their syntax and how to access elements using subscripts. 2) How to directly access elements and use for loops to sequentially access all elements. 3) Arithmetic operations can be performed on array elements. 4) Multidimensional arrays which allow arrays of arrays, creating 2D and 3D arrays to store data. 5) Examples are given of declaring and using one-dimensional and 2D arrays in C++ programs. Practice problems are suggested to work with matrices

Uploaded by

Abdul baseer
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 10

1

Programming with C++

Lecture 7: Arrays
Sikander Naseem
2

Introduction
 Sequential data storage
 Tabulated database
 An array is a sequence of data objects all of which have the
same type.
 The data objects are called the elements of the array and are
numbered as 0, 1, 2, 3, ....
 These numbers are called subscripts of the array pointing to
and giving access to the position of element.
 Types
• One dimensional arrays (linear array)
• Multidimensional arrays
3

One dimensional
 Syntax for defining arrays Data-type name[size];
float a[3]
 Example float a[3]; 0 0.99
a[0] = 0.99;
1 78.1
a[1] = 78.1;
a[2] = 22.1; 2 22.1

OR with initializer list,


float a[3] = {0.99,78.1,23.1};

 So if the array has n elements, their names are a[0], a[1],


a[2], …, a[n-1].
4

One dimensional
 Direct access
int main()
{ double a[3];
cin>>a[0];
cin>>a[1];
cin>>a[2];
cout << "a[0] = " << a[0] << endl;
cout << "a[1] = " << a[1] << endl;
cout << "a[2] = " << a[2] << endl;
}
5

One dimensional
 Sequential access
int main()
{ int n;
cout << " Enter the size of array = ";
cin>>n;
double a[n];
cout << "Enter " <<n<< " numbers = ";
for (int i=0; i<n; i++)
{ cin >> a[i];}
for (int i=0; i<n; i++)
cout << "[" << i << "] = "<< a[i]<<endl;
}
6
 Arithmetic operations between the
elements of arrays
int main()
{ int n;
int cost=0;
cout <<"Enter the no of cost items = ";
cin>>n;
float a[n];
cout<<"Enter "<< n <<" costs of items = ";
for (int i=0; i<n; i++)
cin>>a[i];
for (int i=0; i<n; i++)
cout<<"cost "<<i<<" = " <<a[i]<<endl;
for (int i=0; i<n; i++)
cost=cost+a[i];
cout<<" Total cost = "<<cost<<endl;
}
7

Multidimensional arrays
 The element of an array can be almost any type, including
an array type.
 So an array of array produces multi dimensional array
 A one-dimensional array of one-dimensional arrays gives
a two-dimensional array; a one-dimensional array of two-
dimensional arrays gives a three-dimensional array; etc.
 Example: double a[32][10]; //is a two dimensional array
a[25][8] = 99.1; // assign the value 99.1
to the element with
subscript (25,8).
8

A two dimensional array


int main()
{
int a[2][3]; //a 2x3 array
cout << "Enter 3 numbers per row "<<endl;
for (int i=0; i<2; i++)
{ cout<<"Row number "<<i<<" = ";
for (int j=0; j<3; j++)
cin >> a[i][j]; }
cout<<"The required array is: ";
for (int i=0; i<2; i++)
{ cout << endl;
for (int j=0; j<3; j++)
cout <<" "<<a[i][j]; }
cout << endl;
}
9

Practice
 Use the above example of multi dimensional array and
create a program to enter a matrix with m rows and n
columns.
 Create one dimensional array of size n. The array asks
for data and calculates the average.
 Enter two matrices and perform athematic operations
between the elements of the two.
10

The End

Thanks for coming

Reference book:
Programming with C++, SCHAUM’s outlines, John Hubbard,
3rd/2nd Edition, McGraw Hill, New Delhi

You might also like