Professional Documents
Culture Documents
CS161: Introduction To Computer Science: Week 2
CS161: Introduction To Computer Science: Week 2
Week 2
10/2021
Solving Problems w/ Computers
o Programs are...
n an expression of a series of instructions that the
computer must perform
n written in precise languages, called programming
languages
o Programming languages...
n translate your ideas into specific language that the
computer will understand
n C, C++, Java, Pascal, Visual Basic, Cobol, Fortran,
Python, PHP, Javascript, Ruby…
dbtien – Introduction to CS
Solving Problems w/ Computers
o The most difficult part of programming is
figuring out how to design a method to
solve a problem
dbtien – Introduction to CS
Solving Problems w/ Computers
o An algorithm is a sequence of step by step
instructions for performing some task --
usually for computation
o An algorithm must...
n accomplish the task
n be clear and easy to understand
n define the sequence of steps needed to
accomplish the task in the order specified
dbtien – Introduction to CS
Algorithms
o It is best to ignore the details of your
programming language (C++) when trying
to solve a problem
o Instead, figure out the steps you need to go
thru to solve the problem
o Write these steps down in English
o These steps are called the algorithm!
dbtien – Introduction to CS
Algorithms
o Think of your algorithm as a tool for
creating the instructions for solving a
problem....as if you were to tell them to
another person.
o Remember an algorithm is a sequence of
step by step instructions for performing
some tasks
dbtien – Introduction to CS
For Example...solve a problem
o Convert inches to millimeters
dbtien – Introduction to CS
Convert inches to millimeters
o Next, write the algorithm
dbtien – Introduction to CS
Convert inches to millimeters
o Step 2:
n Get the number of inches from the user
o display a prompt asking the user to enter
o read in the number of inches
o display what was read (echo)
o ask the user if this is really correct (confirm)
o if not, repeat this step until the user is satisfied
dbtien – Introduction to CS
Convert inches to millimeters
o Continuing with Steps 3 and 4:
dbtien – Introduction to CS
Convert inches to millimeters
o The next step is to turn this into a C++ program!
o All programs have the following “form”
#include <iostream>
using namespace std;
//header comments...
int main()
{
//program body goes here...
return 0;
}
dbtien – Introduction to CS
Convert inches to millimeters
#include <iostream>
using namespace std;
// ***********************************
// CS161 Programming Assignment #0
// Purpose of this program is to convert
// inches entered in by the user into
// millimeters and display the results
// **********************************
int main()
{
}
dbtien – Introduction to CS
(Different Kind of Comment...)
#include <iostream>
using namespace std;
/* ***********************************
CS161 Programming Assignment #0
Purpose of this program is to convert
inches entered in by the user into
millimeters and display the results
********************************* */
int main() {
dbtien – Introduction to CS
Convert inches to millimeters
//Define variables
float inches; //to save # inches
float mm; //to save the result
dbtien – Introduction to CS
(A different way to do this...)
//Define variables
float inches, //to save # inches
mm; //to save the result
dbtien – Introduction to CS
Convert inches to millimeters
//Step #2, Get the input (prompt, read)
cout <<“Please enter the number of inches ”
<<“ that you wish to convert: ” ;
dbtien – Introduction to CS
Convert inches to millimeters
//Step #3 Convert inches to millimeters
mm = 25.4 * inches;
dbtien – Introduction to CS
CS161
Exercises!!!
Exercises
o Write the algorithms (steps in natural
languages) to solve the following problems:
n Given a and b, find x which satisfies ax + b = 0
n Given a, b, and c, find x which satisfies
ax2 + bx + c = 0
dbtien – Introduction to CS
What is next?
o Solving Problems with Computers
n Write a short Program
n Basic Form of all C++ programs
n What are Variables, Data Types, Statements
n How do we perform input and output
dbtien – Introduction to CS
Convert inches to millimeters
#include <iostream>
using namespace std;
// ***********************************
// CS161 Programming Assignment #0
// Purpose of this program is to convert
// inches entered in by the user into
// millimeters and display the results
// **********************************
int main() {
dbtien – Introduction to CS
Preprocessor Directives
#include <iostream>
dbtien – Introduction to CS
Header Comments...
// ***********************************
// CS161 Programming Assignment #0
/* ***********************************
CS161 Programming Assignment #0
Purpose of this program is to convert
inches entered in by the user into
millimeters and display the results
********************************* */
int main() {
}
dbtien – Introduction to CS
(Different Kind of Comment...)
/************************************
Tien B. Dinh
******************************** */
o This type of comment is best used when writing a
large block of comments. They begin with a /* and
end when you type a */
o If you forget the ending */ your entire program is
taken as a comment!!!
o I recommend placing a space between the /* and any
other asterisks....
dbtien – Introduction to CS
Variable Definitions
//Define variables
float inches; //to save # inches
float mm; //to save the result
dbtien – Introduction to CS
Displaying Output
dbtien – Introduction to CS
Output after first cout
dbtien – Introduction to CS
(A different way to do this...)
//Define variables
float inches, //to save # inches
mm; //to save the result
dbtien – Introduction to CS
Output after 2nd cout
dbtien – Introduction to CS
Output after 2nd cout
dbtien – Introduction to CS
Output after 2nd cout
dbtien – Introduction to CS
Receiving Input
o Pronounced see in
o >> is the extraction operator
o Think of >> as an arrow. The data you want to
receive is coming from the input device in the
direction of the arrow and being saved in memory:
input_device variable_name
dbtien – Introduction to CS
Convert inches to millimeters
//Step #3 Convert inches to millimeters
mm = 25.4 * inches;
dbtien – Introduction to CS
Assignment Operation
//Step #3 Convert inches to millimeters
mm = 25.4 * inches;
dbtien – Introduction to CS
CS161 – Introduction to CS
Any questions???
Notes
o Put the #include <iostream> in row #1
o endl is “e n d” followed by lower case L
o spaces inside double quotes ARE
important
o count vs cout
o watch your semicolons!
dbtien – Introduction to CS
Homework
o Convert Celcius degree to Farenheit and vice
versa.
n Write the algorithm
n Use the program template to write a program
dbtien – Introduction to CS