Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSCI 161: Introduction to Programming

Similar presentations


Presentation on theme: "CSCI 161: Introduction to Programming"— Presentation transcript:

1 CSCI 161: Introduction to Programming
Lecture 3: First Program Data representation

2 First Program //library inclusion //function “main” //display string
#include <iostream> using namespace std; int main() { cout << ”Hello world.”; return 0; } //library inclusion //function “main” //display string

3 Programming Errors Vocabulary and grammar - syntax
Rules determine if statement legally constructed - syntax rules Compiler checks rules - gives syntax errors Programs also contain errors in logic - bugs Debugging - finding & fixing logic errors

4 A program to output the sum of two numbers

5 A more flexible program: read the two numbers from console
#include <iostream> using namespace std; int main() { int num1, num2; //two int variables to hold the two numbers int sum; //variable sum to hold the result of the sum of the two numbers //input the two numbers, first print message to the users on what they need to input cout << "please input two numbers:" << endl; cout << "num1 = "; cin >> num1; cout << "num2 = "; cin >> num2; //calculate the sum, store the result in variable sum sum = num1 + num2; //output the result cout << "the sum is " << sum << endl; return 0; }

6 Variable A variable should be declare as with a type: Examples: int num1, num2; //two int variables to hold the two numbers int sum; Type example int, char, string

7 Running the program Wrong result! What happen? A run:
please input two numbers: num1 = 2 num2 = 3 the sum is 5 Another run: num1 = 450 num2 = 300 the sum is 750 One more time please input two numbers: num1 = num2 = the sum is Wrong result! What happen?

8 Data Representation

9 Components of a Computer
Start with components of a computer again to talk about memory and data representation. © 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

10 © 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Memory © 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

11 © 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Memory Cells memory cell an individual storage location in memory address of a memory cell the relative position of a memory cell in the computer’s main memory contents of a memory cell the information stored in a memory cell, either a program instruction or data stored program concept a computer’s ability to store program instructions in main memory for execution ….. Memory cells contain a bunch of 0 and 1s. © 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

12 Figure 1.5 Relationship Between a Byte and a Bit
Byte Bit © 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

13 © 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Memory data storage setting the individual bits of a memory cell to 0 or 1, destroying its previous contents data retrieval copying the contents of a particular memory cell to another storage area random access memory (RAM) the part of main memory that temporarily stores programs, data, and results © 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

14 © 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Memory read-only memory (ROM) the part of main memory that permanently stores programs or data volatile memory memory whose contents disappear when the computer is switched off © 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

15 © 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Memory secondary storage units such as disks or flash drives that retain data even when the power to the drive is off disk thin platter of metal or plastic on which data are represented by magnetized spots arranged in tracks optical drive device that uses a laser to access or store data on a CD or DVD or Blu-ray Disk © 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

16 © 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
File System file named collection of data stored on a disk directory a list of the names of files stored on a disk subdirectory a list of the names of files that relate to a particular topic © 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

17 Internal Representation
Each unit of memory a two-state device off or on, 0 or 1 represent in Binary, two Binary Digits (bits) Organized into groups of 8 bits - bytes represents single keyboard character Larger grouping of 16 or 32 bits - word represents single integer value identified by address for access

18 Memory Sizes Kilobyte (K) = 210 = 1,024 bytes
Megabyte (Mb) = 220 = 1,048,576 bytes Gigabyte (Gb) = 230 = 1,073,741,824 bytes

19 Decimal Number Systems
Base 10 Digits - 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 e.g = = 3 x x x100 = 3 x x x 1 =

20 Binary Number System Base 2 Digits 0, 1 e.g. 1102 =
= 1 x x x 20 = 1 x x x 1 = = 6

21 Counting in Binary Decimal Binary 0 0 1 1 2 10 3 11 4 100 5 101 6 110
Decimal Binary

22 How many symbols can N digit represent?
3 digit allows representing 8 numbers (symbols): 0-7 : – 15 N N


Download ppt "CSCI 161: Introduction to Programming"

Similar presentations


Ads by Google