UECS1643 Fundamentals of Programming: 1. Run The Following Programs and Answer The Related Questions. (A)
UECS1643 Fundamentals of Programming: 1. Run The Following Programs and Answer The Related Questions. (A)
(a)
#include <iostream>
#include <cmath>
using namespace std;
int main(void)
{
double sqrt_num;
sqrt_num = sqrt(16.0);
return 0;
}
sqrt ( 16.0 )
What is the value passed to the function?
(b)
#include <iostream>
#include <cmath>
using namespace std;
int main(void)
{
double num, sqrt_num;
sqrt_num = sqrt(num);
return 0;
}
1
UECS1643 Fundamentals Of Programming
(c)
#include <iostream>
void using namespace std;
welcome(void)
void welcome(void);
{welcome();
void <<
cout welcome(void);
"** Welcome **" //
<< function
endl; prototype
}
int main(void)
{
welcome(); // function call
return 0;
}
// function definition
void welcome(void)
{
cout << "*** Welcome ***" << endl;
}
(d)
#include <iostream>
2
UECS1643 Fundamentals Of Programming
int main(void)
{
int number;
number = 456;
func(number); // second function call
return 0;
}
// function definition
void func(int num)
{
cout << num << endl;
}
First Function Call What is the actual parameter in the function call?
func(123);
What is the value passed to the formal parameter
Function Definition
num?
void func(int num)
{ Does the function func return any value?
cout << num << endl;
}
Second Function Call What is the actual parameter in the function call?
func(number);
Function Definition
What is the value passed to the formal parameter
num?
void func(int num)
{
cout << num << endl;
}
(e)
3
UECS1643 Fundamentals Of Programming
#include <iostream>
using namespace std;
int main(void)
{
pattern('a', 2.5); // function call
return 0;
}
// function definition
void pattern(char ch1, double num)
{
cout << ch1 << num << endl;
}
4
UECS1643 Fundamentals Of Programming
2. The following programs contain errors. Compile the programs and answer the related questions.
(a)
#include <iostream>
using namespace std;
int main(void)
{
display(3, 4, 5); // function call
return 0;
}
// function definition
void display(int num)
{
cout << num << endl;
}
Compile the program. What compiler error message do you get? What is wrong?
(b)
#include <iostream>
using namespace std;
int main(void)
{
display(3); // function call
return 0;
}
// function definition
void display(int num1, int num2)
{
cout << num1 << " and " << num2 << endl;
}
Compile the program. What compiler error message do you get? What is wrong?
(c)
#include <iostream>
5
UECS1643 Fundamentals Of Programming
int main(void)
{
display(3, 8); // function call
return 0;
}
// function definition
void display(int num1, num2)
{
cout << num1 << " and " << num2 << endl;
}
Compile the program. What compiler error message do you get? What is wrong?
(d)
#include <iostream>
using namespace std;
int main(void)
{
int number;
number = 5;
display(int number); // function call
return 0;
}
// function definition
void display(int num)
{
cout << num << endl;
}
Compile the program. What compiler error message do you get? What is wrong?
(e)
#include <iostream>
6
UECS1643 Fundamentals Of Programming
int main(void)
{
display(3, 8); // function call
return 0;
}
// function definition
void display(int num1, int num2)
{
cout << num1 << " and " << num2 << endl;
}
Compile the program. What compiler error message do you get? What is wrong?
(f)
#include <iostream>
using namespace std;
int main(void)
{
display(3, 8); // function call
return 0;
}
// function definition
void display(int num1, int num2)
{
cout << num1 << " and " << num2 << endl;
}
Compile the program. What compiler error message do you get? What is wrong?
(g)
#include <iostream>
int main(void)
7
UECS1643 Fundamentals Of Programming
{
display(3, 8); // function call
return 0;
}
// function definition
void display(int num1, int num2);
{
cout << num1 << " and " << num2 << endl;
}
Compile the program. What compiler error message do you get? What is wrong?
(h)
#include <iostream>
using namespace std;
int main(void)
{
void display(3, 8); // function call
return 0;
}
// function definition
void display(int num1, int num2)
{
cout << num1 << " and " << num2 << endl;
}
Compile the program. What compiler error message do you get? What is wrong?
8
UECS1643 Fundamentals Of Programming
#include <iostream>
#include <iomanip>
#include <cmath> // for Math library functions
#include <cstdlib> // for random number generation functions
using namespace std;
int main(void)
{
double number1, number2, power;
number1 = 9.0;
number2 = 1.23;
power = 3;
return 0;
}
9
UECS1643 Fundamentals Of Programming
#include <iostream>
using namespace std;
// Function prototypes
void draw_horizontal(void);
void draw_parallel(void);
int main(void)
{
// Function calls
draw_horizontal();
draw_parallel();
draw_horizontal();
return 0;
}
// Function definitions
/*
* Draws a horizontal line
*/
void draw_horizontal(void)
{
cout << "--------\n";
}
/*
* Draws parallel lines
*/
void draw_parallel(void)
{
cout << "| |\n";
cout << "| |\n";
cout << "| |\n";
}
--------
| |
| |
| |
--------
10
UECS1643 Fundamentals Of Programming
5. Complete the main function in the program below to draw the following figure based on the structure chart
given below.
main
#include <iostream>
using namespace std;
// Function prototypes
void draw_horizontal(void);
void draw_parallel(void);
void draw_intersect(void);
int main(void)
{
// write the function calls to draw the figure
draw_parallel();
draw_horizontal();
draw_intersect();
return 0;
}
/*
* Draws a horizontal line
*/
void draw_horizontal(void)
{
cout << "--------\n";
}
/*
* Draws parallel lines
*/
void draw_parallel(void)
{
cout << "| |\n";
cout << "| |\n";
cout << "| |\n";
}
11
UECS1643 Fundamentals Of Programming
/*
* Draws intersecting lines
*/
void draw_intersect(void)
{
cout << " /\\ \n";
cout << " / \\ \n";
cout << " / \\ \n";
}
main
draw_triangle draw_parallel
draw_intersect draw_horizontal
Write the program. Your program must have the following functions:
main
draw_horizontal
draw_parallel
draw_intersect
draw_triangle
#include <iostream>
using namespace std;
// Function prototypes
void draw_horizontal(void);
void draw_parallel(void);
void draw_intersect(void);
int main(void)
{
// write the function calls to draw the figure
draw_intersect();
draw_horizontal();
12
UECS1643 Fundamentals Of Programming
draw_parallel();
return 0;
}
/*
* Draws a horizontal line
*/
void draw_horizontal(void)
{
cout << "--------\n";
}
/*
* Draws parallel lines
*/
void draw_parallel(void)
{
cout << "| |\n";
cout << "| |\n";
cout << "| |\n";
}
/*
* Draws intersecting lines
*/
void draw_intersect(void)
{
cout << " /\\ \n";
cout << " / \\ \n";
cout << " / \\ \n";
}
2. Consider the program where the function main asks the user for the number of apples and the number of
oranges, computes, and displays the totals in the format shown below. The x’s represent values entered by the
user or computed by the program.
Output Format:
=======My Fruit Store=======
Fruit Qty Price Total
----------------------------
Apple xx RM0.85 RMxx.xx
Orange xx RM1.00 RMxx.xx
----------------------------
Total RMxx.xx
Write the statements in function main to call the other functions and display the output.
#include <iostream>
#include <iomanip>
13
UECS1643 Fundamentals Of Programming
// function prototypes
void display_heading(void);
void display_apple_item(int qty, double total);
void display_orange_item(int qty, double total);
void display_grand_total(double grand_total);
int main(void)
{
int apple_qty, orange_qty;
double apple_total, orange_total, grand_total;
return 0;
}
void display_heading(void)
{
cout << "=========My Fruit Store========\n";
cout << "Fruit Qty Price Total\n";
cout << "-------------------------------\n";
}
14
UECS1643 Fundamentals Of Programming
1. Write a program that displays the initials of your name. Use functions to display each letter. A sample run of
the program is as follows (assuming the initials are HES):
H H
H H
HHHHH
H H
H H
EEEEE
E
EEEEE
E
EEEEE
SSSSS
S
SSSSS
S
SSSSS
#include <iostream>
using namespace std;
// Function prototypes
void draw_h(void);
15
UECS1643 Fundamentals Of Programming
void draw_e(void);
void draw_s(void);
int main(void)
{
// write the function calls to draw the figure
draw_h();
draw_e();
draw_s();
return 0;
}
/*
* Draws h
*/
void draw_h(void)
{
cout << "H H\n";
cout << "H H\n";
cout << "HHHHH\n";
cout << "H H\n";
cout << "H H\n\n";
}
/*
* Draws e
*/
void draw_e(void)
{
cout << "EEEEE\n";
cout << "E \n";
cout << "EEEEE\n";
cout << "E \n";
cout << "EEEEE\n\n";
}
/*
* Draws s
*/
void draw_s(void)
{
cout << "SSSSS\n";
cout << "S \n";
cout << "SSSSS\n";
cout << " S\n";
cout << "SSSSS\n\n";
}
16