Fundamentals of Programming Lecture 2: Input and Output, Lecture notes of Programming for Engineers

An introduction to input and output streams in C++ programming. It explains how to use cout for output, receive user input using cin, and the concept of include directives. It also covers the basics of variables, their types, and memory representation.

Typology: Lecture notes

2020/2021

Uploaded on 04/28/2021

mark-ilyas
mark-ilyas 🇵🇰

3 documents

1 / 37

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Fundamentals of
Programming
Lecture
2
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25

Partial preview of the text

Download Fundamentals of Programming Lecture 2: Input and Output and more Lecture notes Programming for Engineers in PDF only on Docsity!

Fundamentals of

Programming

Lecture

Input and Output

  • (^) A data stream is a sequence of data
    • (^) Typically in the form of characters or numbers
  • An input stream is data for the program to use - (^) Typically originates - (^) at the keyboard
  • (^) at a file
  • An output stream is the program’s output - (^) Destination is typically - (^) the monitor
  • (^) a file

Examples Using cout

  • (^) This produces the same result as the previous sample cout << number_of_bars ; cout << " candy bars\n";
  • (^) Here arithmetic is performed in the cout statement cout << "Total cost is $" << (price + tax);
  • (^) Quoted strings are enclosed in double quotes ("Walter") - Don’t use two single quotes (')
  • (^) A blank space can also be inserted with cout << " " ;

Include Directives

  • (^) Include Directives add library files to our programs
    • (^) To make the definitions of the cin and cout available to the program: #include
  • (^) Using Directives include a collection of defined names
    • (^) To make the names cin and cout available to our program: using namespace std;

Reading Data From cin

  • (^) Multiple data items are separated by spaces
  • (^) Data is not read until the enter key is pressed
    • (^) Allows user to make corrections
  • (^) Example: cin >> v1 >> v2 >> v3;
  • Requires three space separated values
    • (^) User might type 34 45 12

Designing Input and Output

Prompt the user for input that is desired

  • (^) cout statements provide instructions

cout << "Enter your age: ";

cin >> age;

  • Notice the absence of a new line before using cin

Echo the input by displaying

what was read

  • (^) Gives the user a chance to verify

data

Very Simplistic View of a Computer CPU Location 0 Location 1 Location 2 Location 3 Location 4 Location 5 Each location is 1 byte of memory 1 byte = 8 bits Each bit is an electric impulse carrying 1 or 0. This simplistic view is enough to explain the basic concepts of programming to students 5

Valu

e

  • (^) The only task a computer can do is arithmetic e.g. multiplying, dividing, subtracting, etc.
  • (^) Therefore, everything in the computer is represented as a value
  • Numbers, letters, characters, etc are all represented as values
  • (^) Values could change depending on their nature. For example
  • the temperature today is different from the temperature yesterday
  • The number of cars inside Lahore is different then the number of in cars Islamabad.

Variable

To store a value inside a computer a

‘variable’ is used.

A variable is a space in the memory to

store a value.

This space is reserved until the variable

is required.

Session 2 7

What Makes a Variable

Variable has three important characteristics:

  • (^) Type
    • (^) How much memory do a variable need.
      • This information is determined by a type.
  • (^) Name
  • (^) How to differentiate a variable with another variable of the same type.
  • Name refers to the memory location assigned to this variable.
  • (^) Value
    • (^) What is the value?
      • The actual value contained by a variable. Session 2 8

Example of a Variable (Memory View) int temperature = 35 Locations 0 - 3 are collectively called as ‘temperature’ 00000000 00000000 00000000 00100011 100011 is the binary equivalent of 35 Location 0 Location 1 Location 2 Location 3 Location 4 Location 5 Session 2 10

Changing the Value of Variable

Session 2

Lets change the value of

‘temperature’.

temperature = 45902 1011001101001110 is the binary equivalent of 45902 00000000 Location 0 Locations 0 - 3 are collectively (^00000000) Location 1 called as ‘temperature’ 10110011 Location^2 (^01001110) Location 3 Location 4 Location 5 11

Initializing Variables

  • (^) Declaring a variable does not give it a value
    • Giving a variable its first value is initializing the variable
  • (^) Variables are initialized in assignment statements double mpg; mpg = 26.3; // declare the variable // initialize the variable
  • (^) Declaration and initialization can be combined using two methods
  • (^) Method 1 double mpg = 26.3, area = 0.0 , volume;
  • (^) Method 2 double mpg(26.3), area(0.0), volume;

Variable for Real Numbers

int cannot hold a real value.

Therefore, a type “double” is used to hold real

values.

Double takes 8 bytes of memory instead of 4 bytes

of a double.

Out of the 8 bytes in a double 4 bytes are used to

hold the value before the decimal point and 4

bytes for the value after the decimal point.

ession 2 14