COS1511 ASSIGNMENT 2
Due: Monday, 27 May 2024, 11:00 PM
SEMESTER 1 2024
PASS WITH DISTINCTIONS. FORTIS PASSUM
FOR FURTHER ASSISTANCE WITH ECONOMICS,
MANAGEMENT, ACCOUNTING, TAXATION EDUCATION@ LLB
MODULES.
CALL/WHATSAAP0816884518/0816884519
EMAIL:
,INP1501 ASSIGNMENT 2
Question 1
(i) For the function `check`, which takes an integer and a floating point number and returns no value:
python
def check(num: int, value: float) -> None:
pass
(ii) For the function `mult`, which takes two floating point numbers and returns their product:
python
def mult(a: float, b: float) -> float:
return a * b
(iii) For the function `time`, which takes seconds, minutes, and hours and returns them:
python
def time(seconds: int, minutes: int, hours: int) -> tuple:
return (seconds, minutes, hours)
(iv) For the function `countChar`, which counts the occurrences of a character in a string:
python
def countChar(s: str, char: str) -> int:
return s.count(char)
Question 2
Identifying errors and suggesting corrections:
(i) Nested Function Declaration Error:
c++
int function1()
{
, cout << "Inside function function1 " << endl;
int function2()
{
cout << "Inside function function2 " << endl;
}
}
Correction:
Functions cannot be nested in C++. Define `function2` outside of `function1`.
c++
int function2()
{
cout << "Inside function function2 " << endl;
}
int function1()
{
cout << "Inside function function1 " << endl;
}
(ii) Missing Return Statement:
c++
int sum(int x, int y)
{
int result;
result = x + y;
}
Correction:
A function declared to return `int` should explicitly return an integer value.
Due: Monday, 27 May 2024, 11:00 PM
SEMESTER 1 2024
PASS WITH DISTINCTIONS. FORTIS PASSUM
FOR FURTHER ASSISTANCE WITH ECONOMICS,
MANAGEMENT, ACCOUNTING, TAXATION EDUCATION@ LLB
MODULES.
CALL/WHATSAAP0816884518/0816884519
EMAIL:
,INP1501 ASSIGNMENT 2
Question 1
(i) For the function `check`, which takes an integer and a floating point number and returns no value:
python
def check(num: int, value: float) -> None:
pass
(ii) For the function `mult`, which takes two floating point numbers and returns their product:
python
def mult(a: float, b: float) -> float:
return a * b
(iii) For the function `time`, which takes seconds, minutes, and hours and returns them:
python
def time(seconds: int, minutes: int, hours: int) -> tuple:
return (seconds, minutes, hours)
(iv) For the function `countChar`, which counts the occurrences of a character in a string:
python
def countChar(s: str, char: str) -> int:
return s.count(char)
Question 2
Identifying errors and suggesting corrections:
(i) Nested Function Declaration Error:
c++
int function1()
{
, cout << "Inside function function1 " << endl;
int function2()
{
cout << "Inside function function2 " << endl;
}
}
Correction:
Functions cannot be nested in C++. Define `function2` outside of `function1`.
c++
int function2()
{
cout << "Inside function function2 " << endl;
}
int function1()
{
cout << "Inside function function1 " << endl;
}
(ii) Missing Return Statement:
c++
int sum(int x, int y)
{
int result;
result = x + y;
}
Correction:
A function declared to return `int` should explicitly return an integer value.