Unformatted text preview:

Slide 1Slide 2Slide 3Slide 4Slide 5Slide 6Slide 7Slide 8Slide 9Slide 10Slide 11Slide 12Slide 13Slide 14COSC 181 – Foundations of Computer ProgrammingClass 3Last TimeBasic C++ Programs (Output/Input)“escape sequences”Basic main() functionstd::cout and std::cin<< and >>// commentsTaking input from keyboard and adding integerssystem(“PAUSE”);Today’s GoalsFinish from FridayMore basic vocabularyVariables and Computer MemoryPractice with “arithmetic operators”arithmetic precedencePractice with “relational operators”overall operator precedenceIntroduction to Software Engineering processWhat does this program do then?// Text-printing program 2.#include <iostream> // allows program to output data to the screen// function main begins program executionint main(){ std::cout << "Welcome "; std::cout<< " to C++!\n”; system(“PAUSE”); // prepackaged wait function return 0; // indicate that program ended successfully} // end function mainOUTPUT:Welcome to C++!What about this program?// Text-printing program 3.#include <iostream> // allows program to output data to the screen// function main begins program executionint main(){ std::cout << "Wel\ncome\nto\n\nC+\n+!\n”; system(“PAUSE”); // prepackaged wait function return 0; // indicate that program ended successfully} // end function mainOUTPUT:WelcometoC++!Getting input from the keyboardstd::cinStandard character input streamInput in C++ occurs over streams as wellThe standard stream (above) is connected to the keyboard by defaultnamespace :: command“>>” (as opposed to “<<“) OperatorStream extraction operatorVariable DeclarationsVariables are used to store certain dataThe input data for our next program will be stored in variablesMust first be declaredint number1; (type variable_name)int number2;int sum;Then they can be initializedsum = number1 + number2;std::cin>>number1;Using Input/Addition/Output#include <iostream> // allows program to perform input and output// function main begins program executionint main(){ // variable declarations int number1; // first integer to add int number2; // second integer to add int sum; // sum of number1 and number2 std::cout << "Enter first integer: "; // prompt user for data std::cin >> number1; // read first integer from user into number1 std::cout << "Enter second integer: "; // prompt user for data std::cin >> number2; // read second integer from user into number2 sum = number1 + number2; // add the numbers; store result in sum std::cout << "Sum is " << sum << std::endl; // display sum; end line system(“PAUSE”); return 0; // indicate that program ended successfully} // end function mainNow Try it YourselfOpen up DevC++Type in the CodeUnder the “Execute” menu First hit “Compile”Wait for the screen to say Done (0 errors, 0 warnings)Then hit “Run”Vocabularyfundamental (primitive) typesint, char, doubleidentifiera.k.a. – variableIdentifiers can be anything so long as they aren’t keywords and don’t begin with a digitint return;int pq34_34;int 1number;Make sure your variable names make senseC++ is “case-sensitive”“char myVariable;” is different from “char myvariable;”  Only this one is allowable.Memory ConceptsEvery variable is linked to a spot in memoryEach variable has a name, size, type and value.Writing to a memory location (setting a variable) is “destructive”Reading from a memory location (outputting a variable) is “nondestructive”Look at Figs 2.6, 2.7, and 2.8 in your bookEquality and Relational OperatorsFig 2.12 gives a list of these operators><>=<=== (not to be confused with assignment operator “=“)!=binary operatorsEach gives a value of either “true” or “false” (1 or 0)Each is evaluated from left to rightbool answer = 343 > 43;bool answer = 23 <= 3;Code from Fig 2.13A few new tricks are presented here–“using”using std::cout;using std::cin;using std::endl;1. cin >> number1 >> number2;2. if ( number1 = = number2) cout << number1 << “==“ << number2 << endl;Try It YourselfType in the code as we did before•Compile and Run•Enter 3 Test Cases1. 99 982. 5 1003. 23


View Full Document

UVa-Wise COSC 181 - Foundations of Computer Programming

Download Foundations of Computer Programming
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Foundations of Computer Programming and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Foundations of Computer Programming 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?