none
Do I have the right concept, even though my comments maybe wrong, and no answer to my output z?

    Question

  • #include <iostream> 
    using namespace std; 
    int main(){ 
    
    int x = 12, y = 10, z = -6; 
    
    if(x > 0) { //X is greater than zero
    
    if(y <= 0) { //y is not less than or equal to zero
    
    if (z < 0) //z is less than zero, so add 1, which becomes -7
    ++z; 
    
    if(z <= 0) //z is less than zero, subtract 1, which become -6
    z--; 
    } 
    
    if(y > 0) //y is greater than zero, so it 12 + 1 = 13
    x++; 
    } 
    
    if(y <= 0) //is not less than zero, 1 + 10 = 11
    ++y; 
    
    z = x + y; //therefore  -6 = 13 + 11
    
    cout << z << endl; //The output for z is ? 
    
    return 0; 
    } 
    


    C# beginner game developer

    Sunday, June 5, 2016 9:13 AM

Answers

  • Hi BillionaireMan,

    I will go line by line with explanation... btw I can see that you code is not well formatted so I will paste here formated code to be easier to me and I will delete your comments in code:

    #include <iostream> 
    using namespace std;
    int main() {
    
    	int x = 12, y = 10, z = -6;
    
    	if (x > 0) { 
    
    		if (y <= 0) { 
    
    			if (z < 0) 
    				++z;
    
    			if (z <= 0) 
    				z--;
    		}
    
    		if (y > 0) 
    			x++;
    	}
    
    	if (y <= 0) 
    		++y;
    
    	z = x + y; 
    
    	cout << z << endl; //The output for z is:  
    
    	return 0;
    }

    First IF: "if (x > 0)" X is equal to 12 so we will go into the IF statement...

    Second IF: " if (y <= 0) " this is not true so we will not go into this if... in this case Y is equal to 10, we skip all the ifs in this IF and go to next one that is with condition (y > 0).

    Third IF: " if (y > 0) "  in this case Y is greater than zero and we will go into the body of this IF statement... and X value is now equal to 13 because we increment it in the body of this IF.

    Next IF: "if (y <= 0) " In this case Y is 10 so it is not less or equal to zero and we are not going into the IF body.

    So after all the IF statements this calculation is very clear:

    z = x + y; in other words -> Z = 13 + 10;

    Z have value of 23.

    And the answer to your question is: // The output for z is: 23

    Hope this was helpful and clear to you... Best luck with programming.

    Best regards!


    (If this was helpful for you, vote for it and propose it as an answer)

    Sunday, June 5, 2016 10:12 AM
  • I say the same thing. Those are big numbers. I'm still on C++ theory- (chapter 1) I'll be getting to running code next week on whatever IDE the lesson instructs me to. You did good though. Thanks. Also, take into account that came from the community resources. A volunteer who aided in a mock C++ practice test. So I'm going to eventually mark this answered by you.

    C# beginner game developer

    Thanks a lot...

    Best luck with your future programming...

    Best regards!


    (If this was helpful for you, vote for it and propose it as an answer)

    Sunday, June 5, 2016 7:48 PM
  • Thanks Barry. I will do just that in finding another tutorial- No wonder you keep telling me I make no sense.

    But I do say It would be nice if you could volunteer test preparations to that C++ resource community.

    I'm marking this as answered.


    C# beginner game developer

    Monday, June 6, 2016 7:52 PM

All replies

  • Hi BillionaireMan,

    I will go line by line with explanation... btw I can see that you code is not well formatted so I will paste here formated code to be easier to me and I will delete your comments in code:

    #include <iostream> 
    using namespace std;
    int main() {
    
    	int x = 12, y = 10, z = -6;
    
    	if (x > 0) { 
    
    		if (y <= 0) { 
    
    			if (z < 0) 
    				++z;
    
    			if (z <= 0) 
    				z--;
    		}
    
    		if (y > 0) 
    			x++;
    	}
    
    	if (y <= 0) 
    		++y;
    
    	z = x + y; 
    
    	cout << z << endl; //The output for z is:  
    
    	return 0;
    }

    First IF: "if (x > 0)" X is equal to 12 so we will go into the IF statement...

    Second IF: " if (y <= 0) " this is not true so we will not go into this if... in this case Y is equal to 10, we skip all the ifs in this IF and go to next one that is with condition (y > 0).

    Third IF: " if (y > 0) "  in this case Y is greater than zero and we will go into the body of this IF statement... and X value is now equal to 13 because we increment it in the body of this IF.

    Next IF: "if (y <= 0) " In this case Y is 10 so it is not less or equal to zero and we are not going into the IF body.

    So after all the IF statements this calculation is very clear:

    z = x + y; in other words -> Z = 13 + 10;

    Z have value of 23.

    And the answer to your question is: // The output for z is: 23

    Hope this was helpful and clear to you... Best luck with programming.

    Best regards!


    (If this was helpful for you, vote for it and propose it as an answer)

    Sunday, June 5, 2016 10:12 AM
  • As near as I can tell, the answer to your first question is NO.  You have not described any concept in your post nor have you told us what the program is supposed to do.  Dealing with what you actually wrote and the implied concepts therein:

        1st incorrect concept: This is the wrong forum.  Your question has nothing to do with certification.  There are  two Visual C++ forums where this thread would be topical.

        2nd incorrect concept: As you note, y is not less than or equal to zero.  Therefore the two if statements testing z will never execute.

        3rd incorrect concept: Incrementing a negative number results in a value closer to 0, not more negative as you state in the comment in the first "z" if statement.

        4th incorrect concept: Decrementing a negative number results in more negative value in contrast to the comment in the second "z" if.

        5th incorrect concept: Once an if statement evaluates to false, the remaining code is not executed.  Thus y will not be incremented.

        6th incorrect concept: The "=" operator does not mean equals in the comparison sense.  It is the assignment operator.  -6 is obviously not equal to 13 + 11.  If you have to provide a comment for the assignment statement, "z is assigned the value resulting from 13 + 10" would better reflect what the code does

    Since your code is easily compilable and executes with well defined behavior, I don't know why you claim to not get any output.  If you are running under the IDE, the window with the output will close after the return statement executes.  If that is your problem, either step through the program or put a break point on the return statement.  Then there would be no need to ask us what the value of z is.

    And as Almir recommended, please learn to indent you code.


    Sunday, June 5, 2016 11:27 AM
  • Don't be so rough Barrrrrry. My goodness. You sound like an admonishing professor. I'm new at this. This is a practice question from a practice test. (see the comment I left for Almir.

    C# beginner game developer

    Sunday, June 5, 2016 3:55 PM
  • Almir. I appreciate your assistance in breakdown the code in an clear format. As it is- I'm new at this. My third week in fact. As I mentioned to Barry below "This is a practice question from a practice test. The thing about it, is that the practice test was looking for one of the following answers.

    C# beginner game developer

    Sunday, June 5, 2016 4:04 PM
  • It was looking for one of these answers.

    C# beginner game developer

    Sunday, June 5, 2016 4:05 PM
  • Hi BillionaireMan,

    Can you show us the text of this exam that you need to make to get some of these answers... I a wrote about it Z will be 23 at the end but I can't see what these answers are for...

    So if you can provide us a text for this exam it will be helpful for us... and I will be glad to help you.

    Best regards!


    (If this was helpful for you, vote for it and propose it as an answer)


    Sunday, June 5, 2016 5:09 PM
  • It may help if you post the url where this comes from.  The information we currently have makes no sense.
    Sunday, June 5, 2016 5:28 PM
  • http://cppinstitute.org/cc-community-made-resources

    CPA (C++ Certified Associate Programmer)

    Chapter 1, Section 6, Question 1. (It's the first PDF under the above title)


    C# beginner game developer

    Sunday, June 5, 2016 6:22 PM
  • http://cppinstitute.org/cc-community-made-resources

    CPA (C++ Certified Associate Programmer)

    Chapter 1, Section 6, Question 1. (It's the first PDF under the above title)


    C# beginner game developer

    Sunday, June 5, 2016 6:22 PM
  • http://cppinstitute.org/cc-community-made-resources

    CPA (C++ Certified Associate Programmer)

    Chapter 1, Section 6, Question 1. (It's the first PDF under the above title)


    C# beginner game developer

    Hi BillionaireMan,

    I can see it now ... but I don't have any idea how they get this result... and under explanation they had not explained it...

    You can by yourself run the code and you will see results as I wrote them in first my replay but I don't get it how they they have this kind of result from these "small" numbers.

    Maybe there is some error or something, I suggest you to email them or find some forum that is associated with this type of certification.

    Best luck with programming...

    Best regards!


    (If this was helpful for you, vote for it and propose it as an answer)

    Sunday, June 5, 2016 7:15 PM
  • I say the same thing. Those are big numbers. I'm still on C++ theory- (chapter 1) I'll be getting to running code next week on whatever IDE the lesson instructs me to. You did good though. Thanks. Also, take into account that came from the community resources. A volunteer who aided in a mock C++ practice test. So I'm going to eventually mark this answered by you.

    C# beginner game developer

    Sunday, June 5, 2016 7:45 PM
  • I say the same thing. Those are big numbers. I'm still on C++ theory- (chapter 1) I'll be getting to running code next week on whatever IDE the lesson instructs me to. You did good though. Thanks. Also, take into account that came from the community resources. A volunteer who aided in a mock C++ practice test. So I'm going to eventually mark this answered by you.

    C# beginner game developer

    Thanks a lot...

    Best luck with your future programming...

    Best regards!


    (If this was helpful for you, vote for it and propose it as an answer)

    Sunday, June 5, 2016 7:48 PM
  • I suggest you find another tutorial.  This one has numerous problems:

        The code in many of the problems is so badly obfuscated that you lose sight of the lesson being taught.

        A quick review revealed that several of the answers are incorrect.

        Some of the explanations why one answer is the correct one discuss unrelated considerations.

        Some of the code in the problems exhibits undefined behavior.


    Monday, June 6, 2016 2:49 AM
  • Thanks Barry. I will do just that in finding another tutorial- No wonder you keep telling me I make no sense.

    But I do say It would be nice if you could volunteer test preparations to that C++ resource community.

    I'm marking this as answered.


    C# beginner game developer

    Monday, June 6, 2016 7:52 PM