Wednesday 28 November 2012

Converting Inches to CM Part 2

Purpose of Program


This is an improvement on the last version, you can choose to either convert CM to inches or the other way round.

Program



#include 

using namespace std;

// a program that converts cm to inches

int main()
{
    // we declare a numerical constant.
    const float cstCentPerInch = 2.54;

    // menu variable
    string menuchoice ="";

    // declare a floating point number.
    float cm, inches;

    cout << "Select an option:" << endl;
    cout << "1. Inches to CM " << endl;
    cout << "2. CM to Inches " << endl;
    cin >> menuchoice;


     if (menuchoice=="1"){
            //Enter the length
            cout << "Enter a length in inches: ";
            cin >> inches;

            //return the number of inches.
            cm = inches * cstCentPerInch; // this is a divison

            cout << endl << inches << " inches equals " << cm << " cm " << endl;
    }
    else if (menuchoice=="2") {
            //Enter the length
            cout << "Enter a length in cm: ";
            cin >> cm;

            //return the number of inches.
            inches = cm / cstCentPerInch; // this is a divison

            cout << endl << cm << " cm equals " << inches << " inches " << endl;}
     else
        cout << "You chose an option that doesn't exist yet";



    return 0;
}

Thoughts

There are two main things to note in this code.

1. We are using a control statement ( the if... else if... else... statement) and,
2. We are using a new mathematical operator

Multiplication

To carry out multiplication in c++ we use a new operator... the multiplication operator which is * .

In our code cm = inches * cstCentPerInch means cm is assigned the result of multiplying inches with the constant cstCentPerInch (2.54).

Control Statement.
A control statement is a statement which alters the flow of execution in a program. Now, lets back up. The flow of execution is simply the order in which lines of code are run. Without control statements, every line in the program would be run once. You COULD write a program that way, but it would be very limited.

Say, you want to make a cup of tea or coffee depending on what a guest wants. So you ask them "do you want tea or coffee?" and what you do next depends on the answer.

In this above example, we have the same scenario. If they input 1 it will do one thing, if we input 2 it will do another.

"==" is another operator, in this case a Boolean operator. Boolean is another type - it says whether something is true or false.

Look back up to the statement... if our menuchoice is equal to "1" we do one thing, if it is equal to "2" we will do something else.

I'll explain more about the Boolean type in a full post later on as it is so important for programming.



Prgram to convert CM to Inches in C++

Purpose of Program


This program converts CM to inches.

Program



#include 

using namespace std;

// a program that converts cm to inches

int main()
{
    // we declare a numerical constant.
    const float cstCentPerInch = 2.54;

    // declare a floating point number.
    float cm, inches;

    //Enter the length
    cout << "Enter a length in cm: ";
    cin >> cm;

    //return the number of inches.
    inches = cm / cstCentPerInch; // this is a divison

    cout << endl << cm << " cm equals " << inches << " inches " << endl;

    return 0;
}

What are Reals / floating point numbers?

There are two main types of numbers. These are integers and floating point.

We've already come across integers (int) which are numbers like 1,2, -7.

Floating point numbers are numbers with decimals, like 1.2, 3.2, 1.1. In the above program we come across them when we declare the constant. Both the constant cstCentPerInch and 2.54 are floating point numbers.

We also enter a length as a floating point, and the result of the calculation is a floating point.

Division

In the above program we do a divison:

inches = cm / cstCentPerInch;

The result of that division is a floating point number, which we then output. There are many other mathematical operators we can use. In the above code we do a calculation ( cm / cstCentPerInch )  and then assign the result to a variable inches.

Purpose of Return statement in Main

When you finish a program the operating system can be interested in whether it finished successfully. Return 0 tells the operating system that you have completed the program without any errors.

Tuesday 27 November 2012

Doing some addition

Purpose of Program


Add up two integer numbers, and then print out the result

Program




    #include 

    using namespace std;

    int main()
    {
        // declare three integer variables
        int a, b,c;

        // enter the variables

        cout << "Enter the First Number :";
        cin >> a;
        cout << endl << "Enter the Second Number :";
        cin >> b;

        // do some mathematics - add a and b together,
        // then assign the result to c
        c = a + b;
        cout << endl;
        cout << a  << " + " << b << " = " << c ;
    }



Some thoughts

This program declares three integers, a, b, c. Integers are numbers like -1, 2, 5, 4 etc. Whole numbers, in other words. If you put in a number like 1.2 this program will produce an error.

Every variable has a data type. One of the features of most programming languages is that when you put a character like 'c' into an integer they may throw an error. C++ does try to convert types behind the scenes, but it is not always smart about it.

At this stage I am not dealing with these issues. The purpose of the program is simply to add two integers together.




What are Variables and Constants?

So far I haven't written anything about a very important concept. Every program can be divided into two things:

DATA


Data could be thought of as statements about the world. For example, my name is Ray. Or John. Or this box has 12 apples. In procedural programming there are two types of data:

Data that changes - which are often called variables
Data that stays the same - which are often called constants.

We've met data already. In the second program we declared a variable like this:


        // declares three string variables
        string strFirstInitial;

We can then change the variable strFirstInitial in the code, for example like this:

        cin >> strFirstInitial;

In the above code a function reads in a string from the keyboard then stores it in strFirstInitial.

We've also met constants in the first program which we declared like this:


        const string cstStrMyName = "Ray";

The thing to remember is that variables can change. Constants can't.

PROCESS


Process is what we do to the data. For example, above we read in information into strFirstInitial. You can also do mathematical operations (to change the data) and also control statements (such as if) which affect what happens in the program.

There you go: programming is essentially the art of processing data correctly. Where correctly means "the program should do what you want it to do to the data."

Second Program

This is my second program. Again, another very basic "First Year programming course" program. For the first time I am using comments

What does this program do?

It allows you to input two initials as strings, plus a surname, and then prints them out with a welcome message.

To do that it declares three variables. A variable is like a bucket which stores data. Strings are objects in C++ - but that is far in advance of what I need to know for a program like this. There are other concepts I don't really need to know yet like using namespace , and #include . At the moment all I need to do is remember to include these lines.

At this stage I am really just playing around with getting very basic programs to work




    using namespace std;

    // In this program I use comments like this

    // iostream is a standard library containing code to read and write to the console.

    #include <iostream> 
    // this program will read in two initials
    // and the name
    // and welcome you to programming

    // main is the function which C++ always runs first.
    int main()
    {
        // declares three string variables
        string strFirstInitial;
        string strSecondInitial;
        string strSurname;

        // Enter name
        cout << "Enter your First Initial :" ;
        cin >> strFirstInitial;
        cout << endl << "Enter your Second Intitial :";
        cin >> strSecondInitial;
        cout << endl << "Enter your Surname:";
        cin >> strSurname;

        // Output full name buy adding strings together.
        // endl puts a carriage return into the output.
        // strings start with " " then append each initial with a space for formatting.

        cout << endl << endl << "Welcome to programming in C Mr " + strFirstInitial + " " + strSecondInitial + " " + strSurname << endl;
        //end of program
        return 0;
    }

Why use comments?

Comment provide English language information about what you are trying to do with the program at certain points. In this code, every comment starts with // and goes on to the end of the line.

You can also do in line comments like this: /* this is a comment */

In in line comments, everything inside the asterisk is ignored by the compiler.

See you next week for more adventures in very basic programming ;)



Hello World - C++

As people might know, I got pretty good at programming ten years back.

For whatever reasons I burnt out, and it has now been a long time since I have tried to code at all. I used to be pretty good at it and it is shocking how bad you can become if you don't practice.

So I wrote my first piece of code that works:


#include 
using namespace std;
const string cstStrMyName = "Ray";

int main()
{
    string a = "";
    cout << "Hello world! What is your name?" << endl;
    cin >> a;
    if (a != cstStrMyName)
        {cout << "hello " << a;}
    else
        {cout << "hello boss";};
    return 0;
}

Lots of syntax errors and silly mistypes but I have run it through the compiler and it works OKish.

Hope this will be fun ;)

A rare disease

What is Lipodermatosclerosis?

Lipodermatosclerosis is an inflammatory disorder that is caused by venous reflux. Essentially, what happens is that your leg veins contain valves that are incompetent for some reason OR you are unable to walk and so your blood pools in your legs for substantial amounts of time. This causes blood to leak out of your veins and capillaries.

Over time, this blood that leaks out forms scar tissues. Just like when you cut your hand, the body deals with these scar tissues with an inflammation (the red and sore feeling around the cut as it heals!)

The problem is that with long standing venous reflux, the legs never do heal completely. It's almost as if you got a small rubber hammer and lightly tapped your legs fifty times a day. At first there would be no damage, but soon enough... pain city :(

Instead, the inflammation stays for a substantial amount of time until the fat layer under the skin starts to die off, and you get a strange brown or red mark on the skin just above your ankle (often on the inside of your leg). This will slowly grow over time.

Complications

The main complication of lipodermatosclerosis is pain. You can get injections or topical painkillers that can reduce this to an extent. The leg becomes increasingly ugly took, but the pain can be disabling for some people.

Over time, your skin can break down until you get venous ulcers. So if you have LDS it is important to treat it. Ulcers often don’t heal easily, and can become painful and infected. Once you have an ulcer on your leg you have a higher risk of getting another. This is a reason why it is important to use moisturizers on your leg, and protect it from damage.

What can you do about Lipodermatosclerosis?

The main treatment are topical steroids to reduce flairs, and compression with walking. On its own compression isn’t very useful: the compression allows blood to go back up your leg when you walk.

Some people find that compression is very painful when combined with LDS. They may require injections of local painkillers, but in people with a lot of edema or with obesity the cause of the pain may be that the stockings don’t fit properly. It might be good to get the doctor to check the fit, and see whether pneumatic compression devices, dry wraps (ace elastic bandages) or wet wraps (Unna boots) are less painful.

Several people report good results when they take common compression stockings and ask for it to me increased by an inch from the measurement at rest.

Also, a consultation with a vascular surgeon can establish whether it is possible to do something about any venous reflux.

Finally, I hate to say this but losing weight can help since it allows the heart to pump the blood around your veins more easily!

Experimental techniques

Research has shown that stanazol can help reduce inflammation and induration (i.e. make your leg go back to the right shape). Also ultrasound therapy has been shown to improve healing when combined with compression.

Unfortunately, as a rare disease LDS does not have a huge amount of research or clinical trials compared with other diseases, but compression plus pain medication if necessary will prevent your leg being damaged further.

Monday 25 June 2012

Mystery Short Story: THE NEW KILLER IN TOWN




The summer was full of rumours. Even though I was a recluse who barely spent any time talking to the other folk in the village I knew that several murders had taken place just because there were billboards proclaiming them as you walked past the shops. I noticed people often turning away from me, often shielding their kids, but I just thought it was strange until the night where a hard brick came through the window to my house. When I called for the police they came pretty quickly, but the two young officers barely seemed to spend any time investigating the incident, they spent more time asking silly questions about other people in the village. When they left, I boarded up the window, and went to bed.
Three days later a murder happened that came closer to home. Three houses down from me, in fact. The first I realised something was up was when I heard dozens of police car sirens. Someone had shot the family there. Both parents and their young kid.
I went to work as normal. The bookshop was quiet, emptier than normal, and most of the regulars were talking about nothing other than the murders. Because he had started to kill children it became something of a national story. And the presence of police cars so close to my home was very distracting. I guessed their presence would at least stop people from throwing bricks through my window.
Once the shop closed I spent an hour putting new stock on the shelves, and went home. As I walked down the street I noticed three people following me. As I darted my head round I realised that they were all people in dark clothes, with shaved white heads. When they got closer I could smell the beer on them.
“Hey you! Murderer! We don’t want your sort here!” the biggest of the three shouted, his muscles rippling with the effort. I increased my pace, but the three men ran behind me as quickly as could be.
“What’s your problem?” I asked, as they got close to me.
“Our problem is you! We don’t want no crazies, no murderers in this town. Get out of here! Go! Or we’ll bash your head in so bad you’ll be eating through a straw for the rest of your short life!”
“I didn’t murder anyone.” I said the words quietly. I knew that it would make no difference. People always suspect that I am up to some kind of no good, simply because of the way I look. That is why I try to avoid people, except for the book shop. Book people are different from most.
“You get out of town! Get out of town now!” their hands bunched up into fists. I felt the first punch in my stomach, but soon the blows were reigning down so fast I could not feel most of them. Then two coppers run down the street and my attackers ran away from them.
They took me to hospital. Twenty-two stitches. The police officers asked if I wanted to press charges and of course I said not. If I did the guys would come after me again but with knives this time.
Once I was discharged I got a lift home just in front of the fire engine.

“Stay back, sir! Nothing to see here!” the Fireman said to me, shoving his hand out to prevent me going past.
“It’s my home!” I said, pointing to the fire. The Fireman nodded.
“I guess it used to be, at any rate,” he said, “Do you know if anyone else was in there?”
I shook my head. The only living thing in there was my cat. I hoped that he had escaped through the cat flap at the back of my house, but if he did I could not see him anywhere. Maybe he had been spooked by the experience. I hope he had been that he had not been caught in the fire.

“Why do you think you are being targeted?” the policeman asked me.
“They think that I killed those families! That I am a murderer!” I said, and the police officer raised his eyebrows in surprise. One of them looked at the other.
“Why do they think that, sir?”
“Look, I’m odd. I don’t speak to others much. And... Well, people are looking for someone to blame. You know that. Everyone is up in arms, and I’m a strange guy who lives alone, and they hate me for it.”
“I see,” the Police officer flipped closed his notebook, “And I suppose you won’t be charging them even if we do find out who did it, will you?”
I didn’t say anything. They took that as a yes.

It was hard for me to find a hotel. None of the local ones were willing to take me in... I guess the rumour that I had been targeted with my house destroyed put them off. Eventually I found a place at a chain hotel. A few days later I found out that my insurance had a clause that meant it didn’t pay out for arson. Apparently, that is the case with most insurance contracts. All of which meant I had a huge mortgage, and no house in exchange.
People did not come to my shop anymore, either. The rumours had scared them away.
I declared bankruptcy three days later, when I realised my business was going to go under too.

Declaring bankruptcy in the United Kingdom isn’t like you would expect. You fill out a form, and bring the cash into the office, and then you wait for a while with the other unfortunates until the clerk calls you in to speak with the judge. He asks you standard questions, and stamps the paper. And then you are bankrupt.
Of course, without an income I was homeless too.
As I walked down the street I saw a sign in the paper shop window. It said that the murderer had been caught. Apparently he was some drug addict. As I stood there I laughed until my sides hurt. If the police officer had been a little quicker... well, it was too late for that. But the irony was not lost on me.
I had lost everything due to this man. And he had been declared insane. He’d never spend time in prison, and the soft mental health officials would release him in a few years time.

I got a new job three months later as a cleaner in a swimming pool. It was at night. I pushed the broom like I was the keenest employee they had ever had. Sometimes, they even gave me enough hours to pay for the smallest apartment you have ever seen. Barely twenty foot by six foot, even prisoners get paid more.
That was when I hit my plan, you see. And I started going to the doctor, telling him about the stress I suffered, about my house burning up in flames. He prescribed me medicine, wrote down what I said on the computer.

No one ever pays much attention to the cleaner. The interview happened at 10:35 am, in a hot office room with a smart suited manager and a cleaning supervisor. They led me on a tour of the facility afterwards, asked if I had any questions, and generally treated me well. They told me I probably had the job. The facility had two wings, and I would start on the unsecured wing. The murderers, thieves and criminals who were mentally unbalanced were locked away far from where I was working, they said with a smile, so of course I didn’t need to worry.
“I think I can handle myself, anyway,” I said with a grin, and he nodded.
It didn’t take me long to move. Once I started the job I found I got on well with the other workers. Sometimes they had me making tea and coffee for the residents, most of the time I was just a cleaner. A worker that was nearly invisible to the doctors except when they wanted something from me. I found that I could go anywhere in the hospital except for the secured unit. The staff had to have the pass code to get past the door.
Three months after I started to work at the hospital I got my chance. It was my time to make tea for all the cleaners. I had smuggled in a few capsules of a clear tablet which I was told made people sick. Not too ill, just sick enough to go home. A few hours later the first of my colleagues was sent home by a concerned doctor. Others followed, and suddenly the secure unit was short staffed. I’d made sure that I had completed my normal rounds much sooner than usual, and the harassed supervisor saw me lounging around in the tea room.
“What are you doing? Don’t you know we haven’t enough people in the secured unit?” she asked, her eyes narrow, and her hands tapping her side impatiently.
“I’m sorry, no one told me to work there.”
“That’s Okay,” she said, “Just leave what you are doing and ask Sam what help he needs. The pass code into the secure unit is 2323.”
“Right away,” I said, walking quickly to the secure unit.

In most respects the secure unit was the same as the other wings. The only difference was the number of locks on the windows and doors. I found Sam furiously mopping away, complaining under his breath. He seemed grateful that I had turned up to help him, and he soon gave me a list of tasks almost as long as my arm. I worked for hours until the place was gleaming.
When I went home, I made sure to write the pass code down in my book.
More visits to the doctor describing my stress. He wanted me to go to a shrink - a psychologist - so I went to the library and found out all about the subject. It turns out that it is mostly hot air; these guys don’t know what they are talking about in my opinion. But I knew enough about the illness to be able to fake it. It was a tightrope; giving him reason to suspect that I was mentally ill, but not enough to actually section me.
Then all I had to do was wait for the Psychologist to go on hospital.  It was important not to prepare for the operation until he was away.
Smuggling a weapon into the hospital would be almost impossible. Although I worked there, the uniform I wore was simply tracksuit bottoms and a polo t-shirt, and so there was nowhere to bring a slashing weapon into the hospital. Instead, I chose warfarin tablets. They are small, and it doesn’t take many to kill someone. Just ten five milligram pills are enough. I read how to palm objects in something called a thumb tip in a magician’s book.
An hour before I was going to kill the man I called the psychologist up, distraught. He was on holiday. Of course. Once I put the phone down I drove to the hospital, and from there it was not hard to go where I wanted since I wore the hospitals own cleaning uniform and was a legitimate member of staff. No one was going to stop me so long as I looked like I was there officially. Making the tea was easy, as was getting access to the medicine - the staff never kept the medicine cupboard locked. I’d noticed that the first day I was there. Of course they should have, and it was naughty of them not to. I walked down the corridor with the cup of tea in my hand, and put the security code into the door.
 Every step felt like agony. I can still remember feeling that I should turn back. It was wrong. I knew that it was wrong, but I also knew that the guy who killed the kids should not survive. He was a crook, a bastard, the lowest of the low and he had taken every single thing from me.
Before I put the cup of tea through the slit I checked the name twice. It was the correct man.
“Drink up, or it’ll get cold!” I said.
I watched as the hand grabbed the cup of tea, and heard the person sipping. Then he put the cup out of the slit. “It tastes funny,” he said.
“Must be the new brand of tea leaves,” I replied, as I walked back down the corridor. Once I cleaned the cup up, I walked back out of the hospital saying hello to the security guards. I knew they would find the corpse of the murderer the next day.
It seemed strange waiting for the cops to come. Three days passed when I was going to work every day. People were whispering about how odd it was. One of the patients had died of a haemorrhage in the brain. I could hardly keep the glee from showing in my face.
“Mr Golden, I arrest you for murder. You do not have to say anything...” they led me into the cells. At first I denied it. I guess I watched too many detective programs when I was young. But finally, I broke down... saying I had killed him, admitting it, but always making sure throughout the interview I looked unhinged.
“The only thing we don’t understand is why you killed him. He’d never done any harm to you.”
“He killed those poor kids... the ones that used to live near me... they all blamed me for it, and I hated him so much.”
“But it was a kid. He’d just been put in because he was a danger to himself. The one you murdered was a kid.”
I looked at the police officer in horror. “But his name was on the door.”
“They’d only transferred him an hour earlier. You killed a kid.”
That was the moment I really did break down.
The court put a verdict of not guilty by reason of insanity. Sometimes I can hear that bastard shouting out into the corridor, taunting me. I’m so near to him here. So near. I could almost reach out through the walls and grab him. Sometimes at night I see the face of the kid I killed and I promise him that I will make the loss of his life worthwhile.
Until then I spend most of the day looking at the concrete walls, or talking to shrinks. And always planning.