Author Topic: Which code is better?  (Read 2600 times)

0 Members and 1 Guest are viewing this topic.

Offline rasenove

  • Baron
  • ****
  • Posts: 950
  • Cookies: 53
  • ಠ_ಠ
    • View Profile
Which code is better?
« on: March 27, 2013, 06:53:41 am »
Hi folks, I came across a quiz that said,

Quote
Write a program that reads two numbers from the user, adds them together, and then outputs the answer.  The program should use two functions: A function named ReadNumber() should be used to get an integer from the user, and a function named “WriteAnswer” should be used to output the answer.  You do not need to write a function to do the adding.


and the solution they gave was,



Code: (C++) [Select]
#include <iostream>
int ReadNumber()
{
    using namespace std;
    cout << "Enter a number: ";
    int x;
    cin >> x;
    return x;
}
 
void WriteAnswer(int x)
{
    using namespace std;
    cout << "The answer is " << x << endl;
}
 
int main()
{
    int x = ReadNumber();
    int y = ReadNumber();
    WriteAnswer(x+y);
    return 0;
}

I didnt look at that before i solved it by myself.
But my solution is different,

Code: (c++) [Select]
#include <iostream>
using namespace std;

//X & Y are two variables for storing nums
int X,    Y;

int get_num()
{
    cout<< "enter a number: ";
    // X stores the 1st number
    cin>> X;
    cout<< "enter another number: ";
    // Y stores the 1st number
    cin>> Y;
}

int main()
{
    get_num();
    cout<< X << "+" << Y << "=" << X + Y <<endl;
    return 0;
}

So which one is better? does my code have any mistake? it compiles okay and displays the result i wanted to achieve.
My secrets have secrets...

Offline Kulverstukas

  • Administrator
  • Zeus
  • *
  • Posts: 6627
  • Cookies: 542
  • Fascist dictator
    • View Profile
    • My blog
Re: Which code is better?
« Reply #1 on: March 27, 2013, 08:28:14 am »
The second one obviously doesn't meet the specifications, so the first one is better.

Offline rasenove

  • Baron
  • ****
  • Posts: 950
  • Cookies: 53
  • ಠ_ಠ
    • View Profile
Re: Which code is better?
« Reply #2 on: March 27, 2013, 08:50:40 am »
If your talking about the "code should have two functions" than i dont get it, main() is a function too.  So why didnt they count it?
My secrets have secrets...

Offline sudeep

  • Serf
  • *
  • Posts: 30
  • Cookies: -5
    • View Profile
Re: Which code is better?
« Reply #3 on: March 27, 2013, 09:29:17 am »
@rasenove

The code you wrote was same that came to my mind after reading the question, but I think that the previous one is better.
Better know it and don't need rather than need it and don't know it

Offline Kulverstukas

  • Administrator
  • Zeus
  • *
  • Posts: 6627
  • Cookies: 542
  • Fascist dictator
    • View Profile
    • My blog
Re: Which code is better?
« Reply #4 on: March 27, 2013, 10:03:48 am »
If your talking about the "code should have two functions" than i dont get it, main() is a function too.  So why didnt they count it?
When you have a specification, you do it by that specification.
And in your case it says:
Quote
<...> The program should use two functions: A function named ReadNumber() should be used to get an integer from the user, and a function named “WriteAnswer” should be used to output the answer. <...>
« Last Edit: March 27, 2013, 10:04:29 am by Kulverstukas »

Offline Xires

  • Noob Eater
  • Administrator
  • Knight
  • *
  • Posts: 379
  • Cookies: 149
    • View Profile
    • Feed The Trolls - Xires
Re: Which code is better?
« Reply #5 on: March 27, 2013, 07:29:57 pm »
Kulverstukas is correct.  You need to have 2 functions in addition to main().  The first function should be called 'ReadNumber' and the second function should be called 'WriteAnswer'.  Also, try not to use global variables.  The point of this exercise is to pass variables between functions.  Your 'get_num' function does not actually return a value even though it is declared as having an integer return type.
-Xires

Offline Kulverstukas

  • Administrator
  • Zeus
  • *
  • Posts: 6627
  • Cookies: 542
  • Fascist dictator
    • View Profile
    • My blog
Re: Which code is better?
« Reply #6 on: March 27, 2013, 08:30:16 pm »
If your talking about the "code should have two functions" than i dont get it, main() is a function too.  So why didnt they count it?
while main() is a function by definition, it is also an entry point (EP), so it doesn't count as a "function" because it must be there for the program to compile.

Offline 6NavI

  • /dev/null
  • *
  • Posts: 17
  • Cookies: -3
    • View Profile
Re: Which code is better?
« Reply #7 on: May 05, 2013, 12:57:53 pm »
i think the first code is better although even if i was the one i would write a code similar to the 2nd one
****6NavI****

Offline proxx

  • Avatarception
  • Global Moderator
  • Titan
  • *
  • Posts: 2803
  • Cookies: 256
  • ФФФ
    • View Profile
Re: Which code is better?
« Reply #8 on: May 05, 2013, 02:02:13 pm »
I would go for the first one.
When I code I do my very best to keep things seperated apply catogories and structure.
That way you can easily reuse certain pieces of code.
For example when I need to write stuff to files I make a dynamic module that can handle any kind of write operation.
I used to find myself rewriting very similar blocks.

Than again Im no expert.
« Last Edit: May 05, 2013, 02:02:32 pm by proxx »
Wtf where you thinking with that signature? - Phage.
This was another little experiment *evillaughter - Proxx.
Evilception... - Phage