COS1511 - Assignment 02 – 2022
Due date
QUESTION 1
(i) void check(int first, float second);
(ii) float mult(float first, float second;
(iii) void time(int &seconds, int &minutes, int &hours);
(iv) void countChar(char c, string s);
QUESTION 2
(i)
The function function1 consist of a cout statement and a nested function named function2. In C++ it is not allowed
to have nested functions.
To correct the error, function2 must be defined as a function and be called from function1.
#include <iostream>
using namespace std;
int function1()
{
cout << "Inside function function1 " << endl;
int y = function2();
}
int function2()
{
cout << "Inside function function2" << endl;
}
int main ()
{
int call = function1();
return 0;
}
(ii)
The function sum is declared to return a value of type integer. There is no return statement in the function. To
correct the error, add the return statement as follows:
int sum(int x, int y)
My Assignments 27682021794 www.myassignments.co.za
Due date
QUESTION 1
(i) void check(int first, float second);
(ii) float mult(float first, float second;
(iii) void time(int &seconds, int &minutes, int &hours);
(iv) void countChar(char c, string s);
QUESTION 2
(i)
The function function1 consist of a cout statement and a nested function named function2. In C++ it is not allowed
to have nested functions.
To correct the error, function2 must be defined as a function and be called from function1.
#include <iostream>
using namespace std;
int function1()
{
cout << "Inside function function1 " << endl;
int y = function2();
}
int function2()
{
cout << "Inside function function2" << endl;
}
int main ()
{
int call = function1();
return 0;
}
(ii)
The function sum is declared to return a value of type integer. There is no return statement in the function. To
correct the error, add the return statement as follows:
int sum(int x, int y)
My Assignments 27682021794 www.myassignments.co.za