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

T4 Guidelines

Uploaded by

sorkaiho
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)
7 views3 pages

T4 Guidelines

Uploaded by

sorkaiho
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

SEHH2042 Computer Programming

Tutorial 4 – Solution Guidelines

Q1. Follow the steps in the tutorial notes, your program should look like below:

#include <iostream>
using namespace std;

int main()
{
// declare variables
int input;

// prompt for user input


cout << "Enter a positive integer: ";
cin >> input;

// when input is positive


while (input > 0) {
// display factors by loop
cout << "Factors of " << input << ": ";
for (int n = 1; n <= input; n++) {
// check if n is a factor and display.
if (input % n == 0)
cout << n << " ";
}
cout << endl;

// then ask for next input


cout << "Enter a positive integer: ";
cin >> input;
}

// exit loop means input is negative


cout << "Only positive integer is accepted. Program ends.";

return 0;
}

SEHH2042 Computer Programming – Tutorial 4 (Guidelines) 1


Q2. 1. Include all the necessary header file(s)
2. Declare integer variable: count(= 0)
3. Add an outer "for" loop for trying number one by one in the range 2 to 200:
for (n = 2; n <= 200; n++){
// Codes for steps 4 onwards are put here
}
4. In the outer "for" loop, declare a boolean variable (prime = true) for storing the checking
result.
5. Add an inner "for" loop for testing whether n is a prime number:
for (i = 2; i < n; i++){
if (n % i == 0) {
prime = false;
break;
}
}
6. Inside and at the end of the outer "for" loop, print the number if it is prime and print endl when
necessary:
if (prime) {
cout << setw(5) << n;
count++;

if (count % 10 == 0) cout << endl;


}

Q3. 1. Include all the necessary header file(s)


2. Print the header of the table as follows:
cout << setw(10) << "Terms" << setw(20) << "Value of PI" << endl;
cout << setw(10) << "-----" << setw(20) << "-----------" << endl;
3. Set 15 decimal places: cout << fixed << setprecision(15);
4. Add an outer "for" loop for showing the number of terms used:
for (n = 10; n <= 100000; n *= 10){
// Codes for steps 5 onwards are put here
}
5. In the outer "for" loop, declare a double variable (pi = 0) for storing the result.
6. Add an inner "for" loop for calculating the value of pi. The inner "for" loop is shown below:
for (int i = 1; i <= n; i++) {
if (i % 2 == 1)
pi = pi + 4.0 / (2 * i - 1);
else
pi = pi - 4.0 / (2 * i - 1);
}
7. Inside and at the end of the outer "for" loop, add the cout statement just after the inner "for"
loop as below:
cout << setw(10) << n << setw(20) << pi << endl;

SEHH2042 Computer Programming – Tutorial 4 (Guidelines) 2


Q4. 1. Declare integer variable: size
2. Prompt user input for size
3. Use nested for loop as follows to print the pattern:
for (int row = 1; row <= size; row++) {
for (int col = 1; col <= size; col++) {

// Use if-else to check coordinate


// and print * or space accordingly

}
cout << endl; // end the line of inner loop
}
4. Insert the following codes into the nested loop:

(a)
if (row == 1 || row == size || col == 1 || col == size)
cout << "*";
else
cout << " ";

(b)
if (row == 1 || row == size || row == col)
cout << "*";
else
cout << " ";

(c)
if (row == 1 || row == size || row + col == size + 1)
cout << "*";
else
cout << " ";

(d)
if (row == 1 || row == size || row == col || row + col == size + 1)
cout << "*";
else
cout << " ";

(e)
if (row == 1 || row == size || col == 1 || col == size
|| row == col || row + col == size + 1)
cout << "*";
else
cout << " ";

SEHH2042 Computer Programming – Tutorial 4 (Guidelines) 3

You might also like