Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
10 views7 pages

Fundamental of Programming I

The document is an assignment for the Department of Mathematics at Wollea University, submitted by Kume Disea Fagessa. It includes various C++ programming tasks such as writing statements for incrementing variables, performing calculations, and predicting outputs of code fragments. Additionally, it contains flowchart descriptions for determining number properties and calculating areas and averages.

Uploaded by

Getahun shanko
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views7 pages

Fundamental of Programming I

The document is an assignment for the Department of Mathematics at Wollea University, submitted by Kume Disea Fagessa. It includes various C++ programming tasks such as writing statements for incrementing variables, performing calculations, and predicting outputs of code fragments. Additionally, it contains flowchart descriptions for determining number properties and calculating areas and averages.

Uploaded by

Getahun shanko
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

WOLLEGA UNIVERSITY

DEPARTMENT OF MATHEMATICS

Fundamental of programming Assignment

Submitted by Kume Disea Fagessa.................................WU

Submission date: November, 09/2024


I. Answer the following questions accordingly

Answer:

1. Writing C++ Statements for Incrementing x

1. x = x + 1;
2. x += 1;
3. x++;
4. ++x;

2. Writing C++ Statements

a. Print the message "This is a C++ program" with each word on a separate line:

cout << "This\nis\na\nC++\nprogram" << endl;

b .Declare variables sum and x to be of type int:

int sum, x;

c. Set variable x to 1:

x = 1;

d. Set variable sum to 0:

sum = 0;

e. Add variable x to variable sum and assign the result to variable sum:

sum += x;

f. Print "The sum is: " followed by the value of variable sum:

cout << "The sum is: " << sum << endl;

g. In one statement, assign the sum of the current value of x and y to z and post-increment the
value of x:

z = x++ + y;

h. Determine whether the value of the variable count is greater than 10. If it is, print "Count is
greater than 10":

if (count > 10) {


cout << "Count is greater than 10" << endl;
}

i. Pre-decrement the variable x by 1, and then subtract it from the variable total:

total -= --x;

j. Calculate the remainder after q is divided by divisor and assign the result to q:

q %= divisor;

3. State the values of each variable after the calculation is performed. Assume that, when
each statement begins executing, all variables have the integer value 5.

a. product •= x++i
b. quotient /— -1-lx;

a. product *= x++i

This statement involves the multiplication assignment (*=) operator, which multiplies product
by the right-hand side of the equation and assigns the result back to product. Let's look at the
components:

 x++: This is the post-increment operator. It means that the value of x is used first, and
then x is incremented. Since x = 5 at the start, the value used in the calculation is 5, and
then x becomes 6.
 i: The value of i is 5 initially.

So the operation is:

product=product×(x++×i)\text{product} = \text{product} \times (x++ \times


i)product=product×(x++×i)

Substituting the values:

product=5×(5×5)\text{product} = 5 \times (5 \times 5)product=5×(5×5) product=5×25=125\


text{product} = 5 \times 25 = 125product=5×25=125
Now, after this operation:

 product = 125
 x = 6 (since x++ increments it after the multiplication)
 i = 5 (remains unchanged)

b. quotient /= -1 - x;

This statement involves the division assignment (/=) operator, which divides quotient by the
right-hand side expression and assigns the result to quotient. Let's break it down:

 x is now 6 after the previous operation.


 The expression -1 - x becomes:

−1−6=−7-1 - 6 = -7−1−6=−7

So, the operation is:

quotient=quotient/(−7)\text{quotient} = \text{quotient} / (-7)quotient=quotient/(−7)

Since quotient starts at 5, the operation is:

quotient=5/(−7)≈−0.714\text{quotient} = 5 / (-7) \approx -0.714quotient=5/(−7)≈−0.714

Now, after this operation:

 quotient ≈ -0.714
 x remains 6

Final values after both operations:

 product = 125
 quotient ≈ -0.714
 x=6
 i=5
4. Question on Code Fragment

For the code:

if (x < 10)
if (y > 100)
if (!done)
x = z;
else
y = z;
else
cout << "Error";

The last else statement is associated with if (x < 10) due to the way C++ associates else with the
nearest preceding unmatched if.

5. For Loop that Counts from 1000 to 0 by -2

for (int i = 1000;


i >= 0;
i -= 2) {
// Loop body
}
II. Flow Charts

a) Receive a number and determine whether it is positive, negative, or zero.

 Input: Number
 Decision: If the number > 0, print "Positive". If the number < 0, print "Negative". Else,
print "Zero".

b) Find the area of a circle where the radius is provided by the user.

 Input: Radius
 Calculation: Area = π * radius * radius
 Output: Area

c) Find the average of two numbers given by the user.

 Input: Number1, Number2


 Calculation: Average = (Number1 + Number2) / 2
 Output: Average

d) Add the even numbers between 0 and a positive integer provided by the user.

 Input: Integer
 Loop: For numbers from 0 up to input integer, add even numbers to a sum.
 Output: Sum of even numbers
III. Pedicting the Output of Code Fragments

a)

for (i = 1; i <= 10; i++) {


continue;
cout << "i*****\n";
}

 Output: Nothing will be printed because continue skips the cout statement.

b)

if (n > 0)
if (n < 10)
cout << "n is small";
else
cout << "n is negative";

 Assuming n = 20: The output will be "n is negative".

c)

for (i = 1; ; i++) {
cout << "EE...\n";
break;
}

 Output: "EE...\n" (one time, then exits the loop due to break).

d)

int x = 6, y = 2, z;
z = x + y++;
cout << --x << endl;
x += x;
cout << y++ << endl;
z /= 2;
cout << z; Step-by-Step Output Calculation:

Step1: z = 6 + 2 = 8 (post-increment y to 3 after assignment)

Step 2: cout << --x << endl; outputs 5 (x decremented to 5)

Step 3: x += x; doubles x to 10

Step 4: cout << y++ << endl; outputs 3 (y post-increments to 4)

Step 5: z /= 2; makes z = 4

Step 6: cout << z; outputs 4

You might also like