Download Contents 1 Week 1

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Location arithmetic wikipedia , lookup

Arithmetic wikipedia , lookup

Elementary mathematics wikipedia , lookup

Transcript
Contents
1 Week 1
1.1 How to Succeed in MAT2170
1.2 Student Responsibilities . . .
1.3 Data Types . . . . . . . . . .
1.4 Assignment Statement (=) .
1.5 Arithmetic in C++ . . . . . .
1.6 Input / Output in C++ . . .
1.7 C++ Program Format . . . .
1.8 Linux commands . . . . . . .
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
2 Followup 1: Resevoir Water Level
1
1.1
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
1
1
1
1
2
2
3
4
4
5
Week 1
How to Succeed in MAT2170
Welcome to Mat2170, the introductory programming course in C++.
Our Website: www.eiu.edu/∼mathcs/mat2170
• Many students find this course challenging since
– There is an extremely steep learning curve.
– There are many details which must be correct when writing programs.
– Topics are built on one another, so confusion at one point may be detrimental to understanding later
information.
– The course is very time consuming.
– You must think in this course. Learning to program is all about problem-solving.
• Thus, we suggest the following in order to improve your experience with this course:
– Ask questions - either in class or outside of class. Your professors are here to help you learn.
– Attend lectures and labs. Although individual labs do not count for a very large percent of your final
grade, you will find they help prepare you for exams.
– Complete the weekly Lab Preview before lab.
– Keep up with the class and assignments - once you fall behind, it is nearly impossible to catch up.
– Complete the Homework programs yourself.
1.2
Student Responsibilities
• For Week 1:
1. Reading: Chapter Two and all class handouts, including these notes and Lab 1
2. Unix/Linux Quick Guide - print from website and read
3. Lab 1 Preview due Thursday at beginning of Lab
4. Lab 1 Followup due next Thursday, beginning of Lab
5. Homework 1 - complete for next week (due in Week 2 Lab)
c Andrews, Broline, Slough, & Van Cleave
Week 1 Summary
1.3
2
Data Types
While a program is executing, it often needs to store information it is processing or using for calculations.
This information, or data, may take many forms, so we need various types of storage ”bins”: int and long
store whole numbers (integers), while float and double store real numbers (floating point values). These
are some of the simple types available in C++. Soon we’ll learn about other simple data types, and later
will study more complex data types. Here are more details and some examples:
• Each simple object is capable of storing one value of its specified type.
• Each object must be declared and is usually initialized (that is, given a valid value).
• Each simple object has two attributes: its name and its type.
• Examples:
1. Declaring an uninitialized integer object named count:
int count;
2. Declaring an integer object named FavoriteNumber initialized to 29:
int FavoriteNumber = 29;
3. Declaring an uninitialized floating point object named total:
float total;
4. Declaring a floating point object named distance initialized to 3.75:
float distance = 3.75;
1.4
Assignment Statement (=)
An assignment statement is one way to initialize or change the value of a simple object. The object getting
the value occurs on the left-hand side of the assignment operator (=), while the value it is to receive goes on
the right-hand side. This value can be a constant, another object, or an expression of the appropriate type.
If the right-hand side expression involves the object itself, such as in
count = count + 1;
the original value stored in the object is use to evaluate the expression, then the result is stored in the object.
Thus, if count had the value 28 before the statement is executed, afterward it would have the value 29.
• The syntax is:
ObjectName = RightHandSideExpression;
• Some examples (each object has already been declared, and any object used on the right-hand side of
the assignment operator has been initialized):
count = 0;
total = 3.968;
count = count + 1;
sum = sum + value;
maximum = 100 * units;
average = sum / count;
c Andrews, Broline, Slough, & Van Cleave
Week 1 Summary
1.5
3
Arithmetic in C++
Computers are great for numeric calculations - they do what they’re told and rarely make mistakes. The
usual arithmetic operators are available, with surprising results in some cases.
• We can add, subtract, multiply and divide in C++. If data types are mixed integer and floating point,
the value of the integer object is temporarily converted to a floating point number, although the value
stored in the object remains unchanged.
• Addition and subtraction are the usual grade school operations.
x = x + 4;
y = z - 10.9;
// the original value of x is incremented by 4
// y now has the value of z decreased by 10.9
• The only concern with multiplication is that there is no implicit or assumed multiplication. That is, it
must be denoted explicitly by including the multiplication symbol, *.
y = 7 * 8;
z = 4.5 * x;
a = 3b;
// y is 56
// z is 4.5 times the value of x
// illegal - implicit multiplication not allowed
• Division may yield either a floating point quotient, or an integer quotient, depending upon the data types
of the operands. If an integer is divided by another integer, the result is the quotient with any fractional
part truncated. If at least one of the operands is of a floating point type, then a floating point value
results.
int i
int j
float
float
float
=
=
x
y
z
3
4
=
=
=
/ 4;
/ 3;
3.0 / 4;
3 / 4.0;
3.0 / 4.0;
//
//
//
//
//
i
j
x
y
z
will
will
will
will
will
have
have
have
have
have
the
the
the
the
the
integer value 0
integer value 1
floating point value 0.75
floating point value 0.75
floating point value 0.75
• To calculate the remainder when one integer is divided by another, use the modulus operator, (%). Only
integer operands may be used with the modulus operator, and the result is always an integer.
int
int
int
int
int
1.6
m = 8 % 5;
n = 5 % 8;
k = 22 % 3;
h = i % j;
result = value % 2;
//
//
//
//
//
//
m is the remainder (3) when 8 is divided by 5
n is the remainder (5) when 5 is divided by 8
k is the remainder (1) when 22 is divided by 3
h is the remainder when i is divided by j
result will be 0 when value is even,
and 1 when value is odd - WHY?
Input / Output in C++
• To display information on the monitor or screen, use the cout statement with the insertion operator
(<<). We can output unnamed constants (strings, integers, floating point values), simple objects, and
expressions. Use endl to end the current line of output and go on to the next line. Blanks outside
quotation marks make no difference in the output.
cout
cout
cout
cout
<<
<<
<<
<<
"Hello, World!" << endl << endl;
"The value of the object m is: " << m << endl;
"The sum of " << 3 << " and " << 2 << " is " << 5 << endl;
3 * x + 4.8 * y - z / 2.0 << endl;
The output from these cout statements would be (assuming m has value 3 while x is 1, y is 2, and z is
3):
c Andrews, Broline, Slough, & Van Cleave
Week 1 Summary
4
Hello, World!
The value of the object m is: 3
The sum of 3 and 2 is 5
11.1
• To get user input from the keyboard, use the cin statement with the extraction operator (>>). At this
point, we can input integers and floating point values. Generally it is advisable to prompt the user with
a cout statement indicating the required input.
1.7
cout << "Please enter an integer: ";
int value;
cin >> value;
// onscreen prompt to user
// declare int object
// get input from keyboard
cout << "Please enter the angle: ";
float gap;
cin >> gap;
// onscreen prompt to user
// declare float object
// get input from keyboard
C++ Program Format
• A bare bones C++ program shell:
using namespace std;
#include <iostream>
int main(){
// tells computer where to find the
//
standard library classes and functions
// provides definitions for cout and cin
// beginning of main function
// this is where statements will go
return 0;
}
1.8
// tells the computer that the program
//
terminated with no errors
// end of main function
Linux commands
• Logging on:
1. Reboot computer to Linux if necessary
2. At the login prompt, enter your MAT2170 Linux user id
3. At the password prompt, enter your Linux password
4. If your attempt to login fails, try again and check for correct typing and case; contact your instructor
if problems persist
• Open a terminal window (click on icon of screen on the top toolbar)
• Only if you have NOT YET DOWNLOADED the lab files:
– Launch the web browser with the command
www.eiu.edu/∼mathcs/mat2170
mozilla &
then enter the course web site URL:
– Find the line which refers to the “archive file” for the lab
– “shift-click” this link and select OK to download the file
– To uncompress the file use tar -kxvf labn.tar, where n is the current week, for example:
tar -kxvf lab1.tar.
– Change into the lab folder, cd lab1, automatically created by the last step
Week 1 Summary
c Andrews, Broline, Slough, & Van Cleave
5
• Otherwise, simply enter: cd lab1 if you have already downloaded and uncompressed Lab 1
• Enter the command: emacs followup.cpp &
to start the emacs editor.
• Complete the program by adding statements to the program skeleton. To compile, under Files choose
Save Buffer; under Tools choose Compile then complete the line make -k followup. Once you compile
successfully, go to the terminal window and enter: ./followup in order to execute the program. Compare
the output to what you expect. If there are errors, go back to the program to look for problems with
your logic.
• Note: while in the terminal window, the up-arrow key (↑) cycles through previous commands which
you’ve entered during the current session. Retrieved commands may be edited. This saves time spent
retyping.
• Complete and print a copy of the Software Development Report for the followup to hand in.
• Electronically submit the followup. In the terminal window enter: 2170submit lab1 sectionN, where
N is your section number (1, 2, or 3).
2
Followup 1: Resevoir Water Level
• "Prompt for and obtain" means use an output statement (cout) to prompt the user, declare the object
which will receive the value entered, and then use an input statement (cin) to get the value from the
user.
• When you "Compute and store" or perform calculations, generally you use a declaration with assignment statement with some arithmetic expression on the right-hand side.
• "Display the results" requires one or more output statements (cout) to print the information to the
screen.