jxharding
asked on
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)
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)
ASKER
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.
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.
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.
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.
ASKER
steH, i'll attempt it after work, post tomorrow, like u say its the right way.
thanks!
thanks!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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?
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?
ASKER
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!
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!
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.
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.
ASKER
very good explanation!
thanks so much!,
sorry for my screwed up attitude at start of post
thanks so much!,
sorry for my screwed up attitude at start of post
ASKER
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
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
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