Docsity

Exercise 2 - Numerical Data and Basic Input/Output in C++, Slides of Computer Science

Toggle sidebar
bg1
Exercise two February 7, 2006
1
Exercise 2
Exercise 2 –
–N
Numerical data
umerical data
types and simple input/ output
types and simple input/ output
Larry Caretto
Computer Science 106
Computing in Engineering
and Science
February 7, 2006
2
Outline
• Review exercise one (can hand in this
week as late assignment)
• Present assessment results
• Review lecture topics for exercise two
– Basic structure of simple C++ programs
– Basic input and output
• Instructions for preparing file to submit
• List tasks for exercise two
3
Review of Exercise One
• You should have learned how to use the
Visual C++ IDE to enter, compile, link,
and execute programs
• You saw a sample C++ program with
– A semicolon to end each statement
– Braces {} to separate blocks of code
– Use of cin >> and cout << for keyboard
input and screen output
4
Review Syntax Errors
• Click on error message to see line
• Error messages are not always clear
• Error may occur on line above line
identified by error message
• Single error may give multiple error
messages
• Learn how to interpret error messages
during the semester
5
Assessment Results
• 10 students completed assessment
– 8 rate general computer skills above
minimal
– 8 students rate programming skills as
minimal or none
• Two students do not have Math 150A or
higher
• All students have access to a computer
at home (6 windows PC, 2 MAC 2 No
answer or answer unclear)
6
Assessment Quiz
•∫x3dx = x4/4 + C – Three incorrect
(three more missing constant, C)
•d(e
ax)/dx = aeax – Four correct
• 1000112= 3510 – One correct; use
binary numbers to understand storage
• x = 10; x = x + 5; gives x = 15 – Six
correct; shows meaning of = as
replacement of value in computing
pf3
pf4

Partial preview of the text

Download Exercise 2 - Numerical Data and Basic Input/Output in C++ and more Slides Computer Science in PDF only on Docsity!

Exercise 2Exercise 2 – N–Numerical dataumerical data

types and simple input/ outputtypes and simple input/ output

Larry Caretto Computer Science 106

Computing in Engineering

and Science

February 7, 2006

2

Outline

  • Review exercise one (can hand in this

week as late assignment)

  • Present assessment results
  • Review lecture topics for exercise two
    • Basic structure of simple C++ programs
    • Basic input and output
  • Instructions for preparing file to submit
  • List tasks for exercise two

3

Review of Exercise One

  • You should have learned how to use the

Visual C++ IDE to enter, compile, link,

and execute programs

  • You saw a sample C++ program with
    • A semicolon to end each statement
    • Braces {} to separate blocks of code
    • Use of cin >> and cout << for keyboard input and screen output

4

Review Syntax Errors

  • Click on error message to see line
  • Error messages are not always clear
  • Error may occur on line above line

identified by error message

  • Single error may give multiple error

messages

  • Learn how to interpret error messages

during the semester

5

Assessment Results

  • 10 students completed assessment
    • 8 rate general computer skills above minimal
    • 8 students rate programming skills as minimal or none
  • Two students do not have Math 150A or

higher

  • All students have access to a computer

at home (6 windows PC, 2 MAC 2 No

answer or answer unclear)

6

Assessment Quiz

  • ∫x^3 dx = x^4 /4 + C – Three incorrect

(three more missing constant, C)

  • d(eax)/dx = ae ax^ – Four correct
    • 100011 2 = 35 10 – One correct; use

binary numbers to understand storage

  • x = 10; x = x + 5; gives x = 15 – Six

correct; shows meaning of = as

replacement of value in computing

7

Assessment Quiz

  • File definition – Four partially complete
    • a collection of information, which may be programs, data or images, stored on permanent storage (e. g., a hard drive, solid-state USB device, zip drive, CD or floppy) that can be accessed by a unique name.
  • Rounding 123.4567 to four significant

figures produces 123.5 or 1.235x10^2 –

Everyone correct!

8

Exercise Two Goals

  • Understand differences between different data types (double and int)
  • Learn results of division by zero
  • Learn effect of entering a fraction as an input for an int-data-type variable
  • Be able to write input and output
    • Multiple input values with one cin
    • Output statements with spaces and new lines

9

Basic C++ Program Structure

#include

using namespace std;

int main()

return EXIT_SUCCESS;

  • Case sensitive; ignores spaces and new lines

10

Numerical data types

  • Binary representation of numbers
  • Floating point data types (float, double

and long double)

  • Have decimal points and wide range
  • Division behaves in expected manner
  • Integer data types (int, short int, long int,

unsigned int)

  • Have narrow range
  • Integer division truncates

11

Review Output using cout

  • cout << “”; writes the string between the quotation marks to the screen
  • cout << x; writes the value of the variable, x, to the screen.
  • Can have one or more output (<<) operators in a single cout command Code Screen output

cout << “ x = “ << x; x = 2

int x = 2; cout << x; 2

cout << “Name” Name

12

Review Input using cin

  • Input prompt tells user what to input
  • Enter several variables with a single cin command - Separate entries by a space and press (the Enter key) after last entry

Type 1.4 -3.2 12.7 to set x = 1.4, y = -3.2 and z = 12.

cout << “Enter x, y, and z: “; cin >> x >> y >>z;

cout << “Enter x: “; cin >> x; Type 2.3 for x = 2.

Code Actions

19

Tasks for this Exercise

  • Copy and paste task one code from

assignment to IDE and run

  • Copy code and screen input and output

(for six cases) to submission file

  • Task two is repeat of task one with

variables changed from double to int

  • Task three provides a single input

command; run for one case only

20

Task One Code

#include using namespace std; int main() { double x, y, z; cout << “Enter a value for x: “; cin >> x; cout << “Enter a value for y: “; cin >> y; z = x / y; cout << “For x = “ << x << “ and y = “ << y << “ x / y = “ << z << “\n\n”; return EXIT_SUCCESS; }

21

Entering Powers of Ten

  • C++, like most languages, uses an “E” or

“e” to enter powers of ten

  • 3.45x10 12 is entered as 3.45e
  • -6.19x10 -32^ is entered as –6.19e-
  • We write 10 n^ as shorthand for 1x10n
  • How do you enter 10^6 and 10-6?
  • Enter 10^6 = 1x10 6 as 1e6, 1E6 or 1000000
  • Enter 10 -6^ =1x10-6^ as 1e-6, 1E-6 or 0.000001; what are 10e6 or 10E-6?
  • 10e6 = 10x10 6 = 10^7 ; 10e-6 = 10x10-6^ = 10- 22

Remaining Tasks

  • Task four looks at simple output spacing

in cout commands; one case only

  • Task five: more complicated spacing

assignment – write code giving output

exactly as shown in exercise

  • Tasks one and two show differences

between double and int as well as

effects of incorrect data

  • Due this Thursday, February 9

23

Task Five Output

Results for programming exercise 2:

Larry Caretto February 9, 2006

Input value of x = 10 Input value of y = 3 Output value of z = x / y = 3.

Press any key to continue

blank lineblank line

blank line

blank line

Two Spaces

Note alignments

decimal result