You are on page 1of 5

EXPERIMENT 5 Date Perform: ________________

To illustrate the concept of passing arrays in functions and manipulate


them

Objectives:
 Able to pass arrays in functions and manipulate them
 Able to understand problem statements

Equipment Required:
 Computer with Visual Studio 2015

Background:

You can pass array as an argument to a function just like you pass variables as arguments. In
order to pass array to the function you just need to mention the array name during function
call like this:
function_name(array_name);

Example: Passing arrays to a function

In this example, we are passing two arrays a & b to the function sum(). This function adds the
corresponding elements of both the arrays and display them.

Code:
#include <iostream>
using namespace std;
/* This function adds the corresponding
 * elements of both the arrays and
 * displays it.
 */
void sum(int arr1[], int arr2[]){
int temp[5];
for(int i=0; i<5; i++){
temp[i] = arr1[i]+arr2[i];
cout<<temp[i]<<endl;
}
}
int main(){
int a[5] = {10, 20, 30, 40 ,50};
int b[5] = {1, 2, 3, 4, 5};
//Passing arrays to function
sum(a, b);
return 0;
}
Output:
11
22
33
44
55

TASKS:

Task 1:
Write a function named "locationOfTarget" that takes as its arguments the following:
 An array of integer values.
 An integer that tells how many integer values are in the array.
 An integer "target value".
The function should determine whether the given target value occurs in any of the cells of the
array, and if it does, the function should return the subscript of the cell containing the target
value. If more than one of the cells contains the target value, then the function should return
the largest subscript of the cells that contain the target value. If the target value does not
occur in any of the cells, then the function should return the sentinel value -1. Thus, for
example, if the target value that's passed to the function is 34 and the array that's passed to the
function looks like this:

Then the target value occurs in cells 3 and 5, so the function should return the integer value 5.

Task 2:
Write a function named "reverse" that takes as its arguments the following:
 An array of floating point values.
 An integer that tells how many floating point values are in the array.
The function must reverse the order of the values in the array.

Task 3:
Write a function named "sum" that takes as its arguments the following:
 An array of floating point values.
 An integer that tells how many floating point values are in the array.
The function should return as its value the sum of the floating point values in the array.

Task 4:
Write a function “SumOfAlternate” in C++ to find and return the sum of elements from all
alternate elements of a two dimensional array passed as argument with size, starting from [0]
[0].

For Example:
The function should add elements B[0][0], B[0][2], B[1][1], B[2][0] and B[2][2].

Task 5:
Write a function in C++ which accepts a 2D array of integers and its size as arguments and
displays the elements which lie on diagonals.
For example: if the array content is
543
678
129
Output through the function should be:
Diagonal One: 5 7 9
Diagonal Two: 3 7 1

CL214 PROGRAMMING FUNDAMENTALS LAB


The focus of this lab is to cover and apply the basic concepts of Object-Oriented
Programming (OOP) in detail and implement complex problems using OOP concepts in C++
using Microsoft Visual Studio.
CLO Statement ↓ Exemplary Proficient Developing Beginning Novice
Score → (5) (4) (3) (2) (1)

Demonstrate Attempts Attempts Understand


elementary Attempts all
1 partial lab lab tasks the given
skills to write lab tasks No
tasks with with concept but
effective C++ with correct attempt
correct incorrect unable to
programs output
output output apply it
using various
techniques.

Behaves
responsibly Completes
Completes
3 and the lab task
Completes the lab task Poorly
individually with help of Does not
the lab task with minor attempts
performs the instructor work
on his own help of lab tasks
lab. and students
instructor
around

CLO MARKS OBTAINED


1
3

You might also like