thursday, december 07, 2006 “vision without action is a daydream, action without a vision is a...

26
Thursday, December 07, 2006 “Vision without action is a daydream, Action without a vision is a nightmare.- Japanese Proverb

Post on 19-Dec-2015

215 views

Category:

Documents


0 download

TRANSCRIPT

Thursday, December 07, 2006

“Vision without action is a daydream,

Action without a vision is a nightmare.”

- Japanese Proverb

CS 192 / CMPE 191

Change in Grading Breakup20% Quizzes + Homework20% Programming Assignments25% Midterm35% Final Exam (25% Paper-based,

10% Lab exam)

CS 192 / CMPE 191

Sections 1 & 2

Comments have two forms in C++

//I am a Single line comment/*I am a Multi-line

comment andI can spanmultiple linesThis is my line number 5*/

#include <iostream.h>int main( ){cout <<"Welcome to CS192/CMPE191";return 0; //this is a simple program

}//end block of main

int main(){cout <<"Welcome to CS192/CMPE191";return 0;}

Bad programming style!

Statements end with a semi-colon ;

Omitting a semicolon is a syntax error.

Other errors could be:• mis-typing a keyword• omitting braces or parenthesis• forgetting << operator in cout• …

Compiler can catch such errors.

More cout examples

Constants

cout<< -23;

cout<< “Hello”;

cout<< 24.5;

Constants

cout<< -23 <<“\n”;

cout<< “Hello”<<“\n”;

cout<< 24.5 <<“\n”;

Programs need to manipulate data.

Programs need to manipulate data.

C++ uses variables to name and store data.

Variables and IdentifiersThe compiler assigns a memory location to

each variable in the program.

The name of the variable (the identifier) is used to refer to that memory location.

Variables and Identifiers Identifiers

case sensitive must begin with a letter or underscore consist of letter, digits and underscore only (no

special characters) can not use reserve words (key words)

Use meaningful names for variables.

Variable declarations

What kind of data would you be storing in a variable.

Variables – must be declared before use.

int value;

value = 23;

cout<< value;

Variables – must be declared before use.

int value;

value = 23;

cout<< value;

Assignment statement

Simple I/Ocin

Used for input uses the >> (input operator)

cout Used for output uses the << (output) operator

/*****************************************************

* Sum two numbers

******************************************************/

#include <iostream.h>//declares standard I/O library

int main()//must have one and only one function named main

{

int number1, number2; //declare two integer variables

cout << “enter two integers” << endl;//prompt for input

cin >> number1 >> number2; //input values from keyboard

cout << number1 + number2; //output sum to the screen

return 0;

}//end block of main

#include <iostream.h>int main(){

int number_of_pods, peas_per_pod, total_peas;cout<<"Press return after entering number\n";cout<<"Enter number of pods\n";cin>>number_of_pods;cout<<"Enter number of peas in a pod\n";cin>>peas_per_pod;total_peas = number_of_pods * peas_per_pod;cout<<"If you have ";cout<<number_of_pods;cout<< " pea pods\n";cout<<"and ";cout<<peas_per_pod;cout<<" peas in each pod, then\n";cout<<"you have ";cout<<total_peas;cout<<" peas in all the pods.\n";return 0;

}

#include <iostream.h>int main(){int number_of_pods, peas_per_pod, total_peas;

cout<<"Press return after entering number\n";

cout<<"Enter number of pods\n";

cin>>number_of_pods;

cout<<"Enter number of peas in a pod\n";

cin>>peas_per_pod;

total_peas = number_of_pods * peas_per_pod;

cout<<"If you have ";

cout<<number_of_pods;

cout<< " pea pods\n";

cout<<"and ";

cout<<peas_per_pod;

cout<<" peas in each pod, then\n";

cout<<"you have ";

cout<<total_peas;

cout<<" peas in all the pods.\n";return 0;

}

Enter number of pods

20

Enter number of peas in a pod

8

If you have 20 pea pods

and 8 peas in each pod, then

you have 160 peas in all the pods.

C++ Data Types

char short int float double …

--

Type specifiers e.g. int, long, short etc

Specific ranges of values are system dependent

On many systems short integer ranges from -32768 to 32767

unsigned short has range of values 0 to 65535

int 4 bytes -2147,438,648 to 2,147,483,647

float 4 bytes 3.4E-38 to 3.4E+38

Double 8 bytes 1.7E-308 to 1.7E+308