In this lesson you will learn how to all of them and will we be using variables a lot in these examples but no in all of them to show you the many ways you can use them.
First we will use them without variables so I am going to print out result of using all the operators this is shown below i won't be comment on every line of code as before to speed up things.
#include<iostream> using namespace std; int main() { //addition cout <<"Add 5 and 2: " << 5 + 2<<endl; //subtraction cout <<"Minus 5 and 2: "<< 5 - 2<<endl; //division cout << "Divide 5 and 2: " << 5 / 2<<endl; //multiplication cout << "Multiple 5 and 2: " << 5 * 2<<endl; //modulo cout << "Modulo 5 and 2: remainder " << 5 % 2<<endl; system("pause>nul");//doesn't print press any key to continue... when it pause }
That is basically they work I'm not sure if i can break it down any further than showing you the code. but i will show you example how modulo works or alternative to get modulo. and this is shown in the code below as I said modulo is just the remainder of from a division so that what we are going to do find the remain of a division it may seem useless just because i am not using variables.
#include<iostream> using namespace std; int main() { //modulo cout << "normal modulo 5 and 2: remainder " << 5 % 2<<endl; //alternative modulo cout << "alernative mod 5 and 2: remainder " << 5 - (5/2)*2<<endl; //with varibles to make it easier to undestand //declare varibles int num = 5; int num1 = 2; cout <<"alernative with varibles mod 5 and 2: remainder " << num -(num/num1)*num1; system("pause>nul");//doesn't print press any key to continue... }
The theory behind this is pretty simple think about it you have 5 and 2 you want the remainder you know it is 1 right so you probably would say hey the remainder is just the difference of the total that is the 5 minus how much times 2 go in 5 and then that times two to find out exact value that it stops at which is 4 because 2 goes in to five 2 time so 2*2 equal 4 and then 5-4 = 1 easy right?
Okay so now i show how to manipulate variables and operators like in line 17 in the above code.
Here is the senearo person want you to calculate the pay for a week for a person who work 40 hours for the week at a rate of dollars per hour in order to do this you need to multiple the hours work by the pay rate.
#include<iostream> using namespace std; int main() { double hoursWorked = 40.5;//he worked 40 hours double payRatePerHour = 20.3;//pay rate is $20.5 a hour //we use double datatype because of decimals cout << "His Pay is $" << hoursWorked * payRatePerHour<<"\n"; //you could also do this //save pay in a new varible double pay = hoursWorked * payRatePerHour; //display varible cout << "His Pay is $" << pay<<endl; //you can even do this to show how much the person work //and the pay rate cout << "\nWorked: "<<hoursWorked<< "\nPay Rate: " << payRatePerHour << "\nPay: $" << pay<<endl; system("pause>nul");//doesn't print press any key to continue... }
Hope you understand the above, okay so now same seniro but this time the person have sales that he made for the week the commission is 20% of all his sale that means for every thing he sells he get 20% of the price of the item he sold 29 items and the cost of a item is $10 okay so here we go
#include<iostream> using namespace std; int main() { double hoursWorked = 40.5;//he worked 40 hours double payRatePerHour = 20.3;//pay rate is $20.5 a hour //we use double datatype because of decimals // and always use double when its money too double numberOfItemsSold = 29;//amount of items sold double priceOfItem = 10;//cost for the item cout << "His Pay is $" << hoursWorked * payRatePerHour + 0.20*(numberOfItemsSold*priceOfItem)<<"\n"; //you could also do this //save pay in a new varible double pay = hoursWorked * payRatePerHour; //so we have the pay without the item so we just add commission to it pay = pay + 0.20*(numberOfItemsSold*priceOfItem); //display varible cout << "His Pay is $" << pay<<endl; //you can even do this to show how much the person work //and the pay rate cout << "\nWorked: "<<hoursWorked<< "\nPay Rate: $" << payRatePerHour <<"\nCommission: $"<<0.20*(numberOfItemsSold*priceOfItem) << "\nPay: $" << pay<<endl; system("pause>nul");//doesn't print press any key to continue... }
pay += 0.20*(numberOfItemsSold*priceOfItem); //Or with different operator different result tho pay *= 0.20*(numberOfItemsSold*priceOfItem);
I could similarly do this and get the same result and i can not just do it with this
pay = pay + 0.20*(numberOfItemsSold*priceOfItem); //Or with different operator different result tho pay = pay * 0.20*(numberOfItemsSold*priceOfItem);
Please note a variable's value can be changed after declaring it unless of course you put the key const example
#include<iostream> using namespace std; int main() { int a = 10; cout << a <<endl; a = 2; cout << a <<endl; a = a*a;//a square //Or a *= a; cout << a; //you can't change if declared as a constant const int b = 100; //this mean its a read only type if you try //change it complier will give an error //example b = 50 is an error because it read only type cout << b; system("pause>nul"); }
These are just some of the great games they have out there the more you play pass them the more you can overcome problems you become a better thinker basically. One of my all time favorite puzzle game is armadillo run this game was worth buying you can check out the demo this one really will get you thinking.
No comments:
Post a Comment