[go: up one dir, main page]

0% found this document useful (0 votes)
14 views16 pages

Programming Fundamental: Lecture-11

This lecture introduces arrays as a structured data type in programming, contrasting them with simple data types that hold single values. It explains how arrays allow for the storage and manipulation of multiple values of the same type, emphasizing their utility in managing collections of data. The document also covers array declaration, accessing components, and basic operations performed on one-dimensional arrays.

Uploaded by

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

Programming Fundamental: Lecture-11

This lecture introduces arrays as a structured data type in programming, contrasting them with simple data types that hold single values. It explains how arrays allow for the storage and manipulation of multiple values of the same type, emphasizing their utility in managing collections of data. The document also covers array declaration, accessing components, and basic operations performed on one-dimensional arrays.

Uploaded by

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

Programming Fundamental

Lecture-11
Today’s Lecture

➢ Arrays

2
Arrays

Introduction
➢ In the previous lectures we worked with simple data types. A data type is
called simple if it can hold one value at a time.
➢ We also discussed decision making statements and repetition statements
with simple data types.
➢ We write our functions and used system defined function with simple data
types
➢ One data types we have not discussed so far is structured data type
➢ In contrast to simple data types, in a structured data type, each data item is
collection of other data items.
➢ Simple data types are building block for structured data type
➢ The first structured data type that we will discussed is an array.

3
Arrays

Introduction
➢ Before formally defining an array, let us consider the following problem.
We want to write a C++ program that reads five numbers, finds their sum,
and prints the numbers in reverse order.

➢ What are the limitations in this form of programs?


4
Arrays

Introduction
➢ Note the following in the previous program:
1. Five variables must be declared because the numbers are to be printed
in reverse order.
2. All variables are of type int—that is, of the same data type.
3. The way in which these variables are declared indicates that the
variables to store these numbers all have the same name—except the
last character, which is a number.
➢ Statement 1 tells you that you have to declare five variables.
➢ Statement 3 tells you that it would be convenient if you could somehow
put the last character, which is a number, into a counter variable and use
one for loop to count from 0to 4 for reading and another for loop to count
from 4 to 0 for printing.
➢ The data structure that lets you do all of these things in C++ is called an
5
array
Arrays

What is an Array?
➢ An array is a collection of a fixed number of components all of the same
data type.
➢ An array is used to process a collection of data of the same type
Examples: A list of names
A list of temperatures
➢ Why do we need arrays?
❖ Imagine keeping track of 5 test scores, or 100, or 1000in
memory How would you name all the variables?
How would you process each of the variables?
➢ A one-dimensional array is an array in which the components are arranged
in a list form.
6
Arrays

Declaring an Array
➢ The general form for declaring a one-dimensional array is:
dataType arrayName[intExp];
in which intExp is any constant expression that evaluates to a positive
integer. Also, intExp specifies the number of components in the array.
➢ An array, named num, containing five variables of type int can be declared
as
int num[ 5 ];
➢ This is like declaring 5 variables of type int:
num[0], num[1], … , num[4]
➢ The value in brackets is called
A subscript
An index 7
Arrays

The Array Variables


➢ The variables making up the array are referred to as
❖ Indexed variables
❖ Subscripted variables
❖ Elements of the array
➢ The number of indexed variables in an array is the declared size, or size,
of the array
❖ The largest index is one less than the size
❖ The first index value is zero

8
Arrays

The Array Variables Types


➢ An array can have indexed variables of any type
➢ All indexed variables in an array are of the same type
❖ This is the base type of the array
➢ An indexed variable can be used anywhere an ordinary variable of the
base type is used

Using [ ] With Arrays


➢ In an array declaration, [ ]'s enclose the size of the array such as this array
of 5 integers:
int num [5];
➢ When referring to one of the indexed variables, the [ ]'s enclose a number
identifying one of the indexed variables
Num [3] is one of the indexed variables
The value in the [ ]'s can be any expression that evaluates to one of the
integers 0to (size -1) 9
Arrays

Accessing Array Components


➢ The general form (syntax) used for accessing an array component is:
arrayName[indexExp]
in which indexExp, called the index, is any expression whose value is a
nonnegative integer.
➢ The index value specifies the position of the component in the array.
➢ In C++, [] is an operator called the array subscripting operator. Moreover,
in C++, the array index starts at 0.
➢ The assignment statement:
list[5] = 34;
➢ stores 34 in list[5], which is the sixth component of the array list

10
Arrays

Accessing Array Components


➢ Consider Following three statments
list[3] = 10;
list[6] = 35;
list[5] = list[3] + list[6];

➢ The size of the array must be known in advance.


➢ Following statement is not allowed for defining size of array
int arraysize;
cin >> arraysize;
int list[arraysize];
11
Types of Arrays

12
Types of Arrays

Single Dimensional Array


➢ An array is a collection of storage locations for a specific type of data. And
one dimensional arrays grows in one direction only.
➢ One dimensional array is one with single variable index.
➢ Think of an array as a "box" drawn in memory with a series of
subdivisions, called elements, to store data. The box below could
represent an array of 10 integers.

➢ It is important to remember that all of the elements in an array must


contain the same type of data.

13
Single Dimensional Arrays

Processing Single Dimensional Array


➢ Some of the basic operations performed on a one-dimensional array are
initializing, inputting data, outputting data stored in an array, and finding
the largest and/or smallest element.
➢ Moreover, if the data is numeric, some other basic operations are finding
the sum and average of the elements of the array.
➢ Each of these operations requires the ability to step through the elements
of the array. This is easily accomplished using a loop.
➢ Suppose we have the following statements
int list[100]; //list is an array of size 100
int i;
➢ The following for loop steps through each element of the array list,
starting at the first element of list:
for (i = 0; i < 100; i++) //Line 1
//process list[i] //Line 2
14
Single Dimensional Arrays

Processing Single Dimensional Array


#include <iostream>
using namespace std;
main() {
int item[5], sum=0,counter;
cout << "Enter five numbers: ";
for (counter = 0; counter < 5; counter++) {
cin >> item[counter];
sum = sum + item[counter];
}
cout << endl<< "The sum of the numbers is: " << sum << endl;
cout << "The numbers in reverse order are: “<<endl;
for (counter = 4; counter >= 0; counter--)
cout << item[counter] <<“ “; 15
}
Example

Write C++ statement(s) to do the following:


a. Declare an array alpha of 50 components of type int.
b. Initialize each component of alpha to -1.
c. Output the value of the first component of the array alpha.
d. Set the value of the 25th component of the array alpha to 62.
e. Set the value of the 10th component of alpha to three times the
value of the 50th component of alpha plus 10.
f. Use a for loop to output the value of a component of alpha if its
index is a multiple of 2 or 3.
g. Output the value of the last component of alpha.
h. Output the value of the alpha so that 15 components per line
are printed.
i. Use a for loop to increment every other element (the even
indexed elements).

You might also like