Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
My First Project
#1
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;
}
Reply
#2
Not bad for your first project. Now try something diffrent, or find ways of making your code better. Smile
FREE PSN CARDS, XBOX LIVE, GAMES + MORE VIEW THREAD Here Yeye
Reply
#3
Niice, good work!
Reply
#4
yeah it is good!
Reply
#5
(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.
[Image: sig.php]
The little boat gently drifted across the pond exactly the way a bowling ball wouldn't.
Reply
#6
Why didn't you just do:
Code:
int add (int a, int b)
{
    return a+b;
}
for the mathematical functions?
[Image: izsyo6.jpg]


Reply
#7
(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.
Reply
#8
(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
[Image: izsyo6.jpg]


Reply
#9
(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?
Reply
#10
(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 ^^
[Image: sig.php]
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)