Faculty of Engineering

Computer Engineering Department

Islamic University of Gaza

C++ Programming Language Lab # 9

Arrays and Functions

Objective:

To be familiar with C++ Programming Language.

Operations on Arrays:

Each member of an array is a pseudo-variable and can be processed as such. This means that you can add the values of two members of the array(Number[2]+Number[0]), you can subtract the value of one of the members from another member(member[1]-Number[4]). In the same way, you can perform multiplication, division, or remainder operations on members of an array.

a type of operation regularly performed on an array consists of looking for a value held by one of its members. For example, you can try to know if one of the members holds a particular value you are looking for.

Here is an example:

One of the most regular operations performed consists of comparing the values of different members to get the lowest value of the members.

Here is an example:

Arrays and Functions:

An array can be passed to a function as argument. To declare and define that a function takes an array as argument, declare the function as you would do for any regular function and, in its parentheses, specify that the argument is an array.

Here is an example:

Notes:

v  You don't have to specify the dimension of the array. This means that you can leave the square brackets empty

v  When you declare and define a function that takes an array as argument, if you plan to process the array, for example, if you want the calling function to control the number of elements to be processed, you should/must pass another argument that will allow the function to know how many members of the array would be considered.

v  Passing an array variable means that the starting address of the array is passed to the formal parameter. The parameter inside the function references to the same array that is passed to the function. No new arrays are created. This is pass-by-reference.

Exercise :

const Parameters:

Passing arrays by reference makes sense for performance reasons. If an array is passed by value, all its elements must be copied into a new array. For large arrays, it could take some time and additional memory space. However, passing arrays by reference could lead to errors if your function changes the array accidentally. To prevent it from happening, you can put the const keyword before the array parameter to tell the compiler that the array cannot be changed. The compiler will report errors if the code in the function attempts to modify the array.

Exercise :

Lab work:

v  Apply the programming exercises practically on DEV C++ Program , and show the results to your instructor.

Home work:

v  Use a single-subscribed array to solve the following problem. read in 20 numbers, each of which is between 10 and 100,inclusive. As each number is read, print it only if it is not a duplicate of a number already read. provide for the "worst case" in which all 20 numbers are different . Use the smallest possible array to solve this problem.

v  What does the following program do?

v  A palindrome is a string that is spelled the same way forwards and backwards. some examples of palindromes are : "radar". write a recursive function testPalindrome that returns true if the string stored in the array is a palindrome, and false otherwise. The function should ignore spaces and punctuation in the string.

6