2x easy first year c++ q's :1) write recursive function 2) remove string from string

hi, i had these 2 q's in a question paper, and i have no idea how to approach them.
i did try it out though , but i think it'd be a waste to display it here. please help, im rewriting the exam!
i am a pleb at c++ and this is my first c++ post

Q1                                                                                  exponent  
Write a recursive function "Power(base, exponent)" that when invoked returns "BASE ".
For example:
Power(3,4) = 3 *3 *3 *3. Assume that "Exponent" is an INteger greater that or equal to 1.
hint :
The recursion step would use the relationship
--------------------------------------------
    Exponent                Exponent -1
Base          =  Base  . Base          
--------------------------------------------

and the terminatiing condition occurs when exponent is = 1 because
--------------------------------------------
    1
base  = base
--------------------------------------------




Q2
I just need to write the body of the function.
A sentence is being sent to function "removedSubstring" where all the occurences of the substring
"es"
should be removed and then the changed sentence should be returned from the main function.
if for e.b. the sentence
"The Lionesses saw the doves and left the pieces in the houses!"
is sent to the function via "SentenceP" in the parameter list, the sentence
"The Lionss saw the dov and left the piec in the houses!"
should be returned.
Function header:
string removedSubstring(string SentenceP)
jxhardingAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

SteHCommented:
Since this is homework like you won't get the solutions posted. Experts can guide to find the solution yourself, or if you have done something they can tell where to remove errors from that.

Each recursive function needs a check if finished and return some value or else call itself with modified parameters. Together with the Q this should be a good starting point.

Q2 is the example correct? I doubt.
Look at string::erase and string::find
0
jxhardingAuthor Commented:
i expected someone to do the homework chirp.

Q2: this should be the right string that i should have posted.
"The Lionss saw the dov and left the piec in the hous!"

so there are no recursive functions in the second question then?

i am still lost with Q1. for darn sake,  i had the give the c++ book (mcgraw) back.
0
SteHCommented:
Q2: Why not. You can either loop through all instances or do this in a recurcision. In any case you need to find where "es" occurs and then remove it from the string.

Before giving you more help on Q1 I want some input from you. I can write that function down but this won't help you learning. You then just need to memorize the solution. And EE rules forbid this. So, even if you think your approach is not good post it and you get a lot of hints how to improve or where errors are.

And finally you will feel much better if YOU found the solution yourself.
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

jxhardingAuthor Commented:
steH,  i'll attempt it after work, post tomorrow, like u say its the right way.
thanks!
0
jonninCommented:
lets do a different recursive problem.. the famous factorial...

int fact( int n)
{
   if( n > 1)
   return (fact(n-1)*n);

   return 1;
}

so for 5,
you get
5* fact(4)
becomes
5*4*fact(3)
becomes
5*4*3*fact(2)
becomes
5*4*3*2*fact(1)
but fact(1) is 1 so it stops recursing and
you get 5*4*3*2*1 = 120...


now, you should be able to do the first problem from this example.


0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jxhardingAuthor Commented:
im writing the exam again
concerning nr 2

string removedSubstring(string SentenceP){

  int Pos = 0;
  int Pos = SentenceP.Find("es")  ;

  do while Pos <> -1 {
        SentenceP.Erase(Pos,Pos+1);
        removedSubstring(SentenceP);
  }
 
return SentenceP;
}

how about that?
0
jxhardingAuthor Commented:
please guys, just help me with this 1q: PLEASE  PLEASE PLEASE
hi, this is the q:
Company X has bday parties for 15 children at a time.
Kids may choose burgers (choose 1)  or pizza(2).
management requires c++ program that wil read order of 15 choces of codes.
write bodo of function getOrder which reads the orders and stores them in array.
the function should return total nr of burgers and  total of pizzas.
use the  function:  (dont validate input)
void getOrder(int OrderArray[], int & NrBurger, int & NrPizza)



this is my attempt:
#include <iostream>
using namespace std;

void getOrder(OrderArray[],  NrBurger,  NrPizza)
  {
  int Choice = 0;
   for (int i = 0; i < 14; ++i){
      cout << "Enter 1 or 0 : " << endl;
      cin >> Choice;
      if (Choice == 1) {
            NrBurger += 1;
                  }
            else
            NrPizza +=1;
           
  OrderArray[i] = Choice;
   }
  }

int main() {
  int OrderArray[14];
  int  NrBurger = 0, NrPizza = 0;
   
  getOrder(OrderArray, &NrBurger, &NrPizza);
  return 0;
}


 but i get about 5 errors , e.g i dont know how to pass an array to a function
please help , im rewriting the exam in 2 days!
thanks!
0
SteHCommented:
If you only want to change the content of the array its OK like you are doing. The address will be copied to the function and where it points to will be filled. But your array is one element short with 14 elements and 15 orders as your number of orders is (<14).

Next error:

The prototype for getOrder uses two references to int's and you change this in the definition to int values:
void getOrder(int OrderArray[], int & NrBurger, int & NrPizza)  // this is the prototype where NrBurger and NrPizza are references!
void getOrder(OrderArray[],  NrBurger,  NrPizza)                    // and here they get values. In the function definition you need
                                                                                          // the type of those variable as well.

and when you call the function you pass the addresses of the vars which the function body takes as values. And it will change these values. The latter wont harm since the addresses are put into temporary variables which get destroyed when leaving the function.
  getOrder(OrderArray, &NrBurger, &NrPizza);                      

So to get it correct:
void getOrder(int OrderArray[], int & NrBurger, int & NrPizza)
{
   // function code
}

And call it like it main like
 getOrder(OrderArray, NrBurger, NrPizza);                      

Be aware that & has two meanings:
&a  is the address of operator.
int & b     is a variable declaration of type reference to int.
0
jxhardingAuthor Commented:
very good explanation!
thanks so much!,
sorry for my screwed up attitude at start of post
0
jxhardingAuthor Commented:
hi, im trying that this is the last q, cause i know i already received way more than what i bargained for.
if no reply, then i understand:

in a recent exam  ( which im rewriting) there was a class Dog

dog had an enum
DogMF {Male, Female}

and in the Public :
DogMF mf; //dog's male or female status

now the question is
that i needed to make a auxilary function swopGender that changed the
value of data member around. , e.g if Male then Female

how to do this?

it had to be done in the class
                                    ??
void Dog::swopGender(mf) {
// i dont have the foggiest so please dont laugh
if (mf == 0){
    mf == 1;
} else mf == 0;
}

how about that? this was 5 marks in the exam
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.