Sunday, February 22, 2015

Swapping Via Pass-by-Reference and Pass-by-Pointer

There is a function swap in this code, which we use to swap two numbers. If we define the function as swap(x, y) and call it as swap(a, b), the swapping done in the function will not reflect inside the main function. Why? Because it is pass-by-value method. While we call the function, only the copies of the arguments are passed to the function. Changes made in these copies are local to the function. So we should send the addresses of the real arguments (variables), so that the function can handle the original ones. There are two methods in C++ to accomplish this: 1) pass-by-reference and 2) pass-by-pointer.
In pass-by-reference method, we define the function as swap(&x, &y) and call it as swap(a, b). We can handle those variables as normal variables inside the function. All we had done is adding and ampersand(&) sign in front of the variables in the function header. Things like sending the memory addresses will be done by the compiler automatically. That's why pass-by-reference is known as implicit method.
In pass-by-pointer method, we define the function as swap(*x, *y) and call it as swap(&a, &b). We should handle those variables as pointer variables inside the function. i.e., x = y will become *x = *y. Here, things like sending the memory addresses and decoding them is done by the programmer. That is why pass-by-pointer is known as explicit method.

1. Code -- Pass-by-Reference

#include <iostream>
using namespace std;

void swap(int &x, int &y)
{
    int tmp;

    tmp  = y;
    y   = x;
    x   = tmp;
}

int main()
{
    int a, b;

    cout << "Enter two numbers: ";
    cin >> a >> b;

    cout << endl;
    cout << "Before swapping: a = " << a
         << " and b = " << b << endl;
    swap(a, b);
    cout << "After swapping: a = " << a
         << " and b = " << b << endl;

    return 0;
}

2. Code -- Pass-by-Pointer

#include <iostream>
using namespace std;

void swap(int *x, int *y)
{
    int tmp;

    tmp  = *y;
    *y   = *x;
    *x   = tmp;
}

int main()
{
    int a, b;

    cout << "Enter two numbers: ";
    cin >> a >> b;

    cout << endl;
    cout << "Before swapping: a = " << a
         << " and b = " << b << endl;
    swap(&a, &b);
    cout << "After swapping: a = " << a
         << " and b = " << b << endl;

    return 0;
}

Output of 1 and 2

Enter two numbers: 10 20

Before swapping: a = 10 and b = 20
After swapping: a = 20 and b = 10

3. Code -- Pass-by-Value (which won't work)

#include <iostream>
using namespace std;

void swap(int x, int y)
{
    int tmp;

    tmp  = y;
    y   = x;
    x   = tmp;
}

int main()
{
    int a, b;

    cout << "Enter two numbers: ";
    cin >> a >> b;

    cout << endl;
    cout << "Before swapping: a = " << a
         << " and b = " << b << endl;
    swap(a, b);
    cout << "After swapping: a = " << a
         << " and b = " << b << endl;

    return 0;
}

Output

Enter two numbers: 10 20

Before swapping: a = 10 and b = 20
After swapping: a = 10 and b = 20

NOTE: Turbo C++ users please read this (click here).

Tuesday, February 10, 2015

Student Deatils Using Object

Here is the code for calculating the total score of a student. It focuses on the object orientated approach.
Turbo C++ users should read this (click here) first.

Code

#include <iostream>
using namespace std;

class student
{
    private:
        char name[50];
        int rollno;
        float score[6], total_score;
       
    public:
        void input();
        void calculate();
        void output();
};

void student :: input()
{
    cout << "Enter the name of the student: ";
    cin >> name;
    cout << "Enter the roll no. of the student: ";
    cin >> rollno;
    cout << "Enter the scores for six subjects one by one:\n";
    for(int n = 0; n < 6; n++)
    {
        cin >> score[n];
    }
}

void student :: calculate()
{
    total_score = 0;
    for(int n = 0; n < 6; n++)
        total_score += score[n];
}

void student :: output()
{
    cout << "\n\nStudent Details:\n"
         << "\nName    : " << name
         << "\nRoll No.: " << rollno
         << "\nTotal Score   : " << total_score
         << endl;
}
int main()
{
    student stud;
    stud.input();
    stud.calculate();
    stud.output();
   
    return 0;
}


Output

Enter the name of the student: Example
Enter the roll no. of the student: 30
Enter the scores for six subjects one by one:
90 92 89 94 86 90


Student Details:

Name    : Example
Roll No.: 30
Total Score   : 541

A Note on Compatibility

Like languages we use in our daily life, programming languages also have many dialects. Which dialect to follow depends on the compiler you use. In case of C++, we have an international standard for it, devised by ISO. This is known as ISO C++. It is highly recommended to follow this standard, and to use the compilers which support it. On of the major and powerful compilers which support ISO C++ is a free software, GCC, the GNU Compiler Collection, which you can download and use for free. Click here to know more.

What should Turbo C++ users do

It is highly recommended to update yourself from Turbo C++, and outdated and unsupported C++ variant. However, some universities and institutions still follow this archaic stuff. But the codes in this blog follow ISO C++. Students who are forced to use Turbo C++ should make the following changes in their code in order to make them compatible with Turbo C++:
  • Use iostream.h instead of iostream
  • Don't use using namespace std;
  • You will have to press Alt+F5 after program execution in order to see the output. Or you can include conio.h and then write getch(); just before the return statement of the main function.
Consider the following example:

ISO C++

#include <iostream>

using namespace std;

int main()
{
    cout << "Hello, World!\n";
    return 0;

Turbo C++

#include <iostream.h>
#include <conio.h>

int main()
{
    cout << "Hello, World!\n";
    getch();

    return 0;


Happy coding!