Need simple help please!!

This is a discussion on Need simple help please!! within the C++ Programming forums, part of the General Programming Boards category; Hey guys! Could you please tell me what the difference is between these two programs? please explain in depth what ...

  1. #1
    Registered User
    Join Date
    May 2003
    Posts
    42

    Need simple help please!!

    Hey guys!
    Could you please tell me what the difference is between these two programs?

    please explain in depth what the first program does step by step...

    Code:
    // Listing 5.1 - demonstrates the use of function prototypes
    
    #include <iostream.h>
    int FindArea(int length, int width)
    
    int main()
    {
      int lengthOfYard;
      int widthOfYard;
      int areaOfYard;
    
      cout << "\nHow wide is your yard? ";
      cin >> widthOfYard;
      cout << "\nHow long is your yard? ";
      cin >> lengthOfYard;
    
      areaOfYard= FindArea(lengthOfYard, widthOfYard);
    
      cout << "\nYour yard is ";
      cout << areaOfYard;
      cout << " square feet\n\n";
     return 0;
    }
    
    int FindArea(int l, int w)
    {
         return l * w;
    }
    I have written the next program my self, same output:

    Code:
    #include <iostream.h>
    #include <stdlib.h>
    int main()
    {
       int l, w, a;
       cout << "How wide is your yard?\n";
       cin >> w;
       cout << "How long is your yard?\n";
       cin >> l;
       a = (l*w);
       cout << "Your yard is:" << a << "Square Feet\n";
       system ("pause");
       return 0;
    }
    My code is A LOT shorter that the book's code, and it's even easier to understand!

    I dont understand anything about how the first program works, please explain to me!

    (I need to proceed to other chapters of the book)
    Last edited by Nectron; 06-12-2003 at 09:40 AM.
    I have a code which stops life!

  2. #2
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,493
    My code is A LOT shorter that the book's code, and it's even easier to understand!
    True.

    I dont understand anything about how the first program works, please explain to me!
    Which is why the first program is written the way it is. It's meant to teach you a bit about functions and good program design.

    Why do you need to be explained the first program when it's practically the same as the second program? The only differences are that you calculate the area in the main function, and the program calculates it in a seperate function, FindArea(). (And you have a system("pause");, but that's not important.)

  3. #3
    Registered User
    Join Date
    May 2003
    Posts
    42

    What the?

    Ok, It got a little more clear in my mind after your reply...

    But how the hell does the program work ?
    when I enter the width and the length, how does the program deal with the data?
    does it go to line 17 then 25 then 19?

    (i'm talking about code 1)

    I want to know how the program work !
    I have a code which stops life!

  4. #4
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    Well, there are no line numbers so I can't tell you for sure, but the program reads in the users input, then calls the function FindArea(), (i.e. transfers control to the start of the FindArea() function), which calculates and returns the area, (i.e. transfers control back to the calling routine). It then write it out.

    A function is a piece of code which can be called from other places in your program, often from several different places. It breaks your code up into smaller pieces.

    With a 20-30 line program that is not terribly important, but believe me, when you have millions of lines, you will not want to start at the beginning each time and search for the bit you need to fix.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  5. #5
    Iamregistered
    Guest
    at this point:

    Code:
    areaOfYard= FindArea(lengthOfYard, widthOfYard);
    the code for the FindArea function is called along with the values of lengthOfYard and widthOfYard, so you will get this:

    Code:
    int FindArea(int l, int w)
    {
         return l * w;
    }
    with l == lengthOfYard and w == widthOfYard

    The int part that I've highlighted is the return type from the function. This means the function has to return to the point where it was called with an int value. FindArea returns the value of l * w, so now we get :

    Code:
    areaOfYard = l*w
    I hope thats sort of clear, all you are really doing is replacing "FindArea" with the code specified in the FindArea function.

  6. #6
    ...
    ... is offline
    Registered User
    Join Date
    Feb 2002
    Posts
    465
    the first program takes advantage of functions.

    treat FindArea(lengthOfYard, widthOfYard) kind of like a shortcut.
    when the computer sees it, it goes looking for how to handle it.

    the shortcut is defined outside of the main loop, at the bottom. how it works is this:


    int FindArea(int l, int w) /*this names the function and shows what variable goes in it. the 'int' at first tells what kind of variable the function will return (in this case, when it solves for area, the answer will be an int, so it returns an int). the int l and int w in parenthesis tell what will be put into the function to be used (in this example, the function is solving for area using length and width, so you want to put the length and width from your program into the function). */

    {
    return l * w; /*this is where the work is done. your function multiplies l by w and sends that answer back to your main loop. Ideally, functions would hold large amounts of data that would be inconvenient to have to write over and over again in your program, so you put it all in a function and just use the function in your program rather than duplicating the code every instance its used. */
    }


    so in your main, you want it to multiply length times width, so you put whatever variable is holding length and width into the function, in this case lengthOfYard and widthOfYard.

  7. #7
    Registered User
    Join Date
    May 2003
    Posts
    42

    Thumbs up WOW

    Oh thank you very very much guys... all of you who replied!

    you are the kind of people I can rely on when I stock somewhere!

    You made it simple for me, and explained it very well, better than the crappy book...

    Thanks again everyone!
    I have a code which stops life!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. creating very simple text editor using c
    By if13121 in forum C Programming
    Replies: 9
    Last Post: 10-19-2010, 06:26 PM
  2. Simple message encryption
    By Vicious in forum C++ Programming
    Replies: 10
    Last Post: 11-07-2004, 11:48 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 04:00 PM
  4. Simple simple program
    By Ryback in forum C++ Programming
    Replies: 10
    Last Post: 09-09-2004, 06:48 AM
  5. Need help with simple DAQ program
    By canada-paul in forum C++ Programming
    Replies: 12
    Last Post: 03-15-2002, 08:52 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21