Companion C++ Examples

Similar documents
Object Oriented Programming Using C++ Mathematics & Computing IET, Katunayake

A SHORT COURSE ON C++

cast.c /* Program illustrates the use of a cast to coerce a function argument to be of the correct form. */

Outline. Functions. Functions. Predefined Functions. Example. Example. Predefined functions User-defined functions Actual parameters Formal parameters

From Pseudcode Algorithms directly to C++ programs

Class 2: Variables and Memory. Laura Marik Spring 2012 C++ Course Notes (Provided by Jason Minski)

3. Functions. Modular programming is the dividing of the entire problem into small sub problems that can be solved by writing separate programs.

COMPUTER SCIENCE (083)

Functions. Systems Programming Concepts

CS2141 Software Development using C/C++ C++ Basics

Programming Language. Functions. Eng. Anis Nazer First Semester

CHAPTER 2.2 CONTROL STRUCTURES (ITERATION) Dr. Shady Yehia Elmashad

A function is a named group of statements developed to solve a sub-problem and returns a value to other functions when it is called.

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

3/12/2018. Structures. Programming in C++ Sequential Branching Repeating. Loops (Repetition)

COP 3223 Introduction to Programming with C - Study Union - Fall 2017

211: Computer Architecture Summer 2016

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

CHAPTER 4 FUNCTIONS. Dr. Shady Yehia Elmashad

Reviewing all Topics this term

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

Functions in C++ Problem-Solving Procedure With Modular Design C ++ Function Definition: a single

Programming. C++ Basics

Basic program The following is a basic program in C++; Basic C++ Source Code Compiler Object Code Linker (with libraries) Executable


THE INTEGER DATA TYPES. Laura Marik Spring 2012 C++ Course Notes (Provided by Jason Minski)

Input and Expressions Chapter 3 Slides #4

1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol.

Concepts Review. 2. A program is the implementation of an algorithm in a particular computer language, like C and C++.

Pointers, Dynamic Data, and Reference Types

LOGO BASIC ELEMENTS OF A COMPUTER PROGRAM

Recap. ANSI C Reserved Words C++ Multimedia Programming Lecture 2. Erwin M. Bakker Joachim Rijsdam

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

Government Polytechnic, Muzaffarpur. Name of the Lab: OBJECT ORIENTED PROGRAMMING

Object Oriented Pragramming (22316)

GE U111 Engineering Problem Solving & Computation Lecture 6 February 2, 2004

(6) The specification of a name with its type in a program. (7) Some memory that holds a value of a given type.

String Class in C++ When the above code is compiled and executed, it produces result something as follows: cin and strings

C++ Programming Lecture 11 Functions Part I

Local and Global Variables

Main Program. C Programming Notes. #include <stdio.h> main() { printf( Hello ); } Comments: /* comment */ //comment. Dr. Karne Towson University

UNIT- 3 Introduction to C++

Lab Instructor : Jean Lai

Name SECTION: 12:45 2:20. True or False (12 Points)

Computers in Engineering. Moving From Fortran to C Michael A. Hawker

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 8/19/ Review. Here s a simple C++ program:

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

CS 115 Exam 3, Spring 2010

Programming Fundamentals. With C++ Variable Declaration, Evaluation and Assignment 1

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 1/9/ Review. Here s a simple C++ program:

Operations. Making Things Happen

Chapter 7 - Notes User-Defined Functions II

A First Program - Greeting.cpp

CAMBRIDGE SCHOOL, NOIDA ASSIGNMENT 1, TOPIC: C++ PROGRAMMING CLASS VIII, COMPUTER SCIENCE

Split up Syllabus (Session )

Function Example. Function Definition. C Programming. Syntax. A small program(subroutine) that performs a particular task. Modular programming design

LESSON 5 FUNDAMENTAL DATA TYPES. char short int long unsigned char unsigned short unsigned unsigned long

Chapter 3 - Functions

COP 3223 Introduction to Programming with C - Study Union - Fall 2017

Introduction to C++ 2. A Simple C++ Program. A C++ program consists of: a set of data & function definitions, and the main function (or driver)

Week 3. Function Definitions. Example: Function. Function Call, Return Statement. Functions & Arrays. Gaddis: Chapters 6 and 7. CS 5301 Spring 2018

Programming in C++ The manager of a company. Lecture Notes 6. Functions (Procedures) 4/24/2018. he he he. Does Does Does

Fundamentals of Programming CS-110. Lecture 2

Week 3. Function Definitions. Example: Function. Function Call, Return Statement. Functions & Arrays. Gaddis: Chapters 6 and 7.

WARM UP LESSONS BARE BASICS

Add Subtract Multiply Divide

LAB 5 Arithmetic Operations Simple Calculator

CHAPTER 3 Expressions, Functions, Output

AIR FORCE SCHOOL,BAMRAULI COMPUTER SCIENCE (083) CLASS XI Split up Syllabus (Session ) Contents

Basic memory model Using functions Writing functions. Basics Prototypes Parameters Return types Functions and memory Names and namespaces


Pointers Pointer Variables. Pointer Variables Getting the Address of a Variable. Each variable in program is stored at a

UEE1302(1066) F12: Introduction to Computers and Programming Function (II) - Parameter

BITG 1233: Introduction to C++

CS 376b Computer Vision

5.1. Chapter 5: The Increment and Decrement Operators. The Increment and Decrement Operators. The Increment and Decrement Operators

Lecture 3. Review. CS 141 Lecture 3 By Ziad Kobti -Control Structures Examples -Built-in functions. Conditions: Loops: if( ) / else switch

Important Java terminology

Using Free Functions

Outline. Why do we write functions? Introduction to Functions. How do we write functions? Using Functions. Introduction to Functions March 21, 2006

CSCE 110 PROGRAMMING FUNDAMENTALS

Key Concept: all programs can be broken down to a combination of one of the six instructions Assignment Statements can create variables to represent

CSc 372. Comparative Programming Languages. 4 : Haskell Basics. Department of Computer Science University of Arizona

Scientific Computing

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible)

Exam I Review Questions Fall 2010

CSc 372 Comparative Programming Languages. 4 : Haskell Basics

CSCE 206: Structured Programming in C++

C Functions. 5.2 Program Modules in C

2.1-First.cpp #include <iostream> // contains information to support input / output using namespace std;

ASSIGNMENT CLASS-11 COMPUTER SCIENCE [C++]

CHAPTER 4 FUNCTIONS. Dr. Shady Yehia Elmashad

Name. CPTR246 Spring '17 (100 total points) Exam 2

cs3157: another C lecture (mon-21-feb-2005) C pre-processor (3).

Unit 6. Thinking with Functions

Computers in Engineering. Moving From Fortran to C Part 2 Michael A. Hawker

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

Introduction to C++ Introduction to C++ 1

CHAPTER 2.1 CONTROL STRUCTURES (SELECTION) Dr. Shady Yehia Elmashad

Program Organization and Comments

Transcription:

68

Appendix D Companion C++ Examples D.1 Introduction It is necessary to be multilingual in computer languages today. Since C++ is often used in the OOP literature it should be useful to have C++ versions of the same code given earlier in F90. In most cases these examples have the same variable names and the line numbers are usually very close to each other. This appendix will allow you to flip from F90 examples in Chapter 4 of the main body of the text to see similar operations in C++. [ 1] #include <iostream.h> // system i/o files [ 2] #include <math.h> // system math files [ 3] main () [ 4] // Examples of simple arithmetic in C++ [ 5] f int Integer Var 1, Integer Var 2; // user inputs [ 7] int Mult Result, Div Result, Add Result [ 8] int Sub Result, Mod Result; double Pow Result, Sqrt Result; [10] cout << "Enter two integers: "; [11] cin >> Integer Var 1, Integer Var 2; [13] Add Result = Integer Var 1 + Integer Var 2; [14] cout << Integer Var 1 << " + " << Integer Var 2 << " = " [15] << Add Result << endl; [16] Sub Result = Integer Var 1 - Integer Var 2 ; [17] cout << Integer Var 1 << " - " << Integer Var 2 << " = " [18] << Sub Result << endl; [19] Mult Result = Integer Var 1 * Integer Var 2 ; cout << Integer Var 1 << " * " << Integer Var 2 << " = " [21] << Mult Result << endl; [22] Div Result = Integer Var 1 / Integer Var 2 ; [23] cout << Integer Var 1 << " / " << Integer Var 2 << " = " [24] << Div Result << endl; [25] Mod Result = Integer Var 1 % Integer Var 2; // remainder [26] cout << Integer Var 1 << " % " << Integer Var 2 << " = " [27] << Mod Result << endl; [28] Pow Result = pow ((double)integer Var 1, (double)integer Var 2); [29] cout << Integer Var 1 << " ˆ " << Integer Var 2 << " = " [30] << Pow Result << endl; [31] Sqrt Result = sqrt( (double)integer Var 1 ); [32] cout << "Square root of " << Integer Var 1 << " is " [33] << Sqrt Result << endl; [34] g // end main, Running produces: [35] // Enter two integers: 25 4 [36] // 25 + 4 = 29 [37] // 25-4 = 21 [38] // 25 * 4 = 100 [39] // 25 / 4 = 6, note integer [41] // 25 % 4 = 1 [42] // 25 ˆ 4 = 390625 [43] // Square root of 25 = 5 Figure D.1: Typical Math and Functions in C++ cfl2001 J.E. Akin 69

// system i/o files [ 3] // Examples of a simple loop in C++ [ 5] int Integer Var; [ 7] for (Integer Var = 0; Integer Var < 5; Integer Var ++) [ 8] f cout << "The loop variable is: " << Integer Var << endl; [10] g // end for [11] cout << "The final loop variable is: " << Integer Var << endl; [13] [14] g // end main // Running produces: [15] // The loop variable is: 0 [16] // The loop variable is: 1 [17] // The loop variable is: 2 [18] // The loop variable is: 3 [19] // The loop variable is: 4 // The final loop variable is: 5 <- NOTE Figure D.2: Typical Looping Concepts in C++ [ 1] #include <iostream.h> // system i/o files [ 3] // Examples of simple array indexing in C++ [ 5] int MAX = 5, loopcount; int Integer Array[5] ; [ 7] // or, int Integer Array[5] = f10, 20, 30, 40, 50 g; [ 8] Integer Array[0] = 10 ; // C arrays start at zero [10] Integer Array[1] = 20 ; Integer Array[2] = 30 ; [11] Integer Array[3] = 40 ; Integer Array[4] = 50 ; [13] for ( loopcount = 0; loopcount < MAX; loopcount ++) [14] cout << "The loop counter is: " << loopcount [15] << " with an array value of: " << Integer [16] // end for loop [17] cout << "The final loop counter is: " << loopcount << endl ; [18] [19] g // end main [21] // Running produces: [22] // The loop counter is: 0 with an array value of: 10 [23] // The loop counter is: 1 with an array value of: 20 [24] // The loop counter is: 2 with an array value of: 30 [25] // The loop counter is: 3 with an array value of: 40 [26] // The loop counter is: 4 with an array value of: 50 [27] // The final loop counter is: 5 Figure D.3: Simple Array Indexing in C++ [ 1] #include <iostream.h> // system i/o files [ 3] // Examples of relational "if" operator, via C++ [ 5] int Integer Var 1, Integer Var 2; // user inputs [ 7] cout << "\nenter two integers: "; [ 8] cin >> Integer Var 1, Integer Var 2; [10] if ( Integer Var 1 > Integer Var 2 ) [11] cout << Integer Var 1 << " is greater than " << Integer Var 2; [13] if ( Integer Var 1 < Integer Var 2 ) [14] cout << Integer Var 1 << " is less than " << Integer Var 2; [15] [16] if ( Integer Var 1 == Integer Var 2 ) [17] cout << Integer Var 1 << " is equal to " << Integer Var 2; [18] [19] g // end main [21] // Running with 25 and 4 produces: 25 4 [22] // Enter two integers: [23] // 25 is greater than 4 Figure D.4: Typical Relational Operators in C++ cfl2001 J.E. Akin 70

[ 3] // Illustrate a simple if-else logic in C++ [ 5] int Integer Var; [ 7] cout << "Enter an integer: "; [ 8] cin >> Integer Var; [10] if ( Integer Var > 5 && Integer Var < 10 ) [11] f cout << Integer Var << " is greater than 5 and less than 10" [13] << endl; g [14] else [15] f [16] cout << Integer Var << " is not greater than 5 and less than 10" [17] << endl; g // end of range of input [18] [19] g // end program main [21] // Running with 3 gives: 3 is not greater than 5 and less than 10 [22] // Running with 8 gives: 8 is greater than 5 and less than 10 Figure D.5: Typical If-Else Uses in C++ cfl2001 J.E. Akin 71

[ 3] // Examples of Logical operators in C++ [ 5] int Logic Var 1, Logic Var 2; [ 7] cout << "Enter logical value of A (1 or 0): "; [ 8] cin >> Logic Var 1; [10] cout << "Enter logical value of B (1 or 0): "; [11] cin >> Logic Var 2; [13] cout << "NOT A is " <<!Logic Var 1 << endl; [14] [15] if ( Logic Var 1 && Logic Var 2 ) [16] f [17] cout << "A ANDed with B is true " << endl; [18] g [19] else f [21] cout << "A ANDed with B is false " << endl; [22] g // end if for AND [23] [24] if ( Logic Var 1 Logic Var 2 ) [25] f [26] cout << "A ORed with B is true " << endl; [27] g [28] else [29] f [30] cout << "A ORed with B is false " << endl; [31] g // end if for OR [32] [33] if ( Logic Var 1 == Logic Var 2 ) [34] f [35] cout << "A EQiValent with B is true " << endl; [36] g [37] else [38] f [39] cout << "A EQiValent with B is false " << endl; [40] g // end if for EQV [41] [42] if ( Logic Var 1!= Logic Var 2 ) [43] f [44] cout << "A Not EQiValent with B is true " << endl; [45] g [46] else [47] f [48] cout << "A Not EQiValent with B is false " << endl; [49] g // end if for NEQV [50] [51] g // end main [52] // Running with 1 and 0 produces: [53] // Enter logical value of A (1 or 0): 1 [54] // Enter logical value of B (1 or 0): 0 [55] // NOT A is 0 [56] // A ANDed with B is false [57] // A ORed with B is true [58] // A EQiValent with B is false [59] // A Not EQiValent with B is true Figure D.6: Typical Logical Operators in C++ cfl2001 J.E. Akin 72

[ 1] // Program to find the maximum of a set of integers [ 2] #include <iostream.h> [ 3] #include <stdlib.h> // for exit [ 4] #define ARRAYLENGTH 100 [ 5] long integers[arraylength]; [ 7] // Function interface prototype [ 8] long maxint(long [], long); [10] // Main routine [11] main() f // Read in the number of integers [13] long i, n; [14] [15] cout << "Find maximum; type n: "; cin >> n; [16] if ( n > ARRAYLENGTH n < 0 ) f [17] cout << "Value you typed is too large or negative." << endl; [18] exit(1); [19] g // end if [21] for (i = 0; i < n; i++) f // Read in the user s integers [22] cout << "Integer " << (i+1) << ": "; cin >> integers[i]; cout [23] << endl; g // end for [24] cout << "Maximum: ", cout << maxint(integers, n); cout << endl; [25] g // end main [26] [27] // Find the maximum of an array of integers [28] long maxint(long input[], long input length) f [29] long i, max; [30] [31] for (max = input[0], i = 1; i < input length; i++) f [32] if ( input[i] > max ) f [33] max = input[i]; g // end if [34] g // end for [35] return(max); [36] g // end maxint // produces this result [37] // Find maximum; type n: 4 [38] // Integer 1: 9 [39] // Integer 2: 6 [40] // Integer 3: 4 [41] // Integer 4: -99 [42] // Maximum: 9 Figure D.7: Search for Largest Value in C++ cfl2001 J.E. Akin 73

[ 2] [ 3] // declare the interface prototypes [ 4] void Change ( int& Input Val); [ 5] void No Change ( int Input Val); [ 7] main () [ 8] // illustrate passing by reference and by value in C++ f [10] int Input Val; [11] cout << "Enter an integer: "; [13] cin >> Input Val; [14] cout << "Input value was " << Input Val << endl; [15] [16] // pass by value [17] No Change ( Input Val ); // Use but do not change [18] cout << "After No Change it is " << Input Val << endl; [19] // pass by reference [21] Change ( Input Val ); // Use and change [22] cout << "After Change it is " << Input Val << endl; [23] g [24] [25] void Change (int& Value) [26] f [27] // changes Value in calling code IF passed by reference [28] Value = 100; [29] cout << "Inside Change it is set to " << Value << endl; [30] g [31] [32] void No Change (int Value) [33] f [34] // does not change Value in calling code IF passed by value [35] Value = 100; [36] cout << "Inside No Change it is set to " << Value << endl; [37] g [38] // Running gives: [39] // Enter an integer: 12 [40] // Input value was 12 [41] // Inside No Change it is set to 100 [42] // After No Change it is 12 [43] // Inside Change it is set to 100 [44] // After Change it is 100 Figure D.8: Passing Arguments by Reference and by Value in C++ cfl2001 J.E. Akin 74

[ 3] // Compare two character strings in C++ [ 4] // Concatenate two character strings together [ 5] f char String1[40]; [ 7] char String2; [ 8] int length; [10] cout << "Enter first string (20 char max):"; [11] cin >> String1; [13] cout << "Enter second string (20 char max):"; [14] cin >> String2; [15] [16] // Compare [17] if (!strcmp(string1, String2) ) f [18] cout << "They are the same." << endl; [19] g else f [21] cout << "They are different." << endl; [22] g // end if the same [23] [24] // Concatenate [25] strcat(string1, String2) ; // add onto String1 [26] [27] cout << "The combined string is: " << String1 << endl; [28] length = strlen( String1 ); [29] cout << "The combined length is: " << length << endl; [30] length = strlen( String1 ); [31] [32] g // end main [33] // Running with "red" and "bird" produces: [34] // Enter first string (20 char max): red [35] // Enter second string (20 char max): bird [36] // They are different. [37] // The combined string is: redbird [38] // The combined length is: 7 [39] // But, "the red" and "bird" gives unexpected results Figure D.9: Using Two Strings in C++ [ 1] #include <iostream.h> [ 2] #include <stdlib.h> [ 3] #include <math.h> // system math files [ 4] [ 5] main() // Convert a character string to an integer in C++ [ 7] f [ 8] char Age Char[5]; int age; [10] [11] cout << "Enter your age: "; cin >> Age Char; [13] [14] // convert with intrinsic function [15] age = atoi(age Char); [16] [17] cout << "Your integer age is " << age << endl; [18] cout << "Your hexadecimal age is " << hex << age << endl; [19] cout << "Your octal age is " << oct << age << endl; [21] g // end of main [22] [23] // Running gives: [24] // Enter your age: 45 [25] // Your integer age is 45. [26] // Your hexadecimal age is 2d. [27] // Your octal age is 55. Figure D.10: Converting a String to an Integer with C++ cfl2001 J.E. Akin 75

[ 2] [ 3] // Define structures and components in C++ [ 4] [ 5] struct Person // define a person structure type f [ 7] char Name; [ 8] int Age; g; [10] [11] struct Who Where // use person type in a new structure f [13] struct Person Guest; [14] char Address[40]; [15] g; [16] [17] // Fill a record of the Who Where type components [18] main () [19] f struct Who Where Record; [21] [22] cout << "Enter your name: "; [23] cin >> Record.Guest.Name; [24] [25] cout << "Enter your city: "; [26] cin >> Record.Address; [27] [28] cout << "Enter your age: "; [29] cin >> Record.Guest.Age; [30] [31] cout << "Hello " << Record.Guest.Age << " year old " [32] << Record.Guest.Name << " in " << Record.Address << endl; [33] g [34] // Running with input: Sammy, Houston, 104 gives [35] // Hello 104 year old Sammy in Houston [36] // [37] // But try: Sammy Owl, Houston, 104 for a bug Figure D.11: Using Multiple Structures in C++ cfl2001 J.E. Akin 76