🎧 New: AI-Generated Podcasts Turn your study notes into engaging audio conversations. Learn more

EXAM1_CS_Review.pdf

Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...

Transcript

William T. Doan A First Trimesterly Review of CS 1436 Written for Dr. Brian Wescott Ricks and his 001 and 009 sections Confidential and Proprietary — Not for External Distribution. This material is protected by copyright under the Berne Convention for the Protection of Literary and Artistic Works...

William T. Doan A First Trimesterly Review of CS 1436 Written for Dr. Brian Wescott Ricks and his 001 and 009 sections Confidential and Proprietary — Not for External Distribution. This material is protected by copyright under the Berne Convention for the Protection of Literary and Artistic Works, as well as applicable national and international copyright laws. Unauthorized distribution, reproduction, or any other use without express written permission is strictly prohibited and may result in legal action. September 2024 Springer Berlin Heidelberg NewYork Hong Kong London Milan Paris Tokyo Dedication This work is dedicated to Dr. Brian Ricks for having seen potential in one young student—one short year ago. Contents 1 Introduction to Computers and Programming............. 1 1.1 Why Program?.......................................... 1 1.2 Computer Systems: Hardware and Software................. 1 1.2.1 Major Hardware Component Categories.............. 1 1.3 Central Processing Unit (CPU)........................... 1 1.4 Main Memory........................................... 2 1.5 Secondary Storage....................................... 2 1.6 Input & Output Devices.................................. 2 1.7 Software – Programs that Run on a Computer.............. 2 1.8 Programs and Programming Languages.................... 3 1.8.1 Example Programming Problem..................... 3 1.9 Algorithms and Machine Language........................ 3 1.9.1 Machine Language................................. 3 1.10 High-Level Languages.................................... 4 1.11 Integrated Development Environments (IDEs)............... 4 1.12 What is a Program Made of?............................. 4 1.12.1 C++ Key Words.................................. 4 1.12.2 Syntax........................................... 4 1.12.3 Variables......................................... 5 1.13 Input, Processing, and Output............................ 5 1.14 The Programming Process................................ 5 1.14.1 Design........................................... 5 1.15 Procedural and Object-Oriented Programming.............. 6 1.15.1 Procedural Programming........................... 6 1.15.2 Object-Oriented Programming...................... 6 2 Introduction to C++...................................... 7 2.1 The Parts of a C++ Program............................. 7 2.2 Functions............................................... 7 2.2.1 Special Characters................................. 8 2.3 The cout Object........................................ 8 Contents VII 2.3.1 Multiple Insertions................................ 8 2.3.2 The endl Stream Manipulator...................... 9 2.3.3 The Newline Escape Sequence \n.................... 9 2.4 The #include Directive.................................. 9 2.5 Variables, Literals, and Assignment Statements.............. 9 2.5.1 Variables......................................... 9 2.5.2 Variables and Literals.............................. 10 2.5.3 Literals.......................................... 10 3 Expressions and Interactivity.............................. 11 3.1 The cin Object......................................... 11 3.1.1 Using cin for Input................................ 11 3.1.2 How cin Works................................... 11 3.1.3 Potential Buffer Issues............................. 12 3.1.4 Defensive Programming with cin.................... 12 3.2 Mathematical Expressions................................ 12 3.2.1 Order of Operations............................... 12 3.2.2 Operator Precedence and Associativity............... 13 3.2.3 Grouping with Parentheses......................... 13 3.2.4 No Exponentiation Operator in C++................ 13 3.3 Overflow and Underflow.................................. 13 3.4 Type Conversion........................................ 13 3.4.1 Hierarchy of Data Types........................... 14 3.4.2 Type Casting..................................... 14 3.5 Combined Assignment Operators.......................... 14 3.6 Formatting Output...................................... 14 3.6.1 setprecision.................................... 14 3.6.2 fixed and showpoint.............................. 14 3.6.3 setw, left, and right............................. 15 3.7 Working with Characters and string Objects............... 15 3.7.1 Reading Strings with Spaces........................ 15 3.7.2 Reading Characters................................ 15 3.7.3 Using cin.ignore................................. 15 3.8 Mathematical Library Functions........................... 15 3.8.1 Random Numbers................................. 15 3.9 Hand Tracing a Program................................. 16 4 Number Systems.......................................... 17 4.1 Number Systems........................................ 17 4.2 Decimal Numbers....................................... 17 4.3 Binary Numbers......................................... 17 4.3.1 Conversion from Binary to Decimal.................. 18 4.3.2 Powers of 2....................................... 18 4.3.3 Conversion from Decimal to Binary.................. 18 4.4 Hexadecimal Numbers................................... 19 VIII Contents 4.4.1 Conversion from Binary to Hexadecimal.............. 19 1 Introduction to Computers and Programming 1.1 Why Program? A computer is a programmable electronic device that stores, retrieves, and processes a large quantity of data both quickly and accurately. 1.2 Computer Systems: Hardware and Software A computer is a system of hardware and software devices/components. The physical components of the computer are called hardware. 1.2.1 Major Hardware Component Categories 1. Central Processing Unit (CPU) 2. Main memory 3. Secondary storage devices 4. Input devices 5. Output devices 1.3 Central Processing Unit (CPU) The central processing unit, or CPU, is the part of the computer that runs programs. The CPU is comprised of two components: the control unit and the arith- metic and logic unit (ALU). 2 1 Introduction to Computers and Programming Fig. 1.1. Organization of the CPU 1.4 Main Memory Main Memory is rapid-access, relatively low-capacity storage for the in- structions and data of executing/running programs. Main Memory is volatile. Data and instructions stored here are erased when the program terminates or the computer is turned off. Also called Random Access Memory (RAM). Memory is divided into locations called bytes. – One byte is enough memory to store a letter of the alphabet or a small number. Addresses – Each byte in memory is identified by a unique number known as an address. 1.5 Secondary Storage Secondary storage is long-term, high-capacity storage for programs and data not currently in use. Secondary storage is persistent (non-volatile); data is retained when the program stops running or the computer is turned off. 1.6 Input & Output Devices Input is data the computer accepts from outside for processing. Output is data the computer sends to the outside. 1.7 Software – Programs that Run on a Computer Categories of software: System software: programs that manage the computer hardware and the programs that run on them. Examples: operating systems, utility pro- grams, and software development tools. 1.9 Algorithms and Machine Language 3 Application software: programs that provide services to the user. Ex- amples: word processors, spreadsheets, image editing software, games, and other programs to solve specific problems. 1.8 Programs and Programming Languages A program is a set of instructions that the computer follows to perform a task. 1.8.1 Example Programming Problem Write a program that calculates and displays an hourly worker’s gross pay given the hours worked and pay rate entered by the user, when the program runs. Here is a list of things the program should do: 1. Display a message on the screen asking “How many hours did you work?” 2. Wait for the user to enter the number of hours worked. Once the user enters a number, store it in memory. 3. Display a message on the screen asking, “How much do you get paid per hour?” 4. Wait for the user to enter an hourly pay rate. Once the user enters a number, store it in memory. 5. Multiply the number of hours by the hourly pay rate and store the result in memory. 6. Display a message on the screen that tells the amount of money earned. The message must include the result of the calculation performed in Step 5. 1.9 Algorithms and Machine Language The list of steps forms an algorithm. An algorithm is a set of well-defined, ordered steps for performing a task or solving a problem. Although we can easily understand this algorithm, it is not understandable by a computer. Computers only understand algorithms expressed in machine language. 1.9.1 Machine Language Computers execute programs composed of numeric instructions. Machine language is the numeric language understood by a computer processor. A machine language instruction is a binary pattern (containing only 1’s and 0’s). 4 1 Introduction to Computers and Programming It takes multiple instructions to perform a simple mathematical calcula- tion. Example of machine language instructions: 0001 0011 1001 0100 0011 0001 0010 0000 0011 0010 0011 0000 0110 0001 0001 0010 1001 0011 0001 0000 1.10 High-Level Languages Programs today are typically written in programming languages that use words and symbols instead of binary patterns. Special software is used to convert programs from these languages to the machine language understood by a particular computer processor. In C++, the program that does the conversion is called a compiler. 1.11 Integrated Development Environments (IDEs) An Integrated Development Environment (IDE) combines the tools needed to write, compile, and debug a program into a single software application. 1.12 What is a Program Made of ? Common elements in high-level programming languages: Key Words Programmer-Defined Identifiers Operators Punctuation Syntax 1.12.1 C++ Key Words 1.12.2 Syntax The rules of grammar that must be followed when writing a program. Controls the use of key words, operators, programmer-defined symbols, and punctuation. 1.14 The Programming Process 5 1.12.3 Variables A variable is a named storage location in the computer’s memory for holding a piece of data. A variable can hold one data item at a time, but the data held can be changed while the program is running. A variable holds a specific type of data. 1.13 Input, Processing, and Output Three steps that programs typically perform: 1. Gather input data: From keyboard From files on disk drives From other hardware devices 2. Process the input data. 3. Display the results as output: Send it to the screen Write it to a file Send it to another hardware device 1.14 The Programming Process The programming process includes: – Analysis – Design – Coding – Testing – Debugging 1.14.1 Design Pseudocode is a cross between human language and a programming lan- guage that is often used by programmers to create an algorithm. An algorithm is an ordered sequence of well-defined steps for solving a problem or accomplishing a task. Sample pseudocode: Get the number of hours worked Get the hourly pay rate Multiply the hours by the pay rate and store the calculated pay Display the pay 6 1 Introduction to Computers and Programming 1.15 Procedural and Object-Oriented Programming Two popular methodologies for developing computer programs are pro- cedural programming and object-oriented programming. C++ can be used to write programs using either of these methodologies. In this course, our programs will be procedural in nature. 1.15.1 Procedural Programming Focus is on the tasks to be performed. Procedures/functions are written to perform the various tasks. Procedures contain their own variables and commonly share variables with other procedures. 1.15.2 Object-Oriented Programming Focus is on data, designing entities (objects/classes) which contain the data and the means to manipulate the data (methods). Objects encapsulate their data and methods. Objects do not know how other objects are implemented and cannot di- rectly access another object’s data. 2 Introduction to C++ 2.1 The Parts of a C++ Program Let’s examine a simple C++ program from Pr2-1.cpp: // A s i m p l e C++ program t h a t // d i s p l a y s a message on t h e s c r e e n #i n c l u d e u s i n g namespace s t d ; i n t main ( ) { c o u t

Tags

computer science programming C++ computers
Use Quizgecko on...
Browser
Browser