Companion C++ Examples

Size: px
Start display at page:

Download "Companion C++ Examples"

Transcription

1 68

2 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] // = 29 [37] // 25-4 = 21 [38] // 25 * 4 = 100 [39] // 25 / 4 = 6, note integer [41] // 25 % 4 = 1 [42] // 25 ˆ 4 = [43] // Square root of 25 = 5 Figure D.1: Typical Math and Functions in C++ cfl2001 J.E. Akin 69

3 // 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

4 [ 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

5 [ 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

6 [ 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

7 [ 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

8 [ 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

9 [ 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

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

Object Oriented Programming Using C++ Mathematics & Computing IET, Katunayake Assigning Values // Example 2.3(Mathematical operations in C++) float a; cout > a; cout

More information

A SHORT COURSE ON C++

A SHORT COURSE ON C++ Introduction to A SHORT COURSE ON School of Mathematics Semester 1 2008 Introduction to OUTLINE 1 INTRODUCTION TO 2 FLOW CONTROL AND FUNCTIONS If Else Looping Functions Cmath Library Prototyping Introduction

More information

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

cast.c /* Program illustrates the use of a cast to coerce a function argument to be of the correct form. */ cast.c /* Program illustrates the use of a cast to coerce a function argument to be of the correct form. */ #include #include /* The above include is present so that the return type

More information

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

Outline. Functions. Functions. Predefined Functions. Example. Example. Predefined functions User-defined functions Actual parameters Formal parameters Outline Functions Predefined functions User-defined functions Actual parameters Formal parameters Value parameters Variable parameters Functions 1 Functions 2 Functions Predefined Functions In C++ there

More information

From Pseudcode Algorithms directly to C++ programs

From Pseudcode Algorithms directly to C++ programs From Pseudcode Algorithms directly to C++ programs (Chapter 7) Part 1: Mapping Pseudo-code style to C++ style input, output, simple computation, lists, while loops, if statements a bit of grammar Part

More information

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

Class 2: Variables and Memory. Laura Marik Spring 2012 C++ Course Notes (Provided by Jason Minski) Class 2: Variables and Memory Variables A variable is a value that is stored in memory It can be numeric or a character C++ needs to be told what type it is before it can store it in memory It also needs

More information

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

3. Functions. Modular programming is the dividing of the entire problem into small sub problems that can be solved by writing separate programs. 1 3. Functions 1. What are the merits and demerits of modular programming? Modular programming is the dividing of the entire problem into small sub problems that can be solved by writing separate programs.

More information

COMPUTER SCIENCE (083)

COMPUTER SCIENCE (083) Roll No. Code : 112011-083-A Please check that this question paper contains 7 questions and 6 printed pages. CLASS-XI COMPUTER SCIENCE (083) Time Allowed : 3 Hrs. Maximum Marks : 70 General Instructions

More information

Functions. Systems Programming Concepts

Functions. Systems Programming Concepts Functions Systems Programming Concepts Functions Simple Function Example Function Prototype and Declaration Math Library Functions Function Definition Header Files Random Number Generator Call by Value

More information

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

CS2141 Software Development using C/C++ C++ Basics CS2141 Software Development using C/C++ C++ Basics Integers Basic Types Can be short, long, or just plain int C++ does not define the size of them other than short

More information

Programming Language. Functions. Eng. Anis Nazer First Semester

Programming Language. Functions. Eng. Anis Nazer First Semester Programming Language Functions Eng. Anis Nazer First Semester 2016-2017 Definitions Function : a set of statements that are written once, and can be executed upon request Functions are separate entities

More information

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

CHAPTER 2.2 CONTROL STRUCTURES (ITERATION) Dr. Shady Yehia Elmashad CHAPTER 2.2 CONTROL STRUCTURES (ITERATION) Dr. Shady Yehia Elmashad Outline 1. C++ Iterative Constructs 2. The for Repetition Structure 3. Examples Using the for Structure 4. The while Repetition Structure

More information

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.

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. Chapter-12 FUNCTIONS Introduction 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. Types of functions There are two types

More information

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

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures The main body and cout Agenda 1 Fundamental data types Declarations and definitions Control structures References, pass-by-value vs pass-by-references The main body and cout 2 C++ IS AN OO EXTENSION OF

More information

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

3/12/2018. Structures. Programming in C++ Sequential Branching Repeating. Loops (Repetition) Structures Programming in C++ Sequential Branching Repeating Loops (Repetition) 2 1 Loops Repetition is referred to the ability of repeating a statement or a set of statements as many times this is necessary.

More information

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

COP 3223 Introduction to Programming with C - Study Union - Fall 2017 COP 3223 Introduction to Programming with C - Study Union - Fall 2017 Chris Marsh and Matthew Villegas Contents 1 Code Tracing 2 2 Pass by Value Functions 4 3 Statically Allocated Arrays 5 3.1 One Dimensional.................................

More information

211: Computer Architecture Summer 2016

211: Computer Architecture Summer 2016 211: Computer Architecture Summer 2016 Liu Liu Topic: C Programming Data Representation I/O: - (example) cprintf.c Memory: - memory address - stack / heap / constant space - basic data layout Pointer:

More information

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

C++ Programming: From Problem Analysis to Program Design, Third Edition C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 2: Basic Elements of C++ Objectives (continued) Become familiar with the use of increment and decrement operators Examine

More information

CHAPTER 4 FUNCTIONS. Dr. Shady Yehia Elmashad

CHAPTER 4 FUNCTIONS. Dr. Shady Yehia Elmashad CHAPTER 4 FUNCTIONS Dr. Shady Yehia Elmashad Outline 1. Introduction 2. Program Components in C++ 3. Math Library Functions 4. Functions 5. Function Definitions 6. Function Prototypes 7. Header Files 8.

More information

Reviewing all Topics this term

Reviewing all Topics this term Today in CS161 Prepare for the Final Reviewing all Topics this term Variables If Statements Loops (do while, while, for) Functions (pass by value, pass by reference) Arrays (specifically arrays of characters)

More information

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

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur Lecture 04 Programs with IO and Loop We will now discuss the module 2,

More information

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

Functions in C++ Problem-Solving Procedure With Modular Design C ++ Function Definition: a single Functions in C++ Problem-Solving Procedure With Modular Design: Program development steps: Analyze the problem Develop a solution Code the solution Test/Debug the program C ++ Function Definition: A module

More information

Programming. C++ Basics

Programming. C++ Basics Programming C++ Basics Introduction to C++ C is a programming language developed in the 1970s with the UNIX operating system C programs are efficient and portable across different hardware platforms C++

More information

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

Basic program The following is a basic program in C++; Basic C++ Source Code Compiler Object Code Linker (with libraries) Executable Basic C++ Overview C++ is a version of the older C programming language. This is a language that is used for a wide variety of applications and which has a mature base of compilers and libraries. C++ is

More information

POINTERS - Pointer is a variable that holds a memory address of another variable of same type. - It supports dynamic allocation routines. - It can improve the efficiency of certain routines. C++ Memory

More information

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

THE INTEGER DATA TYPES. Laura Marik Spring 2012 C++ Course Notes (Provided by Jason Minski) THE INTEGER DATA TYPES STORAGE OF INTEGER TYPES IN MEMORY All data types are stored in binary in memory. The type that you give a value indicates to the machine what encoding to use to store the data in

More information

Input and Expressions Chapter 3 Slides #4

Input and Expressions Chapter 3 Slides #4 Input and Expressions Chapter 3 Slides #4 Topics 1) How can we read data from the keyboard? 2) How can we calculate values? 3) How can we manage the type of a value? 4) How can we round or get random numbers?

More information

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.

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. 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. B. Outputs to the console a floating point number f1 in scientific format

More information

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

Concepts Review. 2. A program is the implementation of an algorithm in a particular computer language, like C and C++. Concepts Review 1. An algorithm is a sequence of steps to solve a problem. 2. A program is the implementation of an algorithm in a particular computer language, like C and C++. 3. A flowchart is the graphical

More information

Pointers, Dynamic Data, and Reference Types

Pointers, Dynamic Data, and Reference Types Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation The new operator The delete operator Dynamic Memory Allocation for Arrays 1 C++ Data Types simple

More information

LOGO BASIC ELEMENTS OF A COMPUTER PROGRAM

LOGO BASIC ELEMENTS OF A COMPUTER PROGRAM LOGO BASIC ELEMENTS OF A COMPUTER PROGRAM Contents 1. Statements 2. C++ Program Structure 3. Programming process 4. Control Structure STATEMENTS ASSIGNMENT STATEMENTS Assignment statement Assigns a value

More information

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

Recap. ANSI C Reserved Words C++ Multimedia Programming Lecture 2. Erwin M. Bakker Joachim Rijsdam Multimedia Programming 2004 Lecture 2 Erwin M. Bakker Joachim Rijsdam Recap Learning C++ by example No groups: everybody should experience developing and programming in C++! Assignments will determine

More information

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

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements Review: Exam 1 9/20/06 CS150 Introduction to Computer Science 1 1 Your First C++ Program 1 //*********************************************************** 2 // File name: hello.cpp 3 // Author: Shereen Khoja

More information

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

Government Polytechnic, Muzaffarpur. Name of the Lab: OBJECT ORIENTED PROGRAMMING Government Polytechnic, Muzaffarpur. Name of the Lab: OBJECT ORIENTED PROGRAMMING THROUGH C++ Practical: OOPS THROUGH C++ Subject Code: 1618407 PROGRAM NO.1 Programming exercise on executing a Basic C++

More information

Object Oriented Pragramming (22316)

Object Oriented Pragramming (22316) Chapter 1 Principles of Object Oriented Programming (14 Marks) Q1. Give Characteristics of object oriented programming? Or Give features of object oriented programming? Ans: 1. Emphasis (focus) is on data

More information

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

GE U111 Engineering Problem Solving & Computation Lecture 6 February 2, 2004 GE U111 Engineering Problem Solving & Computation Lecture 6 February 2, 2004 Functions and Program Structure Today we will be learning about functions. You should already have an idea of their uses. Cout

More information

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

(6) The specification of a name with its type in a program. (7) Some memory that holds a value of a given type. CS 7A - Fall 2016 - Midterm 1 10/20/16 Write responses to questions 1 and 2 on this paper or attach additional sheets, as necessary For all subsequent problems, use separate paper Do not use a computer

More information

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

String Class in C++ When the above code is compiled and executed, it produces result something as follows: cin and strings String Class in C++ The standard C++ library provides a string class type that supports all the operations mentioned above, additionally much more functionality. We will study this class in C++ Standard

More information

C++ Programming Lecture 11 Functions Part I

C++ Programming Lecture 11 Functions Part I C++ Programming Lecture 11 Functions Part I By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department Introduction Till now we have learned the basic concepts of C++. All the programs

More information

Local and Global Variables

Local and Global Variables Lecture 10 Local and Global Variables Nearly every programming language has a concept of local variable. As long as two functions mind their own data, as it were, they won t interfere with each other.

More information

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

Main Program. C Programming Notes. #include <stdio.h> main() { printf( Hello ); } Comments: /* comment */ //comment. Dr. Karne Towson University C Programming Notes Dr. Karne Towson University Reference for C http://www.cplusplus.com/reference/ Main Program #include main() printf( Hello ); Comments: /* comment */ //comment 1 Data Types

More information

UNIT- 3 Introduction to C++

UNIT- 3 Introduction to C++ UNIT- 3 Introduction to C++ C++ Character Sets: Letters A-Z, a-z Digits 0-9 Special Symbols Space + - * / ^ \ ( ) [ ] =!= . $, ; : %! &? _ # = @ White Spaces Blank spaces, horizontal tab, carriage

More information

Lab Instructor : Jean Lai

Lab Instructor : Jean Lai Lab Instructor : Jean Lai Group related statements to perform a specific task. Structure the program (No duplicate codes!) Must be declared before used. Can be invoked (called) as any number of times.

More information

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

Name SECTION: 12:45 2:20. True or False (12 Points) Name SECION: 12:45 2:20 rue or False (12 Points) 1. (12 pts) Circle for true and F for false: F a) Local identifiers have name precedence over global identifiers of the same name. F b) Local variables

More information

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

Computers in Engineering. Moving From Fortran to C Michael A. Hawker Computers in Engineering COMP 208 Moving From Fortran to C Michael A. Hawker Remember our first Fortran program? PROGRAM hello IMPLICIT NONE!This is my first program WRITE (*,*) "Hello, World!" END PROGRAM

More information

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:

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: Welcome Back CSCI 262 Data Structures 2 - Review What you learned in CSCI 261 (or equivalent): Variables Types Arrays Expressions Conditionals Branches & Loops Functions Recursion Classes & Objects Streams

More information

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

o Counter and sentinel controlled loops o Formatting output o Type casting o Top-down, stepwise refinement Last Time Let s all Repeat Together 10/3/05 CS150 Introduction to Computer Science 1 1 We covered o Counter and sentinel controlled loops o Formatting output Today we will o Type casting o Top-down, stepwise

More information

CS 115 Exam 3, Spring 2010

CS 115 Exam 3, Spring 2010 Your name: Rules You must briefly explain your answers to receive partial credit. When a snippet of code is given to you, you can assume o that the code is enclosed within some function, even if no function

More information

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

Programming Fundamentals. With C++ Variable Declaration, Evaluation and Assignment 1 300580 Programming Fundamentals 3 With C++ Variable Declaration, Evaluation and Assignment 1 Today s Topics Variable declaration Assignment to variables Typecasting Counting Mathematical functions Keyboard

More information

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:

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: Welcome Back CSCI 262 Data Structures 2 - Review What you learned in CSCI 261 (or equivalent): Variables Types Arrays Expressions Conditionals Branches & Loops Functions Recursion Classes & Objects Streams

More information

Operations. Making Things Happen

Operations. Making Things Happen Operations Making Things Happen Object Review and Continue Lecture 1 2 Object Categories There are three kinds of objects: Literals: unnamed objects having a value (0, -3, 2.5, 2.998e8, 'A', "Hello\n",...)

More information

Chapter 7 - Notes User-Defined Functions II

Chapter 7 - Notes User-Defined Functions II Chapter 7 - Notes User-Defined Functions II I. VOID Functions ( The use of a void function is done as a stand alone statement.) A. Void Functions without Parameters 1. Syntax: void functionname ( void

More information

A First Program - Greeting.cpp

A First Program - Greeting.cpp C++ Basics A First Program - Greeting.cpp Preprocessor directives Function named main() indicates start of program // Program: Display greetings #include using namespace std; int main() { cout

More information

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

CAMBRIDGE SCHOOL, NOIDA ASSIGNMENT 1, TOPIC: C++ PROGRAMMING CLASS VIII, COMPUTER SCIENCE CAMBRIDGE SCHOOL, NOIDA ASSIGNMENT 1, TOPIC: C++ PROGRAMMING CLASS VIII, COMPUTER SCIENCE a) Mention any 4 characteristic of the object car. Ans name, colour, model number, engine state, power b) What

More information

Split up Syllabus (Session )

Split up Syllabus (Session ) Split up Syllabus (Session- -17) COMPUTER SCIENCE (083) CLASS XI Unit No. Unit Name Marks 1 COMPUTER FUNDAMENTALS 10 2 PROGRAMMING METHODOLOGY 12 3 INTRODUCTION TO C++ 14 4 PROGRAMMING IN C++ 34 Total

More information

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

Function Example. Function Definition. C Programming. Syntax. A small program(subroutine) that performs a particular task. Modular programming design What is a Function? C Programming Lecture 8-1 : Function (Basic) A small program(subroutine) that performs a particular task Input : parameter / argument Perform what? : function body Output t : return

More information

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

LESSON 5 FUNDAMENTAL DATA TYPES. char short int long unsigned char unsigned short unsigned unsigned long LESSON 5 ARITHMETIC DATA PROCESSING The arithmetic data types are the fundamental data types of the C language. They are called "arithmetic" because operations such as addition and multiplication can be

More information

Chapter 3 - Functions

Chapter 3 - Functions Chapter 3 - Functions 1 Outline 3.1 Introduction 3.2 Program Components in C++ 3.3 Math Library Functions 3.4 Functions 3.5 Function Definitions 3.6 Function Prototypes 3.7 Header Files 3.8 Random Number

More information

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

COP 3223 Introduction to Programming with C - Study Union - Fall 2017 COP 3223 Introduction to Programming with C - Study Union - Fall 2017 Chris Marsh and Matthew Villegas Contents 1 Code Tracing 2 2 Pass by Value Functions 4 3 Statically Allocated Arrays 5 3.1 One Dimensional.................................

More information

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)

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) Introduction to C++ 1. General C++ is an Object oriented extension of C which was derived from B (BCPL) Developed by Bjarne Stroustrup (AT&T Bell Labs) in early 1980 s 2. A Simple C++ Program A C++ program

More information

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

Week 3. Function Definitions. Example: Function. Function Call, Return Statement. Functions & Arrays. Gaddis: Chapters 6 and 7. CS 5301 Spring 2018 Week 3 Functions & Arrays Gaddis: Chapters 6 and 7 CS 5301 Spring 2018 Jill Seaman 1 Function Definitions l Function definition pattern: datatype identifier (parameter1, parameter2,...) { statements...

More information

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

Programming in C++ The manager of a company. Lecture Notes 6. Functions (Procedures) 4/24/2018. he he he. Does Does Does The manager of a company Programming in C++ Lecture Notes 6 Functions (Procedures) Does Does Does he he he pay the bills? answer the phone? clean the office? 2 1 Division of Labor Our programs until now

More information

Fundamentals of Programming CS-110. Lecture 2

Fundamentals of Programming CS-110. Lecture 2 Fundamentals of Programming CS-110 Lecture 2 Last Lab // Example program #include using namespace std; int main() { cout

More information

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

Week 3. Function Definitions. Example: Function. Function Call, Return Statement. Functions & Arrays. Gaddis: Chapters 6 and 7. Week 3 Functions & Arrays Gaddis: Chapters 6 and 7 CS 5301 Fall 2015 Jill Seaman 1 Function Definitions! Function definition pattern: datatype identifier (parameter1, parameter2,...) { statements... Where

More information

WARM UP LESSONS BARE BASICS

WARM UP LESSONS BARE BASICS WARM UP LESSONS BARE BASICS CONTENTS Common primitive data types for variables... 2 About standard input / output... 2 More on standard output in C standard... 3 Practice Exercise... 6 About Math Expressions

More information

Add Subtract Multiply Divide

Add Subtract Multiply Divide ARITHMETIC OPERATORS if AND if/else AND while LOOP Order of Operation (Precedence Part 1) Copyright 2014 Dan McElroy Add Subtract Multiply Divide + Add - Subtract * Multiply / Divide = gives the quotient

More information

LAB 5 Arithmetic Operations Simple Calculator

LAB 5 Arithmetic Operations Simple Calculator LAB 5 Arithmetic Operations Simple Calculator Objective: Practice arithmetic operation for the 80x86, such as add, subtract, multiple, divide, and mod. When dealing with the multiply, divide, and mod instructions

More information

CHAPTER 3 Expressions, Functions, Output

CHAPTER 3 Expressions, Functions, Output CHAPTER 3 Expressions, Functions, Output More Data Types: Integral Number Types short, long, int (all represent integer values with no fractional part). Computer Representation of integer numbers - Number

More information

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

AIR FORCE SCHOOL,BAMRAULI COMPUTER SCIENCE (083) CLASS XI Split up Syllabus (Session ) Contents AIR FORCE SCHOOL,BAMRAULI COMPUTER SCIENCE (083) CLASS XI Split up Syllabus (Session- 2017-18) Month July Contents UNIT 1: COMPUTER FUNDAMENTALS Evolution of computers; Basics of computer and its operation;

More information

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

Basic memory model Using functions Writing functions. Basics Prototypes Parameters Return types Functions and memory Names and namespaces Basic memory model Using functions Writing functions Basics Prototypes Parameters Return types Functions and memory Names and namespaces When a program runs it requires main memory (RAM) space for Program

More information

Consider the following statements. string str1 = "ABCDEFGHIJKLM"; string str2; After the statement str2 = str1.substr(1,4); executes, the value of str2 is " ". Given the function prototype: float test(int,

More information

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

Pointers Pointer Variables. Pointer Variables Getting the Address of a Variable. Each variable in program is stored at a 3.1. Getting the Address of a Variable Pointers (read Chapter 9. Starting Out with C++: From Control Structures through Objects, Tony Gaddis.) Le Thanh Huong School of Information and Communication Technology

More information

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

UEE1302(1066) F12: Introduction to Computers and Programming Function (II) - Parameter UEE1302(1066) F12: Introduction to Computers and Programming Function (II) - Parameter What you will learn from Lab 7 In this laboratory, you will understand how to use typical function prototype with

More information

BITG 1233: Introduction to C++

BITG 1233: Introduction to C++ BITG 1233: Introduction to C++ 1 Learning Outcomes At the end of this lecture, you should be able to: Identify basic structure of C++ program (pg 3) Describe the concepts of : Character set. (pg 11) Token

More information

CS 376b Computer Vision

CS 376b Computer Vision CS 376b Computer Vision 09 / 25 / 2014 Instructor: Michael Eckmann Today s Topics Questions? / Comments? Enhancing images / masks Cross correlation Convolution C++ Cross-correlation Cross-correlation involves

More information

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

5.1. Chapter 5: The Increment and Decrement Operators. The Increment and Decrement Operators. The Increment and Decrement Operators Chapter 5: 5.1 Looping The Increment and Decrement Operators The Increment and Decrement Operators The Increment and Decrement Operators ++ is the increment operator. It adds one to a variable. val++;

More information

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

Lecture 3. Review. CS 141 Lecture 3 By Ziad Kobti -Control Structures Examples -Built-in functions. Conditions: Loops: if( ) / else switch Lecture 3 CS 141 Lecture 3 By Ziad Kobti -Control Structures Examples -Built-in functions Review Conditions: if( ) / else switch Loops: for( ) do...while( ) while( )... 1 Examples Display the first 10

More information

Important Java terminology

Important Java terminology 1 Important Java terminology The information we manage in a Java program is either represented as primitive data or as objects. Primitive data פרימיטיביים) (נתונים include common, fundamental values as

More information

Using Free Functions

Using Free Functions Chapter 3 Using Free Functions 3rd Edition Computing Fundamentals with C++ Rick Mercer Franklin, Beedle & Associates Goals Evaluate some mathematical and trigonometric functions Use arguments in function

More information

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

Outline. Why do we write functions? Introduction to Functions. How do we write functions? Using Functions. Introduction to Functions March 21, 2006 Introduction to User-defined Functions Larry Caretto Computer Science 106 Computing in Engineering and Science March 21, 2006 Outline Why we use functions Writing and calling a function Header and body

More information

CSCE 110 PROGRAMMING FUNDAMENTALS

CSCE 110 PROGRAMMING FUNDAMENTALS CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 2. Overview of C++ Prof. Amr Goneid, AUC 1 Overview of C++ Prof. Amr Goneid, AUC 2 Overview of C++ Historical C++ Basics Some Library

More information

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

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

More information

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

CSc 372. Comparative Programming Languages. 4 : Haskell Basics. Department of Computer Science University of Arizona 1/40 CSc 372 Comparative Programming Languages 4 : Haskell Basics Department of Computer Science University of Arizona collberg@gmail.com Copyright c 2013 Christian Collberg 2/40 The Hugs Interpreter The

More information

Scientific Computing

Scientific Computing Scientific Computing Martin Lotz School of Mathematics The University of Manchester Lecture 1, September 22, 2014 Outline Course Overview Programming Basics The C++ Programming Language Outline Course

More information

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

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible) Name Closed notes, book and neighbor. If you have any questions ask them. Notes: Segment of code necessary C++ statements to perform the action described not a complete program Program a complete C++ program

More information

Exam I Review Questions Fall 2010

Exam I Review Questions Fall 2010 Exam I Review Questions Fall 2010 The following review questions are similar to the kinds of questions you will be expected to answer on Exam I (scheduled for Oct. 14), which will cover LCR, chs. 1 7,

More information

CSc 372 Comparative Programming Languages. 4 : Haskell Basics

CSc 372 Comparative Programming Languages. 4 : Haskell Basics CSc 372 Comparative Programming Languages 4 : Haskell Basics Christian Collberg Department of Computer Science University of Arizona collberg@gmail.com Copyright c 2011 Christian Collberg August 23, 2011

More information

CSCE 206: Structured Programming in C++

CSCE 206: Structured Programming in C++ CSCE 206: Structured Programming in C++ 2017 Spring Exam 2 Monday, March 20, 2017 Total - 100 Points B Instructions: Total of 13 pages, including this cover and the last page. Before starting the exam,

More information

C Functions. 5.2 Program Modules in C

C Functions. 5.2 Program Modules in C 1 5 C Functions 5.2 Program Modules in C 2 Functions Modules in C Programs combine user-defined functions with library functions - C standard library has a wide variety of functions Function calls Invoking

More information

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

2.1-First.cpp #include <iostream> // contains information to support input / output using namespace std; Lafore: Object-Oriented Programming in C++ (4 th ) 2.1-First.cpp // contains information to support input / output // entry point for every C and C++ program (console) cout

More information

ASSIGNMENT CLASS-11 COMPUTER SCIENCE [C++]

ASSIGNMENT CLASS-11 COMPUTER SCIENCE [C++] ASSIGNMENT-1 2016-17 CLASS-11 COMPUTER SCIENCE [C++] 1 Consider the following C++ snippet: int x = 25000; int y = 2*x; cout

More information

CHAPTER 4 FUNCTIONS. Dr. Shady Yehia Elmashad

CHAPTER 4 FUNCTIONS. Dr. Shady Yehia Elmashad CHAPTER 4 FUNCTIONS Dr. Shady Yehia Elmashad Outline 1. Introduction 2. Program Components in C++ 3. Math Library Functions 4. Functions 5. Function Definitions 6. Function Prototypes 7. Header Files 8.

More information

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

Name. CPTR246 Spring '17 (100 total points) Exam 2 Name CPTR246 Spring '17 (100 total points) Exam 2 1. Pointer parameters (the old C way) In the following program, make all of the changes to convert the call-by-reference parameters in the function computeoptions

More information

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

cs3157: another C lecture (mon-21-feb-2005) C pre-processor (3). cs3157: another C lecture (mon-21-feb-2005) C pre-processor (1). today: C pre-processor command-line arguments more on data types and operators: booleans in C logical and bitwise operators type conversion

More information

Unit 6. Thinking with Functions

Unit 6. Thinking with Functions 1 Unit 6 Thinking with Functions Function Decomposition 2 Functions Overview Functions (aka procedures, subroutines, or methods) are the unit of code decomposition and abstraction Map Service ValidateInputs()

More information

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

Computers in Engineering. Moving From Fortran to C Part 2 Michael A. Hawker Computers in Engineering COMP 208 Moving From Fortran to C Part 2 Michael A. Hawker Roots of a Quadratic in C #include #include void main() { float a, b, c; float d; float root1, root2;

More information

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

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible) Name Closed notes, book and neighbor. If you have any questions ask them. Notes: Segment of code necessary C++ statements to perform the action described not a complete program Program a complete C++ program

More information

Introduction to C++ Introduction to C++ 1

Introduction to C++ Introduction to C++ 1 1 What Is C++? (Mostly) an extension of C to include: Classes Templates Inheritance and Multiple Inheritance Function and Operator Overloading New (and better) Standard Library References and Reference

More information

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

CHAPTER 2.1 CONTROL STRUCTURES (SELECTION) Dr. Shady Yehia Elmashad CHAPTER 2.1 CONTROL STRUCTURES (SELECTION) Dr. Shady Yehia Elmashad Outline 1. The if Selection Structure 2. The if/else Selection Structure 3. The switch Multiple-Selection Structure 1. The if Selection

More information

Program Organization and Comments

Program Organization and Comments C / C++ PROGRAMMING Program Organization and Comments Copyright 2013 Dan McElroy Programming Organization The layout of a program should be fairly straight forward and simple. Although it may just look

More information