EE 152 Advanced Programming LAB 7

Similar documents
Classes: A Deeper Look, Part 1

Defining Class Functions.

Constructors.

UEE1303(1070) S12: Object-Oriented Programming Constant Pointer and Class

Circle all of the following which would make sense as the function prototype.

Abstract Data Types. Development and Implementation. Well-defined representations that allow objects to be created and used in an intuitive manner

CS 215 Fundamentals of Programming II Spring 2011 Project 2

Rational numbers as decimals and as integer fractions

Rational number operations can often be simplified by converting mixed numbers to improper fractions Add EXAMPLE:

(3) Some memory that holds a value of a given type. (8) The basic unit of addressing in most computers.

IS 0020 Program Design and Software Tools

17. Classes. Encapsulation, Classes, Member Functions, Constructors

PIC 10A 1C Week 5b Problems. TA: Eric. [Solutions]

UEE1303(1070) S12: Object-Oriented Programming Constructors and Destructors

Introduction to Programming

Review: C++ Basic Concepts. Dr. Yingwu Zhu

Section 2.3 Rational Numbers. A rational number is a number that may be written in the form a b. for any integer a and any nonzero integer b.

Abstract Data Types (ADTs) 1. Legal Values. Client Code for Rational ADT. ADT Design. CS 247: Software Engineering Principles

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

CS 247: Software Engineering Principles. ADT Design

University of Dublin

C++ Basics. Data Processing Course, I. Hrivnacova, IPN Orsay

Programming in C++: Assignment Week 8

Exception Handling Pearson Education, Inc. All rights reserved.

Recitation #2 Abstract Data Types, Collection Classes, and Linked List

An inline function is one in which the function code replaces the function call directly. Inline class member functions

CSC 330 Object-Oriented Programming. Exception Handling CSC 330

Coding Standards for Java

UEE1303(1070) S12: Object-Oriented Programming Advanced Topics of Class

this Pointer, Constant Functions, Static Data Members, and Static Member Functions this Pointer (11.1) Example of this pointer

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

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

CSI33 Data Structures

A.4 Rationalizing the Denominator

A. Incorrect! To simplify this expression you need to find the product of 7 and 4, not the sum.

17. Classes. Encapsulation: public / private. Member Functions: Declaration. Member Functions: Call. class rational { int n; int d; // INV: d!

CISC 2200 Data Structure Fall, C++ Review:3/3. 1 From last lecture:

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

17. Classes. Overloading Functions. Operator Overloading. Function Overloading. operatorop

Intermediate Programming, Spring 2017*

why C++? easier to talk about data representation closer to the hardware heavily related to Java

EINDHOVEN UNIVERSITY OF TECHNOLOGY Department of Mathematics and Computer Science

CSCE 110 PROGRAMMING FUNDAMENTALS

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

19. Classes. Encapsulation: public / private. Member Functions: Declaration. Member Functions: Call. class rational { int n; int d; // INV: d!

TEMPLATE IN C++ Function Templates

Outline. Introduction. Arrays declarations and initialization. Const variables. Character arrays. Static arrays. Examples.

A <Basic> C++ Course

A <Basic> C++ Course

UEE1303(1070) S12: Object-Oriented Programming Operator Overloading and Function Overloading

Welcome to MCS 360. content expectations. using g++ input and output streams the namespace std. Euclid s algorithm the while and do-while statements

Arithmetic Operators. Binary Arithmetic Operators. Arithmetic Operators. A Closer Look at the / Operator. A Closer Look at the % Operator

Fundamentals of Programming CS-110. Lecture 2

Computer Science II Lecture 1 Introduction and Background

Add Subtract Multiply Divide

A First Program - Greeting.cpp

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

REVIEW. The C++ Programming Language. CS 151 Review #2

Constants, References

Due Date: See Blackboard

Friend Functions and Friend Classes

Section A Arithmetic ( 5) Exercise A

Distributed Real-Time Control Systems. Lecture 17 C++ Programming Intro to C++ Objects and Classes

Creating a new data type

Total 100. The American University in Cairo Computer Science & Engineering Department CSCE 106. Instructor: Final Exam Fall Section No.

PIC 10A 1C Week 5b Problems. TA: Eric. [Solutions]

Arrays. Week 4. Assylbek Jumagaliyev

Implementing an ADT with a Class

Elements of C in C++ data types if else statement for loops. random numbers rolling a die

CHAPTER 4 FUNCTIONS. Dr. Shady Yehia Elmashad

50 MATHCOUNTS LECTURES (6) OPERATIONS WITH DECIMALS

Reference Parameters A reference parameter is an alias for its corresponding argument in the function call. Use the ampersand (&) to indicate that

Scientific Computing

Lab 1. largest = num1; // assume first number is largest

6.3 ADDING and SUBTRACTING Rational Expressions REVIEW. When you ADD rational numbers (fractions): 1) Write each number with common denominator

Non-numeric types, boolean types, arithmetic. operators. Comp Sci 1570 Introduction to C++ Non-numeric types. const. Reserved words.

1 #include <iostream > 3 using std::cout; 4 using std::cin; 5 using std::endl; 7 int main(){ 8 int x=21; 9 int y=22; 10 int z=5; 12 cout << (x/y%z+4);

CSS 342 Data Structures, Algorithms, and Discrete Mathematics I. Lecture 2. Yusuf Pisan

Carleton University Department of Systems and Computer Engineering SYSC Foundations of Imperative Programming - Winter Lab 8 - Structures

CIT 590 Homework 6 Fractions

Course Learning Outcomes for Unit I. Reading Assignment. Unit Lesson. UNIT I STUDY GUIDE Number Theory and the Real Number System

Kingdom of Saudi Arabia Princes Nora bint Abdul Rahman University College of Computer Since and Information System CS242 ARRAYS

Objectivities. Experiment 1. Lab6 Array I. Description of the Problem. Problem-Solving Tips

Exercise 1.1 Hello world

Compilation and Execution Simplifying Fractions. Loops If Statements. Variables Operations Using Functions Errors

Makefiles Makefiles should begin with a comment section of the following form and with the following information filled in:

2. Distinguish between a unary, a binary and a ternary operator. Give examples of C++ operators for each one of them.

Floating-point numbers. Phys 420/580 Lecture 6

Computer Programming with C++ (21)

Chapter 12 - Templates

CSCE 110: Programming I

Introduction to Programming EC-105. Lecture 2

Outline. Introduction. Pointer variables. Pointer operators. Calling functions by reference. Using const with pointers. Examples.

Lab 2: ADT Design & Implementation

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

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.

The Bracket Strategy

CS 376b Computer Vision

IS 0020 Program Design and Software Tools

CHAPTER 4 FUNCTIONS. Dr. Shady Yehia Elmashad

Transcription:

EE 152 Advanced Programming LAB 7 1) Create a class called Rational for performing arithmetic with fractions. Write a program to test your class. Use integer variables to represent the private data of the class the numerator and the denominator. Provide a constructor that enables an object of this class to be initialized when it is declared. The constructor should contain default values in case no initializers are provided and should store the function in reduced form. For example, the fraction 2 4 would be stored in the object as 1 in the numerator and 2 in the denominator. Provide public member functions that perform each of the following tasks: a) Adding two Rational numbers. The result should be stored in reduced form. b) Subtracting two Rational numbers. The result should be stored in reduced form. c) Multiplying two Rational numbers. The result should be stored in reduced form. d) Dividing two Rational numbers. The result should be stored in reduced form. e) Printing Rational numbers in the form a/b, where a is the numerator and b is the denominator. f) Printing Rational numbers in floating-point format. #ifndef RATIONAL_H #define RATIONAL_H Rational.h class Rational public: Rational( int = 0, int = 1 ); // default constructor Rational addition( const Rational & ); // function addition Rational subtraction( const Rational & ); // function subtraction Rational multiplication( const Rational & ); // function multi. Rational division( const Rational & ); // function division void printrational (); // print rational format void printrationalasdouble(); // print rational as double format private: int numerator; // integer numerator int denominator; // integer denominator void reduction(); // utility function }; // end class Rational #endif

Rational.cpp // Member-function definitions for class Rational. #include <iostream> using std::cout; #include "Rational.h" // include definition of class Rational Rational::Rational( int n, int d ) numerator = n; // sets numerator denominator = d; // sets denominator reduction(); // store the fraction in reduced form } // end Rational constructor Rational Rational::addition( const Rational &a ) t.numerator = a.numerator * denominator; t.numerator += a.denominator * numerator; t.denominator = a.denominator * denominator; } // end function addition Rational Rational::subtraction( const Rational &s ) t.numerator = s.denominator * numerator; t.numerator -= denominator * s.numerator; t.denominator = s.denominator * denominator; } // end function subtraction Rational Rational::multiplication( const Rational &m ) t.numerator = m.numerator * numerator; t.denominator = m.denominator * denominator; } // end function multiplication Rational Rational::division( const Rational &v )

t.numerator = v.denominator * numerator; t.denominator = denominator * v.numerator; } // end function division void Rational::printRational () if ( denominator == 0 ) // validates denominator cout << "\ndivide BY ZERO ERROR!!!" << '\n'; else if ( numerator == 0 ) // validates numerator cout << 0; else cout << numerator << '/' << denominator; } // end function printrational void Rational::printRationalAsDouble() cout << static_cast< double >( numerator ) / denominator; } // end function printrationalasdouble void Rational::reduction() int largest; largest = numerator > denominator? numerator : denominator; int gcd = 0; // greatest common divisor for ( int loop = 2; loop <= largest; loop++ ) if ( numerator % loop == 0 && denominator % loop == 0 ) gcd = loop; if (gcd!= 0) numerator /= gcd; denominator /= gcd; } // end if } // end function reduction

#include <iostream> using std::cout; using std::endl; Solution.cpp #include "Rational.h" // include definition of class Rational int main() Rational c( 2, 6 ), d( 7, 8 ), x; // creates three rational objects cout << " + "; x = c.addition( d ); // adds object c and d; sets the value to x cout << " - "; x = c.subtraction( d ); // subtracts object c and d cout << " x "; x = c.multiplication( d ); // multiplies object c and d cout << " / "; x = c.division( d ); // divides object c and d

cout << endl; return 0; } // end main