0% found this document useful (0 votes)
24 views8 pages

CP 123

Uploaded by

joseden543
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)
24 views8 pages

CP 123

Uploaded by

joseden543
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/ 8

Question.

I. E
II. C
III. D
IV. B
V. A
VI. A
VII. B
VIII. D
IX. C
X. A

Question. 2

I. I
II. O
III. F
IV. G
V. E
VI. J
VII. P
VIII. H
IX. N
X. M

Question 3.

a. I.char grade=’A’

II.bool paymentStatus=true;

III.int juiceBotleSold=0;

IV.double billingAmount=0.0;

V.float GPA=0.0;

b. I.int temp=10; char letter=’A’;

II.temp=temp+5;

III.double payRate=12.50;

IV.int tempNum=firstNum;

V.int tempSwap=W; W=Z; Z=tempSwap;

Question 4.

a.I.marks
II. double values

III.28

IV.29

b. While loop is used when number of iteration is known ,also has condition statement at the
beginning.while

For loop is used when number of iteration is known,also has initialization, condition,and
increment/decrement statement.

c. sum=sum+values[iI;

d . #include <iostream>

using namespace std;

int main() {

int values[5];

int sum=0;

cout << “enter values of 5 numbers” << endl;

for (int i=0; i<5; i++){

cin >> values[i];

} Sum=values[0]+values[1]+values[2]+values[3]+values[4];

Cout<< “Sum of all numbers=”<<sum;

Return 0;

Question 5.

a. 1. net_price_per_copy: float

2. estimated_copies_sold: int

3. fixed_royalty_upon_delivery: float

4. fixed_royalty_upon_publication: float

5. royalty_rate_option2: float
6. royalty_rate_first_4000: float

7. royalty_rate_above_4000: float

b .#include <iostream>

#include <iomanip>

Using namespace std;

Int main() {

Const float FIXED_ROYALTY_DELIVERY = 10’000’000.0;

Const float FIXED_ROYALTY_PUBLICATION = 50’000’000.0;

Const float ROYALTY_RATE_OPTION2 = 0.125;

Const float ROYALTY_RATE_FIRST_4000 = 0.10;

Const float ROYALTY_RATE_ABOVE_4000 = 0.14;

Float net_price_per_copy;

Int estimated_copies_sold;

Cout << “Enter the net price of each copy of the novel (in Tshs): “;

Cin >> net_price_per_copy;

Cout << “Enter the estimated number of copies that will be sold: “;

Cin >> estimated_copies_sold;

Float total_royalty_option1 = FIXED_ROYALTY_DELIVERY + FIXED_ROYALTY_PUBLICATION;

Float total_royalty_option2 = estimated_copies_sold * net_price_per_copy *


ROYALTY_RATE_OPTION2;

Float total_royalty_option3;

If (estimated_copies_sold <= 4000) {

Total_royalty_option3 = estimated_copies_sold * net_price_per_copy *


ROYALTY_RATE_FIRST_4000;

} else {

Float royalty_first_4000 = 4000 * net_price_per_copy * ROYALTY_RATE_FIRST_4000;

Float royalty_above_4000 = (estimated_copies_sold – 4000) * net_price_per_copy *


ROYALTY_RATE_ABOVE_4000;

Total_royalty_option3 = royalty_first_4000 + royalty_above_4000;

}
Cout << fixed << setprecision(2); // Formatting the output to 2 decimal places

Cout << “\nRoyalties under each option:” << endl;

Cout << “Option 1: Tshs “ << total_royalty_option1 << endl;

Cout << “Option 2: Tshs “ << total_royalty_option2 << endl;

Cout << “Option 3: Tshs “ << total_royalty_option3 << endl;

Return 0;

c.

Question 6.

a.Output:

0(repeated indefinitely due to the infinite loop)

b.output:

27

c.Outputs:

0
1

d.

e. Syntax error

Error : Incorrect cout operator

Cout uses << for output, but >> is incorrectly

Should be written as

#include<iostream>

Using namespace std;

Int main() {

If (0) {

Cout << “Hi”;

else

// else block can stay empty or add a comment

Cout << “Bye”;

Return 0;

Question 7:

a.The program will throw an error, specifically a ValueError, because it cannot convert a string into an
integer.

b.#include <iostream>

using namespace std;


int main() {

int num1, num2;

char operation ;

int addition ;

int subtraction ;

int multiplication ;

cout << “Enter the first integer: “;

cin >> num1;

cout << “Enter the second integer: “;

cin >> num2;

// Display the operation choices

Cout << “Select an operation to perform:\n”;

Cout << “1. Addition (+)\n”;

Cout << “2. Subtraction (-)\n”;

Cout << “3. Multiplication (*)\n”;

Cout << “4. Division (/)\n”;

Cout << “Enter your choice (+, -, *, /): “;

Cin >> operation;

// Perform the corresponding arithmetic operation (operation) {

If(addition){

Cout << num1 << “ + “ << num2 << “ = “ << (num1 + num2) << endl;

If(subtraction){

Cout << num1 << “ – “ << num2 << “ = “ << (num1 – num2) << endl;

If(multiplication){

Cout << num1 << “ * “ << num2 << “ = “ << (num1 * num2) << endl;

If (num2 != 0){
Cout << num1 << “ / “ << num2 << “ = “ << (num1 / num2) << endl;

Else{

Cout << “Error: Division by zero is not allowed.” << endl;

Cout << “Invalid operation. Please select one of +, -, *, /.” << endl;

Return 0;

d.

Question 8.

a.Meaningful Identifiers and Data Types

I.User’s input integer:

Identifier: userInput

Data Type: int

II.Remainder value:

Identifier: remainder

Data Type: int

III.Continue flag:

Identifier: continueFlag

Data Type: char

IV.Input validity check:

Identifier: validInput

Data Type: bool

V.Termination flag:

Identifier: terminateFlag

Data Type: bool


b.

You might also like