0% found this document useful (0 votes)
7 views17 pages

Programming - Lec. 4 (Array of One Dimension)

The document outlines a lecture on one-dimensional arrays in C++ for first-year engineering students. It covers the definition, declaration, initialization, and accessing of array elements, along with several programming examples and assignments. Students are tasked with writing C++ programs to manipulate arrays, including operations like multiplication and summation.

Uploaded by

Zaid Mustafa
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)
7 views17 pages

Programming - Lec. 4 (Array of One Dimension)

The document outlines a lecture on one-dimensional arrays in C++ for first-year engineering students. It covers the definition, declaration, initialization, and accessing of array elements, along with several programming examples and assignments. Students are tasked with writing C++ programs to manipulate arrays, including operations like multiplication and summation.

Uploaded by

Zaid Mustafa
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/ 17

‫جامعة النهرين‪/‬كلية الهندسة‬

‫قسم هندسة الطب الحياتي‬

‫‪Array of One‬‬
‫‪Dimension‬‬

‫‪Lec-4‬‬

‫‪1st year‬‬
‫‪2nd Semester‬‬

‫‪1‬‬
Objectives

• Learn the student the use of one dimension arrays used in C++.

• Write several C++ program which use of one dimension arrays.


Array of One Dimension
Arrays

• An array is a consecutive group of homogeneous memory locations. Each element (location)


can be referred to using the array name along with an integer that denotes the relative position
of that element within the array.

• Array of One Dimension

• It is a single variable specifies each array element. The declaration of one dimensional arrays
is:
Array of One Dimension

• Examples:

int age [10];

int num [30];

float degree[5];

char a [15];

• The item in an array are called elements. The elements in an array are of the same
type only the values vary.
Initializing Array
Elements
Initializing Array Elements
• The first element of array age:

age [0] = 18;

• The last element of array

age: age [9] = 19;

• All elements of array age:

age [9] = { 18, 17, 18 ,18 ,19, 20 ,17, 18 ,19 };


int x [ ] = { 12, 3, 5, 0, 11, 7, 30, 100, 22 };
Accessing Array
Elements
Accessing Array Elements
• We access each array element by written name of array, followed by brackets
delimiting a variable (or constant) in the brackets which are called the array index.

1. Accessing the first element of array num to variable x:


x = num [0];

2. Accessing the last element of array num to variable y:


y = num [9];
cout << num [0] + num [9];
num [0] = num [1] + num[2];
Accessing Array Elements
• Example: Write C++ program to display 2nd and 5th elements of array distance.
Procedure
Procedure
• Example 1: Write C++ program to read 5 numbers and print it in reverse order.
Procedure
• Example 2: Write C++ program, to find the summation of array elements.
Procedure
• Example 3: Write a C++ program, using function, to find the value of array C
from add array A and array B. C[ i ] = A [ i ] + B [ i ];
Procedure
• Example 4: Write C++ program, to find the summation of odd numbers in 1D-array.
Homework and
Assignment
Homework and Assignment

1. Write a C++ program, using function, to multiply the array elements by 2.


A[ i ] = A [ i ] * 2;

2. Write a C++ program, using function, to compute the number of zeros in the array.

3. Write a C++ program, using function, to reads temperatures over the 30 days in
an array and calculate the average of them.

You might also like