Unsure how to start a program...

Here is the code description:

Consider the following function,

f(n) = 3n + 1, if n is odd ; and
f(n) = n/2, if n is even

Use a random number generator to generate a number between 1 and 100 and pass it through
the above function. The function keeps calling itself recursively until the value of n
becomes 1.
Simulate the above operation for 50 times. Each time make a note of how many times
the odd condition of the function was executed and how many times the even condition
of the function was executed.
Create three files, that will save the following information for each 50 simulations
of the above function,
1. the value of 'n'
2. the number of odd operations performed
3. the number of even operations performed



--------------------------------

I am unsure exactly what this is wanting- what it should do, how to start...

Any help would be much appreciated! Thank you!
1
2
3
4
5
if(n%2==0)
//do even operation
else
//do odd operation
Here:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <windows.h>
using namespace std;
const int a=1;
int output=2;
int foo;
int main()
{
srand(time(NULL));
for(;output>1;foo++){
int n=rand()%100+1;
if(n%2==0)
output=3*n+1;
else
output=n/2;
cout<<output<<endl;
}
cout<<endl<<endl<<"Number of runs is "<<foo<<endl;
return 0;
}
@jasonwynn10, I ran the code, however it seems to me like it is just generating a bunch of random numbers until it hits 1, and then it stops and tells how many times it ran. I think it is supposed to generate a random number, decide if it is even or odd, run the respective function on it, and then keep doing that until it finally gets to 1. Then it should do that whole process again 50 times. After that, it needs to tell the user, of the 50 times it ran, show the values of n, tell how many times an even function was performed and tell how many times an odd function was performed.

For example:

#
#
#
#
#
#
1

Odd functions performed: _____
Even functions performed: _____

If you have any ideas on how we can tinker with the code you posted, let me know; and thanks again for the help, I really appreciate it!
I only started the program. I didn't complete it. Build off of what I have given you.
Last edited on
Ahh! That makes much more sense! Thanks so much!
no problem. If you need any more help, just post it here.
Topic archived. No new replies allowed.