1
Off Topic / WhatevaGuy is learning C++!
« on: June 05, 2011, 09:06:43 PM »
And he wants to show off a few select programs o:
Also I would like to invite you to show off your select few programs if you wish! :D
Note - if you don't feel like reading all this, just skip down to the code or leave if you like, this is my excited rambling
Anyways, just for the record, I use CodeBlocks as a compiler and it is delightful and I would recommend it to everybody (never mind the fact that I've never used anything else). I am learning through C++ For Dummies (I don't know if this is a quality book or not, but it's done a fairly good job of teaching me so far) and I have made a few programs on my own and a few programs that the book has told me to make.
I have included a WinRAR of every program I've made thus far, and the .exe is located in bin\debug I believe, so if you don't have a compiler to look at the code you can still see the results!
Also, if you do not have a compiler feel free to ask me for any source code, although I don't mean to flatter myself by saying y'all are indubitably enthralled by my delicious programming, I'm just saying the offer is there.
My favorite programs so far are the square number/cube number calculators. I could easily have done a simple loop saying, "multiply n by itself, output that, add one to n, repeat" but I used a new algorithm that I made all by myself for both!
You'll see in the source code.
Also here's a list of the ones that I made because CppFD told me so:
Just as a warning, there are a few very stupid ones that I made early on (especially 'template', I don't know why I made that) like 'death' and 'counter' but alas, I'm too lazy/packrattish to delete them.
Anyways here is the source to the cubenumbers, squarenumbers, and collatzConjectureTotal:
I actually have a set of hypotheses which I have created: "If x = y - 1, then x2 = y2 - (2y - 1)" and "If x = y + 1, then x2 = y2 + (2y + 1)"
I'll provide my equation as soon as I re-figure it out :(
http://en.wikipedia.org/wiki/Collatz_conjecture
If explanation is needed for anything, go ahead and ask me.
By the way you can tell that I have gotten more wise with my use of functions. It's very apparent in clock, less so in cct and nonexistant in mostly everywhere else
Anyways, please give me feedback and tips and perhaps ideas which are reasonable
And I would be happy to see some of your programs as well.
Archive of all my programs so far :D
http://foco.us/bpdO
Also I would like to invite you to show off your select few programs if you wish! :D
Note - if you don't feel like reading all this, just skip down to the code or leave if you like, this is my excited rambling
Anyways, just for the record, I use CodeBlocks as a compiler and it is delightful and I would recommend it to everybody (never mind the fact that I've never used anything else). I am learning through C++ For Dummies (I don't know if this is a quality book or not, but it's done a fairly good job of teaching me so far) and I have made a few programs on my own and a few programs that the book has told me to make.
I have included a WinRAR of every program I've made thus far, and the .exe is located in bin\debug I believe, so if you don't have a compiler to look at the code you can still see the results!
Also, if you do not have a compiler feel free to ask me for any source code, although I don't mean to flatter myself by saying y'all are indubitably enthralled by my delicious programming, I'm just saying the offer is there.
My favorite programs so far are the square number/cube number calculators. I could easily have done a simple loop saying, "multiply n by itself, output that, add one to n, repeat" but I used a new algorithm that I made all by myself for both!
You'll see in the source code.
Also here's a list of the ones that I made because CppFD told me so:
- booltest
- breakdemo
- loops
Just as a warning, there are a few very stupid ones that I made early on (especially 'template', I don't know why I made that) like 'death' and 'counter' but alas, I'm too lazy/packrattish to delete them.
Anyways here is the source to the cubenumbers, squarenumbers, and collatzConjectureTotal:
I actually have a set of hypotheses which I have created: "If x = y - 1, then x2 = y2 - (2y - 1)" and "If x = y + 1, then x2 = y2 + (2y + 1)"
Code: (squarenumbers) [Select]
//Lists square numbers up to a user-defined point.
#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;
int main(int nNumberofArgs, char* pszArgs)
{
//Set up all necessary variables.
int square;
int root;
int additive;
int limitdecision;
int limit;
//Define a few variables.
square = 0;
root = 0;
additive = 1;
cout << "Would you like a (0) root- or (1) square- limit? ";
cin >> limitdecision;
switch (limitdecision)
{
case 0:
break;
case 1:
break;
default:
return 0;
}
cout << "Please enter the limit for the square numbers: ";
cin >> limit;
limit += 1;
//Loop time!
if (limitdecision == 0)
{
while (root < limit)
{
cout << square << " - " << root << endl;
square += additive;
root += 1;
additive += 2;
}
system("PAUSE");
return 0;
}
if (limitdecision == 1)
{
while (square < limit)
{
cout << square << " - " << root << endl;
square += additive;
root += 1;
additive +=2;
}
system("PAUSE");
return 0;
}
}
I'll provide my equation as soon as I re-figure it out :(
Code: (cubenumbers) [Select]
//Lists square numbers up to a user-defined point.
#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;
int main(int nNumberofArgs, char* pszArgs)
{
//Set up all necessary variables.
int cube;
int root;
int additive1;
int additive2;
int limitdecision;
int limit;
//Define a few variables.
cube = 0;
root = 0;
additive1 = 1;
additive2 = 6;
cout << "Would you like a (0) root- or (1) cube- limit? ";
cin >> limitdecision;
switch (limitdecision)
{
case 0:
break;
case 1:
break;
default:
return 0;
}
cout << "Please enter the limit for the cubed numbers: ";
cin >> limit;
limit += 1;
//Loop time!
if (limitdecision == 0)
{
while (root < limit)
{
cout << cube << " - " << root << endl;
cube += additive1;
additive1 += additive2;
additive2 += 6;
root += 1;
}
system("PAUSE");
return 0;
}
if (limitdecision == 1)
{
while (cube < limit)
{
cout << cube << " - " << root << endl;
cube += additive1;
additive1 += additive2;
additive2 += 6;
root += 1;
}
system("PAUSE");
return 0;
}
}
http://en.wikipedia.org/wiki/Collatz_conjecture
Code: (collatzConjectureTotal) [Select]
//Collatz Conjecture - 1 can be derived from any integer if, while the integer is even,
//it is divided by two and, if it is odd, it is multiplied by three and increased by one.
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <windows.h>
using namespace std;
int collatzConjecture()
{
int num;
cout << "Please enter a number: ";
cin >> num;
system("CLS");
//as long as the number is NOT one
while(num != 1)
{
//output the number for client
cout << num << endl;
//check if the number is odd or even
bool oddeven;
oddeven = (num % 2 == 0);
//take certain action, depending on whether num is odd or even
switch(oddeven)
{
case 1:
num = num/2;
break;
case 0:
num = (num*3) + 1;
break;
}
}
cout << "1\n\n";
cout << "Done!\n\n";
system("PAUSE");
return 0;
}
void collatzConjectureTwo()
{
//setup...
string oper;
int delay;
int num2;
cout << "Starting number: "; //ask for starting number
cin >> num2;
cout << "Delay (millisecs): "; //ask for delaytime
cin >> delay;
//loop forever (unless user inputs 0 or less..)
while (num2 > 0)
{
//clearscreen
system("CLS");
int num1 = num2; //set number1 to be number 2
oper = "start"; //make sure this always reads 'start' on the first time
while(num1 != 1) //as long as number1 is not 1..
{
cout << num1 << " - " << num2 << " - " << oper << endl; //output EVERYTHING
Sleep(delay); //sleep for however long user wants
bool oddeven; //test whether or not the number is even
oddeven = (num1 % 2 == 0); //this is not the work of me, by the way, so don't go giving me credit for this line. the rest is me though
switch(oddeven)
{
case 1: //if even, divide by two
num1 = num1/2;
oper = "n/2";
break;
case 0: //if odd, multiply by three and add 1
num1 = (num1*3) + 1;
oper = "3n+1";
break;
}
}
cout << "1 - " << num2; //finish it up
system("CLS");
num2 += 1; //add one two num2 so we can do the same thing with the next number :D
}
}
int main(int nNumberofArgs, char* pszArgs[])
{
int collatzchoice;
cout << "Collatz Conjecture 1 (1) or 2 (2)? ";
cin >> collatzchoice;
switch(collatzchoice)
{
case 1:
system("CLS");
collatzConjecture();
return 0;
case 2:
system("CLS");
collatzConjectureTwo();
}
}
If explanation is needed for anything, go ahead and ask me.
By the way you can tell that I have gotten more wise with my use of functions. It's very apparent in clock, less so in cct and nonexistant in mostly everywhere else
Anyways, please give me feedback and tips and perhaps ideas which are reasonable
And I would be happy to see some of your programs as well.
Archive of all my programs so far :D
http://foco.us/bpdO