Bui-Csc 201 Lecture Note 4
Bui-Csc 201 Lecture Note 4
#include <iostream>
int main() {
double input;
std::cout << "Enter number: ";
1|Page
std::cin >> input;
double diff;
// Compute a provisional square root
double root = 1.0;
do { // Loop until the provisional root is close enough to
the actual root
root = (root + input/root) / 2.0;
std::cout << "root is " << root << '\n';
// How bad is the approximation?
diff = root * root - input;
}
while (diff > 0.0001 || diff < -0.0001);
// Report approximate square root
std::cout << "Square root of " << input << " = " <<
root << '\n';
}
2|Page
To use any inbuilt function, you must include the header file that
contains the function needed.
Max and min function max (value1, value2) and min (value1, value2)
can be used to compute the maximum or minimum between two values.
It could be found in algorithm header file
Every program that employs a user-defined function must have three
features:
i. Function declaration: the process of informing your compiler
that function will be used is called function declaration. It entails
specifying its return value, function name and its argument type. Its
syntax is:
Return_Data_Type function_name (list_of_parameters);
Function declaration must be terminated with a semicolon.
ii. Function definition: this entails making the function active
by embedding the required statements. This can be
included before or after the main function.
3|Page
After main function : Before main function:
void function_name(); return_type
Function_name(optional_parameters)
main() {
{ //lines of code
….. }
Function call()
} main()
Function_name() {
{ Function call()
//lines of code }
}
iii. Function calls: this entails invoking the function to act at a certain
point in our program by specifying the function name followed by a
4|Page
5|Page
#include <iostream>
using namespace std;
int main()
{ // converts a floating point number into integer and fractional
part
void intfrac (float, float&, float&); //function declaration
float number, intpart, fracpart; // normal variables declaration
do {
cout << “\nEnter a real number: “;
cin >> number;
intfrac (number, intpart, fracpart); // function call
cout << “Integer part is “<< intpart<< “, fraction part is “<<
fracpart << endl;
}
while (number! = 0.0);
return 0;
} //function definition
// finds integer and fractional parts of real number
void intfrac (float n, float& intp, float& fracp)
{
long temp = static_cast<long>(n); //convert the real number to
long integer,
intp = static_cast<float>(temp); //converts the number back to
float
fracp = n - intp; //subtract integer part from the real number
}
Note:
The & indicates that intp is an alias—another name—for whatever
variable is passed as an argument
6|Page
While intpart and fracpart are passed by reference, the variable
number is passed by value.
7|Page
8|Page
Programming Task
Using any control structure and function where
applicable; write a C++ program to generate a
multiplication table. Such that:
1 2 3 4 5
1 1 2 3 4 5
2 2 4 6 8 10
3 3 6 9 12 15
4 4 8 12 16 20
5 5 10 15 20 25
9|Page
READING ASSIGNMENT II
A. Textbooks to Consult:
i. Chapter Two of Object-Oriented Programming in C++
ii. Chapter Two of Shaum’s Outline Series
B. Instructions:
i. You are expected to answer all these in your own words.
ii. You are strongly advised against copying words for words from the
textbook.
iii. Discuss among yourselves but don’t be tempted to allow your friend to
copy your solutions
C. Study Questions
i. Write exhaustively on why you need to employ functions in your
programs?
ii. Itemize the three elements of a function in a program.
iii. Discuss the two ways of defining a function? What are the observed
advantages or disadvantages of these two ways?
iv. Differentiate between a function definition and a function declaration.
v. Explain the process behind passing arguments by value.
vi. In a function declaration line, can you include the data type of the
parameters alone or you must also include the name of the
parameters.
vii. Of what importance is the function return type in a function
declaration?
viii. Of what importance is the return statement in a function definition?
ix. State the difference between the float data type in the following
function declaration: float area (float);
x. How do you handle instances when you need to return more than one
value at the same time from a function?
xi. What is the default return type of all compilers?
10 | P a g e
xii. Itemize the difference between passing arguments by value and
passing arguments by reference.
xiii.
References
1. Robert Lafore (2002), “Object-Oriented Programming in C++”, Sams
Publishing Fourth Edition
2. John R. Hubbard (2000), “Schaum’s Outline of Theory and Problems of
Programming with C++”, McGraw-Hill Publishers, Second Edition.
SOLUTION
#include <iostream>
using namespace std;
int main()
{
int FirstNum, SecNum, m = 60, n = 7;
cout << "Supply your first number " << endl;
cin >> FirstNum;
cout << "Supply your second number " << endl;
cin >> SecNum;
cout << "Their sum is " << (FirstNum + SecNum) << endl;
cout << "Their difference is " << (FirstNum - SecNum) << endl;
cout << "Their product is " << (FirstNum * SecNum) << endl;
cout << "Their quotient is " << (FirstNum / SecNum) << endl;
cout << "Their remainder is " << (FirstNum % SecNum) << endl;
}
11 | P a g e
12 | P a g e