Upcoming SlideShare
×

# Labsheet2 stud

346
-1

Published on

Published in: Technology
0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Views
Total Views
346
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
6
0
Likes
0
Embeds 0
No embeds

No notes for slide

### Labsheet2 stud

1. 1. FP 201 PROGRAMMING FUNDAMENTALLAB 2: OPERATORS AND EXPRESSIONLearning Outcome:By the end of this lab, students should be able to: Understand operators, operator’s precedence and expression.Theory/ Topics A simple C++ program is similar to a C program. In C++ programs the statements to be executed arecontained inside the function. In operators and expressions, student must know about:a)Arithmetic operators ( +, -, *, /, % ) The five arithmetical operations supported by the C++ language are: + addition - subtraction * multiplication / division % modulusb)Compound assignment (+=, -=, *=, /=, %=, >>=, <<=, &=, ^=, |=)c) Increment and decrement (++, --)Shortening even more some expressions, the increase operator (++) and the decrease operator (--) increase or reduceby one the value stored in a variable. They are equivalent to +=1 and to -=1, respectively. Example 1 Example 2 B=3; B=3; A=++B; A=B++; // A contains 4, B contains 4 // A contains 3, B contains 4d) Relational and equality operators (==, !=, >, <, >=, <= )In order to evaluate a comparison between two expressions we can use the relational and equality operators. Theresult of a relational operation is a Boolean value that can only be true or false, according to its Boolean result.e) Logical operators ( !, &&, || )The Operator ! is the C++ operator to perform the Boolean operation NOT, it has only one operand, located at itsright, and the only thing that it does is to inverse the value of it, producing false if its operand is true and true if itsoperand is false. Basically, it returns the opposite Boolean value of evaluating its operand. Precedence of operatorsWhen writing complex expressions with several operands, we must follow the precedence which is what operand isevaluated first and which later. For example, in this expression: a=5+7%2 7%2 is evaluated first, then followed by operator +
2. 2. FP 201 PROGRAMMING FUNDAMENTALActivity 2AProcedure :Step 1: Type the programs given belowStep 2: Compile and run the program. Write the output.Step 3: Save the program as Lab2A.cpp.#include <iostream>using namespace std;int main(){int class1 = 100;int class2 = 200;int class3 = 300;int class4 = 400;int class5 = 500;int sum = 0;double average;sum = class1 + class2 + class3 + class4 + class5;average = sum/5;cout << " Sum = " << sum << endl;cout << " Average = " << average << endl;return 0;}Activity 2BProcedure :Step 1: Type the programs given belowStep 2: Compile and run the program. Write the output.Step 3: Save the program as Lab2B.cpp.#include <iostream>using namespace std;void main(){ int x = 180, y = 200; y = x++; cout << " x : " << x << endl << " y : " << y << endl;}Activity 2CProcedure :Step 1: Type the programs given belowStep 2: Compile and run the program. Write the output.Step 3: Save the program as Lab2C.cpp.#include <iostream>using namespace std;
3. 3. FP 201 PROGRAMMING FUNDAMENTALvoid main(){ int x = 180, y = 200; y = ++ x; cout << " x : " << x << endl << " y : "<< y << endl;}Activity 2DProcedure :Step 1: Type the programs given belowStep 2: Compile and run the program. Write the output.Step 3: Save the program as Lab2D.cpp.#include <iostream>using namespace std;void main(){ double p = 12.5; double q = 3.234; p *= q - 1; q += p + 1; cout << " p is " << p << endl << " q is " << q << "n";}Lab 2EProcedure : Step 1: Type the programs given below Step 2: Compile and run the program. Write the output. Step 3: Save the program as Lab2E.cpp. // Demonstrate the modulus operator. #include <iostream> using namespace std; int main() { int x, y; x = 10; y = 3; cout << x << " / " << y << " is " << x / y << " with a remainder of " << x % y << "n"; x = 1; y = 2; cout << x << " / " << y << " is " << x / y << "n" << x << " % " << y << " is " << x % y; return 0; }
4. 4. FP 201 PROGRAMMING FUNDAMENTALLab 2FProcedure :Step 1: Type the programs given belowStep 2: Compile and run the program. Write the output.Step 3: Save the program as Lab2F.cpp.// Demonstrate the relational and logical operators.#include <iostream>using namespace std;int main(){int i, j;bool b1, b2;i = 10;j = 11;if(i < j) cout << "i < jn";if(i <= j) cout << "i <= jn";if(i != j) cout << "i != jn";if(i == j) cout << "this wont executen";if(i >= j) cout << "this wont executen";if(i > j) cout << "this wont executen";b1 = true; b2 = false;if(b1 && b2) cout << "this wont executen";if(!(b1 && b2)) cout << "!(b1 && b2) is truen";if(b1 || b2) cout << "b1 || b2 is truen";return 0;}Lab 2GProcedure :Step 1: Type the programs given belowStep 2: Compile and run the program. Write the output.Step 3: Save the program as Lab2G.cpp.#include<iostream>using namespace std;void main(){ int i; for(i=1;i<=10;i++) cout<<i<<" /2 is: " << (float) i/2 << "n";}
5. 5. FP 201 PROGRAMMING FUNDAMENTALLAB EXERCISE1. Write the C++ expression for the following mathematical statementa. y=(x-2)(x+3)b. min = a + b + c + d + e 5 (2 marks)2. Given the values x=5, y=5 and c=3. Write a program to calculate the value of z and display the output of z z = xy % c + 10 / 2y + 5; (4 marks)3. Based on the flowchart, find the values for a and b. Write a program to calculate the values and display the output (4 marks) start x=12, y=8,z=5 a=x*y-z b = (6*a/2+3-z)/2 print a,b endCONCLUSION_______________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________