Joined
·
1,201 Posts
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.";
}
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.";
}