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.