C++ Programming Assignment 3

Size: px
Start display at page:

Download "C++ Programming Assignment 3"

Transcription

1 C++ Programming Assignment 3 Author: Ruimin Zhao Module: EEE 102 Lecturer: Date: Fei.Xue April/19/2015

2 Contents Contents ii 1 Question Specification Analysis main() Choice(Date a,date b) operator++() operator==/>/<() Check(string a,int b) Input() Design Implementation main() Head file Function Definition Testing Robustness test of error input Test normal result Question Specification i

3 2.2 Analysis main() same vec (vector<int> a, vector<int> b) Check(vector<char> a) Design Implementation: Headfile main() Testing Robustness test of error input Test normal result Question Specification Analysis main() *findc(char const *source,char const *obj) Design Implementation: Headfile main() Testing Robustness test of error input Test normal result ii

4 Section 1 Question 1 Define a class Date contains two data members, denoting the 3-letter abbreviation of the month and the date. 1. Write the constructor, destructor and output function members of this class; 2. Define the operator = to assign the value of one Date object to the other, > < and == to compare two dates, and the unary sign ++ to add one day to a date object. 1.1 Specification Abstraction This program will ask the user to enter two dates, sort the dates to be in the required case pattern, and conduct operations upon them with the aid of overloading operators =, ==, >, < and ++. Concrete expectation Input pattern: abbreviation and the day of that month; for example: Jan-1 Overloading operators ability: assign object value, compare two dates, and work out the next day of certain date Robustness: Wrong input for abbreviation and integer input such as the day of certain month Additional requirement Constructor and destructor should be established There should be two member variables which are the abbreviation of month and the specific day of month 1

5 1.2 Analysis main() inputs: two strings and two integers outputs: several groups of combination of string and integer, or even with operator marks additional requirement: none. Figure 1.1: Flowchart of function main() Choice(Date a,date b) inputs: two Date objects outputs: guide to difference operation functions additional requirement: none. 2

6 Figure 1.2: Flowchart of function Choice(Date a,date b) operator++() inputs: a Date object outputs: a new temporary Date object additional requirement: none. Figure 1.3: Flowchart of function operator++() 3

7 1.2.4 operator==/>/<() inputs: a Date object outputs: true or false additional requirement: none. Figure 1.4: Flowchart of function operator==/>/<() Check(string a,int b) inputs: a string and a integer outputs: a reminding sentence with a call-off of program or nothing additional requirement: none. 4

8 Figure 1.5: Flowchart of function Check(string a,int b)() Input() inputs: two strings and two integers outputs: guide to Choice function to guide following running additional requirement: none. Figure 1.6: Flowchart of function Input() 5

9 1.3 Design main() algorithm declare two variables date1-represents the first date date2-represents the second date Call member function Input() for Date date1 object to conduct concrete input of first date Call member function Input() for Date date2 object to conduct concrete input of second date Clear the screen using system( cls ) Call member function Display() for Date date1 object to display the first date Call member function Display() for Date date2 object to display the second date Call function Choice(Date a,date b) to transport two Date objects date1 and date2 over to guild following running Choice(Date a,date b) algorithm declare three variables date12-represents the next day of first date date22-represents the next day of second date choice-represents the choice of operations Display the choice guide menu Use while loop to conduct operations until choice equals to 4 Ask user to input an integer choice to choose which operations to conduct If choice equals to 1, work out the next month of first date; If choice equals to 2, work out the next day of second date; If choice equals to 3, compare two dates; if choice equals to 4, call off this program normally; If choice is not one integer from 1 to 4, remind user of the wrong input 6

10 operator++() algorithm declare three variables Date temp-represents the temporary object representing next date of the transported date i-represents the position within the month name string FullName or biggest day string LastDate month-represents the order of month abbreviation Use for loop to scan the name stored in string FullName one by one, increase the value of month until the abbreviation is found in one name, and record the position i When month is Dec, and the day is the last last of Dec (31), assign Jan-1 to Next Month object temp When month is not Dec but the day is the last last of that month, assign next month-1 to Next Month ; For example, next month of Feb-28 is Mar-1 When month is not Dec and the day is not the last day of that month, simply increase the value of member variable date by 1, and remain the month name operator==/>/<() algorithm declare four variables i-represents the position within the month name string FullName or biggest day string LastDate of first date j-represents the position within the month name string FullName or biggest day string LastDate of second date month1-represents the order of month abbreviation of first date month2-represents the order of month abbreviation of second date Use for loop to scan FullName, find matched month name for first date, and record the position i Use for loop to scan FullName, find matched month name for second date, and record the position j For function operator ==: When two dates have same month and day, return true; else return false 7

11 For function operator >: When first date month bigger than second, return true; When two dates have same month but first date has larger day, return true; Else return false For function operator <: When first date month smaller than second, return true; When two dates have same month but first date has smaller day, return true; Else return false Check() algorithm declare one variable i-represents the position within the month name string FullName or biggest day string LastDate of the transported date Use if statement to check whether the transported string has three letters Use for loop to check whether all the elements in the transported string are uppercase letters Convert the last letters in string into lowercase letters and scan FullName to check whether this abbreviation is entered correctly by checking whether matched month name can be found Scan LastDate to find the corresponding biggest day of this month Check whether transported integer is integer and is within the range of day; for example: Feb only had days from 1 to 28 Remind user if wrong type input exist Input() algorithm declare four variables a-represents the abbreviation of the month b-represents the day of the month i-represents the position within the month name string FullName or biggest day string LastDate of the date month-represents the order of month abbreviation of the date Ask user to enter the string a representing abbreviation and the integer b representing day Call function Check(string a,int b) to check the input type 8

12 Assign the value stored in b to member variable date convert the last two letters of a to lowercase Scan the matched month name for converted a abbreviation and assign the matched name to member variable month abb 1.4 Implementation main() 1 //This f i l e contains the main ( ) function 2 # include<iostream> 3 # include Date. h //Head f i l e Date. h contains Date c l a s s 4 using namespace std ; 5 6 i n t main ( ) 7 { 8 Date date1 ; //Declare two input Date o b j e c t 9 Date date2 ; 10 //Enter two dates 11 cout<< Pease enter the f i r s t date <<endl ; 12 date1. Input ( ) ; 13 cout<< \n Pease enter the second date <<endl ; 14 date2. Input ( ) ; 15 system ( c l s ) ; //Clear the screen cout<< \nthe f i r s t date :\ t\t ; //Display two entered dates 18 date1. Display ( ) ; 19 cout<< \nthe second date :\ t ; 20 date2. Display ( ) ; 21 Choice ( date1, date2 ) ; //Guide user to conduct operations 22 return 0 ; 23 } Head file 1 //This f i l e contains declaration of functions 2 # ifndef DATE H 3 # define DATE H 4 5 # include<iostream> 6 # include<string> //Head f i l e contains string 7 using namespace std ; 8 //FullName stores the abbreviations of a l l months in expected for 9 const s t r i n g FullName [12]={ Jan, Feb, Mar, Apr, May, June, July, Aug, Sept, Oct, Nov, Dec }; 10 //LastDate stores the biggest day of each month. For example : 28 for Feb 11 const int LastDate [ 12]={31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; c l a s s Date //Date c l a s s 14 { 15 private : //Member variable : 16 string month abb ; 17 i n t date ; 18 public : 19 Date : : Date ( ) ; //Constructor 20 Date : : Date ( ) ; //Destructor void Date : : operator =( Date &a ) ; //Overloading operators : 23 Date Date : : operator + + ( ) ; 24 bool Date : : operator ==(Date a ) ; 25 bool Date : : operator >(Date a ) ; 26 bool Date : : operator <(Date a ) ; void Date : : Display ( ) ; //Display the Date 29 void Date : : Check ( string a, int b ) ; //Check the input type 30 void Date : : Input ( ) ; //Guide user to enter dates 31 }; void Choice ( Date a, Date b ) ; //Guide user to conduct operations # endif 9

13 1.4.3 Function Definition 1 //This f i l e contains definition of functions 2 # include<iostream> 3 # include Date. h //Head f i l e Date. h contains Date class 4 using namespace std ; 5 6 / / 7 void Choice ( Date a, Date b ) //Guide user to conduct operations 8 { 9 int choice =0; 10 Date date12, date22 ; //Temporary o b j e c t s represeting next month 11 cout<< \n\n Choice <<endl ; 12 cout<< 1 The next day of f i r s t date. <<endl ; 13 cout<< 2 The next day of second date. <<endl ; 14 cout<< 3 Compare two dates. <<endl ; 15 cout<< 4 Exit. <<endl ; 16 cout<< <<endl ; 17 cout<< Please enter your choice : ; 18 while ( choice! = 4 ) 19 { 20 cin>>choice ; 21 switch ( choice ) 22 { 23 case 1 : 24 { //Work out the Next Month of f i r s t date 25 date12=++a ; 26 cout<< The next day of the f i r s t date is : ; 27 date12. Display ( ) ; 28 cout<< \n\nplease enter your choice : ; 29 break ; 30 } 31 case 2 : 32 { //Work out the Next Month of second date 33 date22=++b ; 34 cout<< The next day of the second date is : ; 35 date22. Display ( ) ; 36 cout<< \n\nplease enter your choice : ; 37 break ; 38 } 39 case 3 : 40 { //Work out the comparison r e s u l t of two dates 41 cout<< The comparison r e s u l t of two dates i s : <<endl ; 42 i f ( a>b ) //The f i r s t date i s bigger ( l a t e r ) than the second 43 { 44 a. Display ( ) ; 45 cout<< > ; 46 b. Display ( ) ; 47 } 48 e l s e i f ( a<b ) //The f i r s t date i s smaller ( e a r l i e r ) than the second 49 { 50 a. Display ( ) ; 51 cout<< < ; 52 b. Display ( ) ; 53 } 54 else i f ( a==b ) //Two dates are the same 55 { 56 a. Display ( ) ; 57 cout<< == ; 58 b. Display ( ) ; 59 } 60 cout<< \n\nplease enter your choice : ; 61 break ; 62 } 63 case 4 : 64 { // E x i t the operation normally 65 getchar ( ) ; 66 e x i t ( 1 ) ; 67 } 68 default : //Remind wrong type input 69 { 70 cout<< Wrong input, please r e s t a r t. <<endl ; 71 getchar ( ) ; 72 e x i t ( 1 ) ; 73 break ; 74 } 75 } 76 } 77 } 78 / / 79 Date : : Date ( ) //Constructor 80 {} Date : : Date ( ) //Destructor 83 {} 84 / / 85 void Date : : operator =( Date &a ) //Overloading operator = 86 { //Assign one o b j e c t to another 87 month abb=a. month abb ; 88 date=a. date ; 89 } 90 / / 91 Date Date : : operator ++() //Overloading operator ++ 10

14 92 { 93 Date temp ; //Declare a temporary object storing Next Month 94 i n t i, month ; 95 //Scan the name stored in s t r i n g FullName one by one, 96 // i n c r e a s e the value of month u n t i l the a b b r i v i a t i o n i s 97 //found in one name, and record the position i. 98 f o r ( i =0; i <12; i ++) 99 { 100 i f ( FullName [ i ]. find ( month abb )! = s t r i n g : : npos ) 101 { 102 month=i ; 103 temp. month abb=fullname [ month ] ; 104 break ; 105 } 106 } 107 //When month i s Dec, assign Jan 1 to Next Month 108 i f ( month==11&&date==lastdate [ month ] ) 109 { 110 temp. month abb=fullname [ 0 ] ; 111 temp. date =1; 112 } 113 //When month is not Dec but the day is the l a s t l a s t of that 114 //month, assign next month 1 to Next Month ; For example, 115 //next month of Feb 28 i s Mar else i f ( month!=11&&date==lastdate [ month ] ) 117 { 118 month=month+1; 119 temp. month abb=fullname [ month ] ; 120 temp. date =1; 121 } 122 //When month is not Dec and the day is not the l a s t day of 123 //that month, simply increase the value of menber variable date 124 //by 1, and remain the month name 125 e l s e 126 temp. date=date +1; 127 return temp ; 128 } 129 / / 130 bool Date : : operator ==(Date a ) //Overloading operation == 131 { 132 i n t i, j, month1, month2 ; 133 //Scan FullName, find matched month name for f i r s t date, 134 //and record the position i 135 f o r ( i =0; i <12; i ++) 136 { 137 i f ( FullName [ i ]. find ( month abb )! = s t r i n g : : npos ) 138 { 139 month1=i ; 140 break ; 141 } 142 } 143 //Scan FullName, find matched month name for second date, 144 //and record the position j 145 f o r ( j =0; j <12; j ++) 146 { 147 i f ( FullName [ j ]. find ( a. month abb )! = s t r i n g : : npos ) 148 { 149 month2= j ; 150 break ; 151 } 152 } 153 //When two dates have same month and day, return true ; else return false 154 i f ( i == j&&date==a. date ) 155 return true ; 156 e l s e 157 return false ; 158 } 159 / / 160 bool Date : : operator >(Date a ) //Overloading operation > 161 { 162 i n t i, j, month1, month2 ; 163 //Scan FullName, find matched month name for f i r s t date, 164 //and record the position i 165 f o r ( i =0; i <12; i ++) 166 { 167 i f ( FullName [ i ]. find ( month abb )! = s t r i n g : : npos ) 168 { 169 month1=i ; 170 break ; 171 } 172 } 173 //Scan FullName, find matched month name for second date, 174 //and record the position j 175 f o r ( j =0; j <12; j ++) 176 { 177 i f ( FullName [ j ]. find ( a. month abb )! = s t r i n g : : npos ) 178 { 179 month2= j ; 180 break ; 181 } 182 } 183 i f ( i>j ) //When f i r s t date month bigger than second, return true 184 return true ; 185 else i f ( date>a. date ) //When two dates have same month but f i r s t date has larger day, return true 11

15 186 return true ; 187 e l s e //Else return f a l s e 188 return false ; 189 } 190 / / 191 bool Date : : operator <(Date a ) //Overloading operator < 192 { 193 i n t i, j, month1, month2 ; 194 //Scan FullName, find matched month name for f i r s t date, 195 //and record the position i 196 f o r ( i =0; i <12; i ++) 197 { 198 i f ( FullName [ i ]. find ( month abb )! = s t r i n g : : npos ) 199 { 200 month1=i ; 201 break ; 202 } 203 } 204 //Scan FullName, find matched month name for second date, 205 //and record the position j 206 f o r ( j =0; j <12; j ++) 207 { 208 i f ( FullName [ j ]. find ( a. month abb )! = s t r i n g : : npos ) 209 { 210 month2= j ; 211 break ; 212 } 213 } 214 i f ( i<j ) //When f i r s t date month smaller than second, return true 215 return true ; 216 else i f ( date<a. date ) //When two dates have same month but f i r s t date has smaller day, return true 217 return true ; 218 e l s e //Else return f a l s e 219 return false ; 220 } 221 / / 222 void Date : : Display ( ) //Display the date 223 { 224 cout<<month abb<< <<date ; 225 } 226 / / 227 void Date : : Check ( string a, int b ) //Check whether input string and integer are in correct type 228 { 229 i n t i ; 230 i f ( a. s i z e ( ) = = 3 ) // S t r i n g should have three l e t t e r s 231 { 232 f o r ( i =0; i <3; i ++) 233 { // L e t t e r s should be in uppercase 234 i f (! ( a [ i ]>= A a [ i ]<= Z ) ) 235 { 236 cout<< Wrong type input, please r e s t a r t <<endl ; 237 e x i t ( 1 ) ; 238 } 239 } 240 } 241 else //Remind user i f wrong type input exist 242 { 243 cout<< Wrong type input, please r e s t a r t <<endl ; 244 e x i t ( 1 ) ; 245 } 246 f o r ( i =1; i <3; i ++) //Convert l a s t two l e t t e r s i n t o lowercase 247 { 248 a [ i ]= a [ i ] ; 249 } 250 //Scan the name stored in s t r i n g FullName one by one, 251 // i n c r e a s e the value of month u n t i l the a b b r i v i a t i o n i s 252 //found in one name, and record the position i. 253 f o r ( i =0; i <12; i ++) 254 { 255 i f ( FullName [ i ]. find ( a )! = s t r i n g : : npos ) 256 { 257 break ; 258 } 259 } 260 i f ( b==int ( b ) ) //Integer date should be integer and within correct range 261 //for example, Feb only had days from 1 to { 263 i f ( b>lastdate [ i ] b<1) 264 { 265 cout<< Wrong type input, please r e s t a r t <<endl ; 266 e x i t ( 1 ) ; 267 } 268 } 269 else //Remind user i f wrong type input exist 270 { 271 cout<< Wrong type input, please r e s t a r t <<endl ; 272 e x i t ( 1 ) ; 273 } 274 } 275 / / 276 void Date : : Input ( ) //Guide user to enter two dates 277 { 278 string a ; // String as input abbreviation 279 i n t b, i, month ; 12

16 280 cout<< Pease enter the abbriviation of month:\ t ; 281 cin>>a ; 282 cout<< Pease enter the date of this month:\ t ; 283 cin>>b ; 284 Check ( a, b ) ; //Call Check function to check input type //Assign input s t r i n g and i n t e g e r to member v a r i a b l e s 287 // month abb and date r e s p e c t i v e l y 288 date=b ; 289 f o r ( i =1; i <3; i ++) 290 { 291 a [ i ]= a [ i ] ; 292 } 293 month abb=a ; 294 //Scan the name stored in s t r i n g FullName one by one, 295 // i n c r e a s e the value of month u n t i l the a b b r i v i a t i o n i s 296 //found in one name, and record the position i. 297 f o r ( i =0; i <12; i ++) 298 { 299 i f ( FullName [ i ]. find ( a )! = s t r i n g : : npos ) 300 { 301 month=i ; 302 month abb=fullname [ month ] ; 303 break ; 304 } 305 } 306 } 1.5 Testing Both the fundamental functionalities and robustness of this program are tested: Robustness test of error input Abbreviation String: For the string month abb, this program is able to check out the wrong input when the entered characters are not all uppercase letters as required; or the entered letters are not three; or the entered uppercase three letters are not correct form for abbreviations of months. Date Integer: For the integer date, this program is able to check out the wrong input when the entered characters are not digit; or the digit is not in the correct range for its corresponding month: for example, Feb can only allow date from 1 to 28. Choice Integer: For the integer choice, this program is able to check out the wrong input when the entered characters are not digit; or the digit is not 1 or 2 as required. 13

17 Abbreviation String Please enter the abbreviation of month: 123/??? Wrong type input, Please restart. (the screenshots of these two testing are shown in Figures below) (a) (b) Date Integer Please enter the date of this month: JAN/FEB Please enter the date of this month: 0/28 Wrong type input, Please restart. (the screenshots of these two testing are shown in Figures below) (c) (d) 14

18 Choice Integer Please enter your choice: 123/??? Wrong input, Please restart. (the screenshots of these two testing are shown in Figures below) (e) (f) Test normal result Feb-28 and Jan-31: The addition operator ++ is tested. The ability of correctly working out the next month when the date is the last day of that month is especially tested: 28 and 31 are both the last day of its corresponding month Jan and Feb respectively, thus the correct next month should not be Jan-32 and Feb-29, but should be Feb-1 and Mar-1 as tested. Also, the comparison operator > is tested in this particular case. Mar-31 and Mar-31: The comparison operator == is especially tested in this particular case. Also, the ++ in the similar case as in first test is tested here again. Jan-30 and Dec-31: The addition operator ++ is tested. The ability of correctly working out the next month when the month is the last month of a year is especially tested: Dec is the last month of a year, thus the correct next month should not be some 13th month, but should be Jan-1 as tested. Also, the comparison operator < is tested in this particular case. 15

19 Attention: The ability of operator =, which is to assign the value of one object to the other as required in question, is tested within the program writing as illustrated in the last part of this section. Feb-28 and Jan-31 Next day of first date: Mar-1 Next day of second date: Feb-1 Comparison result if these two dates: Feb-28>Jan-31 (the screenshots of these two steps are shown in Figures below) (g) (h) Mar-31 and Mar-31 Next day of first date: Apr-1 Next day of second date: Apr-1 Comparison result if these two dates: Mar-31==Mar-31 (the screenshots of these two steps are shown in Figures below) (i) (j) 16

20 Jan-30 and Dec=31 Next day of first date: Jan-31 Next day of second date: Jan-1 Comparison result if these two dates: Jan-30<Dec-31 (the screenshots of these two steps are shown in Figures below) (k) (l) Operator = One object is date12 The other object is ++a (the screenshots of the related code is shown below) 17

21 Section 2 Question 2 Write a function bool same vec (vector<int> a, vector<int> b) that checks whether two vectors have the same elements, ignoring the order and multiplicities. For example, the two vectors {1, 4, 9, 16, 9, 7, 4, 9, 11} and {11, 11, 7, 9, 16, 4, 1} would be considered i- dentical. Requirements: 1. Get the numbers from keyboard input; 2. The length of the vectors are unknown, it is only determined by the input. 2.1 Specification Abstraction This program will ask user to input two vectors, compare the elements of these two vectors and decide whether they are identical or not. Concrete expectation Input pattern: the elements should all be integers Standard of being Identical: contain same elements but ignore the different orders and different times of appearance. Robustness: Wrong input for the elements Additional requirement To conduct the concrete the comparison, two vectors should be transported to a callable function as two arguments While after comparison, a boolean variable suggesting comparison result should be returned to main function 18

22 2.2 Analysis main() inputs: three integers and four vectors(two in char type and two in int type) outputs: a statement telling whether two vectors are the same additional requirement: elements of vector must all be integers Figure 2.1: Flowchart of function main() same vec (vector<int> a, vector<int> b) inputs: two vectors outputs: a statement tell whether two transported vectors are the same additional requirement: elements of vector must all be integers 19

23 Figure 2.2: Flowchart of function same vec (vector<int> a, vector<int> b) Check(vector<char> a) inputs: transported vector in char type outputs: a statement reminding wrong type input or nothing additional requirement: none. Figure 2.3: Flowchart of function Check(vector<char> a) 20

24 2.3 Design main() algorithm declare seven variables choice-represents the choice of whether to continue comparison a-represents the element character entered in first vector b-represents the element character entered in second vector vector<char> v1-represents the first entered vector that is to be tested whether input type is correct vector<char> v2-represents the second entered vector that is to be tested whether input type is correct vector<int> v3-represents the first vector with checked correct type elements to be transported vector<int> v4-represents the second vector with checked correct type elements to be transported Display the NOTICE of entering vector Use while loop to conduct comparison of vectors continuously until choice equals to 2 Use while loop to enter elements to vectors Call function Check(vector<char> a) to check whether all the element in vector are integers Assign the checked correct vector v1, v2 to vectors to be transported out v3 v4 respectively Call function same vec (vector<int> a, vector<int> b) to compare whether t- wo vectors have the same elements ignoring the order and multiplicities though Ask user to enter integer choice to decide whether to continue comparison with other pairs of vectors Remind user if choice is entered wrongly 21

25 same vec (vector<int> a, vector<int> b) algorithm declare two variables i-represents the position of character in first transported int type vector j-represents the position of character in first transported int type vector Use for loop to scan all the elements to see if all the elements in transported v4 can be found in transported v3 If not all the elements in v4 can be found in v3, the j will be the size of v4, then return false, else return true Use for loop to scan all the elements to see if all the elements in transported v3 can be found in transported v4 If not all the elements in v3 can be found in v4, the j will be the size of v3, then return false, else return true Check(vector<char> a) algorithm declare one variable i-represents the position of character in transported char type vector Use for loop to check every elements in transported char type vector to see whether all of them are integers Remind user of the existence of wrong type input 2.4 Implementation: Headfile 1 //This f i l e contains declaration and definition of functions 2 # include<iostream> 3 # include<vector> 4 using namespace std ; 5 6 //Compare two input vectors, decide whether they are identical 7 bool same vec ( vector<int> a, vector<int> b ) 8 { 9 i n t i, j ; 10 //Scan a l l the elements to see i f a l l the elements in 11 //transported v4 can be found in transported v3 12 f o r ( i =0; i<a. s i z e ( ) ; i ++) 13 { 14 f o r ( j =0; j<b. s i z e ( ) ; j ++) 15 { 16 i f ( a [ i ]==b [ j ] ) 17 break ; 18 } 19 // I f not a l l the elements in v4 can be found in v3, the j will be 20 //smaller than the s i z e of v4, then return f a l s e 21 i f ( j ==b. s i z e ( ) ) 22

26 22 return false ; 23 } 24 //Scan a l l the elements to see i f a l l the elements in 25 //transported v3 can be found in transported v4 26 f o r ( i =0; i<b. s i z e ( ) ; i ++) 27 { 28 f o r ( j =0; j<a. s i z e ( ) ; j ++) 29 { 30 i f ( b [ i ]==a [ j ] ) 31 break ; 32 } 33 // I f not a l l the elements in v3 can be found in v4, the j will be 34 //smaller than the s i z e of v3, then return f a l s e 35 i f ( j ==a. s i z e ( ) ) 36 return false ; 37 } 38 return true ; 39 } void Check ( vector<char> a ) //Check whether a l l the elements in vector are integers 42 { 43 i n t i ; 44 f o r ( i =0; i<a. s i z e ( ) ; i ++) 45 { 46 i f ( a [ i ]< 0 a [ i ]> 9 ) 47 { 48 cout<< Wrong type input included, please r e s t a r t. <<endl ; 49 getchar ( ) ; 50 e x i t ( 1 ) ; 51 } 52 } 53 } main() 1 //This f i l e contains the main ( ) function 2 # include<iostream> 3 # include compare. h 4 using namespace std ; 5 6 i n t main ( ) 7 { 8 int choice =0; 9 char a, b ; //Each element of input vector 10 vector<char> v1, v2 ; //Input vector 11 vector<int> v3, v4 ; //Vector copied from v1 and v2, to be compared //Display the Notice of inputting 14 cout<< NOTICE <<endl ; 15 cout<< Press SPACE after entering each element <<endl ; 16 cout<< Press Ctrl+Z after enter the each vector <<endl ; 17 cout<< <<endl ; 18 //The method to end the inputting of one vector was l e a r n t from 19 // internet source : http :// blog. itpub. net / / viewspace / while ( choice! = 2 ) //Conduct comparison u n t i l choice =2 22 { 23 cout<< Please enter the f i r s t vector :\ t\t ; 24 while ( cin>>a ) //Input vector with unkown length 25 { 26 v1. push back ( a ) ; 27 } 28 cin. c l e a r ( ) ; 29 Check ( v1 ) ; //Check the input element type of v1 30 //Copy vector v1 to v3 i f inputs type are correct 31 v3. assign ( v1. begin ( ), v1. end ( ) ) ; cout<< Please enter the second vector :\ t\t ; 34 while ( cin>>b ) 35 { 36 v2. push back ( b ) ; 37 } 38 cin. c l e a r ( ) ; 39 Check ( v2 ) ; //Check the input element type of v2 40 //Copy vector v2 to v4 i f inputs type are correct 41 v4. assign ( v2. begin ( ), v2. end ( ) ) ; same vec ( v3, v4 ) ; //Call same vec function to decide whether v3 and v4 are identical 44 i f ( same vec ( v3, v4 ) ) 45 cout<< These two vectors are I d e n t i c a l. <<endl ; 46 e l s e 47 cout<< These two vectors are Not I d e t i c a l <<endl ; 48 //Continue comparison of other p a i r s of v e c t o r s i f choice =1 49 cout<< \nwhether to continue comparing( 1 Yes; 2 No ) : ; 50 cin>>choice ; 51 i f ( choice!=1&& choice! = 2 ) //Remind user i f choice i s input wrongly 52 { 53 cout<< Wrong type input included, please r e s t a r t. <<endl ; 54 getchar ( ) ; 23

27 55 e x i t ( 1 ) ; 56 } 57 } 58 return 0 ; 59 } 2.5 Testing Both the fundamental functionalities and robustness of this program are tested: Robustness test of error input Vector String: For the string a, this program is able to check out the wrong input when the entered characters are not all digits as required. Choice Integer: For the integer choice, this program is able to check out the wrong input when the entered characters are not digit; or the digit is not 1 or 2 as required. Vector Please enter the vector: a b c/??? Wrong type input included, Please restart. (the screenshots of these two testing are shown in Figures below) (a) (b) 24

28 Choice Whether to continue the comparison(1 or 2): 0/? Wrong type input included, Please restart. (the screenshots of these two testing are shown in Figures below) (c) (d) Test normal result The ability of comparing elements ignoring order and multiplicities is effectively tested: For example, in the first example test, the obj elements are listed in different appearance order in source, and some of the elements appeared more than one time in source sequence such as However, since the elements in obj indeed all appear in source, these two sequences are still regarded as Identical as tested. While for testing Not Identical vectors, the ability is tested in second example clearly. 25

29 Three normal text examples Please enter the first vector: Please enter the second vector: These two vectors are Identical. Please enter the first vector: Please enter the second vector: 5 6 These two vectors are Not Identical. Please enter the first vector: Please enter the second vector: These two vectors are Not Identical. (the screenshots of these three tests are shown in Figures below) 26

30 Section 3 Question 3 Write a function to search a character sequence pointed by a pointer (called obj), in another character sequence (called source). Return the pointer pointing to the found character. Eg1: search for C in ABCDEF, return the pointer pointing to C. Eg2: search for Z in ABCDEF, return a NULL pointer. Eg3: search for CD in ABCDEF, return the pointer pointing to C. Eg4: search for CF in ABCDEF, return a NULL pointer. Eg5: search for A in ABCAFC, return the pointer pointing to the first A. The function header is given by: char *findc (char const *source, char const *obj); 3.1 Specification Abstraction This program will ask user to enter two sequence, search the second sequence content within the first sequence using pointer, display the index position and the pointed value. Concrete expectation Input pattern: the elements can be any types of characters including digit, letter, and marks such as?. Returned value: the first character of the searched sequence obj, and the position pointed by pointer is in source where that character first found as required. 27

31 Additional requirement To conduct the concrete searching, two char pointers should be transported to the callable function as two arguments. While the returned value suggesting result should be a pointer as well. 3.2 Analysis main() inputs: one integer and two strings outputs: a pointer additional requirement: none. Figure 3.1: Flowchart of function main() 28

32 3.2.2 *findc(char const *source,char const *obj) inputs: two transported pointers outputs: a pointer additional requirement: none. Figure 3.2: Flowchart of function *findc(char const *source,char const *obj) 29

33 3.3 Design main() algorithm declare five variables choice-represents the choice of whether to continue searching for other pairs of sequences a-represents the sequence source b-represents the sequence obj *source-represents the char type pointer to string a of source *obj-represents the char type pointer to string b of obj Use while loop to conduct searching until choice equals to 2 Ask user to enter two sequence strings a and b Use if statement to call function *findc(char const *source,char const *obj) to search obj from source Return the pointed value and the index position if successfully found, else return pointer NULL Ask user to enter integer choice to decide whether to continue searching with other pairs of sequences Remind user if choice is entered wrongly *findc(char const *source,char const *obj) algorithm declare two variables i-represents the position of character in first transported char pointer string a j-represents the position of character in first transported char pointer string b Use for loop to scan source to find obj If all the elements in obj are found in source, the j should be the length of obj Return the first found letter in source Return NULL if obj cannot be found in source 30

34 3.4 Implementation: Headfile 1 //This f i l e contains declarationa and definition of functions 2 # include<iostream> 3 using namespace std ; 4 5 //search sequence obj from sequence source 6 char findc ( char const source, char const obj ) 7 { 8 i n t i, j ; 9 //Scan source to find obj 10 f o r ( i =0; i<s t r l e n ( source ) ; i ++) 11 { 12 f o r ( j =0; j<s t r l e n ( obj ) ; j ++) 13 { 14 i f ( obj [ j ]! = source [ i + j ] ) 15 break ; 16 } 17 // I f a l l the elements in obj are found in source, 18 //the j should be the length of obj 19 i f ( j == s t r l e n ( obj ) ) 20 return ( char )&source [ i ] ; //Return the f i r s t found l e t t e r in source 21 } return NULL; //Return NULL i f obj cannot be found in source 24 } main() 1 //This f i l e contains main ( ) function 2 # include <iostream> 3 # include <string> 4 # include search. h //Head f i l e search. h 5 using namespace std ; 6 7 i n t main ( ) 8 { 9 int choice =0; 10 s t r i n g a, b ; //Two sequence 11 char const source=a. c s t r ( ) ; 12 char const obj=b. c s t r ( ) ; 13 //The conversion method between s t r i n g and char const i s l e a r n t from 14 // i n t e r n e t source : http :// blog. csdn. net/perfumekristy/ a r t i c l e / d e t a i l s / while ( choice! = 2 ) //Continue searching u n t i l choice =2 17 { 18 cout<< Please enter the source sequence :\ t ; 19 cin>>a ; 20 cout<< Please enter the obj sequence :\ t ; 21 cin>>b ; i f ( findc ( source, obj )! =NULL) //Call findc function to conduct searching 24 { //Return the pointed value and the index p o s i t i o n 25 cout<< The returned pointer\t : << findc ( source, obj)<<endl ; 26 cout<< This index in source\t : <<findc ( source, obj) source+1<<endl ; 27 } 28 else //Return NULL i f no pointed value can be found 29 cout<< The returned pointer i s NULL <<endl ; 30 //Continue searching i f choice =1 31 cout<< \nwhether to continue serching( 1 Yes ; 2 No ) : ; 32 cin>>choice ; 33 i f ( choice!=1&& choice! = 2 ) //Remind user i f choice i s entered wrongly 34 { 35 cout<< Wrong type input included, please r e s t a r t. <<endl ; 36 getchar ( ) ; 37 e x i t ( 1 ) ; 38 } 39 } 40 return 0 ; 41 } 31

35 3.5 Testing Robustness test of error input Choice Integer: For the integer choice, this program is able to check out the wrong input when the entered characters are not digit; or the digit is not 1 or 2 as required. Choice Whether to continue the comparison(1 or 2): 0/? Wrong type input included, Please restart. (the screenshots of these two testing are shown in Figures below) (a) (b) Test normal result First, all the examples listed in question are tested fully Second, four more examples are tested, where the input elements have more various types to verify the ability of searching character sequence (here the word character is understood as any type of character including integers and marks like? ). 32

36 Examples in question Please enter the source sequence: ABCDEF Please enter the obj sequence: C / Z / CD / CF / A The returned pointer: C / NULL / C / NULL / A The index in source: 3 / NULL / C / NULL / 1 (the screenshots of these five tests are shown in Figure below) 33

37 Various sequence Please enter the source sequence: 123??123??123 Please enter the obj sequence:??123 / 1234 The returned pointer:? / NULL The index in source: 4 / NULL Please enter the source sequence: <>?? <>?? <> Please enter the obj sequence:?? <> / <><> The returned pointer: /? / NULL The index in source: 3 / NULL (the screenshots of these four tests are shown in Figure below) 34

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

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

CHAPTER 1.2 INTRODUCTION TO C++ PROGRAMMING. Dr. Shady Yehia Elmashad

CHAPTER 1.2 INTRODUCTION TO C++ PROGRAMMING. Dr. Shady Yehia Elmashad CHAPTER 1.2 INTRODUCTION TO C++ PROGRAMMING Dr. Shady Yehia Elmashad Outline 1. Introduction to C++ Programming 2. Comment 3. Variables and Constants 4. Basic C++ Data Types 5. Simple Program: Printing

More information

Operator overloading: extra examples

Operator overloading: extra examples Operator overloading: extra examples CS319: Scientific Computing (with C++) Niall Madden Week 8: some extra examples, to supplement what was covered in class 1 Eg 1: Points in the (x, y)-plane Overloading

More information

CSCE 110 PROGRAMMING FUNDAMENTALS

CSCE 110 PROGRAMMING FUNDAMENTALS CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table Class Prof. amr Goneid, AUC 1 Dictionaries(1): A Key Table Class Prof. Amr Goneid, AUC 2 A Key Table

More information

Review Questions for Final Exam

Review Questions for Final Exam CS 102 / ECE 206 Spring 11 Review Questions for Final Exam The following review questions are similar to the kinds of questions you will be expected to answer on the Final Exam, which will cover LCR, chs.

More information

C++_ MARKS 40 MIN

C++_ MARKS 40 MIN C++_16.9.2018 40 MARKS 40 MIN https://tinyurl.com/ya62ayzs 1) Declaration of a pointer more than once may cause A. Error B. Abort C. Trap D. Null 2Whice is not a correct variable type in C++? A. float

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

CS31 Discussion 1E. Jie(Jay) Wang Week3 Oct.12

CS31 Discussion 1E. Jie(Jay) Wang Week3 Oct.12 CS31 Discussion 1E Jie(Jay) Wang Week3 Oct.12 Outline Problems from Project 1 Review of lecture String, char, stream If-else statements Switch statements loops Programming challenge Problems from Project

More information

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

Distributed Real-Time Control Systems. Lecture 17 C++ Programming Intro to C++ Objects and Classes Distributed Real-Time Control Systems Lecture 17 C++ Programming Intro to C++ Objects and Classes 1 Bibliography Classical References Covers C++ 11 2 What is C++? A computer language with object oriented

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

Introduction to Programming using C++

Introduction to Programming using C++ Introduction to Programming using C++ Lecture One: Getting Started Carl Gwilliam gwilliam@hep.ph.liv.ac.uk http://hep.ph.liv.ac.uk/~gwilliam/cppcourse Course Prerequisites What you should already know

More information

Exercise 1.1 Hello world

Exercise 1.1 Hello world Exercise 1.1 Hello world The goal of this exercise is to verify that computer and compiler setup are functioning correctly. To verify that your setup runs fine, compile and run the hello world example

More information

CMSC 202 Midterm Exam 1 Fall 2015

CMSC 202 Midterm Exam 1 Fall 2015 1. (15 points) There are six logic or syntax errors in the following program; find five of them. Circle each of the five errors you find and write the line number and correction in the space provided below.

More information

CHRIST THE KING BOYS MATRIC HR. SEC. SCHOOL, KUMBAKONAM CHAPTER 9 C++

CHRIST THE KING BOYS MATRIC HR. SEC. SCHOOL, KUMBAKONAM CHAPTER 9 C++ CHAPTER 9 C++ 1. WRITE ABOUT THE BINARY OPERATORS USED IN C++? ARITHMETIC OPERATORS: Arithmetic operators perform simple arithmetic operations like addition, subtraction, multiplication, division etc.,

More information

6.096 Introduction to C++ January (IAP) 2009

6.096 Introduction to C++ January (IAP) 2009 MIT OpenCourseWare http://ocw.mit.edu 6.096 Introduction to C++ January (IAP) 2009 For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms. Welcome to 6.096 Lecture

More information

int n = 10; int sum = 10; while (n > 1) { sum = sum + n; n--; } cout << "The sum of the integers 1 to 10 is " << sum << endl;

int n = 10; int sum = 10; while (n > 1) { sum = sum + n; n--; } cout << The sum of the integers 1 to 10 is  << sum << endl; Debugging Some have said that any monkey can write a program the hard part is debugging it. While this is somewhat oversimplifying the difficult process of writing a program, it is sometimes more time

More information

Linked List using a Sentinel

Linked List using a Sentinel Linked List using a Sentinel Linked List.h / Linked List.h Using a sentinel for search Created by Enoch Hwang on 2/1/10. Copyright 2010 La Sierra University. All rights reserved. / #include

More information

Getting started with C++ (Part 2)

Getting started with C++ (Part 2) Getting started with C++ (Part 2) CS427: Elements of Software Engineering Lecture 2.2 11am, 16 Jan 2012 CS427 Getting started with C++ (Part 2) 1/22 Outline 1 Recall from last week... 2 Recall: Output

More information

The Hong Kong Polytechnic University Faculty of Engineering

The Hong Kong Polytechnic University Faculty of Engineering The Hong Kong Polytechnic University Faculty of Engineering Programme(s) : BEng(Hons) in Transportation Systems Engineering (41481, 41481SY) BSc(Hons) in Internet and Multimedia Technologies (42477) Higher

More information

Data Types. 9. Types. a collection of values and the definition of one or more operations that can be performed on those values

Data Types. 9. Types. a collection of values and the definition of one or more operations that can be performed on those values Data Types 1 data type: a collection of values and the definition of one or more operations that can be performed on those values C++ includes a variety of built-in or base data types: short, int, long,

More information

EEE145 Computer Programming

EEE145 Computer Programming EEE145 Computer Programming Content of Topic 2 Extracted from cpp.gantep.edu.tr Topic 2 Dr. Ahmet BİNGÜL Department of Engineering Physics University of Gaziantep Modifications by Dr. Andrew BEDDALL Department

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

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

Function Overloading

Function Overloading Function Overloading C++ supports writing more than one function with the same name but different argument lists How does the compiler know which one the programmer is calling? They have different signatures

More information

Due Date: See Blackboard

Due Date: See Blackboard Source File: ~/2315/45/lab45.(C CPP cpp c++ cc cxx cp) Input: under control of main function Output: under control of main function Value: 4 Integer data is usually represented in a single word on a computer.

More information

Data type of a pointer must be same as the data type of the variable to which the pointer variable is pointing. Here are a few examples:

Data type of a pointer must be same as the data type of the variable to which the pointer variable is pointing. Here are a few examples: Unit IV Pointers and Polymorphism in C++ Concepts of Pointer: A pointer is a variable that holds a memory address of another variable where a value lives. A pointer is declared using the * operator before

More information

The sequence of steps to be performed in order to solve a problem by the computer is known as an algorithm.

The sequence of steps to be performed in order to solve a problem by the computer is known as an algorithm. CHAPTER 1&2 OBJECTIVES After completing this chapter, you will be able to: Understand the basics and Advantages of an algorithm. Analysis various algorithms. Understand a flowchart. Steps involved in designing

More information

Lab 2: ADT Design & Implementation

Lab 2: ADT Design & Implementation Lab 2: ADT Design & Implementation By Dr. Yingwu Zhu, Seattle University 1. Goals In this lab, you are required to use a dynamic array to design and implement an ADT SortedList that maintains a sorted

More information

CSci 1113 Final. Name: Student ID:

CSci 1113 Final. Name: Student ID: CSci 1113 Final Name: Student ID: Instructions: Please pick and answer any 10 of the 12 problems for a total of 100 points. If you answer more than 10 problems, only the first 10 will be graded. The time

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

Lecture on pointers, references, and arrays and vectors

Lecture on pointers, references, and arrays and vectors Lecture on pointers, references, and arrays and vectors pointers for example, check out: http://www.programiz.com/cpp-programming/pointers [the following text is an excerpt of this website] #include

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

Do not turn to the next page until the start of the exam.

Do not turn to the next page until the start of the exam. Introduction to Programming, PIC10A E. Ryu Fall 2017 Midterm Exam Friday, November 3, 2017 50 minutes, 11 questions, 100 points, 8 pages While we don t expect you will need more space than provided, you

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

Chapter 15 - C++ As A "Better C"

Chapter 15 - C++ As A Better C Chapter 15 - C++ As A "Better C" Outline 15.1 Introduction 15.2 C++ 15.3 A Simple Program: Adding Two Integers 15.4 C++ Standard Library 15.5 Header Files 15.6 Inline Functions 15.7 References and Reference

More information

Short Notes of CS201

Short Notes of CS201 #includes: Short Notes of CS201 The #include directive instructs the preprocessor to read and include a file into a source code file. The file name is typically enclosed with < and > if the file is a system

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

ADARSH VIDYA KENDRA NAGERCOIL COMPUTER SCIENCE. Grade: IX C++ PROGRAMMING. Department of Computer Science 1

ADARSH VIDYA KENDRA NAGERCOIL COMPUTER SCIENCE. Grade: IX C++ PROGRAMMING. Department of Computer Science 1 NAGERCOIL COMPUTER SCIENCE Grade: IX C++ PROGRAMMING 1 C++ 1. Object Oriented Programming OOP is Object Oriented Programming. It was developed to overcome the flaws of the procedural approach to programming.

More information

Programming in C++: Assignment Week 8

Programming in C++: Assignment Week 8 Programming in C++: Assignment Week 8 Total Marks : 20 Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology Kharagpur 721302 partha.p.das@gmail.com April 12,

More information

CS201 - Introduction to Programming Glossary By

CS201 - Introduction to Programming Glossary By CS201 - Introduction to Programming Glossary By #include : The #include directive instructs the preprocessor to read and include a file into a source code file. The file name is typically enclosed with

More information

CSCS 261 Programming Concepts Exam 1 Fall EXAM 1 VERSION 1 Fall Points. Absolutely no electronic devices may be used during this exam.

CSCS 261 Programming Concepts Exam 1 Fall EXAM 1 VERSION 1 Fall Points. Absolutely no electronic devices may be used during this exam. Name: Print legibly! Section: COMPUTER SCIENCE 261 PROGRAMMING CONCEPTS EXAM 1 VERSION 1 Fall 2014 150 Points Absolutely no electronic devices may be used during this exam. 1. No cell phones, computers,

More information

Recharge (int, int, int); //constructor declared void disply();

Recharge (int, int, int); //constructor declared void disply(); Constructor and destructors in C++ Constructor Constructor is a special member function of the class which is invoked automatically when new object is created. The purpose of constructor is to initialize

More information

LECTURE NOTES ON PROGRAMMING FUNDAMENTAL USING C++ LANGUAGE

LECTURE NOTES ON PROGRAMMING FUNDAMENTAL USING C++ LANGUAGE Department of Software The University of Babylon LECTURE NOTES ON PROGRAMMING FUNDAMENTAL USING C++ LANGUAGE By Dr. Samaher Hussein Ali Collage of Information Technology, University of Babylon, Iraq Samaher_hussein@yahoo.com

More information

CIS 190: C/C++ Programming. Classes in C++

CIS 190: C/C++ Programming. Classes in C++ CIS 190: C/C++ Programming Classes in C++ Outline Header Protection Functions in C++ Procedural Programming vs OOP Classes Access Constructors Headers in C++ done same way as in C including user.h files:

More information

Lecture 7. Log into Linux New documents posted to course webpage

Lecture 7. Log into Linux New documents posted to course webpage Lecture 7 Log into Linux New documents posted to course webpage Coding style guideline; part of project grade is following this Homework 4, due on Monday; this is a written assignment Project 1, due next

More information

Fundamentals of Programming Session 25

Fundamentals of Programming Session 25 Fundamentals of Programming Session 25 Instructor: Reza Entezari-Maleki Email: entezari@ce.sharif.edu 1 Fall 2013 These slides have been created using Deitel s slides Sharif University of Technology Outlines

More information

Increment and the While. Class 15

Increment and the While. Class 15 Increment and the While Class 15 Increment and Decrement Operators Increment and Decrement Increase or decrease a value by one, respectively. the most common operation in all of programming is to increment

More information

Introduction to Programming EC-105. Lecture 2

Introduction to Programming EC-105. Lecture 2 Introduction to Programming EC-105 Lecture 2 Input and Output A data stream is a sequence of data - Typically in the form of characters or numbers An input stream is data for the program to use - Typically

More information

CPSC 427: Object-Oriented Programming

CPSC 427: Object-Oriented Programming CPSC 427: Object-Oriented Programming Michael J. Fischer Lecture 10 October 1, 2018 CPSC 427, Lecture 10, October 1, 2018 1/20 Brackets Example (continued from lecture 8) Stack class Brackets class Main

More information

Fast Introduction to Object Oriented Programming and C++

Fast Introduction to Object Oriented Programming and C++ Fast Introduction to Object Oriented Programming and C++ Daniel G. Aliaga Note: a compilation of slides from Jacques de Wet, Ohio State University, Chad Willwerth, and Daniel Aliaga. Outline Programming

More information

Jordan University of Science & Technology Department of Computer Science CS 211 Exam #1 (23/10/2010) -- Form A

Jordan University of Science & Technology Department of Computer Science CS 211 Exam #1 (23/10/2010) -- Form A Jordan University of Science & Technology Department of Computer Science CS 211 Exam #1 (23/10/2010) -- Form A Name: ID#: Section #: Day & Time: Instructor: Answer all questions as indicated. Closed book/closed

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

COMP322 - Introduction to C++ Lecture 01 - Introduction

COMP322 - Introduction to C++ Lecture 01 - Introduction COMP322 - Introduction to C++ Lecture 01 - Introduction Robert D. Vincent School of Computer Science 6 January 2010 What this course is Crash course in C++ Only 14 lectures Single-credit course What this

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

CSCE 110 PROGRAMMING FUNDAMENTALS

CSCE 110 PROGRAMMING FUNDAMENTALS CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 16. Linked Lists Prof. amr Goneid, AUC 1 Linked Lists Prof. amr Goneid, AUC 2 Linked Lists The Linked List Structure Some Linked List

More information

CS3157: Advanced Programming. Outline

CS3157: Advanced Programming. Outline CS3157: Advanced Programming Lecture #12 Apr 3 Shlomo Hershkop shlomo@cs.columbia.edu 1 Outline Intro CPP Boring stuff: Language basics: identifiers, data types, operators, type conversions, branching

More information

Computer Programming. Basic Control Flow - Loops. Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons

Computer Programming. Basic Control Flow - Loops. Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons Computer Programming Basic Control Flow - Loops Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons Objectives To learn about the three types of loops: while for do To avoid infinite

More information

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

Review: C++ Basic Concepts. Dr. Yingwu Zhu Review: C++ Basic Concepts Dr. Yingwu Zhu Outline C++ class declaration Constructor Overloading functions Overloading operators Destructor Redundant declaration A Real-World Example Question #1: How to

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

CS242 COMPUTER PROGRAMMING

CS242 COMPUTER PROGRAMMING CS242 COMPUTER PROGRAMMING I.Safa a Alawneh Variables Outline 2 Data Type C++ Built-in Data Types o o o o bool Data Type char Data Type int Data Type Floating-Point Data Types Variable Declaration Initializing

More information

IV. Stacks. A. Introduction 1. Consider the 4 problems on pp (1) Model the discard pile in a card game. (2) Model a railroad switching yard

IV. Stacks. A. Introduction 1. Consider the 4 problems on pp (1) Model the discard pile in a card game. (2) Model a railroad switching yard IV. Stacks 1 A. Introduction 1. Consider the problems on pp. 170-1 (1) Model the discard pile in a card game (2) Model a railroad switching yard (3) Parentheses checker () Calculate and display base-two

More information

Final exam. Final exam will be 12 problems, drop any 2. Cumulative up to and including week 14 (emphasis on weeks 9-14: classes & pointers)

Final exam. Final exam will be 12 problems, drop any 2. Cumulative up to and including week 14 (emphasis on weeks 9-14: classes & pointers) Review Final exam Final exam will be 12 problems, drop any 2 Cumulative up to and including week 14 (emphasis on weeks 9-14: classes & pointers) 2 hours exam time, so 12 min per problem (midterm 2 had

More information

Lab 14 SERACHI G Dr. John P. Abraham

Lab 14 SERACHI G Dr. John P. Abraham Lab 14 SERACHI G Dr. John P. Abraham We can only do a linear search on an unsorted array, but on a sorted array we can do a binary search. In this chapter we will study two different algorithms for searching,

More information

Homework 5. Yuji Shimojo CMSC 330. Instructor: Prof. Reginald Y. Haseltine

Homework 5. Yuji Shimojo CMSC 330. Instructor: Prof. Reginald Y. Haseltine Homework 5 Yuji Shimojo CMSC 330 Instructor: Prof. Reginald Y. Haseltine July 13, 2013 Question 1 Consider the following Java definition of a mutable string class. class MutableString private char[] chars

More information

INTRODUCTION TO PROGRAMMING

INTRODUCTION TO PROGRAMMING FIRST SEMESTER INTRODUCTION TO PROGRAMMING COMPUTER SIMULATION LAB DEPARTMENT OF ELECTRICAL ENGINEERING Prepared By: Checked By: Approved By Engr. Najeeb Saif Engr. M.Nasim Kha Dr.Noman Jafri Lecturer

More information

EP241 Computer Programming

EP241 Computer Programming EP241 Computer Programming Topic 2 Dr. Ahmet BİNGÜL Department of Engineering Physics University of Gaziantep Modifications by Dr. Andrew BEDDALL Department of Electric and Electronics Engineering Sep

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

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

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

Circle all of the following which would make sense as the function prototype. Student ID: Lab Section: This test is closed book, closed notes. Points for each question are shown inside [ ] brackets at the beginning of each question. You should assume that, for all quoted program

More information

University of Illinois at Urbana-Champaign Department of Computer Science. First Examination

University of Illinois at Urbana-Champaign Department of Computer Science. First Examination University of Illinois at Urbana-Champaign Department of Computer Science First Examination CS 225 Data Structures and Software Principles Spring 2007 7p-9p, Thursday, March 1 Name: NetID: Lab Section

More information

Discussion 1E. Jie(Jay) Wang Week 10 Dec.2

Discussion 1E. Jie(Jay) Wang Week 10 Dec.2 Discussion 1E Jie(Jay) Wang Week 10 Dec.2 Outline Dynamic memory allocation Class Final Review Dynamic Allocation of Memory Recall int len = 100; double arr[len]; // error! What if I need to compute the

More information

2 2

2 2 1 2 2 3 3 C:\Temp\Templates 4 5 Use This Main Program 6 # include "Utilities.hpp" # include "Student.hpp" Copy/Paste Main void MySwap (int Value1, int Value2); int main(int argc, char * argv[]) { int A

More information

University of Illinois at Urbana-Champaign Department of Computer Science. First Examination

University of Illinois at Urbana-Champaign Department of Computer Science. First Examination University of Illinois at Urbana-Champaign Department of Computer Science First Examination CS 225 Data Structures and Software Principles Spring 2007 7p-9p, Thursday, March 1 Name: NetID: Lab Section

More information

Topics. bool and string types input/output library functions comments memory allocation templates classes

Topics. bool and string types input/output library functions comments memory allocation templates classes C++ Primer C++ is a major extension of c. It is similar to Java. The lectures in this course use pseudo-code (not C++). The textbook contains C++. The labs involve C++ programming. This lecture covers

More information

CS304 Object Oriented Programming Final Term

CS304 Object Oriented Programming Final Term 1. Which of the following is the way to extract common behaviour and attributes from the given classes and make a separate class of those common behaviours and attributes? Generalization (pg 29) Sub-typing

More information

Introduction to C++ Systems Programming

Introduction to C++ Systems Programming Introduction to C++ Systems Programming Introduction to C++ Syntax differences between C and C++ A Simple C++ Example C++ Input/Output C++ Libraries C++ Header Files Another Simple C++ Example Inline Functions

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

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

Unit 1: Preliminaries Part 4: Introduction to the Standard Template Library

Unit 1: Preliminaries Part 4: Introduction to the Standard Template Library Unit 1: Preliminaries Part 4: Introduction to the Standard Template Library Engineering 4892: Data Structures Faculty of Engineering & Applied Science Memorial University of Newfoundland May 6, 2010 ENGI

More information

CSci 1113 Final. Name: Student ID:

CSci 1113 Final. Name: Student ID: CSci 1113 Final Name: Student ID: Instructions: Please pick and answer any 10 of the 12 problems for a total of 100 points. If you answer more than 10 problems, only the first 10 will be graded. The time

More information

C++ For Science and Engineering Lecture 12

C++ For Science and Engineering Lecture 12 C++ For Science and Engineering Lecture 12 John Chrispell Tulane University Monday September 20, 2010 Comparing C-Style strings Note the following listing dosn t do what you probably think it does (assuming

More information

III. Classes (Chap. 3)

III. Classes (Chap. 3) III. Classes III-1 III. Classes (Chap. 3) As we have seen, C++ data types can be classified as: Fundamental (or simple or scalar): A data object of one of these types is a single object. int, double, char,

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

CMSC 341 Lecture 6 Templates, Stacks & Queues. Based on slides by Shawn Lupoli & Katherine Gibson at UMBC

CMSC 341 Lecture 6 Templates, Stacks & Queues. Based on slides by Shawn Lupoli & Katherine Gibson at UMBC CMSC 341 Lecture 6 Templates, Stacks & Queues Based on slides by Shawn Lupoli & Katherine Gibson at UMBC Today s Topics Data types in C++ Overloading functions Templates How to implement them Possible

More information

A506 / C201 Computer Programming II Placement Exam Sample Questions. For each of the following, choose the most appropriate answer (2pts each).

A506 / C201 Computer Programming II Placement Exam Sample Questions. For each of the following, choose the most appropriate answer (2pts each). A506 / C201 Computer Programming II Placement Exam Sample Questions For each of the following, choose the most appropriate answer (2pts each). 1. Which of the following functions is causing a temporary

More information

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

REVIEW. The C++ Programming Language. CS 151 Review #2 REVIEW The C++ Programming Language Computer programming courses generally concentrate on program design that can be applied to any number of programming languages on the market. It is imperative, however,

More information

A class is a user-defined type. It is composed of built-in types, other user-defined types and

A class is a user-defined type. It is composed of built-in types, other user-defined types and Chapter 3 User-defined types 3.1 Classes A class is a user-defined type. It is composed of built-in types, other user-defined types and functions. The parts used to define the class are called members.

More information

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

Reference Parameters A reference parameter is an alias for its corresponding argument in the function call. Use the ampersand (&) to indicate that Reference Parameters There are two ways to pass arguments to functions: pass-by-value and pass-by-reference. pass-by-value A copy of the argument s value is made and passed to the called function. Changes

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

CE221 Programming in C++ Part 1 Introduction

CE221 Programming in C++ Part 1 Introduction CE221 Programming in C++ Part 1 Introduction 06/10/2017 CE221 Part 1 1 Module Schedule There are two lectures (Monday 13.00-13.50 and Tuesday 11.00-11.50) each week in the autumn term, and a 2-hour lab

More information

CPSC 427: Object-Oriented Programming

CPSC 427: Object-Oriented Programming CPSC 427: Object-Oriented Programming Michael J. Fischer Lecture 7 September 21, 2016 CPSC 427, Lecture 7 1/21 Brackets Example (continued) Storage Management CPSC 427, Lecture 7 2/21 Brackets Example

More information

COMP322 - Introduction to C++

COMP322 - Introduction to C++ COMP322 - Introduction to C++ Winter 2011 Lecture 2 - Language Basics Milena Scaccia School of Computer Science McGill University January 11, 2011 Course Web Tools Announcements, Lecture Notes, Assignments

More information

Engineering Problem Solving with C++, Etter/Ingber

Engineering Problem Solving with C++, Etter/Ingber Engineering Problem Solving with C++, Etter/Ingber Chapter 2 Simple C++ Programs C++, Second Edition, J. Ingber 1 Simple C++ Programs Program Structure Constants and Variables C++ Operators Standard Input

More information

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

PART I.   Part II Answer to all the questions 1. What is meant by a token? Name the token available in C++. Unit - III CHAPTER - 9 INTRODUCTION TO C++ Choose the correct answer. PART I 1. Who developed C++? (a) Charles Babbage (b) Bjarne Stroustrup (c) Bill Gates (d) Sundar Pichai 2. What was the original name

More information

This test is OPEN Textbook and CLOSED notes. The use of computing and/or communicating devices is NOT permitted.

This test is OPEN Textbook and CLOSED notes. The use of computing and/or communicating devices is NOT permitted. University of Toronto Faculty of Applied Science and Engineering ECE 244F PROGRAMMING FUNDAMENTALS Fall 2013 Midterm Test Examiners: T.S. Abdelrahman, V. Betz, M. Stumm and H. Timorabadi Duration: 110

More information

PIC 10A Objects/Classes

PIC 10A Objects/Classes PIC 10A Objects/Classes Ernest Ryu UCLA Mathematics Last edited: November 13, 2017 User-defined types In C++, we can define our own custom types. Object is synonymous to variable, and class is synonymous

More information

Object oriented programming

Object oriented programming Exercises 12 Version 1.0, 9 May, 2017 Table of Contents 1. Virtual destructor and example problems...................................... 1 1.1. Virtual destructor.......................................................

More information

Program template-smart-pointers-again.cc

Program template-smart-pointers-again.cc 1 // Illustrate the smart pointer approach using Templates 2 // George F. Riley, Georgia Tech, Spring 2012 3 // This is nearly identical to the earlier handout on smart pointers 4 // but uses a different

More information

CSCE 121 ENGR 112 List of Topics for Exam 1

CSCE 121 ENGR 112 List of Topics for Exam 1 List of Topics for Exam 1 If statements o How is an if statement constructed? o Does every if need an else? Looping o While loop! What does a while loop look like?! How do you ensure you will not have

More information