0% found this document useful (0 votes)
18 views14 pages

Geronimo Le3

Uploaded by

ryangeronimo639
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)
18 views14 pages

Geronimo Le3

Uploaded by

ryangeronimo639
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/ 14

COMPUTER ENGINEERING DEPARTMENT

Computer Fundamentals and Programming

Activity 3: Introduction to C++ Programming


Data Types Variables Operators

3.1 Program Outcomes (POs)

a. ability to apply knowledge of mathematics and science to solve engineering problems


b. ability to design and conduct experiments, as well as to analyze and interpret data
d. ability to function on multidisciplinary teams
e. ability to identify, formulate and solve engineering problems

3.2 Activity’s Intended Learning Outcomes (AILOs)


At the end of this activity, the student shall be able to:
a. define terms
b. familiarize students with arithmetic, relational and logical operators
c. apply correct style requirements

3.3 Objectives of the Activity


The objectives of this activity are to:
a. know the structure of C++ program
b. define programming terminologies
c. familiar with the use of Microsoft Visual Studio 2010 to develop C++ programs
d. know the console output and escape sequences
e. use the console input
f. enumerate and define the data types used in developing C++ programs
g. enumerate Operators & Expression and know they are used
h. know the order of precedence and associatively.
i. declare a variable

3.4 Principle of the Activity

Structure of a C++ program


// my first program in C++
#include <iostream>
using namespace std;
int main ()
{ cout << "Hello World!";
return 0;

Activity 3: Introduction to C++ Programming


}
// my first program in C++

This is a comment line. All lines beginning with two slash signs (//) are considered
comments and do not have any effect on the behavior of the program.
#include <iostream>
Lines beginning with a hash sign (#) are directives for the preprocessor. This specific
file (iostream) includes the declarations of the basic standard input-output library in
C++.

cout << "Hello World!";


This line is a C++ statement. A statement is a simple or compound expression that can
actually produce some effect.
Each statement must be terminated by a semicolon (;)
return 0;
The return statement causes the main function to finish, return may be followed by a
return code 0;

3.5 Materials/Equipment

Computer Unit

4.6 Procedure/s

I. Create a C++ program for each case problems.


II. Encode and run the program in Visual C++
III. Copy the and paste the source code to this word file.
IV. Screenshot the result/output of the program, then paste it in the word file.
V. Submit this word file to canvas.

Activity 3: Introduction to C++ Programming


ACTIVITY 3.7.1: Console Output and Escape Sequences

1. Write a program that will display your weekly class schedule in a listed view.
Consider the following output:

Program code
#include<iostream>
using namespace std;
int main()
{
int Schedule;
cout << "Weekly Schedule of ME21";
cout << "\nDay\t\t\tSubject\t\t\t\t\t\tTime\t\t\t\tRoom";
cout << "\nMonday (Face to Face)\n";
cout << "\n\t\t\tEngineering Drawing\t\t\t\t7:00am - 9:00am\t\t\t(408)\n";
cout << "\n\t\t\tDifferential Equation\t\t\t\t11:00am - 12:50pm\t\t(312)\n";
cout << "\n\t\t\tStatics of Rigid Bodies\t\t\t\t1:00pm - 2:50pm\t\t\t(514)\n";
cout << "\n\t\t\tReadings In Philippine History\t\t\t5:00pm - 6:50pm\t\t\t(311)\n";
cout << "\nTuesday (Online Class)\n";
cout << "\t\t\tGE Elective:Bio Engineering Engineering\t\t1:00pm -
2:50pm\t\t\t(Online Class)\n";
cout << "\t\t\tLife Works of Rizal\t\t\t\t3:00pm - 4:50pm\t\t\t(Online Class)\n";
cout << "\nWednesday (Face to Face)\n";
cout << "\t\t\tWorkshop and Theory and Practice\t\t9:00am - 11:50am\t\t(LG201)\n";
cout << "\t\t\tComputer Fundamentals and Programming\t\t4:00pm -
6:50pm\t\t\t(404)\n";
cout << "\nThursday (Face to Face)\n";
cout << "\t\t\tDifferential Equation\t\t\t\t11:00am - 12:50pm\t\t(312)\n";
cout << "\n\t\t\tStatics of Rigid Bodies\t\t\t\t1:00pm - 2:50pm\t\t\t(514)\n";
cout << "\n\t\t\tReadings In Philippine History\t\t\t5:00pm - 6:50pm\t\t\t(311)\n";
cout << "\nFriday (Online Class)\n";
cout << "\t\t\tOrientation to Mechanical Engineering\t\t11:00am - 12:50pm\t\t(Online
Class)\n";
cout << "\t\t\tGE Elective:Bio Engineering Engineering\t\t1:00pm -
2:50pm\t\t\t(Online Class)\n";
cout << "\t\t\tLife Works of Rizal\t\t\t\t3:00pm - 4:50pm\t\t\t(Online Class)\n";
return 0;
}

Activity 3: Introduction to C++ Programming


2. Write a program that will display your autobiography. Your autobiography should consist
of not less than 200 words.
Program code
#include <iostream>
using namespace std;
int main() {
cout << "My name is Mark Ryan R. Geronimo, 18 years old and I’m from
Quezon Province. Right after senior high school I pursued college here
at FEU Alabang with the program of Bachelor of Science in Mechanical
Engineering.";
cout << "Before I even started high school I have already set my
goal which is to be an engineer, I wanted to be an engineer because I
grew up watching my Dad fix things in our house and he is even the one
who built our house with the help of his engineer friends too.";
cout << "But I decided to take Mechanical engineering instead of
Civil Engineering because I wanted to specialize in engines and other
stuff that involves machinery and mechanical bodies. ";
cout << "I personally chose to study here in Alabang even though it
is very far from our province, its because I want to grow independently
and get out of my comfort zone. I also believe that I can hone and
acquire new set of skills while studying here in FEU Alabang which is
what Im experiencing right now and I also made new connection and new
circle of friends that hace the same goal which is to be an engineer in
the future.";
cout << "With the help of FEU Alabang and everyone that believes in
me I will be an Mechanical Engineer in the future. ";
return 0;
}

Activity 3: Introduction to C++ Programming


3. Write a program that will display your first impression to your classmates, to your course
professor, and to the school. It should not be less than 200 words.
Program code
#include <iostream>
using namespace std;
int main() {
cout << "Ever since I always attended public schools for my
elementary school, junior and senior high school so when I started to
going to FEU Alabang my first impression to my classmates is that they
would be extremely rich and they would be like conyo’s and would be
speaking full English when having a casual conversation.";
cout << "But my first impression changed when I started making
friends through the months of staying here in FEUA because all of my
friends have the same vibes as me. ";
cout << "As for the professors at first I thought that they would
all be strict but as time goes on we met a lot of professors and they
were not that strict in fact some of them are our friends outside of
the school premises. ";
cout << "I personally chose to study here in Alabang even though it
is very far from our province, its because I want to grow independently
and get out of my comfort zone. I also believe that I can hone and
acquire new set of skills while studying here in FEU Alabang which is
what Im experiencing right now and I also made new connection and new
circle of friends that hace the same goal which is to be an engineer in
the future.";
cout << "While as for FEUA my impression is that the environment
feels like very pleasing from the people to the school itself and that
impression of mine still remains as of today. ";
return 0;
}

Activity 3: Introduction to C++ Programming


ACTIVITY 3.7.2: Data Types, Variable Declarations, and Operators

4. Given the table below, declare the following variables with the corresponding data
types and initialization values. Output the variable names together with the values.

Variable name Data Type Initial value


Number integer 10
Letter character a
Result boolean true
Float float 3.14
Program code
#include <iostream>
using namespace std;
int main() {
// Declare variables with initialization values
int Number = 10;
char Letter = 'a';
bool Result = true;
float FloatNumber = 3.14;
// Output the variable names and their values with some design
cout << "==========================" << endl;
cout << " Variable Values Output " << endl;
cout << "==========================" << endl;
cout << "* Number : " << Number << endl;
cout << "* Letter : " << Letter << endl;
cout << "* Result : " << (Result ? "true" : "false") << endl;
cout << "* FloatNumber : " << FloatNumber << endl;
cout << "==========================" << endl;
return 0;
}

Activity 3: Introduction to C++ Programming


5. Create a program that will accept two numbers. Display the sum, difference, product,
and quotient.
Program code
#include <iostream>
using namespace std;
int main() {
// Declare variables for input
float num1, num2;
// Input two numbers from the user
cout << "Enter the first number: ";
cin >> num1;
cout << "Enter the second number: ";
cin >> num2;
// Perform calculations
float sum = num1 + num2;
float difference = num1 - num2;
float product = num1 * num2;
float quotient = num1 / num2;
// Output the results
cout << "\nResults:\n";
cout << "Sum: " << sum << endl;
cout << "Difference: " << difference << endl;
cout << "Product: " << product << endl;
cout << "Quotient: " << quotient << endl;
return 0;
}

6. Given the following expressions, re-write and add parentheses based on the sequence
of how they will be evaluated.

a/b^c^d–e+f–g*h+i
3 * 10 *2 / 15 – 2 + 4 ^ 2 ^ 2
r ^ s * t / u – v + w ^ x – y++

Activity 3: Introduction to C++ Programming


7. Create a program that will convert:
Degree Fahrenheit to Celsius and vice versa.
Centimeter to feet and inches to feet.
Program code
#include <iostream>
using namespace std;
int main() {
// Variables for temperature conversion
float fahrenheit, celsius;
// Variables for length conversion
float centimeters, inches, feetFromCm, feetFromInches;
int choice;
cout << "Conversion Menu:\n";
cout << "1. Fahrenheit to Celsius\n";
cout << "2. Celsius to Fahrenheit\n";
cout << "3. Centimeters to Feet\n";
cout << "4. Inches to Feet\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
// Fahrenheit to Celsius conversion
cout << "Enter temperature in Fahrenheit: ";
cin >> fahrenheit;
celsius = (fahrenheit - 32) * 5.0 / 9.0;
cout << fahrenheit << " Fahrenheit is " << celsius << " Celsius." <<
endl;
break;
case 2:
// Celsius to Fahrenheit conversion
cout << "Enter temperature in Celsius: ";
cin >> celsius;
fahrenheit = (celsius * 9.0 / 5.0) + 32;
cout << celsius << " Celsius is " << fahrenheit << " Fahrenheit." <<
endl;
break;
case 3:
// Centimeters to Feet conversion
cout << "Enter length in centimeters: ";
cin >> centimeters;
feetFromCm = centimeters / 30.48; // 1 foot = 30.48 cm
cout << centimeters << " centimeters is " << feetFromCm << " feet." <<
endl;
break;
case 4:
// Inches to Feet conversion

Activity 3: Introduction to C++ Programming


cout << "Enter length in inches: ";
cin >> inches;
feetFromInches = inches / 12; // 1 foot = 12 inches
cout << inches << " inches is " << feetFromInches << " feet." << endl;
break;
default:
cout << "Invalid choice!" << endl;
}
return 0;
}

8. The user will input a Philippine amount, then the said amount will be converted to
different currencies namely: US Dollar, Euro, Yuan, Koruna, Krone, Sheqel and Dinar

Sample run:

Enter Philippine peso: 43.33089

The amount's equivalent to:

US Dollar is : 1.000
Euro : 0.734719
Yuan : 6.346934
Koruna : 18.77263
Krone : 5.449007
Sheqel : 3.726334
Dinar : 0.274588

Program code
#include <iostream>
using namespace std;
int main() {
// Declare variables
float phpAmount;

// Exchange rates (sample values, you can update these based on currentrates)

Activity 3: Introduction to C++ Programming


const float usdRate = 43.33089; // 1 USD = 43.33089 PHP
const float euroRate = 43.33089 / 1.36; // 1 Euro = 1.36 USD (for example)
const float yuanRate = 6.346934;
const float korunaRate = 18.77263;
const float kroneRate = 5.449007;
const float sheqelRate = 3.726334;
const float dinarRate = 0.274588;
// Input: User enters the amount in PHP
cout << "Enter Philippine peso amount: ";
cin >> phpAmount;
// Conversion calculations
float usdAmount = phpAmount / usdRate;
float euroAmount = phpAmount / euroRate;
float yuanAmount = phpAmount / yuanRate;
float korunaAmount = phpAmount / korunaRate;
float kroneAmount = phpAmount / kroneRate;
float sheqelAmount = phpAmount / sheqelRate;
float dinarAmount = phpAmount / dinarRate;
// Output the converted amounts
cout << "The amount's equivalent to:\n";
cout << "US Dollar : " << usdAmount << endl;
cout << "Euro : " << euroAmount << endl;
cout << "Yuan : " << yuanAmount << endl;
cout << "Koruna : " << korunaAmount << endl;
cout << "Krone : " << kroneAmount << endl;
cout << "Sheqel : " << sheqelAmount << endl;
cout << "Dinar : " << dinarAmount << endl;
return 0;
}

9. Suppose, 1.000 US Dollar is equivalent to Php. 43.33089, 0.734719Euro, 6.346934 Yuan,


18.77263 Koruna, 5.449007 Krone, 3.726334 Sheqel, and 0.274588 Dinar.
The amount's equivalent to:

US Dollar is : 1.000
Euro : 0.734719

Activity 3: Introduction to C++ Programming


Yuan : 6.346934
Koruna : 18.77263
Krone : 5.449007
Sheqel : 3.726334
Dinar : 0.274588
Program code
#include <iostream>
using namespace std;
int main() {
// Declare variables
float phpAmount;
// Given exchange rates for 1 US Dollar
const float usdRate = 43.33089; // 1 USD = 43.33089 PHP
const float euroRate = 0.734719; // 1 USD = 0.734719 EUR
const float yuanRate = 6.346934; // 1 USD = 6.346934 CNY
const float korunaRate = 18.77263; // 1 USD = 18.77263 CZK
const float kroneRate = 5.449007; // 1 USD = 5.449007 NOK
const float sheqelRate = 3.726334; // 1 USD = 3.726334 ILS
const float dinarRate = 0.274588; // 1 USD = 0.274588 JOD
// Input: User enters the amount in PHP
cout << "Enter Philippine peso amount: ";
cin >> phpAmount;
// Conversion calculations based on Php to USD
float usdAmount = phpAmount / usdRate;
float euroAmount = usdAmount * euroRate;
float yuanAmount = usdAmount * yuanRate;
float korunaAmount = usdAmount * korunaRate;
float kroneAmount = usdAmount * kroneRate;
float sheqelAmount = usdAmount * sheqelRate;
float dinarAmount = usdAmount * dinarRate;
// Output the converted amounts
cout << "\nThe amount's equivalent to:\n";
cout << "US Dollar : " << usdAmount << endl;
cout << "Euro : " << euroAmount << endl;
cout << "Yuan : " << yuanAmount << endl;
cout << "Koruna : " << korunaAmount << endl;
cout << "Krone : " << kroneAmount << endl;
cout << "Sheqel : " << sheqelAmount << endl;
cout << "Dinar : " << dinarAmount << endl;
return 0;
}

Activity 3: Introduction to C++ Programming


10. Write a program to give the examples of operators.

* Arithmetic Operators
* Logical Operators
* Relational Operators
Program code
#include <iostream>
using namespace std;
int main() {
// Declare variables
int a = 10, b = 5, c = 20;
bool result;
// 1. Arithmetic Operators
cout << "Arithmetic Operators:" << endl;
cout << "a + b = " << a + b << endl; //
Addition
cout << "a - b = " << a - b << endl; //
Subtraction
cout << "a * b = " << a * b << endl; //
Multiplication
cout << "a / b = " << a / b << endl; //
Division
cout << "a % b = " << a % b << endl; //
Modulus (remainder)
// 2. Relational Operators
cout << "\nRelational Operators:" << endl;
cout << "a == b: " << (a == b) << endl; //
Equal to
cout << "a != b: " << (a != b) << endl; //
Not equal to
cout << "a > b : " << (a > b) << endl; //
Greater than
cout << "a < c : " << (a < c) << endl; //
Less than
cout << "a >= b: " << (a >= b) << endl; //
Greater than or equal to

Activity 3: Introduction to C++ Programming


cout << "a <= c: " << (a <= c) << endl; //
Less than or equal to
// 3. Logical Operators
cout << "\nLogical Operators:" << endl;
result = (a > b) && (c > a); // Logical
AND
cout << "(a > b) && (c > a) : " << result
<< endl;
result = (a > b) || (a == c); // Logical OR
cout << "(a > b) || (a == c) : " << result
<< endl;
result = !(a == b); // Logical NOT
cout << "!(a == b) : " << result << endl;
return 0;
}

3.8 QUESTION AND ANSWER

1. What is the purpose of preprocessor directive iostream in C++ program?


_Tells the compiler to include “text” from another file. That file, iostream in this case, has
declarations that provide information about the input/output streams provided by C++.

2. How do you end C++ program statement?

_Semi-colon is what is used to end the C++ program statement.

3. What is the difference between endl and \n in C++ program?


_In C++, both endl and \n are used to insert new lines, but there's a difference. endl adds a new
line and also flushes the output buffer, which forces the program to output everything immediately.
This can slow down the program if used too often. On the other hand, \n just adds a new line
without flushing the buffer, making it faster since the output is only flushed when necessary. So,
use \n if you don’t need to flush the buffer right away.

Activity 3: Introduction to C++ Programming


VI. Assessment

Department Computer Engineering Department


Subject Code CE0002L
Description Computer Fundamentals and Programming
Term/Academic Year 1 / 2024-2025

Topic Introduction to C++


Programming
Data Types Variables
Operators
Lab Summative 4
No.
Lab Summative Introduction to C++
Language
CLO 2,3,4

Note: The following rubrics/metrics will be used to grade students’ output in the Lab
Criteria Descriptions Points
Task N.o 1 The variables and the corresponding values are properly 50
assigned and used in the program.
Output The output of the program is correct and solved based on the 50
problem stated.
Total 100%
Summative 4.

Activity 3: Introduction to C++ Programming

You might also like