Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

BTC 216 – Computer Programming

Installing the DevC++ in your Computer

Download and install the DevC++ 5.11 from the Internet or you may
access the BTC 216 – Computer Programming class of the University Virtual
Learning Environment where an installer is already uploaded.

The DevC++ Integrated Development Environment (IDE)

Getting reading for coding


To open a new C++ workspace click on the highlighted icon. You may use
the shortcut key by simultaneously pressing the keys CTRL+N as alternative.

1|Page
BTC 216 – Computer Programming

The highlighted part is here you can write your codes.

The C++ Structure


The following are important parts of the C++ structures that need to be
understood before we start programming or coding:
1) The input and output library header. This is where we declare the
needed C++ library headers which contain the C++ functions needed in
developing your program. The first library header that you need to
familiarize is the <iostream>.
2) The using namespace std. This tells the compiler that you will be using
the standard namespace.
3) The main. This is the main program and where the execution of
program begins.
4) The return 0. This terminates the main() function and causes it to return
the value zero (0) to the calling process.

2|Page
BTC 216 – Computer Programming

In order for us to have the same reference during class discussion. We are
going to set the line numbers. To access and activate this, click on the Tools
menu and then select the Editor options.

After clicking the Editor Options option, click on the font tab. Click the
checkbox with Line numbers option. You can also adjust the font size by
adjusting the Size option. After you have check the Line number checkbox or set
the font size, you may now click the Ok option.

3|Page
BTC 216 – Computer Programming

The Input and Output

The terminology input and output in computer programming refers to the


communication between the user and the computer program. The term input
means that the user is giving an instruction to the computer program and the
computer program response will be the output based on the instruction from the
user.
The standard C++ library has the header file iostream which we discuss
earlier. The function we will be using under this header file are the following:
1) The cout and insertion (<<) operator. This is use to print a character,
series of characters as message, variables and constant values which
is in between the open and close double quotation mark. The format is:
cout<< “ ”;

In this example, the message in between the double quotation


marks in line 9 and 10 will be displayed as output. While in line 11 the
assigned value which is 5 in the variable a will be displayed as output.

4|Page
BTC 216 – Computer Programming

2) The cin and extraction (>>)operator. This is use to input the value or
data you entered from the keyboard to be stored in an already declared
variable. The format is: cin>>variable;

In figure, line 8 is the variable declaration with variable name number. This
variable holds or stores any integer value that will be entered by the user. Line
14 will accept any integer number as input from the user and store the input value
in the variable number. Line 16 displays on your computer screen the integer
number you enter which was stored in the variable number in line 14.

5|Page
BTC 216 – Computer Programming

Example 1 Basic Input and Output


1) Open a new blank DevC++ source file. Make a program that would require
user’s first name, last name and its body temperature. Process these data
to obtain a user’s information.

2) Save your program as Example 1.

Line 8 is a variable declaration that will store any numeric values entered
by the user as temperature value while Line 9 is a variable declaration
using the data type string for the variables firstname and lastname. Lastly
Line 20 will display the values entered by the user.

These materials will help you to further understand how to declare


variables needed in your program:
1) http://www.cplusplus.com/doc/tutorial/variables/
2) https://www.tutorialspoint.com/cplusplus/cpp_variable_types.htm
3) https://www.programiz.com/cpp-programming/input-output
4) https://www.youtube.com/watch?v=D6nYXIZdrMg&feature=youtu.be

6|Page
BTC 216 – Computer Programming

Example 2 Using Simple Equation


Develop a program that would ask for two numbers from the user. The
program must then process these numbers to produce its sum, difference,
product and quotient.
In this example, we will go directly into the computing solution. When you
do this exercise do not forget the other important parts of the C++ programming
structure as discussed.

Line 8 shows the variables needed in the computing problem. Lines 16 to


26 are the part of the computing solution which solves for the mathematical
processes needed. Lastly, Lines 28 to 31 displays the values obtain based on
each of the mathematical processes.

These materials will help you to further understand on how to convert


equation in to a C++ code in your program:
1) http://icarus.cs.weber.edu/~dab/cs1410/textbook/2.Core/formulas.html
2) https://www.javatpoint.com/cpp-expression

7|Page
BTC 216 – Computer Programming

Example 3

Write a program that would solve for the circumference of a circle.

In example 3, line 8 variable pi was assigned a value of 3.1416 while in


line 13 is the formula to compute the circumference of a circle written in C++
code.

8|Page
BTC 216 – Computer Programming

Example 4
A Fahrenheit temperature can be converted to an equivalent Celsius
temperature according the following formula: Celsius = (5/9 x (Fahrenheit –
32.0)). Write a program that will read in a Fahrenheit temperature and then output
the equivalent Celsius temperature.

In this example, line 13 is the converted C++ code formula to convert


Fahrenheit temperature to its equivalent Celsius temperature. The open and
closed parentheses are used to group mathematical terms.

9|Page
BTC 216 – Computer Programming

Example 5
A class has four tests in one term. Write a program that will read student’s
test scores and output the student’s average exam score.

Similar to previous example 2, we will go directly into the computing


solution. When you do this exercise do not forget the other important parts of the
C++ programming structure as discussed. Line 22 will is the established equation
to solve for the test average from the four test results.

10 | P a g e
BTC 216 – Computer Programming

Example 6 Using constant


Write a program that accepts covered distance in mile and convert it into
kilometers.

These materials will help you to further understand on how to declare


constant in programming:
1) http://www.cplusplus.com/doc/tutorial/constants/
2) https://www.geeksforgeeks.org/constants-in-c-cpp/

11 | P a g e

You might also like