Thread: code help!

  1. #1
    Registered User
    Join Date
    Mar 2006

    code help!

    can someone please help with describing what the output of this program would be?^

    		for (int i = 0; i < 8; i++)
    		         if (i%2 == 0) cout << i +1 << “\t”;
    		          else if (i%3) == 0) cout << i * i  << “\t”;
    		          else if (i%5 == 0) cout << 2*i – 1 << “\t”;
    		          else cout << i << “\t”;

  2. #2
    Registered User
    Join Date
    Nov 2005
    Surely you have a problem if the number divides by 3 and 2 like 6

    if int i/2 gives no remainder then it will output i+1
    else if i/3 gives no remainder then the output will be i^2
    else if i/5 gives no remainder the output will be 2*i - 1
    otherwise the output will be i
    Last edited by bumfluff; 03-24-2006 at 09:20 AM.

  3. #3
    Registered User major_small's Avatar
    Join Date
    May 2003
    well, if you #include <iostream>, use namespace std, and put an int before main, you get... nothing.
    jshao@MCP ~/Programming/C++/test $ g++ test.cpp -Wall -W -ansi -pedantic -o test.exe && ./test.exe
    test.cpp: In function `int main()':
    test.cpp:9: error: syntax error before `==' token
    test.cpp:10: error: syntax error before `else'
    that for loop is constructed wrong, and your parenthesis are messed up.

    after fixing the loop and parenthesis and making it more readable, and adding comments, it becomes more clear:
    #include <iostream>
    using namespace std;
    int main()
    	for (int i=0;i<8;i++)
    		if (i%2==0)		//if the remainder of i/2 is zero
    			cout<<i+1<<'\t';	//output i+1 and a tab
    		else if (i%3 == 0)	//if the remainder of i/3 is zero
    			cout<<i*i<<'\t';	//output i*i and a tab
    		else if (i%5 == 0)	//if the remainder of i/5  is zero
    			cout<<2*i-1<<'\t';	//output (2*i)-1 and a tab
    		else			//otherwise
    			cout<<i<<'\t';		//just output i
    and now when you compile and run that, you get:
    jshao@MCP ~/Programming/C++/test $ g++ test.cpp -Wall -W -ansi -pedantic -o test.exe && ./test.exe
    1       1       3       9       5       9       7       7
    basically, if i is an even number (in this case, 2,4,6,8), it'll add one to i, and if it's a multiple of three (in this case, only when i is 3), it'll square the nubmer, and when it's a multiple of 5 (in this case only when it's 5), it'll multiply i by 2 and subtract 1. That leaves you with 1 and 7, which just gets printed for what it is.
    Last edited by major_small; 03-24-2006 at 09:53 AM.
    Join is in our Unofficial Cprog IRC channel
    Channel: #Tech

    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Extended ASCII Characters in an RTF Control
    By JustMax in forum C Programming
    Replies: 18
    Last Post: 04-03-2009, 08:20 PM
  2. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 07:34 AM
  3. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 03:17 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 05:05 PM