What 'C' output? And Why?
Page 1 of 2 12 LastLast
Results 1 to 10 of 17

Thread: What 'C' output? And Why?

  1. #1
    Senior Member
    Join Date
    Dec 2001
    Posts
    134

    What 'C' output? And Why?

    1. What is the final value of x when the code for(int x=0; x<10; x++) is run?

    A. 10
    B. 9
    C. 0
    D. 1

  2. #2
    Senior Member
    Join Date
    Jul 2002
    Posts
    744
    x will hit ten and the for loop will exit.
    -edit- Sorry didn't respond to the question in the title. . . .

    for(x=0; this part initializes x at zero to ctart counting.
    x<10; this part will make the loop stop the count at ten.
    x++ this increments 'x' by one each time through the loop.
    Every now and then, one of you won't annoy me.

  3. #3
    () \/V |\| 3 |) |3\/ |\|3G47|\/3
    Join Date
    Sep 2002
    Posts
    744
    Is this a test??? :-) Yes... x increments to ten.

    I'm not sure why you are asking (all questions are good, though). Anyway, the for statement is an important concept. I use it in virtually everything I write.


    Oh! You asked why?? You have i initialized as int. Beginning at zero it will increment by one every time the loop is executed until i is no longer less than 10.

    Go Finland!
    Deviant Gallery

  4. #4
    Senior Member
    Join Date
    Aug 2002
    Posts
    310
    Technically though since x < 10 isn't the final value 9?
    10 isn't < 10 therefore ten definately can't be the answer.
    I'd say the answer's 9
    [shadow]I don\'t believe in anarchy.If you\'re not smart enough to beat the system it\'s your problem. [/shadow]


  5. #5
    Senior Member
    Join Date
    Jul 2002
    Posts
    744
    Yeah but it goes through the loop one more time, I think, to check the value of 'x' one more time, it won't fail the x<10, until it is ten so, ending value of x is 10.
    Every now and then, one of you won't annoy me.

  6. #6
    Webius Designerous Indiginous
    Join Date
    Mar 2002
    Location
    South Florida
    Posts
    1,123
    Yes the answer will be 9.

    The for statement:

    for(initialization; test; update) //notice there is no ; at the end of the for statment
    {
    statment;
    statment;
    //place as many statments here
    //as needed.
    }


    This is the for loop. An int can only hold a whole number value between:

    -2,147,483,648 and +2,147,483,647.

    This type of variable takes up 4 bytes of memory.

    What happens if you go over or under this number? BUFFER OVERFLOW!! heh.. just figured I would throw that in, since everyone has heard of a buffer overflow but not all understand what it really is.

  7. #7
    Senior Member
    Join Date
    Jul 2002
    Posts
    744
    Wouldn't x have to actually come to a point where it fails the X<10, though?? I mean if it stops at 9, it would still be true, wouldn't it??

    _edit, ah wait I see, it fails at the test and is never incremented, heh heh.
    Every now and then, one of you won't annoy me.

  8. #8
    Senior Member
    Join Date
    Aug 2002
    Posts
    310
    Well, it would try 10,but when it does in a much less verbose fashion it says HEY JACKA55 10 isn't a logical value because it's not less than ten. Since the value 10 is False it has no value in the expression.
    [shadow]I don\'t believe in anarchy.If you\'re not smart enough to beat the system it\'s your problem. [/shadow]


  9. #9
    Jaded Network Admin nebulus200's Avatar
    Join Date
    Jun 2002
    Posts
    1,356
    1. What is the final value of x when the codeis run?

    A. 10
    B. 9
    C. 0
    D. 1
    In the x++ part, that is essentially executed at the end of the loop, so if you look at what happens when X=9, it gets to the end of whatever you have in there, increments x, goes back to the conditional statement X < 10, which it isn't, so it goes on to whatever is after the for.

    Just a quick prog to show:

    int i=0;
    for(int x=0; x < 10; x++)
    {
    printf("Loop i: %i %i", i++, x);
    }


    Gives:

    Loop i: 0 0Loop i: 1 1Loop i: 2 2Loop i: 3 3Loop i: 4 4Loop i: 5 5Loop i: 6 6Loop i: 7 7Loop i: 8 8Loop i: 9 9

    /nebulus
    There is only one constant, one universal, it is the only real truth: causality. Action. Reaction. Cause and effect...There is no escape from it, we are forever slaves to it. Our only hope, our only peace is to understand it, to understand the 'why'. 'Why' is what separates us from them, you from me. 'Why' is the only real social power, without it you are powerless.

    (Merovingian - Matrix Reloaded)

  10. #10
    () \/V |\| 3 |) |3\/ |\|3G47|\/3
    Join Date
    Sep 2002
    Posts
    744
    Doh! You guys are right. The statement only executes for x LESS THAN ten! True it will go through the loop that tenth time - but, like you said, it won't actually cout ten.

    OK! OK! I feel like I'm on a merry-go-round.

    Tedob1, that might explain why this chunk of a program I'm turning in tonight works!!! I had the right answer....then starting reading the posts, got confused, then wondered: "If I am wrong, why does my code work??" Geeeez...I'm getting confused. :-) No more C posts for me!
    Ahhhh...but you know I am kidding.

    str.testScoresPtr = numTestScores;
    for (int i = 0; i < numStudents; i++)
    getStudentData(&str, x);
    }
    void getStudentData(StudentRecord *s, int scores)
    {
    float average;
    cout << "Student name: ";
    cin.ignore();
    cin.getline(s->studentName, 80);
    cout << "Student ID: ";
    cin.ignore(); // To ignore leftover newline
    cin >> s->studentID;
    cout << "Please enter test scores: " << endl;
    for (int i = 0; i < scores; i++)
    {
    cout << i+1 << ":";
    cin >> s->testScoresPtr[i];
    }

    Go Finland!
    Deviant Gallery

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •