Thread: Test your knowledge

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    9

    Test your knowledge

    What is the last value displayed when this code is run?


    Code:
    int *secret;
    int j;
    secret=new int[10];
    secret [0]=[10];	
    
    for (j=1;j<10;j++)
    {
    	secret [j]=secret[j-1]+5;
    }
    
    for(j=0;j<10;j++)
    {
    	cout<<secret[j]<<" "<<endl;
    }
    Is it 45, 50, 55, 60, or 80 ?

  2. #2
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    It can't compile; it can't run; it doesn't print out anything. (That code is syntactically invalid.)

    Soma

  3. #3
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    Code:
    int *secret;
    That is also very dangerous... pointer not assigned on initalization, should be at least NULL
    Double Helix STL

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by swgh View Post
    Code:
    int *secret;
    That is also very dangerous... pointer not assigned on initalization, should be at least NULL
    I would certainly not assign it a value and then immediately assigning it something else. Simply combine the declaration and the assignment in such a case:
    Code:
    int *secret=new int[10];
    or better yet, use a statically sized array in this case, and avoid the memory leak:
    Code:
    int secret[10] = {10};
    (combines lines 1, 3 and 4).

    btw, the bug is here:
    Code:
    secret [0]=[10];
    [10] should be just 10. That and the fact that the code needs to be placed within an actual program i.e. main, and have using and include statements added.

    Well at least this wasn't one of those ones with undefined behaviour for a change, like asking what is the value of ++x + ++x...
    Last edited by iMalc; 06-07-2008 at 02:49 PM.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    Registered User Terran's Avatar
    Join Date
    May 2008
    Location
    Nashua, NH
    Posts
    100
    not to be a jerk but.

    FAIL
    Sorry, but i'm a Code::Blocks man now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sustring test puzzle
    By WDT in forum C# Programming
    Replies: 3
    Last Post: 06-29-2009, 07:19 AM
  2. Why is my program freezing?
    By ShadowMetis in forum Windows Programming
    Replies: 8
    Last Post: 08-20-2004, 03:20 PM
  3. Question About Linker Errors In Dev-C++
    By ShadowMetis in forum C++ Programming
    Replies: 9
    Last Post: 08-18-2004, 08:42 PM
  4. Looking for sample MC test questions for a job
    By garp in forum C Programming
    Replies: 2
    Last Post: 09-29-2003, 05:30 AM
  5. test msgQ
    By jaz in forum Linux Programming
    Replies: 0
    Last Post: 05-21-2003, 05:11 AM