While User Guilible

This is another simple project using while loops and if statements.

The program continues to ask the user to input a number other than the number equal to the number of tries they’re had. If the user does, they lose. If they are patient for ten tries, they win.


#include <iostream>

using namespace std;

int main() {
 int i = 0;
 int x;

while (i<10 && x!=i-1){
 cout << "Please enter a number!\nAny number...\nNOT "<< i << "!!!\n";
 cin >> x;
 i += 1;
 }

 if (i==10){
 cout << "Wow, you're more patient then I am, you win.";
 }
 else{
 cout << "Hey! you weren't supposed to enter " << i-1 << "!";
 }


 }

Advertisements

Pancake Glutton

This is a simple project that uses logic (if statements) and loops (for).

The user inputs the number of pancakes ten people have eaten. It sorts the list and outputs in order.

#include <iostream>

using namespace std;

int main() {
 int numberOfPancakes[10];

 int person[10] = {1,2,3,4,5,6,7,8,9,10};
 int minPancakes = 100000;

 cout << "How many pancakes did you eat this morning?\n";

 //iterates from 0-9
 for (int i=0; i<10; i++){

 cout << "Person " << i+1 << "?";
 cin >> numberOfPancakes[i];

 }

 //SORTING USING BUBBLE SORT
 for (int i=0; i<10; i++){
 for(int j=0; j<9; j++){
 if(numberOfPancakes[j]>numberOfPancakes[j+1]){

 int temp = numberOfPancakes[j+1];
 int temp2 = person[j+1];

 numberOfPancakes[j+1] = numberOfPancakes[j];
 person[j+1] = person[j];

 numberOfPancakes[j] = temp;
 person[j] = temp2;
 }
 }
 }

 for(int i=0; i<10; i++){
 cout << "Person " << person[i];
 cout << " ate " << numberOfPancakes[i] << " pancakes." << endl;
 }

}

Bubble Sort Algorithm

After first sort. The highest number has bubbled to the end of the array.

After first sort. The highest number has bubbled to the end of the array.

The Bubble Sort arranges the numbers in ascending order. To do this first it compares the first element with the element on it’s right; if the first element is larger; the two elements swap. It then moves onto the second element and repeats the comparing process. It continues this until the (n-1)th element; by this time, the largest number should be in the nth element. The largest number bubbles to the top! See Image above for a more visual explanation.

Now the largest number is in the nth element, we can say it has been sorted. Now we have a slightly smaller array to sort.

The second sort begins with the first element, compares, swaps if necessary, up to the (n-2)th element. It doesn’t need to check the last element because it’s already sorted.┬áThe third sort begins with the first element, compare, swaps if necessary, up to the (n-3)th element. The fourth, fifth, sixth sort continue in the same way until the array is sorted.

The total number of comparisons is equal to n(n-1)/2. This can be simplified to n^2/2 – n/2, where n^2 dominates. The bubble sort is simple and effective for small arrays, but this is a very long process as n increases.

Here’s an example of using it in a program:

</pre>
int main(){

 //Array I want to sort.
 int a[7] = {1,5,9,6,2,3,8};

 //Finding size of array
 int s = sizeof(a)/sizeof(*a);

 //Bubble Sort
 for (int i=0; i<s; i++)
 {
 for (int j=0; j<s-1; j++)
 {
 if (a[j]>a[j+1])
 {
 int temp = a[j+1];
 a[j+1] = a[j];
 a[j] = temp;
 }
 }
 }
 for (int i=0; i<s; i++){
 cout << a[i];
 }
}
<pre>

Strings

A String is an array of characters; like a word or a sentence.

Here’s an example:


#include <iostream>

using namespace std;

int main()
{
 //Declare string size 100.
 char string[100];

 //Prompt user to input a word.
 cout << "Please enter a word." << endl;

 //Reads input, store in string, \n waits for user to push enter.
 cin.getline(string, 100, '\n');

 //Outputs first character in string.
 cout << "Your word started with a: " << string[0] << endl;
 cin.get();
}

Arrays

I just finished a course in Python which I used lists for everything. So I couldn’t wait to learn arrays.

But C++ does not make it easy… where’s my .sort function?

An Array is a list of elements, each with it’s own index; kind of like a set of shelves.

Here’s a simple example:


#include <iostream>

 using namespace std;
 int main()
{
 //DECLARED A NEW ARRAY, TYPE INT, SIZE 8.
 int array[8];

 //ASSIGNING VALUES 0-7 TO MY EIGHT ELEMENTS
 for(int x=0; x<8; X++)
 cin>> ;

 //OUTPUT ARRAY TO SCREEN ONE ELEMENT AT A TIME
 for(int x = 0; x<8; x++)
 cout<<array[x];

 return 1;
}

Note: I have only used type int here, and I can use other numerical types easily. I will write another post on using characters to make words. For now, I don’t need them.

Note II: Next post, how to sort!

Note III: Just for fun, here’s a 2D array.


//DECLARING A 2D ARRAY

int twodimensionalarray[8][8];

//HOW TO ACCESS EACH ELEMENT

twodimensionalarray[arrayindexnumber1][arrayindexnumber2] = someInt;

Switch Case

At university I was lazy when it came to programming, and relied on IFs, FORs and WHILEs, and didn’t get much further.

Here’s a switch statement. It’s kind of like a IF statement.


int a;

int b;

int c;

switch (a) {

case b:

//code

break;

case c:

//code

break;

default:

//code

break;

}

A quick explanation:

If a is equal to Case b do the code, etc. The breaks are there to exit the loop, otherwise the program will fall to the next one, and eventually output the default too. The default is like else.

A good example of using the Switch Case is a cola machine; the user has to input the number assigned to the drink.


#include <iostream>

using namespace std;

int main()
{
 int input;

 cout<<"1 Coke\n";
 cout<<"2 Water\n";
 cout<<"3 Sprite\n";
 cout<<"4 Fanta\n";
 cout<<"5 Pocari Sweat\n";
 cin>> input;
 switch (input) {
 case 1:
 cout<<"Your Coke is ready!";
 break;
 case 2:
 cout<<"Your Water is ready!";
 break;
 case 3:
 cout<<"Your Sprite is ready!";
 break;
 case 4:
 cout<<"Your Fanta is ready!";
 break;
 case 5:
 cout<<"Your Pocari Sweat is ready!";
 break;
 default:
 cout <<"Error. choice was not valid, here is your money back.\n";
 break;
 }
 cin.get();

}

 

Functions

When I was at university I had a mental block when it came to functions. I’m slowly getting used to them.

They are important. We take them for granted, especially in a language such as Python, every line I wrote included a built in function. It’s a good idea to use functions, it’s more efficient and when it comes to the next time you want to use a similar method, you already have your template.

I’m going to keep this brief, as I’m not 100% confident with functions yet. Here’s an example of an Addition Function:


#include <iostream>

using namespace std;

/* This function takes in two arguments a and b.

Then calculates the sum and return the answer */

int addition(int a, int b)
{
 int r;
 r = a + b;
 return r;
}

int main()
{
 int z;
 z = addition(5,3);             // Calling the Addition function
 cout << "The result is " << z;
}

Just a note, the addition function is type int because it returns an integer. A function with type void has no return value.