The first one I created solves for the hypotenuse vector of a right triangle, using the Pythagorean Theorem. This program is rather straightforward, and took little to no time to write.
// Hypotenuse
#include <iostream>
#include <cmath>
#include <cstdlib>
using namespace std;
int main()
{
int x; //position of x-axis
int y; //position of y-axis
int A; //square A + square B
float V; //resultant vector
cout << "Enter in both adjecants." << '\n';
cin >> x;
cin >> y;
A = (x * x) + (y * y);
V = sqrt ((double) A );
cout<< V << "\n"; //output of resultant vector
cin.get();
cin.get();
return 0;
}
After creating this program in a few minutes, I went on to try something a bit more challenging. This program below took me about two hours to write; a lot of debugging. At first, I tried to write the entire program within the main function, although, that didn't really challenge me or enhance my abilities in any way, and it was extremely buggy. I decided to seperate each step in the process of matrix multiplication into seperate functions, and create my main function as a do while loop. It turned out producing correct results! Here is the code, keep in mind this only works with 2x2 matrices:
#include<iostream>
using namespace std;
/*The functions in this program are fired in the order of top to bottom for readability*/
void Readmatrix(int a, int b, int c, int d, int e, int f, intg, int h)
{
/*This function records the integers placed into each matrices' variable*/
cout << "Enter in your first matrix (4 integers): ";
cin >> a;
cin >> b;
cin >> c;
cin >> d;
cout << "Enter in your second matrix (4 integers): ";
cin >> e;
cin >> f;
cin >> g;
cin >> h;
}
void multiply(int a, int b, int c, int d, int e, int f, intg, int h)
{
/*This is the algorithm for multiplying the matrices together. It is only compatible with 2x2 matrices*/
int a1, b1, c1, d1 = 0;
a1 = ((a * e) + (b * g));
b1 = ((a * f) + (b * h));
c1 = ((c * e) + (d * g));
d1 = ((c * f) + (d * h));
a = a1;
b = b1;
c = c1;
d = d1;
}
void Displaymatrix(int a, int b, int c, int d)
{
/*The functions simply displays the 2x2 matrix product*/
cout << a << " " << b << '\n' << c << " " << d;
}
int main()
{
/*The functions is my main loop, which calls all the above functions*/
char rawr;
do
{
int a, b, c, d, e, f, g, h = 0;
Readmatrix(a, b, c, d, e, f, g, h);
multiply(a, b, c, d, e, f, g, h);
Displaymatrix(a, b, c, d);
cout << endl;
cout <<"Would you like to do another matrix (y/n)? ";
cin >> rawr;
cout << endl;
} while ((rawr == 'y') || (rawr == 'Y'));
return(0);
}
I feel like I'm getting a little better each day I do this! Tomorrow, I plan to write out a program that will add, subtract, and reduce fractions. I will be using a reference source code, found here: http://cplusplus.com/files/fraction.zip
I can't wait to start moving onto programs using windows coding! I plan to become accustomed to using arrays, functions, structures, pointers, and dynamic memory before I move onto more advanced concepts though. If I can write simple programs using the tools I just listed, I'll have some fundamental skills to use for those more advanced concepts.
Heh, pretty good. Course, programming a game will be more complex than this, but you'll be there soon enough.
ReplyDeleteHey thanks! Follow along with me if you want :D
ReplyDeleteOnce I hit college, I'll be posting what I learn here on this blog. Might be cool to follow along with me till then, so we can all start at the same level.
"This took me about 2 hours to complete, and about 25 minutes to write. Yep, that means I sat there and debugged the program for over an hour and a half. Sheesh, I hope this isn't what programming is always like ;)"
ReplyDeleteIf you think half an hour of debugging is a lot, you're in for a nasy surprise. I've been known to a week working on a single bug, multiple hours a day. :P
Hour and a half, not half an hour. I didn't actually think it was significant, it was just very annoying- hence my winking face ;)
ReplyDelete