Window s Visual Studio Output

Similar documents
C/C++ Programming Lecture 7 Name:

Chapter 3: Expressions and Interactivity. Copyright 2012 Pearson Education, Inc. Thursday, October 9, 14

3.1. Chapter 3: The cin Object in Program 3-1. Displaying a Prompt 8/23/2014. The cin Object

Chapter 3: Expressions and Interactivity

Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

3.1. Chapter 3: Displaying a Prompt. Expressions and Interactivity

CHAPTER 3 Expressions, Functions, Output

Chapter 3. Numeric Types, Expressions, and Output

3.1. Chapter 3: The cin Object. Expressions and Interactivity

CS 151 Review #3. // More than one variable can be defined // in a statement. Multiple variables are // separated by a comma.

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

Expressions, Input, Output and Data Type Conversions

The cin Object. cout << "Enter the length and the width of the rectangle? "; cin >> length >> width;

Engineering Problem Solving with C++, Etter/Ingber

UNIT- 3 Introduction to C++

GO - OPERATORS. This tutorial will explain the arithmetic, relational, logical, bitwise, assignment and other operators one by one.

Introduction to C++ (Extensions to C)

c++ keywords: ( all lowercase ) Note: cin and cout are NOT keywords.

CSc 10200! Introduction to Computing. Lecture 4-5 Edgardo Molina Fall 2013 City College of New York

Lecture 4 Tao Wang 1

Review for COSC 120 8/31/2017. Review for COSC 120 Computer Systems. Review for COSC 120 Computer Structure

Week 2: Console I/O and Operators Arithmetic Operators. Integer Division. Arithmetic Operators. Gaddis: Chapter 3 (2.14,3.1-6,3.9-10,5.

I/O Streams and Standard I/O Devices (cont d.)

! A program is a set of instructions that the. ! It must be translated. ! Variable: portion of memory that stores a value. char

C++ Programming: From Problem Analysis to Program Design, Third Edition

Week 3: File I/O and Formatting 3.7 Formatting Output

Operators. Lecture 3 COP 3014 Spring January 16, 2018

BITG 1233: Introduction to C++

LECTURE 3 C++ Basics Part 2

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Chapter 2: Basic Elements of C++

Operators and Expressions in C & C++ Mahesh Jangid Assistant Professor Manipal University, Jaipur

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

Informatics Ingeniería en Electrónica y Automática Industrial

Operators in C. Staff Incharge: S.Sasirekha

Chapter 2 Basic Elements of C++

Add Subtract Multiply Divide

JAVA OPERATORS GENERAL

3. EXPRESSIONS. It is a sequence of operands and operators that reduce to a single value.

Expressions and Precedence. Last updated 12/10/18

Reserved Words and Identifiers

Character Set. The character set of C represents alphabet, digit or any symbol used to represent information. Digits 0, 1, 2, 3, 9

o Counter and sentinel controlled loops o Formatting output o Type casting o Top-down, stepwise refinement

Basic operators, Arithmetic, Relational, Bitwise, Logical, Assignment, Conditional operators. JAVA Standard Edition

Chapter 2: Introduction to C++

Chapter 2: Special Characters. Parts of a C++ Program. Introduction to C++ Displays output on the computer screen

Engineering Problem Solving with C++, 3e Chapter 2 Test Bank

I Internal Examination Sept Class: - BCA I Subject: - Principles of Programming Lang. (BCA 104) MM: 40 Set: A Time: 1 ½ Hrs.

C++ Basic Elements of COMPUTER PROGRAMMING. Special symbols include: Word symbols. Objectives. Programming. Symbols. Symbols.

Objectives. In this chapter, you will:

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

Definition Matching (10 Points)

LECTURE 02 INTRODUCTION TO C++

COMP Primitive and Class Types. Yi Hong May 14, 2015

Operators & Expressions

Department of Computer Science

C++ PROGRAMMING. For Industrial And Electrical Engineering Instructor: Ruba A. Salamh

9/10/10. Arithmetic Operators. Today. Assigning floats to ints. Arithmetic Operators & Expressions. What do you think is the output?

Chapter 2. Outline. Simple C++ Programs

1. Match each of the following data types with literal constants of that data type. A data type can be used more than once. A.

A flow chart is a graphical or symbolic representation of a process.

Fundamentals of Programming

Will introduce various operators supported by C language Identify supported operations Present some of terms characterizing operators

Numerical Computing in C and C++ Jamie Griffin. Semester A 2017 Lecture 2

Unit-2 (Operators) ANAND KR.SRIVASTAVA

The C++ Language. Arizona State University 1

CS313D: ADVANCED PROGRAMMING LANGUAGE

Sample Code: OUTPUT Daily Highs & Lows

Lecture 3 Tao Wang 1

WARM UP LESSONS BARE BASICS

Chapter 3: Operators, Expressions and Type Conversion

Objects and streams and files CS427: Elements of Software Engineering

Java enum, casts, and others (Select portions of Chapters 4 & 5)

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

Chapter 3 - Notes Input/Output

PART I. Part II Answer to all the questions 1. What is meant by a token? Name the token available in C++.

Chapter 12 Variables and Operators

COMP322 - Introduction to C++

The C++ Language. Output. Input and Output. Another type supplied by C++ Very complex, made up of several simple types.

Chapter 7. Additional Control Structures

Le L c e t c ur u e e 2 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Variables Operators

Operators. Java operators are classified into three categories:

Arithmetic Operators. Portability: Printing Numbers

Introduction. Following are the types of operators: Unary requires a single operand Binary requires two operands Ternary requires three operands

Chapter Two: Fundamental Data Types

CS103L FALL 2017 UNIT 1: TYPES, VARIABLES,EXPRESSIONS,C++ BASICS

UNIT-2 Introduction to C++

3.1 The cin Object. Expressions & I/O. Console Input. Example program using cin. Unit 2. Sections 2.14, , 5.1, CS 1428 Spring 2018

C++ Programming: From Problem Analysis to Program. Design, Fifth Edition. Chapter 1: An Overview of Computers and Programming Languages

Structured Programming Using C++ Lecture 2 : Introduction to the C++ Language. Dr. Amal Khalifa. Lecture Contents:

Infix to Postfix Conversion

UNIT 3 OPERATORS. [Marks- 12]

Operators and Type Conversion. By Avani M. Sakhapara Assistant Professor, IT Dept, KJSCE

Chapter 1. C++ Basics. Copyright 2010 Pearson Addison-Wesley. All rights reserved

LESSON 1. A C program is constructed as a sequence of characters. Among the characters that can be used in a program are:

A complex expression to evaluate we need to reduce it to a series of simple expressions. E.g * 7 =>2+ 35 => 37. E.g.

Information Science 1

This watermark does not appear in the registered version - Slide 1

Programming for Engineers: Operators, Expressions, and Statem

The Arithmetic Operators. Unary Operators. Relational Operators. Examples of use of ++ and

Transcription:

1. Explain the output of the program below. Program char str1[11]; char str2[11]; cout << "Enter a string: "; cin >> str1; Window s Visual Studio Output Enter a string: 123456789012345678901234567890 Enter another string: abcdefghijklmnopqrstuvwxyz str1 is "uvwxyz" and str2 is "abcdefghijklmnopqrstuvwxyz" cout << "Enter another string: "; cin >> str2; cout << "str1 is \"" << str1 << "\" and str2 is \"" << str2 << "\"" << endl; Run-Time Error Message in Visual Studio Pop-up Window: Run-Time Check Failure #2 - Stack around the variable 'str2' was corrupted. Linux Output with the Same Program and Input Enter a string: 123456789012345678901234567890 Enter another string: abcdefghijklmnopqrstuvwxyz str1 is "qrstuvwxyz" and str2 is "abcdefghijklmnopqrstuvwxyz" Segmentation fault (core dumped) 2. In an expression with more than one operator, evaluate in this order: - (unary negation), in order, left to right * / % (remainder), in order, left to right + -, in order, left to right Parentheses ( ) can be used to override the order of operations. Evaluate each of the following: a) 6 + 3 * 5 b) (6 + 3) / 2 c) 4 + 2 * 3-5 d) 7 % 4 + 5 * 6 e) 1 + 2 + 3 % 2 Lecture 3 Page1

3. Operations are performed between operands of the same type. If operands are not of the same type, C++ will automatically convert one operand to be the type of the other, called type coercion. The type coercion rules are: 1) char, short, unsigned short automatically promoted to int 2) When operating on values of different data types, the lower one is promoted to the type of the higher one according to the ranking:. long double (Highest) double float unsigned long long unsigned int int (Lowest) 3) When using the assignment operator =, the type of expression on right will be converted to type of variable on left. For the variables: short myshort=2; int myint=3; double mydouble=3; determine the type and value of the following expressions: a) myint / myshort * mydouble b) mydouble / myshort * myint c) myint * mydouble / myshort 4. You can explicitly convert a value to a specific type (called casting) by using C-Style cast: data type name in (): cout << ch << " is " << (int)ch; Prestandard C++ cast: value in (): cout << ch << " is " << int(ch); Both are still supported in C++, although static_cast is preferred: cout << ch << " is " << static_cast<int>(ch); Evaluate each of the following: a) float(2) + 5 / 2 b) 2.0 + 5 / static_cast<float>(2) c) (float)(2 + 5) / 2 5. For the variables: int myint, myotherint; double mydouble, myotherdouble; what value is assigned to each of the following variables? a) mydouble = myint = myotherdouble = 3.9; b) mydouble = myotherdouble = myint = 3.9; c) mydouble = myint = myotherint = 7; myint += 3; myotherint /= 3; mydouble /= 3; Lecture 3 Page2

6. The header file iomanip contains the following manipulators to control the formatting of the output (and input) of numbers and strings. Stream Manipulator setw(n) fixed scientific showpoint setprecision(n) left right Explain the output of the following program. // Program to experiment with formatted output #include <iomanip> Description Establishes a print field of at least size n Displays floating-point numbers in fixed-point notation Displays floating-point numbers in scientific notation Causes a decimal point and trailing zeroes to be displayed, even if there is no fractional part. Sets the precision of floating-point numbers to n Left justify the output Right justify the output cout <<"(default) with << setw(15) << right:" << setw(15) << right << endl; cout << "\ncout << fixed" << fixed << endl; cout << "\ncout << scientific" << scientific << endl; cout << "setprecision(6) << \"1234.5678\": " << setw(15) << right << setprecision(6) << "1234.5678" << endl; Output: (default) with << setw(15) << right: setprecision(3) << 1234.5678: 1.23e+003 setprecision(4) << 1234.5678: 1235 setprecision(6) << 1234.5678: 1234.57 cout << fixed setprecision(3) << 1234.5678: 1234.568 setprecision(4) << 1234.5678: 1234.5678 setprecision(6) << 1234.5678: 1234.567800 cout << scientific setprecision(3) << 1234.5678: 1.235e+003 setprecision(4) << 1234.5678: 1.2346e+003 setprecision(6) << 1234.5678: 1.234568e+003 setprecision(6) << "1234.5678": 1234.5678 Lecture 3 Page3

7. Consider the user interaction of the program below. Program #include <iomanip> const int SIZE = 11; char str1[size]; char str2[size]; cout << "Enter a string: "; cin >> setw(size) >> str1; cout << "Enter another string: "; cin.getline(str2,size); cout << "str1 is \"" << str1 << "\" and str2 is \"" << str2 << "\"" << endl; char ch; cout << endl << "Hit any key to continue..." << endl; cin.get(ch); a) Explain why the user was only able to input to enter the first string. Window s Visual Studio Output Enter a string: abcdefghijklmn Enter another string: str1 is "abcdefghij" and str2 is "klmn" Hit any key to continue... b) What would we need to do to fix the above program? 8. Complete the C++ program to calculate the hypotenuse of a right triangle using the Pythagorean theorem: c = a 2 + b 2, where c is the length of the hypotenuse, and a and b are the other sides of the triangle. #include <cmath> double a, b, c; cout << Enter the length of sides a and b: ; cin >> a >> b; cout << The length of the hypotenuse is << c << endl; Lecture 3 Page4

Operator :: () [] ->. ++ -- - +! ~ (type) * & sizeof * / % + - << >> < <= > >= ==!= & ^ &&?: = += -= *= /= %= &= ^= = <<= >>=, Operator Precedence and Associativity Associativity unary: binary: Usage(s) parenthesis index object pointer/structure pointer dot operator increment and decrement unary negation and plus logical negation one s complement operator type cast indirection address-of/reference multiply, division, remainder addition and subtraction io: insertion and extraction bit-wise shift left and right comparisons for inequality comparison for equality bit-wise AND bit-wise exclusive-or bit-wise OR logical AND logical OR conditional assignment comma operator Lecture 3 Page5