Get the latest Education e-news
 

Go Back   Game Career Guide Forums > Programming
Forum Home Register Members List Mark Forums Read

Reply
 
Thread Tools Display Modes
Old 12-19-2008, 07:09 AM   #11
yaustar
Administrator
 
yaustar's Avatar

Activity Longevity
2/20 19/20
Today Posts
0/11 sssss2270
Location: UK
Default

Quote:
Originally Posted by Protector one View Post
Will only call it once. Isn't the latter better? Or are they behaviorally equivalent? (I.e. will they compile to the same byte code?)
The latter is 'better' but I have no idea whether they will. compile out to be the same
yaustar is offline   Reply With Quote
Old 12-19-2008, 07:12 AM   #12
yaustar
Administrator
 
yaustar's Avatar

Activity Longevity
2/20 19/20
Today Posts
0/11 sssss2270
Location: UK
Default

Quote:
Originally Posted by nef View Post
On a side note, to Adrir. I've never, ever seen someone using a namespace or class within a function. I don't doubt it's possible, but what are the advantages to this? I would have to strictly disagree it's conventional, solely because I've never seen it done
As side by Adrir, the using directive obeys scope and it is something I started doing due to past experiences with projects that have batch/unity builds where doing anything in global scope can potentially lead to a problem.
yaustar is offline   Reply With Quote
Old 12-19-2008, 12:11 PM   #13
nef
Member

Activity Longevity
0/20 16/20
Today Posts
0/11 sssssss58
Default

Quote:
Originally Posted by Protector one View Post
Will only call it once. Isn't the latter better? Or are they behaviorally equivalent? (I.e. will they compile to the same byte code?)
I don't think it would compile into the same thing. I mean, compilers are smart, but i doubt their that smart.
nef is offline   Reply With Quote
Old 12-20-2008, 06:13 AM   #14
Protector one
Moderator

Activity Longevity
2/20 17/20
Today Posts
0/11 ssssss698
Location: Netherlands
Default

Well, it would be a simple rule. Check if a function is called sequentially, see if the calls can be combined, combine if so. Compilers can do pretty freaky shit. I wouldn't be surprised if it is implemented somewhere. (-O6? )
__________________
My name is Louis. Hi!
Uh oh! The princess is in another signature!
Protector one is offline   Reply With Quote
Old 12-21-2008, 09:06 AM   #15
Adrir
Administrator
 
Adrir's Avatar

Activity Longevity
1/20 20/20
Today Posts
0/11 sssss1466
Location: London, UK
Default

Quote:
Originally Posted by Protector one View Post
Well, it would be a simple rule. Check if a function is called sequentially, see if the calls can be combined, combine if so. Compilers can do pretty freaky shit. I wouldn't be surprised if it is implemented somewhere. (-O6? )
Some of my friends did some experiments with the Java compiler a while back when we just started our course. If I remember correctly, I believe that the Java compiler will produce the same bytecode from a sequence of simple string concatenation methods than as placing each concatenation in a single statement. However, if they are being printed onto the console then the bytecode is different.

For example, the following two code extracts produced the same bytecode:

Code:
String str = "a" + "b" + "c" + "d" + "e";
Code:
String str = "a";
str += "b";
str += "c";
str += "d";
str += "e";
However the following two did not:

Code:
System.out.print("a" + "b" + "c" + "d" + "e");
Code:
System.out.print("a");
System.out.print("b");
System.out.print("c");
System.out.print("d");
System.out.print("e");
__________________
Michael 'Adrir' Scott :: Games, Virtual Worlds, Education
Networking | Current Project | Research | Teaching
Adrir is offline   Reply With Quote
Old 12-21-2008, 02:09 PM   #16
Protector one
Moderator

Activity Longevity
2/20 17/20
Today Posts
0/11 ssssss698
Location: Netherlands
Default

Interesting stuff! A bit scary, though. You can't find this sort of stuff in documentation.

Quote:
Originally Posted by Adrir View Post
For example, the following two code extracts produced the same bytecode:

Code:
String str = "a" + "b" + "c" + "d" + "e";
Code:
String str = "a";
str += "b";
str += "c";
str += "d";
str += "e";
That's quite weird! I would have expected that the first would compile to a set of string concatenations and a single assignment, and the second would compile to 5 distinct concatenations and assignments.
So the question rises: is the first method very inefficient, or is the second simply very efficiently resolved?
__________________
My name is Louis. Hi!
Uh oh! The princess is in another signature!
Protector one is offline   Reply With Quote
Old 12-23-2008, 05:09 PM   #17
MDBurpo
Junior Member

Activity Longevity
0/20 16/20
Today Posts
0/11 sssssss14
Location: Martinsville, IN
Default Rewrote the quiz...

nef,

I took into account all the stuff you have said. I've been taking a break from learning C++ to work on my resume and school work, so I just got around to rewriting today.

Code:
//Matthew D. Burpo - Beginning C++ - December 18, 2008 - Third Program - Basic Quiz
#include <iostream>
using std::cout; //Load cout from namespace std - A standard output stream object
using std::cin; //Load cin from namespace std - A standard input stream object

int QuestionOne()
{
    int answer; //Initialize "answer" variable.
    int answer1; //Initialize "answer1" variable.
    int answer2; //Initialize "answer2" variable.
    int answer3; //Initialize "answer3" variable.
    int answer4; //Initialize "answer4" variable.
    answer1 = 1; //Set "answer1" variable to 1.
    answer2 = 2; //Set "answer2" variable to 2.
    answer3 = 3; //Set "answer3" variable to 3.
    answer4 = 4; //Set "answer4" variable to 4.
    cout <<"What is the programmer's full name?\n"
           "1.) Matthew Benjamin Durpo\n"
	       "2.) David Matthew Burpo\n"
	       "3.) Matthew David Burpo\n"
           "4.) Benjamin Matthew Durpo\n"
           "\n"
           "Answer: "; //Display all strings between cout and ; on screen.
    cin >> answer; //Answer input.
    cin.ignore(); //Ignore a single character and allow prompt to continue.
	if (answer == answer3) //If correct, do this function.
	{
	    cout <<"\n"
	           "That's correct!\n"
	           "\n"
	           "Press 'Enter' to continue.\n"; //Display all strings between cout and ; on screen.        	
        cin.get(); //Press Enter Key to execute next command.
	}
	else //If incorrect, do this function.
	{
        cout <<"\n"
		       "Incorrect answer! The programmer is Matthew David Burpo.\n"
		       "\n"
		       "Press 'Enter' to continue.\n"; //Display all strings between cout and ; on screen.
	    cin.get(); //Press Enter Key to execute next command.
    }
    return(0);
}

int QuestionTwo()
{
    int answer; //Initialize "answer" variable.
    int correctanswer;
    correctanswer = 24; //Assign "answer" variable to 24.
    cout <<"How old is Matthew?\n"
	       "\n"
	       "Answer: "; //Display all strings between cout and ; on screen.
	cin >>answer; //Answer input.
	cin.ignore(); // Ignore a single character and allow prompt to continue.
	if (answer == correctanswer) //If correct, do this function.
	{
		cout <<"\n"
		       "That's Correct!\n"
		       "\n"
		       "Press 'Enter' to continue.\n"; //Display all strings between cout and ; on screen.
		cin.get(); //Press Enter Key to execute next command.
	}
	else //If incorrect, do this function.
	{
		cout <<"\n"
		       "Incorrect answer! Matthew is 24 years old.\n"
		       "\n"
		       "Press 'Enter' to continue.\n"; //Display all strings between cout and ; on screen.
		cin.get(); //Press Enter Key to execute next command.
	}
    return (0);
}

int QuestionThree()
{
    int answer; //Initialize "answer" variable.
    int answer1; //Initialize "answer1" variable.
    int answer2; //Initialize "answer2" variable.
    int answer3; //Initialize "answer3" variable.
    int answer4; //Initialize "answer4" variable.
    int answer5; //Initialize "answer5" variable.
    int answer6; //Initialize "answer6" variable.
    int answer7; //Initialize "answer7" variable.
    answer1 = 1; //Set "answer1" variable to 1.
    answer2 = 2; //Set "answer2" variable to 2.
    answer3 = 3; //Set "answer3" variable to 3.
    answer4 = 4; //Set "answer4" variable to 4.
    answer5 = 5; //Set "answer5" variable to 5.
    answer6 = 6; //Set "answer6" variable to 6.
    answer7 = 7; //Set "answer7" variable to 7.
	cout <<"Matthew wants to be a:\n"
	       "1.) Video Game Designer\n"
	       "2.) Video Game Programmer\n"
	       "3.) Video Game Producer\n"
	       "4.) Both 1 & 2\n"
	       "5.) Both 1 & 3\n"
	       "6.) Both 2 & 3\n"
	       "7.) 1, 2, & 3\n"
	       "\n"
	       "Answer: "; //Display all strings between cout and ; on screen.
	cin >>answer; //Answer input.
	cin.ignore(); // Ignore a single character and allow prompt to continue.
	if (answer == answer4) //If correct, do this function.
	{
		cout <<"\n"
		       "That's correct!\n"
		       "\n"
		       "Press 'Enter' to continue.\n"; //Display all strings between cout and ; on screen.
		cin.get(); //Press Enter Key to execute next command.
	}
	else //If incorrect, do this function.
	{
		cout <<"\n"
		       "Incorrect answer! Matthew wants to be a video game programmer and designer.\n"
		       "\n"
		       "Press 'Enter' to continue.\n"; //Display all strings between cout and ; on screen.
		cin.get(); //Press Enter Key to execute next command.
	}
return (0);
}

int QuestionFour()
{
    int answer; //Initialize "answer" variable.
    int answer1; //Initialize "answer1" variable.
    int answer2; //Initialize "answer2" variable.
    int answer3; //Initialize "answer3" variable.
    int answer4; //Initialize "answer4" variable.
    answer1 = 1; //Set "answer1" variable to 1.
    answer2 = 2; //Set "answer2" variable to 2.
    answer3 = 3; //Set "answer3" variable to 3.
    answer4 = 4; //Set "answer4" variable to 4.
    cout <<"What school is Matthew attending?\n"
           "1.) Ivy Tech\n"
           "2.) ITT Tech\n"
           "3.) DeVry\n"
           "4.) University of Phoenix\n"
	       "\n"
	       "Answer: "; //Display all strings between cout and ; on screen.
	cin >>answer; //Answer input.
	cin.ignore(); // Ignore a single character and allow prompt to continue.
	if (answer == answer2) //If correct, do this function.
	{
		cout <<"\n"
		       "That's correct!\n"
		       "\n"
		       "Press 'Enter' to continue.\n"; //Display all strings between cout and ; on screen.
		cin.get(); //Press Enter Key to execute next command.
	}
	else //If incorrect, do this function.
	{
		cout <<"\n"
		       "Incorrect answer! Matthew is attending ITT Tech.\n"
		       "\n"
		       "Press 'Enter' to continue.\n"; //Display all strings between cout and ; on screen.
		cin.get(); //Press Enter Key to execute next command.
	}
    return(0);
}

int main() //Start program - Entry point of main function.
{
    cout <<"Welcome to the quiz.\n"
           "Please, enter the number corresponding to the correct answer.\n"
           "Press 'Enter' to begin.\n"
           "\n"; //Display all strings between cout and ; on screen.
    cin.get(); //Press Enter Key to execute next command.
    int AskQuestion;
    AskQuestion = QuestionOne();
    cout << AskQuestion;
    AskQuestion = QuestionTwo();
    cout << AskQuestion;
    AskQuestion = QuestionThree();
    cout << AskQuestion;
    AskQuestion = QuestionFour();
    cout << AskQuestion;
    AskQuestion = QuestionFive();
    cout << AskQuestion;
    AskQuestion = QuestionSix();
    cout << AskQuestion;
    AskQuestion = QuestionSeven();
    cout << AskQuestion;
	cout <<"Press 'Enter' to exit the program...\n"; //Display on screen.
	cin.get(); //Press Enter Key to exit the program.
	return 0; //End program - Normal termination of main function.
}
I still need to go through and add some comments, but I think I have it fixed using your suggestions. I cut out questions 5, 6, and 7 to keep from exceeding the max characters restriction on posts.
MDBurpo is offline   Reply With Quote
Old 12-23-2008, 06:39 PM   #18
MDBurpo
Junior Member

Activity Longevity
0/20 16/20
Today Posts
0/11 sssssss14
Location: Martinsville, IN
Default

Forgot to mention I'm having problems in there with the returns. It's returning 0 as an output on screen where it says "0Question text here"
MDBurpo is offline   Reply With Quote
Old 12-24-2008, 12:06 AM   #19
Claxon
Senior Member

Activity Longevity
0/20 18/20
Today Posts
0/11 ssssss345
Location: London
Default

Quote:
Originally Posted by MDBurpo View Post
Forgot to mention I'm having problems in there with the returns. It's returning 0 as an output on screen where it says "0Question text here"
That's because of these lines:

Code:
    AskQuestion = QuestionOne();
    cout << AskQuestion;
The QuestionOne() function is handling all of it's own cout calls, and you're assigning the resulting value (0 in all cases) to AskQuestion, then you're telling it to output the value of AskQuestion. if you just remove the cout and (since you don't seem to be using the value of AskQuestion anywhere else either) simple call the functions, it should work properly.

In other words, change the above code to:
Code:
QuestionOne();
QuestionTwo();
QuestionThree();
.....
Claxon is offline   Reply With Quote
Old 12-24-2008, 12:44 AM   #20
Protector one
Moderator

Activity Longevity
2/20 17/20
Today Posts
0/11 ssssss698
Location: Netherlands
Default

You know you can define and initialize variables at the same time, right?

Code:
int answer;
answer1 = 1;
is equivalent to:

Code:
int answer1 = 1;
Makes your code a bit shorter.
__________________
My name is Louis. Hi!
Uh oh! The princess is in another signature!
Protector one is offline   Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Powered by vBulletin® Version 3.6.9
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
All times are GMT -8. The time now is 01:13 AM.






UBM Tech