This is just some random lines of code to demonstrate variables and assignment

#include<iostream>
#include<limits> // we will talk about this below
 
using namespace std;
 
int main() {
	int totalAttendees;
 
	cout << "Part 1: Example from physical demonstration" << endl;
	cout << "-------" << endl;
	cout << endl;
	const int OZ_PER_PERSON = 16;
	const int OZ_PER_GALLON = 128;
	int gallonsNeeded;
 
	totalAttendees =  24;
	gallonsNeeded = (totalAttendees * OZ_PER_PERSON) / OZ_PER_GALLON;
 
	cout << "Gallons needed is: " << gallonsNeeded << endl;
	cout << endl;
 
	cout << "Part 2:" << endl;
	cout << "-------" << endl;
	cout << endl;
	cout << "Let's look at these ideas in greater detail..." << endl;
	cout << endl;
	cout << "From the demonstration we can see that computers can do math (evaluate expressions):" << endl;
	cout << "the sum of 23 and 56 is " << 23 + 56 << endl;
	cout << endl;
 
	cout << "... and computers can remember:" << endl;
	// Remember an integer:
	int dogs = 37;
	cout << "dogs: " << dogs << endl;
	int drinks; // no value, allowed by c++ but not a good idea
 
	// print them out
	// cout << "dogs:" << dogs << " and drinks: " << drinks << endl;
	drinks = 4;
	cout << "dogs: " << dogs << " and drinks: " << drinks << endl;
 
	cout << "Change the number of drinks:" << endl;
	drinks = dogs + 8;
	cout << "dogs: " << dogs << " and drinks: " << drinks << endl;
	cout << endl;
        // in contrast constants can not be changed: OZ_PER_PERSON = 20;
 
	cout << "The Naming of Cats is a difficult matter... Sorry TS" << endl;
	cout << "I mean the naming of VARIABLES is a difficult matter:" << endl;
	// Identifiers names start with letter or "_",
	//    and contain letters, numbers or "_"
	//    no spaces, no "reserved words"
	// int int;
	// int total dogs;
	int d0gs = 44; // don't do that!
	cout << "d0gs: " << d0gs << " works but don't make your names hard to read." << endl;
	cout << "Likewise names are case sensitive so \"Dogs\" is a different name:" << endl; 
	int Dogs = -42; // be consistent. names are case sensitive. we will use
	// capitalization for other purposes later
	cout << "dogs: " << dogs << " Dogs: " << Dogs << endl;
	// Required Style:
	// Use two word variable names and use a capital letter to separate them
	// but not at the beginning (we will use initial caps for something else.
	// This is called camel case. Note that the names of variables does not affect how they work.
	int totalDogs = 42;
	int totalDrinks = totalDogs + 8;
	cout << "dogs: " << totalDogs << " and drinks: " << totalDrinks << endl;
	cout << endl;
 
	cout << "Part 3:" << endl;
	cout << "-------" << endl;
	cout << endl;
	cout << "There are limits on the magnitude of numbers you can store:" << endl;
	// See https://msdn.microsoft.com/en-us/library/s3f49ktz.aspx
	totalDrinks = 2147483647;
	cout << "drinks: " << totalDrinks << endl;
 
	// mydrinks = totalDrinks + 1 // they must be defined
	cout << "Add one to the total number of drinks, which should be 2147483648, but..." << endl;
	totalDrinks = totalDrinks + 1;
	cout << "drinks: " << totalDrinks << endl;
	cout << "Really??, stupid computer! No. Foolish programmer!\n";
	// C++ knows its limits:
	cout << "A int can only store values starting at " << numeric_limits<int>::min() << endl;
	cout << " and ending with " << numeric_limits<int>::max() << endl;
	// note that we included limits at the start.
 
	// lets fix that before we go on
	totalDrinks = totalDogs + 8;
	cout << endl;
 
	cout << "Part 4:" << endl;
	cout << "-------" << endl;
	cout << endl;
	cout << "C++ is very sensitive to the TYPE of data stored in its variables:" << endl;
 
   cout << "floats and doubles store values with decimal values:" << endl;
	float liquidInOz = 34.5;
	double lengthInInches = 123.0;
	cout << "liquid: " << liquidInOz << " brick: " << lengthInInches<< endl;
	cout << endl;
 
	cout << "Rarely worth the trouble of using a float, they also store values that might contain decimal points" << endl;
	cout << "(the following not scientifically factual)" << endl;
	float distanceToNearestInhabitableStar = 0.9E38;
	float distatnceToFurthestInhabitableStar = distanceToNearestInhabitableStar*distanceToNearestInhabitableStar;
	cout << "Distance to nearest inhabitable star: " << distanceToNearestInhabitableStar << endl;
	cout << " distatnce to furthest inhabitable star: " << distatnceToFurthestInhabitableStar << endl;
	cout << "Floats know when they have exceeded their range and say \"inf\"" << endl;
	double distanceToNearestKnownGalaxy = 0.9E120;
	double distanceToFurthestKnownGalaxy = distanceToNearestKnownGalaxy*distanceToNearestKnownGalaxy;
	cout << "Distance to nearest known Galaxy: " << distanceToNearestKnownGalaxy << endl;
	cout << " distance to furthest known galaxy: " << distanceToFurthestKnownGalaxy << endl;
	cout << "Doubles hold really big numbers." << endl;
	cout << endl;
 
	//rounding:
	double priceOfCar = 12432.80;
 
	cout << "C++ does not round by default, it truncates:" << endl;
	int approximatePrice = priceOfCar;
	cout << "price " << priceOfCar << " or about " << approximatePrice << " dollars?\n";
	// This will not work here, but will in other c++ environments: approximatePrice = round(priceOfCar);
	// cout << "price " << priceOfCar << " or about " << approximatePrice << " dollars?\n";
	// round was added in c++11 so it may not work in earlier versions. Also Microsoft
	// did not put this feature in visual studio until the 2013 version.
	// The old way to round was:
	cout << "Round by adding 0.5:" << endl;
	approximatePrice = priceOfCar+0.5;
	cout << "price " << priceOfCar << " or about " << approximatePrice << " dollars?\n";
	cout << endl;
 
   cout << "There is an implicit change of type here, we prefer an explicit change." << endl;
	cout << "We can control and make explicit the change by \"casting\"." << endl;
 
	cout << "price " << static_cast<int>(priceOfCar+0.5) << ". Please use static_cast<int>(price)" << endl;
	// These also work, but we will not use them.
	// cout << "price " << int(priceOfCar+0.5) << endl;
	// cout << "price " << (int)(priceOfCar+0.5) << endl;
	// This is syntactially correct but does not do the same thing.
	// cout << "price " << (int)priceOfCar+0.5 << endl;
	cout << endl;
 
  	cout << "Part 5:" << endl;
	cout << "-------" << endl;
	cout << endl;
        cout << "Math, we have to write it sequentially:" << endl;
	totalAttendees = 123;
	double milkInGallons = (totalAttendees * 16.0) / 128.0;
	// c++ follows the normal order of operations: double milkInGallons = totalAttendees * 16.0 / 64.0;
	cout << "Milk: " << milkInGallons << " gallons." << endl;
	cout << endl;
 
	cout << "Be careful on mixing types in an expression, unintended integer division:" << endl;
	milkInGallons = (totalAttendees * 16) / 128;
	cout << "Milk: " << milkInGallons << " gallons." << endl;
	cout << endl;
 
	cout << "No magic numbers in code! Better, even though it does the same thing." << endl;
	// we defined these at the top of the program, so we do not need them again.
	// const int OZ_PER_PERSON = 16;
	// const int OZ_PER_GALLON = 128;
 
	// By the way you can not change these OZ_PER_PERSON = 32; // try it if you like, but it is not allowed
	milkInGallons = (totalAttendees * OZ_PER_PERSON) / OZ_PER_GALLON;
	cout << "Milk: " << milkInGallons << " gallons." << endl;
	cout << endl;
 
	cout << "But we still have that unintended integer division problem." << endl;
	cout << "We can fix that by having at least one double or float in the division." << endl;
	milkInGallons = (totalAttendees * OZ_PER_PERSON) / static_cast<double>(OZ_PER_GALLON);
	cout << "Milk: " << milkInGallons << " gallons." << endl;
	cout << endl;
 
	cout << "Sometimes we WANT integer division:" << endl;
   totalAttendees = 345;
   const int DOGS_PER_CRATE = 80;
   int cratesNeeded = totalAttendees / DOGS_PER_CRATE; // is that what we want?? Yes
   int strayDogsNeeded = totalAttendees / DOGS_PER_CRATE;
	cout << "For " << totalAttendees << " people, we will need " << cratesNeeded << " crates of hotdogs " << endl;
	cout << "\tand " << strayDogsNeeded << " strays." << endl;
	cout << endl;
 
   cout << "Suppose, we could buy just packages of dogs in addition to whole crates:" << endl;
 	const int DOGS_PER_PACKAGE = 8;
	int packagesNeeded = strayDogsNeeded / DOGS_PER_PACKAGE;
	strayDogsNeeded = strayDogsNeeded % DOGS_PER_PACKAGE; // Remember "="  means assignment
	cout << "For " << totalAttendees << " people," << endl;
   cout << "\tWe will need " << cratesNeeded << " crates of hotdogs," << endl;
	cout << "\t" << packagesNeeded << " packages of hotdogs" << endl;
	cout << "\tand " << strayDogsNeeded << " strays." << endl;
	cout << endl;
 
  	cout << "Part 6:" << endl;
	cout << "-------" << endl;
	cout << endl;
	cout << "Short hand:" << endl;
	cout << "add one to strays" << endl;
	strayDogsNeeded++;
	cout << "strays: " << strayDogsNeeded << endl;
	cout << "strays (minus one after printing): " << strayDogsNeeded-- << endl;
	cout << "strays: " << strayDogsNeeded << endl;
	cout << "strays: (plus one before printing):" << ++strayDogsNeeded << endl;
	cout << "strays: " << strayDogsNeeded << endl;
	cout << "Add 47 to strays." << endl;
	strayDogsNeeded += 47;  // for me!
	cout << "strays: " << strayDogsNeeded << endl;
 
 
	return 0;
}
cs-142/types-and-assignment.txt · Last modified: 2016/09/10 19:48 by kseppi
Back to top
CC Attribution-Share Alike 4.0 International
chimeric.de = chi`s home Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0