Computer Programming Basics Jeon, Seokhee Assistant Professor Department of Computer Engineering,...
description
Transcript of Computer Programming Basics Jeon, Seokhee Assistant Professor Department of Computer Engineering,...
![Page 1: Computer Programming Basics Jeon, Seokhee Assistant Professor Department of Computer Engineering, Kyung Hee University, Korea](https://reader035.fdocuments.net/reader035/viewer/2022062814/56816812550346895ddda1fb/html5/thumbnails/1.jpg)
ComputerComputer Programming BasicsProgramming Basics
Jeon, Seokhee
Assistant ProfessorAssistant ProfessorDepartment of Computer Engineering,Department of Computer Engineering,
Kyung Hee University, KoreaKyung Hee University, Korea
![Page 2: Computer Programming Basics Jeon, Seokhee Assistant Professor Department of Computer Engineering, Kyung Hee University, Korea](https://reader035.fdocuments.net/reader035/viewer/2022062814/56816812550346895ddda1fb/html5/thumbnails/2.jpg)
Pre-compiler directive
Opening brace
Closing brace
Opening brace
Closing brace
Structure of a C++ ProgramStructure of a C++ Program
![Page 3: Computer Programming Basics Jeon, Seokhee Assistant Professor Department of Computer Engineering, Kyung Hee University, Korea](https://reader035.fdocuments.net/reader035/viewer/2022062814/56816812550346895ddda1fb/html5/thumbnails/3.jpg)
Without namespaceWithout namespace
Hello World!Hello World!
Namespace std contains all the classes, objects and functions of the standard
C++ library. #include <iostream>int main () { std::cout << "Hello world!\n"; return 0;}
![Page 4: Computer Programming Basics Jeon, Seokhee Assistant Professor Department of Computer Engineering, Kyung Hee University, Korea](https://reader035.fdocuments.net/reader035/viewer/2022062814/56816812550346895ddda1fb/html5/thumbnails/4.jpg)
Preprocessor DirectivesPreprocessor Directives
#include <iostream>•“I want to use a predefined library called iostream”•Always start with a ‘#’•iostream: a library for inputs (from e.g., a user) and outputs (to e.g., the monitor)
![Page 5: Computer Programming Basics Jeon, Seokhee Assistant Professor Department of Computer Engineering, Kyung Hee University, Korea](https://reader035.fdocuments.net/reader035/viewer/2022062814/56816812550346895ddda1fb/html5/thumbnails/5.jpg)
““using” Directivesusing” Directives
using namespace std;•“I want to use objects in a name group ‘std’ ”•Tells the compiler where to look for names in the library•Can deal with the situation where two or more objects in different libraries share a same name (naming confliction).
– Read Appendix N for more about namespace
![Page 6: Computer Programming Basics Jeon, Seokhee Assistant Professor Department of Computer Engineering, Kyung Hee University, Korea](https://reader035.fdocuments.net/reader035/viewer/2022062814/56816812550346895ddda1fb/html5/thumbnails/6.jpg)
main functionmain function
int main()•The main body of the program.•Compiler first tries to locate “main()” to find where to begin the program•In the form of a function
– I will cover “function” soon
![Page 7: Computer Programming Basics Jeon, Seokhee Assistant Professor Department of Computer Engineering, Kyung Hee University, Korea](https://reader035.fdocuments.net/reader035/viewer/2022062814/56816812550346895ddda1fb/html5/thumbnails/7.jpg)
CommentComment
• Internal program document• Not considered as a program code
Start of comment
End of commentStart of comment
End of comment
![Page 8: Computer Programming Basics Jeon, Seokhee Assistant Professor Department of Computer Engineering, Kyung Hee University, Korea](https://reader035.fdocuments.net/reader035/viewer/2022062814/56816812550346895ddda1fb/html5/thumbnails/8.jpg)
Nested Block Comments are InvalidNested Block Comments are Invalid
![Page 9: Computer Programming Basics Jeon, Seokhee Assistant Professor Department of Computer Engineering, Kyung Hee University, Korea](https://reader035.fdocuments.net/reader035/viewer/2022062814/56816812550346895ddda1fb/html5/thumbnails/9.jpg)
VariablesVariables
• Named memory locations that have a type– Named: identifier– Type: needs declaration
• What you can do with variables– Storing data– Modifying data– Reading data
![Page 10: Computer Programming Basics Jeon, Seokhee Assistant Professor Department of Computer Engineering, Kyung Hee University, Korea](https://reader035.fdocuments.net/reader035/viewer/2022062814/56816812550346895ddda1fb/html5/thumbnails/10.jpg)
Variables and IdentifiersVariables and Identifiers
MemoryAddress of memory:
Hard to remember
Identifier: name of address
![Page 11: Computer Programming Basics Jeon, Seokhee Assistant Professor Department of Computer Engineering, Kyung Hee University, Korea](https://reader035.fdocuments.net/reader035/viewer/2022062814/56816812550346895ddda1fb/html5/thumbnails/11.jpg)
Variables and IdentifiersVariables and Identifiers
Memory
studentIDstudentGrade1
studentGrade2
Identifiers
![Page 12: Computer Programming Basics Jeon, Seokhee Assistant Professor Department of Computer Engineering, Kyung Hee University, Korea](https://reader035.fdocuments.net/reader035/viewer/2022062814/56816812550346895ddda1fb/html5/thumbnails/12.jpg)
Variables and IdentifiersVariables and Identifiers
Memory
studentID
studentGrade
studentNameCompiler keeps trackof [identifier-address]table
![Page 13: Computer Programming Basics Jeon, Seokhee Assistant Professor Department of Computer Engineering, Kyung Hee University, Korea](https://reader035.fdocuments.net/reader035/viewer/2022062814/56816812550346895ddda1fb/html5/thumbnails/13.jpg)
Variables and IdentifiersVariables and Identifiers
In programstudentID_Total_Grade = studentGrade1 + studentGrade2
![Page 14: Computer Programming Basics Jeon, Seokhee Assistant Professor Department of Computer Engineering, Kyung Hee University, Korea](https://reader035.fdocuments.net/reader035/viewer/2022062814/56816812550346895ddda1fb/html5/thumbnails/14.jpg)
Naming IdentifiersNaming Identifiers
• Allowed characters: A-Z, a-z, 0-9, _ (underscore)• Not allowed to start with a digit. E.g., 3class (x),
class3(o)• The identifier cannot duplicate a reserved word. e.g.,
if, case, while…• Good names descriptive but short• C++ is case sensitive; PI, Pi and pi are different.
![Page 15: Computer Programming Basics Jeon, Seokhee Assistant Professor Department of Computer Engineering, Kyung Hee University, Korea](https://reader035.fdocuments.net/reader035/viewer/2022062814/56816812550346895ddda1fb/html5/thumbnails/15.jpg)
Standard Data TypesStandard Data Types
![Page 16: Computer Programming Basics Jeon, Seokhee Assistant Professor Department of Computer Engineering, Kyung Hee University, Korea](https://reader035.fdocuments.net/reader035/viewer/2022062814/56816812550346895ddda1fb/html5/thumbnails/16.jpg)
Integer and Floating Point TypesInteger and Floating Point Types
2 or 4 Bytes2 or 4 Bytes
4 Bytes4 Bytes
2 Bytes2 Bytes
8 Bytes8 Bytes
10 Bytes10 Bytes
4 Bytes4 Bytes
Size of value type depends on computer architectureSize of value type depends on computer architecture
![Page 17: Computer Programming Basics Jeon, Seokhee Assistant Professor Department of Computer Engineering, Kyung Hee University, Korea](https://reader035.fdocuments.net/reader035/viewer/2022062814/56816812550346895ddda1fb/html5/thumbnails/17.jpg)
Maximum/Minimum of Integer Value Maximum/Minimum of Integer Value TypeType
Type Sign Byte Minimum value Maximum value
short int/shortsigned
2-32,768 32,767
unsigned 0 65,535
int (PC)signed
2-32,768 32,767
unsigned 0 65,535
int (Mainframe)signed
4-2,147,483,648 2,147,483,647
unsigned 0 4,294,967,295
long int/longsigned
4-2,147,483,648 2,147,483,647
unsigned 0 4,294,967,295
![Page 18: Computer Programming Basics Jeon, Seokhee Assistant Professor Department of Computer Engineering, Kyung Hee University, Korea](https://reader035.fdocuments.net/reader035/viewer/2022062814/56816812550346895ddda1fb/html5/thumbnails/18.jpg)
Maximum/Minimum of C++ data typesMaximum/Minimum of C++ data types
![Page 19: Computer Programming Basics Jeon, Seokhee Assistant Professor Department of Computer Engineering, Kyung Hee University, Korea](https://reader035.fdocuments.net/reader035/viewer/2022062814/56816812550346895ddda1fb/html5/thumbnails/19.jpg)
Variables DeclarationVariables Declaration
![Page 20: Computer Programming Basics Jeon, Seokhee Assistant Professor Department of Computer Engineering, Kyung Hee University, Korea](https://reader035.fdocuments.net/reader035/viewer/2022062814/56816812550346895ddda1fb/html5/thumbnails/20.jpg)
Variable InitializationVariable Initialization
• Variable declaration ≠ variable initialization• Should be initialized by a programmer before it is
usede.g.,int count; declaration (o), initialization(x)char grade = ‘d’; declaration (o), initialization(o)
![Page 21: Computer Programming Basics Jeon, Seokhee Assistant Professor Department of Computer Engineering, Kyung Hee University, Korea](https://reader035.fdocuments.net/reader035/viewer/2022062814/56816812550346895ddda1fb/html5/thumbnails/21.jpg)
ConstantsConstants
• Data values that cannot be changed during program execution
• E.g.,– 3.141592– ‘d’– “Hello word”– ‘\0’
![Page 22: Computer Programming Basics Jeon, Seokhee Assistant Professor Department of Computer Engineering, Kyung Hee University, Korea](https://reader035.fdocuments.net/reader035/viewer/2022062814/56816812550346895ddda1fb/html5/thumbnails/22.jpg)
![Page 23: Computer Programming Basics Jeon, Seokhee Assistant Professor Department of Computer Engineering, Kyung Hee University, Korea](https://reader035.fdocuments.net/reader035/viewer/2022062814/56816812550346895ddda1fb/html5/thumbnails/23.jpg)
To RememberTo Remember
• A character constant is enclosed by the single quotes. (e.g. ‘a’)
• Use double quotes for string constants. (e.g. “Jeon, Seokhee”)
• bool types are treated as a number. True: non-zero. False: zero.
![Page 24: Computer Programming Basics Jeon, Seokhee Assistant Professor Department of Computer Engineering, Kyung Hee University, Korea](https://reader035.fdocuments.net/reader035/viewer/2022062814/56816812550346895ddda1fb/html5/thumbnails/24.jpg)
Standard streamsStandard streams
• A mapping between data and input/output device
![Page 25: Computer Programming Basics Jeon, Seokhee Assistant Professor Department of Computer Engineering, Kyung Hee University, Korea](https://reader035.fdocuments.net/reader035/viewer/2022062814/56816812550346895ddda1fb/html5/thumbnails/25.jpg)
More about More about coutcout
• width(int) function sets the width for printing a value
• Only works until the next insertion command comes
int x = 42;cout.width(5);cout << x << ‘\n’; // Outputs 42
cout << x << ‘\n’; // Outputs 42
![Page 26: Computer Programming Basics Jeon, Seokhee Assistant Professor Department of Computer Engineering, Kyung Hee University, Korea](https://reader035.fdocuments.net/reader035/viewer/2022062814/56816812550346895ddda1fb/html5/thumbnails/26.jpg)
![Page 27: Computer Programming Basics Jeon, Seokhee Assistant Professor Department of Computer Engineering, Kyung Hee University, Korea](https://reader035.fdocuments.net/reader035/viewer/2022062814/56816812550346895ddda1fb/html5/thumbnails/27.jpg)
More about More about coutcout
• fill(char) function sets the fill character. • The character remains as the fill character until set
again.
int x = 42;cout.width(5);cout.fill(‘*’);cout << x << ‘\n’; // Outputs ***42
![Page 28: Computer Programming Basics Jeon, Seokhee Assistant Professor Department of Computer Engineering, Kyung Hee University, Korea](https://reader035.fdocuments.net/reader035/viewer/2022062814/56816812550346895ddda1fb/html5/thumbnails/28.jpg)
![Page 29: Computer Programming Basics Jeon, Seokhee Assistant Professor Department of Computer Engineering, Kyung Hee University, Korea](https://reader035.fdocuments.net/reader035/viewer/2022062814/56816812550346895ddda1fb/html5/thumbnails/29.jpg)
More about More about coutcout
• precision (int) sets the number of significant digits of float type numbers
float y = 23.1415;cout.precision(1);cout << y << '\n'; // Outputs 2e+01cout.precision(2);cout << y << '\n'; // Outputs 23cout.precision(3);cout << y << '\n'; // Outputs 23.1
![Page 30: Computer Programming Basics Jeon, Seokhee Assistant Professor Department of Computer Engineering, Kyung Hee University, Korea](https://reader035.fdocuments.net/reader035/viewer/2022062814/56816812550346895ddda1fb/html5/thumbnails/30.jpg)
More about More about coutcout
• Output Manipulators (not a function)endl - outputs a new line character, flushes outputdec - sets int output to decimalhex - sets int output to hexadecimaloct - sets int output to octal
#include <iomanip.h>int x = 42;cout << oct << x << endl; // Outputs 52\ncout << hex << x << endl; // Outputs 2a\ncout << dec << x << endl; // Outputs 42\n
![Page 31: Computer Programming Basics Jeon, Seokhee Assistant Professor Department of Computer Engineering, Kyung Hee University, Korea](https://reader035.fdocuments.net/reader035/viewer/2022062814/56816812550346895ddda1fb/html5/thumbnails/31.jpg)
![Page 32: Computer Programming Basics Jeon, Seokhee Assistant Professor Department of Computer Engineering, Kyung Hee University, Korea](https://reader035.fdocuments.net/reader035/viewer/2022062814/56816812550346895ddda1fb/html5/thumbnails/32.jpg)
Example codes reading (Program 2-2)Example codes reading (Program 2-2)
• #include <iostream>• using namespace std;
• int main (void) • {• int a;• int b;• int c;• int sum;
• cout << "Welcome. This program adds\n";• cout << "three numbers. Enter three numbers\n";• cout << "in the form: nnn nnn nnn <return>\n";
• cin >> a >> b >> c;
• // Numbers are now stored in a, b, and c. Add them.• sum = a + b + c;
• cout << "\nThe total is: " << sum << "\n";• cout << "\nThank you. Have a good day.\n";• return 0;• } // main
Welcome. This program addsthree numbers. Enter three numbersin the form: nnn nnn nnn <return>11 22 33
The total is: 66
Thank you. Have a good day.
![Page 33: Computer Programming Basics Jeon, Seokhee Assistant Professor Department of Computer Engineering, Kyung Hee University, Korea](https://reader035.fdocuments.net/reader035/viewer/2022062814/56816812550346895ddda1fb/html5/thumbnails/33.jpg)
Try to understand other examples in Try to understand other examples in textbook!textbook!
• Program 2-3 ~ 2-13
![Page 34: Computer Programming Basics Jeon, Seokhee Assistant Professor Department of Computer Engineering, Kyung Hee University, Korea](https://reader035.fdocuments.net/reader035/viewer/2022062814/56816812550346895ddda1fb/html5/thumbnails/34.jpg)
![Page 35: Computer Programming Basics Jeon, Seokhee Assistant Professor Department of Computer Engineering, Kyung Hee University, Korea](https://reader035.fdocuments.net/reader035/viewer/2022062814/56816812550346895ddda1fb/html5/thumbnails/35.jpg)