0% found this document useful (0 votes)
0 views3 pages

C Labs-2

The document outlines a series of exercises for a C++ programming lab focused on computer vision and software engineering. It includes tasks such as creating header and source files, implementing functions for basic operations, handling arrays, and working with recursive and overloaded functions. The exercises progressively build skills in function implementation, argument passing, and matrix operations.

Uploaded by

hari190804
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)
0 views3 pages

C Labs-2

The document outlines a series of exercises for a C++ programming lab focused on computer vision and software engineering. It includes tasks such as creating header and source files, implementing functions for basic operations, handling arrays, and working with recursive and overloaded functions. The exercises progressively build skills in function implementation, argument passing, and matrix operations.

Uploaded by

hari190804
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/ 3

Master Computer Vision – Software Engineering C++ Labs2

Preliminaries

Create and add to your project two files called Labs2.h and Labs2.cpp.
(in code::blocks, you can add "virtual folders" to separate headers and cpp files...)

Labs2.h file is your first personal header file and is going to contain all the signatures of the
procedures and functions that will be implemented in the Labs2.cpp file. It may also contain
variable definitions. For instance:

Within the .h file Within the .cpp


int MyFunction1 (int, int); #include <iostream>
void MyFunction2 (float); using namespace std;
int dumvar = 3; #include "my.h file"

MyFunction1 (int a, int b) {


return (a+b+dumvar);
}

MyFunction2(float x) {
if (x == 0) cout <<"x is null"<<endl;
else cout <<"x is not null"<<endl;
}

Notice the differences:


1. In the .h file: for the function signatures (also known as prototypes) the variable name is not
mandatory, only the type identifiers matter
2. In the .cpp file: do not forget to include the corresponding header file, and include other
headers if needed (iostream or cmath today). Personal header files are included with " ",
standard header with <>.

Include your header within the main.cpp file.

Now, from the main.cpp file, you can call any function declared in Labs2.h and implemented in
Labs2.cpp, without taking care of the order of implementation.

Exercice 1 - Beginning

Within the main function: declare two integers a nd b, ask the user to enter these two variables, and
display them on the console screen.

Exercice 2 - Elementary functions

Implement two functions min and max which take 2 parameters and return the minimum and
maximum values, respectively. Call both functions from the main function using values stored in
variables a and b, and display the results.

Exercice 3 - Passing arguments by reference


Implement two functions called swap_1(int, int) and swap2( int &, int &) that are supposed to swap
two values. Display the final values just before the end of each function, and display the results
from the main function, before and after the call. What can you remark?

Exercice 4 – Multiple returned values

The keyword return can only return one value. You may sometimes need to return several values,
sometimes an error code and several results as well. To do so, pass arguments by references to
"return" multiple results.

As an exercice, declare and implement a function SQ(...) that returns simultaneously the square
and the square root of a number, and an error code if the square root can not be computed. Include
<cmath> for the sqrt() function.

Exercice 5 – Default parameters

Implement a function IsMultipleOf(...) that determines if a number p is a multiple of number


q. By default, q will be assigned to 2. In the main function, test this function with a given value for
q, or without providing a value for q.

Exercice 6 – Overloaded functions

Implement 3 overloaded functions named Sums(...) that return the sum of all the parameters. The
first will use 2 integers, the second 2 floats, and the third 3 floats.

Exercice 7 – Recursive functions

A recursive functions is a function that calls itself during its execution. Declare and implement
function called RecursiveFactorial(...) to compute the factorial of a number defined as :
p
p != p∗ p−1∗...∗2∗1=∏i =1 i

Exercice 8 – Monodimensional array

Declare an array called Tab1 of 10 integers, where each element of index i contains the value i!
Display the complete array.

Exercice 9 – Bidimensional array – Pascal's triangle revisited

Declare a bidimensional array Tab2 that contains 8x8 integers. Initialize the first value (Tab2[0]
[0]) to 1, and all the other to 0.

In the Pascal triangle, sne can remark that the following relations hold:
Tab[i][0] = 1 and Tab[i][j] = Tab[i -1][j] + Tab[i -1][j -1] if j>0
Completely fill the array using the previous relationship, then display it. You should get the
following result:
1 0 0 0 0 0 0 0
1 1 0 0 0 0 0 0
1 2 1 0 0 0 0 0
1 3 3 1 0 0 0 0
1 4 6 4 1 0 0 0
1 5 10 10 5 1 0 0
1 6 15 20 15 6 1 0
1 7 21 35 35 21 7 1

Exercice 10 – Simple array as a function parameter


Reconsider Ex8, but now you are going to pass the array Tab1 as a parameter to the function
FillIt. Display the array before and after the call to the function FillIt.
What is the syntax main difference? Is the array modified?

Exercice 11 – Multidimensional arrays as functions parameters (read and write).


In the main function:
Declare and initialize two bidimensional arrays, A and B, with 3x3 elements.Values are up to you to
verify the computations. Declare and initialize one bidimensional array, C, with 3x3 null elements.
Declare and implement a function MultMatrix(...) that has A, B, C as parameters. After the
function call, matrix C should contain the results of the matricial product of A x B.

You might also like