Bashing away in C++

When learning code, pundits say one of the best things you can do is modify existing code to make it do something you want it to do.

For the last few day’s I’ve been going through the The Complete Idiots Complete Guide to a Career in Computer Programming (Jessie Liberty 1999). On page 124-125 it talks about using functions and local variables and includes the code to convert a user entered Farenheit temperature into a Celsius temperature which is then output to screen.

Here’s the original code.

// demonstrates using functions and local variables

#include <iostream>

float Convert(float);
int main()
{
float TempFer;
float TempCel;

std::cout << “Please enter the temperature in Fahrenheit: “;
std::cin >> TempFer;
TempCel = Convert(TempFer);
std::cout << “\nHere’s the temperature in Celsius: “;
std::cout << TempCel << std::endl;
return 0;

}
float Convert(float TempFer)
{
float TempCel;
TempCel = ((TempFer – 32) * 5) / 9;
return TempCel;
}

My cycle computer calculates my energy expenditure in Calories (no idea why it will not show this in kJ, despite being able to configure the system to display metric or imperial during set-up),  I thought I’d modify the program to take a Calorie value and output a kJ value.   As my training log automatically displays energy used in kJ, so the only practical aspect to my code modification is to learn to make software do what I want.  The modified code follows:

// demonstrates using functions and local variables

// modified on 15-05-2104 by Paul Yeatman to convert Calories to kilojoules
#include <iostream>

float Convert(float);
int main()
{
float TempCal;
float TempKjs;

std::cout << “Please enter the energy used in calories (Cal): “;
std::cin >> TempCal;
TempKjs = Convert(TempCal);
std::cout << “\nHere’s the energy used in kilojoules (kJ): “;
std::cout << TempKjs << std::endl;
return 0;

}
float Convert(float TempCal)
{
float TempKjs;
TempKjs = TempCal * 4.1858;
return TempKjs;
}

Of course, once the code is written it needs to be compiled and output to a new file and then run.  To do this, I’m telnetting into a headless server I’ve set up running ubuntu, coding in the nano text editor (it has colour coded hints, but no line numbering) and then compiling and running things using the command line.  I’ve set up my main computer with a compiler as well, but that’s for if I want to port anything into windows.  The beauty of C++ is I only have to write the code once and then use an OS specific compiler to port it to a new system.

*Update 20170309*

Looking at the code, the output’s actually in Joules!  To produce KJ, I needed to add a /1000 to TempKjs = TempCal * 4.1858; making it TempKjs = (TempCal * 4.1858)/1000;

 

*Update 20170728*

While preparing for Stanford’s CS106B as part of my Poor Humans Bachelor of Computer Science, I installed Qt-Creator, a modern IDE.  That’s much better than the round about what I was doing it with my server.  The server might still be good for compiling (I’m yet to check out many of the IDE’s features).

Graduation Looms

There appears to be a little debate regarding the menu for our diploma graduation evening. The theme was chosen to be environmental sustainability.  That is a little naive given any computer based industry thrives on computers and the Internet and these are great carbon dioxide producers.  Not to mention the waste created by “needing” to upgrade equipment every couple of years.

Back to the menu.  Vegetarian fare.  The Vegetarian Society says that the word “vegetarian” is derived from the Latin word vegetus meaning lively or vigorous.  Feed me meat and I’ll be lively.  Feed me roots and leaves and I’ll probably feel dejected.

As I am a microbiologist in my day job, I shall attempt to explain the science by providing one for and one against link addressing each argument. Note, if this was a scientifically rigorous post, I would have cited at least 3 for and against articles.  I’ll also provide translations for the non scientifically minded.  If I cannot find a scientific article, it does not mean they are out there.

Arguments used for far have been:

  • -Cattle used by “greedy” people contribute to 95% of the world’s pollution
  • -Organic food is better for the environment
  • -No food is as bad for the environment as meat.

Apologies for the squished table…this site was not designed with them in mind (opportunity for improvement perhaps).

Argument Supporting Refuting
Cattle are a major producer of “pollution”.The 5th and 12th of August Occam’s Razor radio broadcasts may be of interest. Report from 2006 Public version Study abstract Public version
Organic is better than non organic. can not find any citations. Public version Verena Seufert, Navin Ramankutty, Jonathan A. Foley. Comparing the yields of organic and conventional agriculture. Nature, 2012. Public version
Any food that is not meat is better for the environment. Every internet article I can find refers back to the discredited 2006 FAO study. can not find any citations. Public version This appears to be based on the original FAO – no studies to back it up. Public version
Organic is healthier (just chucked that one in myself) can not find any citations. Public version The science is linked to form the public version. Public Version

Other considerations. Organic food supposedly does not contain genetically modified food. Umm, ever seen a banana – selective breeding (and this modified genetics) created that. This clip is hilarious and shows how uneducated or deluded people can get things totally wrong. If someone could PROVE a statement to me and back it up with reproducible, reliable data, as a scientist, I would be obligated to change my views.

Another example of incorrect information is provided by PETA of India. I am fond of their doppelgänger, People Eating Tasty Animals. Here’s my counter argument:

  1. Help The Poor: Having everyone one eat only veges increases demand and prices go up!
  2. Stop Cruelty to Animals: I am still capable of pouring salt on slugs, kicking mangy dogs or incinerating ants with a magnifying class while eating only carrots.
  3. Save The Environment: From what? The next ice age? I happen to like our current interglacial period and extending it will prolong the next mass extinction event.
  4. Avoid Bird Flu: coulda woulda shoulda. Flawed argument.
  5. Prolong Your Life: unsubstantiated claims.
  6. Avoid the World’s #1 Killer: Malaria is the number one global killer of human, not heart disease. That’s a first world problem.
  7. Reduce Risk of Cancer: Drinking too much water will give you bladder cancer. Staying exposed to the sun will give you skin cancer. The sun is natural. Nakedness is natural. Therefore the cooked lobster look is natural and thus should be healthy. Nope. The argument may be correct, but you’ll spend the rest of your life feeling cold due to a reduced metabolism and you will not be happier, unless you like feeling hungry and chilly all the time.
  8. Fit into a Bikini: I already do (and looked better in one than my girl friend at the time, so she said).
  9. Creat Global Peace: So, vegetarian religious nutters will no longer try to kill me for saying God does not exist or by preferring to worship the holy gourd rather than the holy sandal? FFS.
  10. Discover the Joys of Veges: I love my gherkins, carrots, beans, tofu and chillies. I also happen to love my blue steaks, ribs, thighs and breasts.

Given I’m the apex predator on the planet, evolved to eat everything I can forage, eating meat is natural.  I’ll be packing cheeseburgers.  Can I use the moral high ground as I rode my bicycle to class for much of the last year and am 82kg with a fat percentage of 11%?

Running Out of Time and ARGH!

While saving my second last assignment, I wound out with an out of memory error.  Now when I try to open the file, I get a “file not readable error”.  Given it is 01:00hrs and my work is due in 2 days, I’m not too happy.  Especially if I cannot resurrect the file and I have to start again from scratch.

One that was partially irritating was starting the packaging subject with one lecturer and ending with another. Three weeks into it, the first lecturer liked where I was going with my box (a sort of egg carton 6 pack ice cream container.  I roll up last week and we have a different teacher and this one cans my design – I  have to start from scratch.  Fark!  Not that it would have been a problem if my damn file would open!

Phew, after closing some programs, disaster averted!