Mitsubishi 3000GT & Dodge Stealth Forum banner

1 - 20 of 23 Posts

·
West Side!
Joined
·
1,201 Posts
Discussion Starter #1 (Edited)
I'm actualy writing these two functions for a job application test for EA, I have untill 3:30pm tomorrow to email them back with the answers for these and a few other questions. I'm just wondering what I've missed as far as speeding up the code in these two programs. I'll print all the code here but if you would rather download the code goto my
website and click on the two files at the very bottom of the page. (oh and check out the really crappy flash animation on that page... it's a first attempt so take it easy... click on the arrow.) Both of these run and do exactly what they're supposed to but I don't know how dirty my coding is after not haveing done any for about a year.
thanx to anybody who can help,
Gene Crumpler
1992 Stealth ES

Here's the code:

//code for converting a base 16 number to a base 10 number
// Gene Crumpler Nov. 2001
#include <iostream.h>

int power(int, int);

void main(){
bool out=true;
int total=0;
char hexa[100];
int deci[100];
cout << "Please input a hexadecimal number using any number keys and capitol letters only. ";
cin >> hexa;
int i = 0;
while (hexa){
switch (hexa){
case '0':
deci=0;break;
case '1':
deci=1;break;
case '2':
deci=2;break;
case '3':
deci=3;break;
case '4':
deci=4;break;
case '5':
deci=5;break;
case '6':
deci=6;break;
case '7':
deci=7;break;
case '8':
deci=8;break;
case '9':
deci=9;break;
case 'A':
deci=10;break;
case 'B':
deci=11;break;
case 'C':
deci=12;break;
case 'D':
deci=13;break;
case 'E':
deci=14;break;
case 'F':
deci=15;break;
default:
cout << "This is not a base 16 number.";out=false;break;
}
i++;
}
if (out==false)
return;
i--;
int j=i;
for (int k=0;k<=i;k++){
total = total + (deci[k] * power(16,j));
j--;
}
cout << '\n' << "This number is "<< total << " in decimal notation.";
}

int power (int base, int exponent){
if (exponent == 0)
return 1;
if (exponent == 1)
return base;
else
return base * power(base, exponent - 1);
}


//code for inputing two matrices and calculating there product
// Gene Crumpler Nov. 2001
#include <iostream.h>

void main(){
int rows1, colms1, rows2, colms2;
cout << "Please input the number of rows and columns for matrix 1 and press enter after each number. ";
cin >> rows1 >> colms1;
cout << "Please input the number of rows and columns for matrix 2 and press enter after each number. ";
cin >> rows2 >> colms2;
int matrix1[20][20];
int matrix2[20][20];
int matrix3[20][20];
if (colms1 == rows2){
cout << "Input matrix 1 row by row, digit by digit press enter after each digit.";
for(int i=0;i<rows1;i++)
for (int j=0;j<colms1;j++)
cin >> matrix1[j];
cout <<"Input matrix 2 row by row, digit by digit press enter after each digit.";
for(int i=0;i<rows2;i++)
for (int j=0;j<colms2;j++)
cin >> matrix2[j];
for (int i=0;i<rows1;i++)
for (int j=0;j<colms2;j++)
for (int k=0;k<colms1;k++){
matrix3[j] += matrix1[k] * matrix2[k][j];
}
cout << "The product of these two matrices is: ";
for (int i=0;i<rows1;i++){
cout << '\n';
for(int j=0;j<colms2;j++)
cout << matrix3[j] << " ";
}
}
else
cout << "These two matrices cannot be multiplied.";
}
 

·
Registered
Joined
·
715 Posts
im not an expert c++ programmer!
but you might try taking the recursion out of the power function
it may be sped up by not having those extra function pointers on the stack from the recursion
(iow try looping)

the 16-to-10 looks optimized for speed (thinking from an assembly standpoint)

i would just try different ways and check out the assembly listing each time to see whats going on. thats give you clues as to improvement

again i am NOT an expert!!!
geez i finally got to do a component in ATL for the first time in 6months! been all VB in-between (and some PIC assembly during lunch breaks ;))
 

·
Premium Member
Joined
·
2,986 Posts
The first one is easy if you do it in C:D

#include ~stdio.h~

void main()
{
int num;

printf("Enter a an integer in hexidecimal: ");
scanf("%x", &num);
printf("%x in hexidecimal is equal to %d in decimal\n\n", num, num);
}

If you must do it in C++ get rid of that damn power function. The more functions you call the slower it will run, although with inline functions in C++ you can speed it up considerably. In C you can use a macro (not used in C++).
 

·
Grrr, baby, grrr
Joined
·
6,116 Posts
I wonder how weird I can make this ;-)
------

#include ~string~
#include ~iostream.h~

int unhex(string hex)
{
string decode("0123456789abcdef");
int bits = 0;
unsigned int i;

// Convert our string to lowercase
for (i = 0; i < hex.length(); i++)
hex = tolower(hex);

// Find our value, for each character, in the deocoding string above.
// Since hex is base 16, it's fairly simple from here.
for (i = 0; i < hex.length(); i++)
bits = (bits * 16) + (decode.find(hex));

return bits;
}

int main()
{
string hexa;

cout << "please input a hexidecimal number: ";
cin >> hexa;

cout << endl << "This number is " << unhex(hexa) << " in decimal notation." << endl;

}


<a href="http://waddles.linux-support.net/hexcode.cpp">Click here for the pretty code</a>

~edit~
I didn't do any error checking, but that's trivial... You guys can have run ripping this one apart. I guess I could time it and find the best method :) I just wanted to do something 'different' :)
 

·
West Side!
Joined
·
1,201 Posts
Discussion Starter #6
The hex to dec problem explicitly states that you cannot use the stdio library so that's how I ended up with a page and a half of code... as for your code akira... it'll take me a little while to get it but that may be a much better option. I'm at work and when I get home i'll look through the code more...
thanx guys...
any other help is welcomed,
Gene Crumpler
1992 Stealth ES
 

·
Grrr, baby, grrr
Joined
·
6,116 Posts
I can do another version without using the STL, if you like ...
 

·
West Side!
Joined
·
1,201 Posts
Discussion Starter #8
Sure why not, I could use any extra options to try out... on another note... anybody know how to tell if a triangle in a positive x,y plane is front facing or back facing given the coordinates in a counter clockwise order? I didn't even know what the hell this question was asking untill I started looking into it on the web and now I only have the most basic understanding of what it means, but I still don't know how to tell...
laters,
Gene Crumpler
1992 Stealth ES
 

·
Ferrari Nut
Joined
·
2,675 Posts
I see things! (just my .2 cents)

CLS
SCREEN 9
RANDOMIZE TIMER
LET ROW% = INT(RND * 600) +1
LET COL% = INT(RND * 600) +1
LET SIZ% = INT(RND * 200) +1
LET CLR% = INT(RND * 15) +1
WHILE INKEY$ = ""
CLS
FOR X = 1 TO 1000
CIRCLE(ROW%, COL%),SIZ%, CLR%
NEXT X
WEND
CLS
 

·
IDIC
Joined
·
4,390 Posts
OpenGL, by default, treats counter-clockwise defined polygons as front-facing and clockwise poly's as back-facing.

(please pardon the terrible representation, hopefully the point gets across though)

1
23

^That's a front-facing polygon by default in OpenGL

1
32

^That's a back-facing polygon

In opengl, you can also modify this default behavior and basically swap the definitions.

Whenever you define polygons, you must always make sure to define them in the same clockwise or counter-clockwise motion, otherwise you'll have front and back facing polygons facing the same way...and if you're culling (not displaying) back-facing polygons, you'll have "holes" in your polygon object where back-facing polygons are actually facing foward..
have i confused you yet? Drop me a line and i'll try to explain it in more detail :)

-Aaron
'94 R/T TT
'95 VR-4 Spyder
'95 Pearl Yellow R/T (belongs to Mom)
 

·
West Side!
Joined
·
1,201 Posts
Discussion Starter #11
Actually I found that exact same info on a website describing culling and other assorted info... It took me a while but I get it now... Just in theory though, I haven't ever done any work with polygon rendering and from what I've read tonight it actually looks more interesting than I thought.
thanx all, I just have to hope I get at least an interview now... I doubt it but hey for once I'm being positive, god know I need a new job.
laters,
Gene Crumpler
1992 Stealth ES
 

·
Registered
Joined
·
4,503 Posts
><i>I'm actualy writing these two functions for a job application test for EA, I have untill 3:30pm tomorrow</i>

And the answer to that is this - "Is this typical of the working conditions at your company? Does your company place more value on efficient code than it does on well-designed, documented, and tested code, code that can be easily comprehended, modified, and enhanced by others? Do your programmers all work in isolation from each other? Is it more important to you to have crappy software that met a deadline than it is to have high quality software that meets requirements and good engineering standards? Thanks, but no thanks, I'll keep looking."

This isn't high school anymore - in the real world, it's okay to cheat by asking other people for help (as you are doing right now on the internet). In fact, it's downright beneficial to a company that the employees cheat by sharing their knowledge.

Unless they're particularly stupid employees, like where I work.
 

·
Registered
Joined
·
4,503 Posts
And while you're at it...

><i>and capitol letters</i>

><i>and calculating there product</i>

These assholes can't even spell properly. Are you sure you really want to work for them? From personal experience - take your skills and look elsewhere. You'll get nothing but frustration from working for dinks like that. This will be the kind of place that thinks planning to process training while accessing valuing diverse quality paradigms of market positioning for tasking touching bases is what makes software good, while out in the field their software is spilling heavy water all over the floor of the reactor.
 

·
Registered
Joined
·
154 Posts
Iagree

Andrew's right, and moreover, most of these big companies are looking for the cleanest, easiest to read code, not the fastest. The thing is, you can spend an extra month devloping the fastest code in the world, or you can write the easiest to understand and read code and let the processor speed catch up to it in a month or two. Believe me I hate this. I was a big assembly fan when I was younger, but it's just not cost effective. Most companies know they have you for a year or two tops andthen they're going to have to fill your position with someone else. Rather than redesigning everything you've done, they want it easy enough to use that someone else can just jump right in. Now if you happen to be applying as a game programmer for EA(higly likely), all bets are off. Games are getting more and more kludgy, but optimization actually DOES count in games. Cramming AI, physics, 3D, and interface instructions into every frame at a silky smooth 50 fps is tough work no matter what processor you're using. But with DirectX and such, a lot of the hard work is taken off your hands. Okay I'm rambling now. I'd just say try to see if they'll let you submit two versions, the hack fast way, and the pretty easily readable way.
 

·
Registered
Joined
·
154 Posts
Okay, I had to

I can never pass up a challenge, so here's my take on it.
<pre>
#include <iostream.h>

void main(){

char hexa[100];
int dec=0;
int curdec;
bool error=false;

cout << "Please input a hex number: ";
cin >> hexa;
for (int i=0;hexa;i++){
curdec=hexa-48;
if (curdec >= 0 && curdec < 10){ dec=dec<<4; dec+=curdec ;}
else if(curdec>16 && curdec <23){
dec=dec<<4;dec+=curdec-7;
}else{
cout<<"Not Hex\n";error=true;break;}


}
if (!error) cout<<"The number in decimal is: "<<dec<<"\n";
}
</pre>

Okay so it works and checks for errors and whatnot. It accumulates as you go, avoiding the power thing and the second loop. the dec=dec<<4 line, multiply the current number by 16(bitwise shift left 4 places) and then add the new number. I also played on ascii with the minus 48 thing in curdec=hexa-48; line. Ascii 48 is the char 0, and ascii 65 is the char A. These then go up in order, so the char 1 is ascii 49, etc. If it's a number we just accumulate and move on. If it's a letter, we have to subtract 7 to account for the seven characters between 9 and A, and then we add it. If it's ANYTHING else we throw an error. It may not be the best, and yes if you weren't using ascii(or i believe unicode would work) this wouldn't work, but there are very few systems that don't use ascii characters. This removes a lot of the slow if thens that a select statement is compiled into. The bitwise shift may be overkill, because the compiler if decent will recognize that multiply by 16 means shift left 4 bits. Plus this allows you to put an insanely large number and it will spit something out, even if it's wrong, whereas a multiply by 16 will barf when you reach the limit of an int. Do whichever. I think the shift left shows a better understanding of programming and optimization, but like I said it may be overkill. Anyway, I've wasted enough of my day with this. Good luck on the job. I'd love to work for ea, but relocating to CA or FL isn't an option. I'd kill to work on a good lacrosse game based on the madden engine, but that's just me.
 

·
Registered
Joined
·
154 Posts
It cut off my post

Okay so it works and checks for errors and whatnot. It accumulates as you go, avoiding the power thing and the second loop. the dec=dec<<4 line, multiply the current number by 16(bitwise shift left 4 places) and then add the new number. I also played on ascii with the minus 48 thing in curdec=hexa-48; line. Ascii 48 is the char 0, and ascii 65 is the char A. These then go up in order, so the char 1 is ascii 49, etc. If it's a number we just accumulate and move on. If it's a letter, we have to subtract 7 to account for the seven characters between 9 and A, and then we add it. If it's ANYTHING else we throw an error. It may not be the best, and yes if you weren't using ascii(or i believe unicode would work) this wouldn't work, but there are very few systems that don't use ascii characters. This removes a lot of the slow if thens that a select statement is compiled into. The bitwise shift may be overkill, because the compiler if decent will recognize that multiply by 16 means shift left 4 bits. Plus this allows you to put an insanely large number and it will spit something out, even if it's wrong, whereas a multiply by 16 will barf when you reach the limit of an int. Do whichever. I think the shift left shows a better understanding of programming and optimization, but like I said it may be overkill. Anyway, I've wasted enough of my day with this. Good luck on the job. I'd love to work for ea, but relocating to CA or FL isn't an option. I'd kill to work on a good lacrosse game based on the madden engine, but that's just me.
 

·
Grrr, baby, grrr
Joined
·
6,116 Posts
By the way,

If you'd like a slightly more detailed description of front and back facing triangles:

Given triangle ABC, you'd first calculate the vector of AB, and of BC.

Then, you would find the cross product of the two vectors.

The resulting vector would be your normal. Look at the direction the normal is going. (It will be a vector). It'll show you which way the triangle is facing, as it will be perpindicular from the surface of the triangle.
 

·
West Side!
Joined
·
1,201 Posts
Discussion Starter #18
jschloer- That program is good... I was not thinking of the problem in those terms but it works really well. I actually just started trying to work with bitwise operators to answer the brain teaser somebody posted on the board last night... can't seem to get it to work right though... thanx and if you don't mind I'm going to add that as another option for speed when I turn all this in today...
laters,
Gene Crumpler
1992 Stealth ES
 

·
West Side!
Joined
·
1,201 Posts
Discussion Starter #20
I don't remember who posted it but it was basically to find a 10 digit number using the digits 0-9 only once whose first n digits are evenly divisible by n. ie. the first two digits by 2, first 3 by 3, ect...
and don't worry I'll definatly let everybody know if i somehow manage to get the job...
thanx all,
Gene Crumpler
1992 Stealth ES
 
1 - 20 of 23 Posts
Top