cs 1430: programming in c++

27
1 CS 1430: Programming in C++ Turn in your Quiz1- 2

Upload: maxine-rivera

Post on 01-Jan-2016

29 views

Category:

Documents


2 download

DESCRIPTION

CS 1430: Programming in C++. Turn in your Quiz1-2. Add Two Numbers. Sum = num1 + num2; // Where to store the values? // Must declare Sum, num1, num2. Declaration Statements. int num1, num2; int Sum; float average; // One or more variables each line // Style: one space after comma. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: CS 1430: Programming in C++

1

CS 1430: Programming in C++

Turn in your Quiz1-2

Page 2: CS 1430: Programming in C++

2

Add Two Numbers

Sum = num1 + num2;

// Where to store the values?

// Must declare Sum, num1, num2

Page 3: CS 1430: Programming in C++

3

Declaration Statements

int num1, num2;

int Sum;

float average;

// One statement each line

// One or more variables of same

// type each declaration statement

// Style: one space after comma

Page 4: CS 1430: Programming in C++

4

Variables and Memory

num1 num2

average

Sumint num1, num2;int Sum;float average;//What values in the memory?// Uninitialized// Garbage!

num1 = 4;num2 = 5;

Sum = num1 + num2;average = Sum / 2;// What is value of average?// How to get 4.5?average = Sum / 2.0;

4 5 9

4.0

Page 5: CS 1430: Programming in C++

5

Identifiers

• To identify memory space • Good Identifiers Sum, num1, num2, average total_score, totalScore, TotalScore• Bad Identifiers 1num, 2num total-score, totalscore, Total Score x, y, z, t, s, n, o

Page 6: CS 1430: Programming in C++

6

C++ Data Types

int : 2 bytes, range: (-32,768 to 32,767)

float: 4 bytes, range (much larger)

Storage (and Range) is machine/system dependent

Other Numerical Data Types

short, long

double

Page 7: CS 1430: Programming in C++

7

C++ Data Types (II)

// char : 1 byte

char theChar = ‘A’;

// string: one byte for each char

// one more byte at the end to

// indicating the end

string myString = “CS 143”;

Page 8: CS 1430: Programming in C++

8

C++ Data Types and Storage

num1 num2

average

Sumint num1, num2;int Sum;float average;

num1 = 4;num2 = 5;

Sum = num1 + num2;average = Sum / 2.0;

char grade = ‘A’;string courseName;

courseName = “CS143”;

4 5 9

4.5

AC S 1 4 3 \0

courseName grade

Page 9: CS 1430: Programming in C++

9

Char and Integer

One Byte 01000011

What is the value of the byte?

As integer: 67

As ASCII char: ‘C’

Page 10: CS 1430: Programming in C++

10

ASCII Code Table

‘C’: 67

All upper case letters together

All lower case letters are together

All digits 0 through 9 are together

‘Y’: 89 ‘9’: 57

0 1 2 3 4 5 6 7 8 9

4 0 1

5 2 3 4 5 6 7 8 9

6 A B C D E

7 F G H I J K L M N O

8 P Q R S T U V W X Y

9 Z a b c

10 d e f

Page 11: CS 1430: Programming in C++

11

ASCII Code

Char ASCII Code

‘C’: 67

‘D’: ?

‘B’: ?

‘0’: 48

‘5’: ?

Page 12: CS 1430: Programming in C++

12

Input

cin >> num1; // cin: standard input stream // input comes in from the keyboard // >> : input operator // Extraction operator

cin >> num1 >> num2; // Style: one space before and after // each operator (>>)

Page 13: CS 1430: Programming in C++

13

Output

Sum = num1 + num2; average = Sum / 2.0;

cout << average; // cout: standard output stream // output goes to the monitor// << : output operator // Insertion operator

Page 14: CS 1430: Programming in C++

14

Input Prompt

Prompt: Message to tell user what to do.

cin >> num1; // Program is waiting for input// User does not know what to do

cout << “Enter the first number: “; cin >> num1; cout << “Enter the second number: “; cin >> num2;

Page 15: CS 1430: Programming in C++

15

Input Prompt (II)

// Another way to do it: cout << "Enter two numbers: "; cin >> num1 >> num2; // Two numbers separated by spaces/[Enter]

// Still another way to do it: cout << "Enter two numbers: "; cin >> num1; cin >> num2;

Page 16: CS 1430: Programming in C++

16

Output Messagecout << average; // 4.5// 4.5// User does not know what it is

cout << “The average of the two ”

<< “numbers is ” << average;

// The average of the two number is 4.5

cout << “The average of the two ”

<< “numbers is ” << average << “.”;

// The average of the two number is 4.5.

cout << “The average of the two ”

<< “numbers is ” << average << ‘.’;

// The average of the two number is 4.5.

// STYLE: Align the output operators!

Page 17: CS 1430: Programming in C++

17

Using String Variables

string prompt = "Enter two integers: "; string message = “The average of the two numbers is “;

cout << prompt; // Enter two integers: cin >> num1 >> num2; // 4 5 [ENTER]

Sum = num1 + num2; average = Sum / 2.0;

cout << message << average << ‘.’; // The average of the two numbers is 4.5.

Page 18: CS 1430: Programming in C++

18

Include File

#include <iostream>

using namespace std;

// standard

Page 19: CS 1430: Programming in C++

19

Organize Output// endl: go to the beginning of next line// ‘\n’: special char, same as endl

cout << ‘\n’ << “Enter the first number: ”; cin >> num1; cout << “\n\nEnter the second number: ”; cin >> num2;

Sum = num1 + num2; average = Sum / 2.0;

cout << endl << endl;cout << “The average of the two ” << “numbers is ” << average << ‘.’ << endl;

Page 20: CS 1430: Programming in C++

20

Organize Output (II)string prompt2 = “\nEnter the second number: ”; string messageAvg = “\n\nThe average of the two numbers is ”;

cout << endl << “Enter the first number: ”; cin >> num1; cout << prompt2;cin >> num2;

Sum = num1 + num2; average = Sum / 2.0;

cout << messageAvg << average << “.” << endl;

Page 21: CS 1430: Programming in C++

21

A Complete Program

Semantics

Syntax

Style

Page 22: CS 1430: Programming in C++

22

//--------------------------------------------------------------// Name: Qi Yang//// Course: CS143, Section 9, Spring 2011//// Description: This program computes the float average of two // integers.//// Input: Two integers.// // Output: The float average of the two integers.////--------------------------------------------------------------

#include <iostream> using namespace std;

int main(){ float average; int num1, num2; cout << endl << "Enter the first integer: "; cin >> num1; // 3 cout << endl << "Enter the second integer: "; cin >> num2; // 4

average = (num1 + num2) / 2; // Do we get float average?

cout << “\nThe average is " << average << ‘.’;

return 0; }

Page 23: CS 1430: Programming in C++

23

//--------------------------------------------------------------// Name: Qi Yang//// Course: CS143, Section 9, Spring 2011//// Description: This program computes the float average of two // integers//// Input: Two integers// // Output: The float average of the two integers////--------------------------------------------------------------

#include <iostream>using namespace std;

int main(){ float average; int num1, num2; cout << endl << "Enter the first integer: "; cin >> num1; // 3 cout << endl << "Enter the second integer: "; cin >> num2; // 4

average = (num1 + num2) / 2.0;

cout << “\nThe average is " << average << ‘.’;

return 0; }

Page 24: CS 1430: Programming in C++

24

Quiz 1-31 point

Due 5 PM, Friday

1. Download Quiz1-3.cpp (Go to my home page first)2. Treat it like a Lab3. Complete the program (Follow DO instructions) (Do not delete DO instructions)4. Submit to Grader YangQ-Quiz1-35. Differences: NONE6. Lab0 instructions

Page 25: CS 1430: Programming in C++

Lab1

• 5 points by Thursday, at 5pm

• 3 points by the following Tuesday, at 5pm

• Zero points after that

• Where to find the Lab?

K: drive

• Lab Help Session

Thursday, 9 - 325

Page 26: CS 1430: Programming in C++

Program 1

• Available on Web site and K: drive

• Can wait until IF statement is covered

• Due Date: 9/21/2015

• Grace Date: 9/24/2015

26

Page 27: CS 1430: Programming in C++

Notes

• On our class home page at

http://people.uwplatt.edu/~yangq/cs143/

• Can print out before class

• Should review the notes

27