Advertisement
Guest User

Untitled

a guest
Jul 24th, 2019
328
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.13 KB | None | 0 0
  1. Programming Final for CIS 17A C++
  2.  
  3. Addendum: Extra Credit (Choose several for no more than 10 pts)
  4. Alert me to any extra credit in the output of the problem.
  5. 1) 5 pts->Modify the toString() function in Problem 4 or 5 to return
  6. a string or for the most credit a char *;
  7. 2) 5 pts->Utilize problem 4 or 5 and Serialize the object to a
  8. binary file. Prove that it was done correctly.
  9. Get even more credit by serializing Problem 3.
  10. 3) 2.5 pts->Utilize problem 3 to overload the + operator. Then take
  11. 2 arrays and add them together from this class.
  12. 4) 2.5 pts->Convert Problem 1 by using templates in place of the
  13. character arrays.
  14. 5) 2.5 pts->Exception handling for Problems 4 and 5
  15. 6) 10 pts->Do problem 4 from the programming competition in folder, Use a class
  16. that can be called to perform the sort. You might want to look
  17. at problem 2 and maybe kill 2 birds with one stone! :)
  18. 7) 1 pt->Discuss any code that you would make better and
  19. support your reasoning.
  20. 8) 5 pts->Utilize regular expressions on all problems requiring inputs.
  21.  
  22. Note: The final is due on the canvas assignment post.
  23. The hints given for each problem are not such that you
  24. can code them directly but do provide you enough
  25. information for solving the problem.
  26.  
  27. Send the zipped files to my email address mark.lehr@rcc.edu with
  28. subject heading LastName, FirstName - Final - class section.
  29.  
  30.  
  31. I want one program that provides a menu for
  32. selecting the problem solution. When the problem
  33. is done executing then I want it to return to
  34. the main menu. Exiting the program should be one
  35. of the options. Note: There will be multiple
  36. files since classes can be utilized for all problems.
  37.  
  38. Problem #1 (Random Sequence)
  39. Create a class that
  40. returns a random number from the following set,
  41. {18,33,56,78,125}. Loop 100,000 times with this
  42. procedure and print the frequency of each of the
  43. 5 numbers obtained. The following is the
  44. specification for the class.
  45.  
  46. Specification
  47.  
  48. class Prob1Random
  49. {
  50. private:
  51. char *set; //The set of numbers to draw random numbers from
  52. char nset; //The number of variables in the sequence
  53. int *freq; //Frequency of all the random numbers returned
  54. int numRand; //The total number of times the random number function is called
  55. public:
  56. Prob1Random(const char,const char *); //Constructor
  57. ~Prob1Random(void); //Destructor
  58. char randFromSet(void); //Returns a random number from the set
  59. int *getFreq(void) const; //Returns the frequency histogram
  60. char *getSet(void) const; //Returns the set used
  61. int getNumRand(void) const; //Gets the number of times randFromSet has
  62. //been called
  63. };
  64.  
  65. Driver program to return a random sequence
  66. char n=5;
  67. char rndseq[]={18,33,56,79,125};
  68. int ntimes=100000;
  69. Prob1Random a(n,rndseq);
  70. for(int i=1;i<=ntimes;i++)
  71. {
  72. a.randFromSet();
  73. }
  74. int *x=a.getFreq();
  75. char *y=a.getSet();
  76. for(int i=0;i<n;i++)
  77. {
  78. cout<<int(y[i])<<" occured "<<x[i]<<" times"<<endl;
  79. }
  80. cout<<"The total number of random numbers is "<<a.getNumRand()<<endl;
  81.  
  82. Sample Output
  83.  
  84. 18 occured 20045 times
  85. 33 occured 19952 times
  86. 56 occured 20035 times
  87. 79 occured 20039 times
  88. 125 occured 19929 times
  89. The total number of random numbers is 100000
  90.  
  91. Note: Your results are not expected to be exactly the
  92. same! After all these are pseudo-random number sequences
  93. with different seeds.
  94.  
  95.  
  96.  
  97. Problem #2 (All Kinds of Sorting)
  98. Sort a single column array and/or sort a 2 dimensional array
  99. of characters given any column. Here is what I used as my
  100. template specification.
  101.  
  102. //This class sorts arrays either ascending or descending
  103. template<class T>
  104. class Prob2Sort
  105. {
  106. private:
  107. int *index; //Index that is utilized in the sort
  108. public:
  109. Prob2Sort(){index=NULL;}; //Constructor
  110. ~Prob2Sort(){delete []index;}; //Destructor
  111. T * sortArray(const T*,int,bool); //Sorts a single column array
  112. T * sortArray(const T*,int,int,int,bool); //Sorts a 2 dimensional array represented as a 1 dim array
  113. };
  114.  
  115. Driver program for the above class. Create your own file to read.
  116.  
  117. cout<<"The start of Problem 2, the sorting problem"<<endl;
  118. Prob2Sort<char> rc;
  119. bool ascending=true;
  120. ifstream infile;
  121. infile.open("Problem2.txt",ios::in);
  122. char *ch2=new char[10*16];
  123. char *ch2p=ch2;
  124. while(infile.get(*ch2)){cout<<*ch2;ch2++;}
  125. infile.close();
  126. cout<<endl;
  127. cout<<"Sorting on which column"<<endl;
  128. int column;
  129. cin>>column;
  130. char *zc=rc.sortArray(ch2p,10,16,column,ascending);
  131. for(int i=0;i<10;i++)
  132. {
  133. for(int j=0;j<16;j++)
  134. {
  135. cout<<zc[i*16+j];
  136. }
  137. }
  138. delete []zc;
  139. cout<<endl;
  140.  
  141. The output from this problem.
  142.  
  143. The start of Problem 2, the sorting problem
  144. Lbekoeddhoffbmg
  145. Lkcmggjcdhhglif
  146. Cgldjhcekjigcdd
  147. Cgldjhcekjigcdo
  148. Bffmdbkcenlafjk
  149. Fggdijijegfblln
  150. Jjlncnimjldfedj
  151. Amliglfohajcdmn
  152. Balgfcaelhfkgea
  153. Kmlhmhcddfoeilc
  154.  
  155. Sorting on column 15
  156. Cgldjhcekjigcdo
  157. Fggdijijegfblln
  158. Amliglfohajcdmn
  159. Bffmdbkcenlafjk
  160. Jjlncnimjldfedj
  161. Lbekoeddhoffbmg
  162. Lkcmggjcdhhglif
  163. Cgldjhcekjigcdd
  164. Kmlhmhcddfoeilc
  165. Balgfcaelhfkgea
  166.  
  167.  
  168.  
  169. Problem #3 (Spreadsheet Stuff)
  170. Class Specifications
  171.  
  172. template<class T>
  173. class Prob3Table
  174. {
  175. protected:
  176. int rows; //Number of rows in the table
  177. int cols; //Number of cols in the table
  178. T *rowSum; //RowSum array
  179. T *colSum; //ColSum array
  180. T *table; //Table array
  181. T grandTotal; //Grand total
  182. void calcTable(void); //Calculate all the sums
  183. public:
  184. Prob3Table(char *,int,int); //Constructor then Destructor
  185. ~Prob3Table(){delete [] table;delete [] rowSum;delete [] colSum;};
  186. const T *getTable(void){return table;};
  187. const T *getRowSum(void){return rowSum;};
  188. const T *getColSum(void){return colSum;};
  189. T getGrandTotal(void){return grandTotal;};
  190. };
  191.  
  192. template<class T>
  193. class Prob3TableInherited:public Prob3Table<T>
  194. {
  195. protected:
  196. T *augTable; //Augmented Table with sums
  197. public:
  198. Prob3TableInherited(char *,int,int); //Constructor
  199. ~Prob3TableInherited(){delete [] augTable;}; //Destructor
  200. const T *getAugTable(void){return augTable;};
  201. };
  202.  
  203. Driver code
  204. cout<<"Entering problem number 3"<<endl;
  205. int rows=5;
  206. int cols=6;
  207. Prob3TableInherited<int> tab("Problem3.txt",rows,cols);
  208. const int *naugT=tab.getTable();
  209. for(int i=0;i<rows;i++)
  210. {
  211. for(int j=0;j<cols;j++)
  212. {
  213. cout<<naugT[i*cols+j]<<" ";
  214. }
  215. cout<<endl;
  216. }
  217. cout<<endl;
  218. const int *augT=tab.getAugTable();
  219. for(int i=0;i<=rows;i++)
  220. {
  221. for(int j=0;j<=cols;j++)
  222. {
  223. cout<<augT[i*(cols+1)+j]<<" ";
  224. }
  225. cout<<endl;
  226. }
  227.  
  228.  
  229. Example Input Table
  230. 101 101 102 103 104 105
  231. 106 107 108 109 110 111
  232. 112 113 114 115 116 117
  233. 118 119 120 121 122 123
  234. 124 125 126 127 128 128
  235. Example Output Table with rows summed,
  236. columns summed and the grand total printed.
  237. 101 101 102 103 104 105 616
  238. 106 107 108 109 110 111 651
  239. 112 113 114 115 116 117 687
  240. 118 119 120 121 122 123 723
  241. 124 125 126 127 128 128 758
  242. 561 565 570 575 580 584 3435
  243.  
  244.  
  245.  
  246. Problem #4 (Savings Account Class)
  247. Create a SavingsAccount class with the following
  248. specification
  249. public:
  250. SavingsAccount(float); //Constructor
  251. void Transaction(float); //Procedure
  252. float Total(float=0,int=0); //Savings Procedure
  253. float TotalRecursive(float=0,int=0);
  254. void toString(); //Output Properties
  255. private:
  256. float Withdraw(float); //Utility Procedure
  257. float Deposit(float); //Utility Procedure
  258. float Balance; //Property
  259. int FreqWithDraw; //Property
  260. int FreqDeposit; //Property
  261.  
  262. 1) The constructor initilizes the balance if greater than
  263. 0 and sets the other properties to 0.
  264. 2) If the transaction is greater than 0 then a Deposit is
  265. made else a Withdraw is made.
  266. 3) The balance is increased with a deposit but decreased
  267. if a Withdrawal. This assumes the Withdrawal is less than
  268. the balance. Can't have a negative balance. Tell the user
  269. that he is trying to make a withdrawal that exceeds his
  270. balance.
  271. 4) When a WithDrawal is made increment FreqWithDraw else
  272. if a Deposit is made increment FreqDeposit.
  273. 5) The toString procedure outputs all properties.
  274. 6) The total procedure tells you how much you will have in
  275. savings given the interest rate and the amount of time.
  276. Total(float savint,int time) returns Balance*(1+savint)^time.
  277. Utilize a for loop for this calculation.
  278. 7) See if you can write a recursive procedure that does
  279. the same thing as 6). Call it TotalRecursive.
  280. 8) Think of what follows as pseudocode. The random number
  281. generator below produces a number between 0 and 32,767. If
  282. you fashion a random number that will do the same then you
  283. will get positive and negative transactions (-500,500).
  284. The output simply calculates the current balance with a 10
  285. percent interest rate and 7 years worth of compounding.
  286. Also, you tried to start out with a negative balance which
  287. should have been initialized to 0.
  288.  
  289. SavingsAccount mine(-300);
  290.  
  291. for(int i=1;i<=10;i++)
  292. {
  293. mine.Transaction((float)(rand()%500)*(rand()%3-1));
  294. }
  295. mine.toString();
  296. cout<<"Balance after 7 years given 10% interest = "
  297. <<mine.Total((float)(0.10),7)<<endl;
  298. cout<<"Balance after 7 years given 10% interest = "
  299. <<mine.TotalRecursive((float)(0.10),7)
  300. <<" Recursive Calculation "<<endl;
  301.  
  302. Example Output:
  303. WithDraw not Allowed
  304. WithDraw not Allowed
  305. Balance=1056
  306. WithDraws=4
  307. Deposit=5
  308. Balance after 7 years given 10% interest = 2057.85
  309. Balance after 7 years given 10% interest = 2057.85 Recursive Calculation
  310.  
  311.  
  312. Problem #5 (Employee Class)
  313. Create an Employee class with the following
  314. specification
  315. public:
  316. Employee(char[],char[],float); //Constructor
  317. float CalculatePay(float,int); //Procedure
  318. float getGrossPay(float,int); //Procedure
  319. float getNetPay(float); //Procedure
  320. void toString(); //Procedure
  321. int setHoursWorked(int); //Procedure
  322. float setHourlyRate(float); //Procedure
  323. private:
  324. double Tax(float); //Utility Procedure
  325. char MyName[20]; //Property
  326. char JobTitle[20]; //Property
  327. float HourlyRate; //Property
  328. int HoursWorked; //Property
  329. float GrossPay; //Property
  330. float NetPay; //Property
  331.  
  332. 1) The constructor inputs the Name, Job Title and Hourly rate of
  333. the employee.
  334. 2) All other properties are initialized to zero in the constructor.
  335. 3) The Tax utility routine calculates the tax based on
  336. .1 for GrossPay < 500,
  337. .2 for GrossPay<1000 but greater than 500, and
  338. .3 for anything above 1000.
  339. 4) The Set procedures simply write and return the obvious
  340. properties.
  341. 5) The toString procedure prints all the properties.
  342. 6) The getGrossPay procedure calculates straight time for hours
  343. worked < 40, time and 1/2 for hours worked < 50 but greater than
  344. 40, double time for all hours above 50. The inputs to this
  345. routine are the hourly rate and the hours worked.
  346. 7) The net pay routine returns the gross pay subtracting
  347. off any taxes. Realize that the Tax procedure is utilized
  348. but these taxes are progressive rates for income above the
  349. cutoff limit as described in 3). Taxes are calculated for
  350. 10% below 500, 20% for amounts between 500 and 1000 and
  351. finally anything above 1000 is calculated at the 30% rate.
  352. 8) The Hoursworked must be > 0 and < 84
  353. 9) The Hourlyrate must be > 0 and < 200
  354. 10) The calculate pay routine returns the net pay.
  355. return getNetPay(getGrossPay(setHourlyRate(x)
  356. ,setHoursWorked(y)));
  357.  
  358. Example Input
  359. Employee Mark("Mark","Boss",215.50);
  360. Mark.setHoursWorked(-3);
  361. Mark.toString();
  362. Mark.CalculatePay(Mark.setHourlyRate(20.0),
  363. Mark.setHoursWorked(25));
  364. Mark.toString();
  365. Mark.CalculatePay(Mark.setHourlyRate(40.0),
  366. Mark.setHoursWorked(25));
  367. Mark.toString();
  368. Mark.CalculatePay(Mark.setHourlyRate(60.0),
  369. Mark.setHoursWorked(25));
  370. Mark.toString();
  371.  
  372. Employee Mary("Mary","VP",50.0);
  373. Mary.toString();
  374. Mary.CalculatePay(Mary.setHourlyRate(50.0),
  375. Mary.setHoursWorked(40));
  376. Mary.toString();
  377. Mary.CalculatePay(Mary.setHourlyRate(50.0),
  378. Mary.setHoursWorked(50));
  379. Mary.toString();
  380. Mary.CalculatePay(Mary.setHourlyRate(50.0),
  381. Mary.setHoursWorked(60));
  382. Mary.toString();
  383.  
  384. Example Output
  385. Unacceptable Hourly Rate
  386. Unacceptable Hours Worked
  387. Name = Mark Job Title = Boss
  388. Hourly Rate = 0 Hours Worked = 0 Gross Pay = 0 Net Pay = 0
  389. Name = Mark Job Title = Boss
  390. Hourly Rate = 20 Hours Worked = 25 Gross Pay = 500 Net Pay = 450
  391. Name = Mark Job Title = Boss
  392. Hourly Rate = 40 Hours Worked = 25 Gross Pay = 1000 Net Pay = 850
  393. Name = Mark Job Title = Boss
  394. Hourly Rate = 60 Hours Worked = 25 Gross Pay = 1500 Net Pay = 1200
  395. Name = Mary Job Title = VP
  396. Hourly Rate = 50 Hours Worked = 0 Gross Pay = 0 Net Pay = 0
  397. Name = Mary Job Title = VP
  398. Hourly Rate = 50 Hours Worked = 40 Gross Pay = 2000 Net Pay = 1550
  399. Name = Mary Job Title = VP
  400. Hourly Rate = 50 Hours Worked = 50 Gross Pay = 2750 Net Pay = 2075
  401. Name = Mary Job Title = VP
  402. Hourly Rate = 50 Hours Worked = 60 Gross Pay = 3750 Net Pay = 2775
  403.  
  404.  
  405. Problem #6 (Conversions)
  406.  
  407. Given the following base 10 decimals
  408. a)3.75
  409. b)0.7
  410. c)89.9
  411. 1) Convert to binary, octal and hex, then
  412. 2) Convert to Hex float with first 24 bits representing the
  413. signed fraction and the last 8 bits representing the
  414. signed exponent. Scaled as 0.FRACTION x 2^EXPONENT
  415. 3) convert a) to scaled integer binary 1 unsinged byte maxium bits,
  416. convert b) to scaled integer binary 2 unsinged byte maxium bits,
  417. convert c) to scaled integer binary 3 unsinged byte maxium bits,
  418. 4) multiply 3) by 1 byte value 7 then shift back to integer and output the result
  419. 5) Then convert using IEEE 754 format
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement