Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit


CSC 17a C++:Objects

Fall 2021

Final

The Midterm contains 9 pages and 7 questions. Total points possible are 100.

Submit the Final to the canvas assignment marked Final. The solutions/programs should be in a folder marked final and the contents zipped. Use Netbeans, delete the build and dist folders before zipping. Do the best you can and turn in as much as you can.

I just want one program for the solution to the problems that follow. The program should prompt the user for which problem solution to display. Use a do-while and switch construct like the included menu program.

Develop each in a separate project. Then combine into one project when done. You are to include each project as well as the combined menu project when submitting your final.

Addendum:  Extra  Credit    (Choose  several  for no more  than  10 pts) Alert me  to  any  extra  credit  in  the  output  of  the problem.

1)    5     pts->Modify  the  toString()  function  in  Problem  4  or  5  to  return  a string  or  for  the most  credit  a  char  *;

2)    5     pts->Utilize problem  4  or  5  and  Serialize  the  object  to  a binary

file.   Prove  that  it was  done  correctly.

Get  even more  credit by  serializing  Problem  3.

3)    2.5 pts->Utilize problem  3  to  overload  the  +  operator.   Then  take

2  arrays  and  add  them  together  from  this  class.

4)    2.5 pts->Convert  Problem  1 by using  templates  in place  of  the  character arrays.

5)    2.5 pts->Exception handling  for  Problems  4  and  5

6)    10   pts->Do problem  4  from  the programming  competition  in  folder, Use  a class  that  can be  called  to perform  the  sort.   You might want  to      look  at problem  2  and maybe  kill  2 birds with  one  stone!  :)

7)    1     pt->Discuss  any  code  that  you would make better  and  support  your reasoning.

8)    5     pts->Utilize  regular  expressions  on  all problems  requiring  inputs.



1.  (15 points) Problem 1 (Random Sequence) Create a class that returns a random number from the following set, 29,34,57,89,126. Loop 100,000 times with this procedure and print the frequency of each of the 5 numbers obtained.  The following is the specification for the class.

Specification




class  Prob1Random{ private:

char  *set;




//The  set  of numbers  to  draw  random numbers  from





char   nset;      int    *freq;      int     numRand;



//The number  of  variables  in  the  sequence

//Frequency  of  all  the  random numbers  returned

//The  total number  of  times  the  random number

//function  is  called


public:

Prob1Random(const  char,const  char  *);//Constructor

~Prob1Random(void);                                //Destructor

char  randFromSet(void);                         //Returns  a  random number  from  the  set int  *getFreq(void)  const;                     //Returns  the  frequency histogram         char  *getSet(void)  const;                     //Returns  the  set used                               int  getNumRand(void)  const;                 //Gets  the number  of  times  randFromSet

//has been  called

};


Driver program  to  return  a  random  sequence

char n=5;

char  rndseq[]={29,34,57,89,126};

int ntimes=100000;

Prob1Random  a(n,rndseq);

for(int  i=1;i<=ntimes;i++){

a.randFromSet();

}

int  *x=a.getFreq();

char  *y=a.getSet();

for(int  i=0;i

cout<

}

cout<<"The total number of random numbers is "<

29    occurred  20045  times

34    occurred  19952  times

57    occurred  20035  times

89    occurred  20039  times

126    occurred  19929  times

The total number of random numbers is 100000

Note: Your results are not expected to be exactly the same! After all these are pseudo- random number sequences with different seeds.

2.  (15 points) Problem 2 (All Kinds of Sorting) Sort a single column array and/or sort a 2 dimensional array of characters given any column. Here is what I used as my template specification.



//This  class  sorts  arrays  either  ascending  or  descending

template

class  Prob2Sort{



private:

int  *index;


public:

Prob2Sort(){index=NULL;};               ~Prob2Sort(){delete  []index;};      T  *  sortArray(const  T*,int,bool);



//Index  that  is utilized

//in  the  sort


//Constructor

//Destructor

//Sorts  a  single  column  array




T  *  sortArray(const  T*,int,int,int,bool);//Sorts  a  2  dimensional

//represented  as  a  1  dim };

Driver program  for  the  above  class.    Create  your  own  file  to  read.


cout<<"The start of Problem 2, the sorting problem"<  rc;

bool  ascending=true;

ifstream  infile;

infile.open ("Problem2.txt",ios::in);

char  *ch2=new  char[10*16];

char  *ch2p=ch2;

while(infile.get(*ch2)){cout<<*ch2;ch2++;}

infile.close();

cout<

cout<<"Sorting on which column"<

int  column;

cin>>column;

char  *zc=rc.sortArray(ch2p,10,16,column,ascending);

for(int  i=0;i<10;i++){

for(int  j=0;j<16;j++){

cout<

}

}

delete  []zc;

cout<

The output from this problem.


The  start  of  Problem  2,  the  sorting problem

Lbekoeddhoffbmg

Lkcmggjcdhhglif

Cgldjhcekjigcdd

Cgldjhcekjigcdo


array array



Bffmdbkcenlafjk

Fggdijijegfblln

Jjlncnimjldfedj

Amliglfohajcdmn

Balgfcaelhfkgea

Kmlhmhcddfoeilc


Sorting  on  column  15

Cgldjhcekjigcdo

Fggdijijegfblln

Amliglfohajcdmn

Bffmdbkcenlafjk

Jjlncnimjldfedj

Lbekoeddhoffbmg

Lkcmggjcdhhglif

Cgldjhcekjigcdd

Kmlhmhcddfoeilc

Balgfcaelhfkgea



3.  (15 points) Problem 3 (Spreadsheet Stuff) Class Specifications


template

class  Prob3Table{



protected:

int  rows;

int  cols;


T

T

T

T

*rowSum; *colSum; *table; grandTotal;

void  calcTable(void);

public:

Prob3Table(char  *,int,int);



//Number  of  rows  in  the  table //Number  of  cols  in  the  table //RowSum  array

//ColSum  array

//Table  array

//Grand  total

//Calculate  all  the  sums


//Constructor  then  Destructor


~Prob3Table(){delete  []  table;delete  []  rowSum;delete  []  colSum;}; const  T  *getTable(void){return  table;};

const  T  *getRowSum(void){return  rowSum;};

const  T  *getColSum(void){return  colSum;};

T  getGrandTotal(void){return  grandTotal;};

};


template

class  Prob3TableInherited:public  Prob3Table{

protected:

T  *augTable;                                                                 //Augmented  Table with  sums



public:

Prob3TableInherited(char  *,int,int);                   //Constructor ~Prob3TableInherited(){delete  []  augTable;};    //Destructor  const  T  *getAugTable(void){return  augTable;};

};


Driver  code

cout<<"Entering problem number 3"<

int  rows=5;

int  cols=6;

Prob3TableInherited  tab("Problem3.txt",rows,cols);

const  int  *naugT=tab.getTable();

for(int  i=0;i

for(int  j=0;j

cout<

}

cout<

}

cout<

const  int  *augT=tab.getAugTable();

for(int  i=0;i<=rows;i++){

for(int  j=0;j<=cols;j++){

cout<

}

cout<

}




Example

Input

Table

101

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

Example  Output  Table with  rows  summed,  columns  summed,  and  the  grand  total printed.


101

101

102

103

104

105

616

106

107

108

109

110

111

651

112

113

114

115

116

117

687

118

119

120

121

122

123

723

124

125

126

127

128

129

759

561

565

570

575

580

585

3436


4.  (15 points) Problem 4 (Savings Account Class) Create a SavingsAccount class with the following specification





public:

SavingsAccount(float);         void   Transaction(float);    float  Total(float=0,int=0);



//Constructor

//Procedure

//Savings  Procedure




float void

private:  float float float int    int


TotalRecursive(float=0,int=0); toString();

Withdraw(float);

Deposit(float);

Balance;

FreqWithDraw;

FreqDeposit;



//Output  Properties


//Utility  Procedure

//Utility  Procedure

//Property

//Property

//Property



1)   The  constructor  initilizes  the balance  if  greater  than  0  and sets  the  other properties  to  0.

2)    If  the  transaction  is  greater  than  0  then  a  Deposit  is made else  a Withdraw  is made.

3)   The balance  is  increased with  a  deposit but  decreased  if  a  Withdrawal.  This  assumes  the Withdrawal  is  less  than  the      balance.  Can’t have  a negative balance.   Tell  the user  that he  is  trying  to make  a withdrawal  that  exceeds his balance.

4)   When  a WithDrawal  is made  increment  FreqWithDraw  else  if  a Deposit  is made  increment  FreqDeposit.

5)   The  toString procedure  outputs  all properties.

6)   The  total procedure  tells  you how much  you will have  in

savings  given  the  interest  rate  and  the  amount  of  time.             Total(float  savint,int  time)   returns  Balance*(1+savint)^time. Utilize  a  for  loop  for  this  calculation.

7)    See  if  you  can write  a  recursive procedure  that  does  the  same thing  as  6).  Call  it  TotalRecursive.

8)   Think  of what  follows  as pseudocode.   The  random number        generator below produces  a number between  0  and  32,767.    If you  fashion  a  random number  that will  do  the  same  then  you  will  get positive  and negative  transactions  (-500,500).        The  output  simply  calculates  the  current balance with  a  10  percent  interest  rate  and  7  years worth  of  compounding.        Also,  you  tried  to  start  out with  a negative balance which  should have been  initialized  to  0.


SavingsAccount mine(-300);


for(int  i=1;i<=10;i++){

mine.Transaction((float)(rand()%500)*(rand()%3-1)); }

mine.toString();




cout<<"Balance after 7 years given 10% interest = "

<

cout<<"Balance after 7 years given 10% interest = "

<

<<" Recursive Calculation "<


Example  Output:

WithDraw not  Allowed

WithDraw not  Allowed

Balance=1056

WithDraws=4

Deposit=5

Balance  after  7  years  given  10%  interest  =  2057.85

Balance  after  7  years  given  10%  interest  =  2057.85  Recursive  Calculation