Support Forums

Full Version: My First Project
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
Code:
#include <iostream>
using namespace std;

int addition (int a,int b)
{
    int r;
    r=a+b;
    return(r);
}



int subtraction (int a,int b)
{
    int r;
    r=a-b;
    return(r);
}


int multiplication (int a,int b)
{
    int r;
    r=a*b;
    return(r);
}



int division (int a,int b)
{
    int r;
    r=a/b;
    return(r);
}







int main ()
{
    int z;
    int a1;
    int a2;
    int o;
    cout << "First number" << endl;
    cin >> a1;
    cout << "Second number" << endl;
    cin >> a2;
    cout << "1) Addition" << endl;
    cout << "2) Subtraction" << endl;
    cout << "3) Multiplication" << endl;
    cout << "4) Division" << endl;
    cout << "What operation?: ";
    cin >> o;    
    if (o == 1) {    
        z = addition (a1,a2);
    }
    else {    
        if (o == 2) {
            z = subtraction(a1,a2);
        }
        else {
            if (o == 3){
                z = multiplication (a1,a2);
        }
        else {
            if (o ==4){    
                z = division (a1,a2);
            }
        }
    }
}    
    cout << "The result is " << z << endl;
    return 0;
}
Not bad for your first project. Now try something diffrent, or find ways of making your code better. Smile
Niice, good work!
yeah it is good!
(11-10-2009, 11:16 PM)nevets04 Wrote: [ -> ]
Code:
int addition (int a,int b)
{
    int r;
    r=a+b;
    return(r);
}

Just a few points I will make about this which are good habits to get into.

1) Pass parameters that aren't going to be changed in the function as "const", pass complex data types (such as classes, structs, etc) as a const reference (or const pointer if you really must). The "const" part of this will allow the compiler to optimise better since you are telling it that this parameter won't be changed, the reference/pointer part will save copying the complex type (which pass by value would have done).

Example:
Code:
int addition(const int a, const int b)
{
    int r;
    r=a+b;
    return(r);
}

And for a complex data type:
Code:
int printLine(const std::string &str)
{
    std::cout << str << std::endl;
}

2) You creating a temporary variable (which is using it's default constructor), you are then assigning to that variable (using the assignment operator), before returning by value (which will take yet another copy). All in all that int is having it's value set twice, and copied once; this can be done cheaper.

Firstly, you could always use the copy-constructor of the int. (Be careful of this when using complex data types as the copy-constructor might not have been defined properly). This will save the initial defaulting of the value:
Code:
int addition(const int a, const int b)
{
    int r(a + b);
    return(r);
}

However you are still creating a temporary instance of the int to just return it by value, so the most optimal way would be to just return the sum of the two numbers:
Code:
int addition(const int a, const int b)
{
    return a + b;
}

These are trivial points for such a small application, but for larger applications which are performance critical they all start to add up. You might want to read GotW.
Why didn't you just do:
Code:
int add (int a, int b)
{
    return a+b;
}
for the mathematical functions?
(03-30-2010, 04:54 PM)uber1337 Wrote: [ -> ]Why didn't you just do:
Code:
int add (int a, int b)
{
    return a+b;
}
for the mathematical functions?

This was so long ago, I have no idea what I was thinking.
(03-31-2010, 11:45 PM)nevets04 Wrote: [ -> ]This was so long ago, I have no idea what I was thinking.
Heh yeah now I see where you got that code from. You should really cite your sources
(04-02-2010, 03:23 PM)uber1337 Wrote: [ -> ]Heh yeah now I see where you got that code from. You should really cite your sources

Unless I take an entire program, I'm not going to cite sources in most cases. Do you site python doc pages?
(11-11-2009, 07:28 AM)MrD. Wrote: [ -> ]Just a few points I will make about this which are good habits to get into.

1) Pass parameters that aren't going to be changed in the function as "const", pass complex data types (such as classes, structs, etc) as a const reference (or const pointer if you really must). The "const" part of this will allow the compiler to optimise better since you are telling it that this parameter won't be changed, the reference/pointer part will save copying the complex type (which pass by value would have done).

Example:
Code:
int addition(const int a, const int b)
{
    int r;
    r=a+b;
    return(r);
}

And for a complex data type:
Code:
int printLine(const std::string &str)
{
    std::cout << str << std::endl;
}

2) You creating a temporary variable (which is using it's default constructor), you are then assigning to that variable (using the assignment operator), before returning by value (which will take yet another copy). All in all that int is having it's value set twice, and copied once; this can be done cheaper.

Firstly, you could always use the copy-constructor of the int. (Be careful of this when using complex data types as the copy-constructor might not have been defined properly). This will save the initial defaulting of the value:
Code:
int addition(const int a, const int b)
{
    int r(a + b);
    return(r);
}

However you are still creating a temporary instance of the int to just return it by value, so the most optimal way would be to just return the sum of the two numbers:
Code:
int addition(const int a, const int b)
{
    return a + b;
}

These are trivial points for such a small application, but for larger applications which are performance critical they all start to add up. You might want to read GotW.


wow, im learning C++ right now and i found this post really helpful, its good to see people giving really decent pointers/tips ^^
Pages: 1 2