Tag: cpp

Simple C++ Bean machine program @KTSI

This is a simple Bean machine program done in C++ for KTSI.

This is how it works, you drop 100 balls and they fall in 6 different boxes.

Bean MachineAnd this is how the C++ output should look like:

Bean Machine outputAnd here is how its done:

#include <iostream>
#include <cstdlib>
#include <ctime>
 
using namespace std;
 
int main (int argc, char * const argv[]) {
 
 
	// initial
	int ballCount = 100;
	int box[6] = { 0, 0, 0, 0, 0 };
	string line = " +-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-->";
	string numbers = " 0     5     10     15     20     25     30     35     40     45     50";
	int leftorright = 0;
 
	// Need for Random Numbers
	srand ( time(NULL) );
 
	// Rund ballCount (100) zahl = rand() % 2;
	for (int i=0; i<ballCount; i++) {
			int counter = 0;
 
		for (int j=0; j<5; j++) {
			// left or right
			leftorright = rand() % 2;
			counter = counter + leftorright;
		}
		box[counter] = box[counter]++;
 
	}	
 
	// Output
	cout << numbers << endl << line << endl;
	for (int t=0; t<6; t++) {
		cout << t << " |";
		for (int u=0; u < box[t]; u++) {
			cout << "#";
		}
		cout << " " << box[t] << endl << line << endl;
	}
	return 0;
}

I also did this program in Powershell



Simple C++ rect2polar program @KTSI

This is another simple C++ program done for KTSI. This one converts rectangular to polar coordinates.

#include 
#include 
 
using namespace std;
 
void rect2polar(double &amp;value, double &amp;angle, double x, double y) {
    value = sqrt(pow(x, 2) + pow(y, 2));
    angle = 180 / M_PI * atan2(y,x);
}
 
int main(){
    double value;
    double angle;
    double x = 4;
    double y = 3;
 
    rect2polar(value, angle, x, y);
 
    cout &lt;&lt; "x = " &lt;&lt; x &lt;&lt; endl;
    cout &lt;&lt; "y = " &lt;&lt; y &lt;&lt; endl;
    cout &lt;&lt; "value = " &lt;&lt; value &lt;&lt; endl;
    cout &lt;&lt; "angle = " &lt;&lt; angle &lt;&lt; endl;
    //system("PAUSE");
    return 0;
}


Simple C++ lottery program @KTSI

Here is a simple C++ lottery program done for the KTSI.

#include <iostream>
#include <cstdlib>
 
using namespace std;
 
int main (int argc, char * const argv[]) {
 
	int numberCount;
	int maxNumbers;
 
	cout << "Lottery Game" << endl << "=================================" << endl;
	cout << "How many Lottery Numbers = ";
	cin >> numberCount;
	cout << "from 1 to ? ";
	cin >> maxNumbers;
	cout << "You have chosen " << numberCount << " Lottery Numbers from 1 to " << maxNumbers << endl;
 
	int lotteryNumbers[numberCount];
	int i, j;
	bool newNumber;
 
	srand(0);
	for(i=0; i<numberCount; i++) // get numbers
	{
		do  
		{   // Check Random
			lotteryNumbers[i] = rand() % maxNumbers + 1;
			newNumber = true;
			for (j=0; j<i; j++)
			{  
				if (lotteryNumbers[j]==lotteryNumbers[i])
				{ // Check for existing numbers
					newNumber = false;
				}
			}
		} while (!newNumber);
	}
	for (i=0; i<numberCount; i++)
	{
		cout << lotteryNumbers[i] << " ";
	}
	cout << endl;
}