Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


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

Posted on 2004-10-27
Medium Priority
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
base  = base

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
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)
Question by:jxharding
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 3
LVL 13

Expert Comment

ID: 12420745
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

Author Comment

ID: 12420863
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.
LVL 13

Expert Comment

ID: 12420904
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.
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 12421068
steH,  i'll attempt it after work, post tomorrow, like u say its the right way.

Accepted Solution

jonnin earned 160 total points
ID: 12422361
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)
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.


Author Comment

ID: 12925229
im writing the exam again
concerning nr 2

string removedSubstring(string SentenceP){

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

  do while Pos <> -1 {
return SentenceP;

how about that?

Author Comment

ID: 12969581
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;
            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!
LVL 13

Expert Comment

ID: 12970141
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.

Author Comment

ID: 12970342
very good explanation!
thanks so much!,
sorry for my screwed up attitude at start of post

Author Comment

ID: 12971823
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

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
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 technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

610 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