GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 11-Apr-2007, 21:42
allenfanwenyuan allenfanwenyuan is offline
Junior Member
 
Join Date: Mar 2007
Posts: 38
allenfanwenyuan is an unknown quantity at this point

Problems about simple tricked loop question


This is a part of large question :

5.The result of the transformation f (explained in the following) applied month times in a row to the year. The transformation f sends a positive integer n to n/2 if n is even and to 3n+1 if n is odd. So if the year is 2007, we see that f(2007) = 6022. If the month is 1 we stop there. Otherwise we apply f again to 6022. We see that f(6022) = 3011 and we stop if the month is 2. And so on. If the month is 5, the value generated should be 13552, since f(3011) = 9034, f(9034) = 4517 and f(4517) = 13552.

I wrote a function for this question but it seems can not generate expected output, anyone can help me to work it out ,my code is following:

CPP / C++ / C Code:
#include<iostream>
using namespace std;

int transyear(int year,int month);

int main()
{
   int day, month, year;
   cout << "Enter a date (dd mm yyyy): " << flush;  
   cin  >> day >> month >> year; 
   cout<<transyear(year,month)<<endl;
   system("pause");
   return 0;
}



int transyear(int month,int year)
{
    int tran=0;
    for(int i=1;i<=month;i++)
    {
    if(year%2==0)
    tran=year/2;
    else
    tran=3*year+1;
    }


    return tran;
    
}
Last edited by admin II : 12-Apr-2007 at 05:02. Reason: [tagindicator] should be [CPP] ... [/CPP] for C++ code
  #2  
Old 12-Apr-2007, 07:50
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,310
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: problems about simple tricked loop question


Quote:
Originally Posted by allenfanwenyuan
This is a part of large question :
anyone can help me to work it out

Make the program tell you what is seeing and what it is doing at each step;

For example, in main:
CPP / C++ / C Code:
    cin >> day >> month >> year;
    cout << "In main: You entered: month = " << month << ", year = " << year << endl;


In the function:

CPP / C++ / C Code:
    cout << "In transyear: month = " << month << ", year = " << year << endl;
    for (int i = 1; i <= month; i++) {
        cout << "top of loop: i = " << i << ", tran = " << tran << endl;

        if (tran % 2 == 0) {
.
.
.
        }
        cout << "bottom of loop: year = " << tran << endl;
    }
    return tran;

Regards,

Dave
  #3  
Old 19-Apr-2007, 20:03
allenfanwenyuan allenfanwenyuan is offline
Junior Member
 
Join Date: Mar 2007
Posts: 38
allenfanwenyuan is an unknown quantity at this point

Re: Problems about simple tricked loop question


cheers .... davekw7x ....i 've already worked it out with ur hint , just let tran=year at the bottom of the function,then repeat it . Thanks a lot !!!!!
  #4  
Old 30-Apr-2007, 22:45
distilled23 distilled23 is offline
New Member
 
Join Date: Apr 2007
Posts: 2
distilled23 is on a distinguished road

Re: Problems about simple tricked loop question


what was the final code output for that question? i tried 2 do the same thing and it only outputs the number two over 200 times then ends.
  #5  
Old 30-Apr-2007, 23:47
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,310
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Problems about simple tricked loop question


Quote:
Originally Posted by distilled23
what was the final code output for that question? i tried 2 do the same thing and it only outputs the number two over 200 times then ends.

Here's how it works:

1. Post your code. Tell us what compiler you are using. If there were any compiler messages, paste them into the post. Don't edit; don't paraphrase --- give us the entire message(s).

2. Tell us exactly you gave it as an input

3. Tell us what you expected to get from the program. (Did you put any debug print statements to make the program tell you what it was doing?)

Regards,

Dave
  #6  
Old 01-May-2007, 07:03
distilled23 distilled23 is offline
New Member
 
Join Date: Apr 2007
Posts: 2
distilled23 is on a distinguished road

Re: Problems about simple tricked loop question



c++
hey ok sorry im new to this, im trying to figure out what is happening in allens code. i inputted this but i have a feeling that i am missing some brackets somewhere along the line. i recieve no errors but my function just isnt agreeing with me.

as the function

int TransYear( int month, int year )
{
int tran;
tran = year;

for (int i = 1; i <= month; i++)
{
if
(tran % 2 == 0)
tran = tran / 2;
else
tran = 3 * tran + 1;
}


return tran;
}

in main

cout << TransFormer (year, month) << endl;



about two mins after posting this however i realised my mistake ...in which my main was returning (year, month) not month year.
cout << TransFormer (year, month) << endl;
change to
cout << TransFormer (month, year) << endl;

thanks for the help
  #7  
Old 01-May-2007, 09:19
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,310
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Problems about simple tricked loop question


Quote:
Originally Posted by distilled23
about two mins after posting this however i realised my mistake
That is why I try to get people to post the code. Sometimes in explaining things to others and pasting the code into a post, they actually look at it. I have done this (spotting a mistake after explaining it to someone else and before they actually responded) about a million times. Maybe more.

Quote:
Originally Posted by distilled23
...in which my main was returning (year, month) not month year.

And if you hadn't spotted it, then the print statements that I showed in my previous post would have shouted it at you. Really; it would just have jumped off of the page and yelled in your ear. The point of the exercise is not just to write a program to do some trivial task, but to get people into the mindset of attacking a problem and using the two most important resources at their disposal to solve it:

1. The programmer's brain and the brain extension known as the optical nerve.
2. The program itself.


Regards,

Dave

"We can face our problem.
We can arrange such facts as we have with order and method."
Hercule Poirot
--- in Murder on the Orient Express
Last edited by davekw7x : 01-May-2007 at 10:26.
 
 

Recent GIDBlogProblems with the Navy (Enlisted) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
a simple question sharwar .NET Forum 3 28-Sep-2006 05:18
simple file question dabigmooish C++ Forum 2 12-Sep-2006 09:01
my 1st simple program, 3 slight problems. skizer C++ Forum 11 07-Feb-2006 15:31
Simple question about while loop Lej C++ Forum 6 07-Aug-2005 13:57
Simple question about file opening eddo C++ Forum 3 09-Jun-2005 22:04

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 06:52.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.