Test your knowledge

This is a discussion on Test your knowledge within the C++ Programming forums, part of the General Programming Boards category; What is the last value displayed when this code is run? Code: int *secret; int j; secret=new int[10]; secret [0]=[10]; ...

  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
    4,370
    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,479
    Code:
    int *secret;
    That is also very dangerous... pointer not assigned on initalization, should be at least NULL
    I'm just trying to be a better person - My Name Is Earl

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    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 03: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, 08:19 AM
  2. Why is my program freezing?
    By ShadowMetis in forum Windows Programming
    Replies: 8
    Last Post: 08-20-2004, 04:20 PM
  3. Question About Linker Errors In Dev-C++
    By ShadowMetis in forum C++ Programming
    Replies: 9
    Last Post: 08-18-2004, 09: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, 06:30 AM
  5. test msgQ
    By jaz in forum Linux Programming
    Replies: 0
    Last Post: 05-21-2003, 06:11 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21