Solved

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

Posted on 2004-10-27
744 Views
Last Modified: 2012-05-05
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)
0
Question by:jxharding
    10 Comments
     
    LVL 13

    Expert Comment

    by:SteH
    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
     

    Author Comment

    by:jxharding
    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
     
    LVL 13

    Expert Comment

    by:SteH
    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
     

    Author Comment

    by:jxharding
    steH,  i'll attempt it after work, post tomorrow, like u say its the right way.
    thanks!
    0
     
    LVL 2

    Accepted Solution

    by:
    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
     

    Author Comment

    by:jxharding
    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
     

    Author Comment

    by:jxharding
    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
     
    LVL 13

    Expert Comment

    by:SteH
    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
     

    Author Comment

    by:jxharding
    very good explanation!
    thanks so much!,
    sorry for my screwed up attitude at start of post
    0
     

    Author Comment

    by:jxharding
    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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
    IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
    The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
    The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

    931 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now