Thread: My very first own C program (I need a hand!)

  1. #16
    Registered User
    Join Date
    Nov 2009
    Posts
    15
    thanks all!!
    I think some algebra needed to be rethought
    Here's the right code:
    Code:
    #include <stdio.h>
    int main()
    {
           float e,x,y,z;
           printf("Enter value for x :");
           scanf("%f",&x);
           y = (60/x);
           e = (y/2);
           z = e*1000;
           printf("Echo = %f\n",z);
           getch();
           return 0;
    }

  2. #17
    Registered User
    Join Date
    Jan 2009
    Location
    Australia
    Posts
    375
    Well done.

    One last thing, you should only declare main in one of two ways:
    int main( void ) /*When it takes no arguments.*/
    int main( int argc, char **argv ) /*When it takes arguments from the command line*/

    In your case you want the first one, you will probably learn about the second one later on.

    It will still work if you use 'int main()' but it's good practice to get things right from the start, so you don't have to relearn them later.

  3. #18
    Learning C. JOZZY& Wakko's Avatar
    Join Date
    Nov 2009
    Posts
    59
    Quote Originally Posted by DeadPlanet View Post
    Well done.

    One last thing, you should only declare main in one of two ways:
    int main( void ) /*When it takes no arguments.*/
    int main( int argc, char **argv ) /*When it takes arguments from the command line*/

    In your case you want the first one, you will probably learn about the second one later on.

    It will still work if you use 'int main()' but it's good practice to get things right from the start, so you don't have to relearn them later.
    Then I have a question "what is an argument exactly?" does this mean that a value is returned to a different function?

  4. #19
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by JOZZY& Wakko View Post
    Then I have a question "what is an argument exactly?" does this mean that a value is returned to a different function?
    Arguments supplied to main come from the command-line:

    myprog arg1 arg2

    Using (void) as a parameter set tells the compiler that this function does not take any arguments. This is more significant with misc functions:

    int myfunc(void);
    int myfunc();

    Neither of these uses any parameters internally. However, with the second one you could mistakenly supply an argument:

    int y=666, x = myfunc(y);

    y will be ignored in myfunc(), since it serves no purpose, but it will still be placed on the stack and not throw an error. With the first prototype, where the parameter set is (void) the compiler will throw an error if you try and supply one by mistake (or because the code is updated, etc).
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #20
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by Exutus View Post
    thanks all!!
    I think some algebra needed to be rethought
    Here's the right code:
    Code:
    #include <stdio.h>
    int main()
    {
           float e,x,y,z;
           printf("Enter value for x :");
           scanf("%f",&x);
           y = (60/x);
           e = (y/2);
           z = e*1000;
           printf("Echo = %f\n",z);
           getch();
           return 0;
    }
    Single letter variables are an excellent way to confuse people. It's bad news that you are apparently done, and I still have no idea, at a glance, what this calculates. Your poor user must be confused as well. What the heck is an X? How could he possibly avoid typing errors with that one?

    Quote Originally Posted by JOZZY& Wakko View Post
    Then I have a question "what is an argument exactly?" does this mean that a value is returned to a different function?
    In common parlance the terms argument and parameter are interchangeable, but the word argument defines the expression you passed in (e.g. foo, foo+4, 42, etc.) and parameter defines the variable you use in the function (e.g. dest, where, coolWindow, etc.).

  6. #21
    Registered User
    Join Date
    Feb 2009
    Posts
    35
    Quote Originally Posted by whiteflags View Post
    Single letter variables are an excellent way to confuse people. It's bad news that you are apparently done, and I still have no idea, at a glance, what this calculates. Your poor user must be confused as well. What the heck is an X? How could he possibly avoid typing errors with that one?
    I agree.

    im also confused as to why you have variable names x,y,z and e. to me that means the e is special, significant in some way.

    also, why do you have 3 lines of arithmetic? as opposed to something like this?

    Code:
    #include <stdio.h>
    int main()
    {
           float x;
           printf("Enter value for x :");
           scanf("%f",&x);
           printf("Echo = %f\n", 30000/x);
           return 0;
    }

  7. #22
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by Brain_Child View Post
    also, why do you have 3 lines of arithmetic? as opposed to something like this?
    If you want to quibble about that, wait until you calculate something complicated, then you'll rethink measuring good code by line count. I do not think that and (60.0f / x / 2.0f) * 1000.0f can be equivalent, either.

    Of course, I'm also assuming integer math is unwanted.

  8. #23
    Registered User
    Join Date
    Feb 2009
    Posts
    35
    Quote Originally Posted by whiteflags View Post
    If you want to quibble about that, wait until you calculate something complicated, then you'll rethink measuring good code by line count. I do not think that and (60.0f / x / 2.0f) * 1000.0f can be equivalent, either.
    I've written some complicated code and I am in no way measuring the quality of this program by line count.

    I just dont understand why 3 extra variables have to be created (I also dont understand what x is and what is being calculated)

    That is why I dont think it is good code

  9. #24
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Quote Originally Posted by DeadPlanet View Post
    Well done.

    One last thing, you should only declare main in one of two ways:
    ...
    This is wrong, and now I'll give you a chance to correct your statement and redeem yourself.
    Mainframe assembler programmer by trade. C coder when I can.

  10. #25
    Registered User
    Join Date
    Jan 2009
    Location
    Australia
    Posts
    375
    Quote Originally Posted by Dino View Post
    This is wrong, and now I'll give you a chance to correct your statement and redeem yourself.
    Hmm, I'm not sure where I went wrong with that?

  11. #26
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by DeadPlanet
    Hmm, I'm not sure where I went wrong with that?
    You are not exactly wrong since you made a recommendation instead of stating a requirement. However, an empty parameter list in a function definition that does not also serve as a forward declaration is effectively the same as a parameter list with void as the parameter.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #27
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    You were incomplete. There is a 3rd parameter that points to a hash of environment variables.
    Mainframe assembler programmer by trade. C coder when I can.

  13. #28
    Registered User
    Join Date
    Nov 2009
    Posts
    15
    Hello and thanks for all your replies.
    I understand that it does not look user-friendly, and this program is for me, not for other users. But I'll post it simple so you will know what it was all about.

    a minute divided by the bpm
    (the result divided by 2) * 1000

    So I guess it is
    ((60/BPM)/2)*1000

    So I made:
    y = 60/bpm
    then
    e=y/2
    then
    z=e*1000

    Can I do it in a single equation ? It could be z = ((60/BPM)/2)*1000

    Can try it now, I'm at work on (lunch time)


    Code:
    #include <stdio.h>
    int main()
    {
           float e,BPM,y,z;
           printf("Enter the tempo in Beats Per Minute :");
           scanf("%f",&BPM);
           y = (60/BPM); // The result of a minute (60 seconds) divided by the number of beats per minute
           e = (y/2);
           z = e*1000;
           printf("Set the DELAY to %f miliseconds\n",z);
           getch();
           return 0;
    }
    Also, there are things I haven't understood yet that you are talking about (arguments ?)

  14. #29
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Dino
    You were incomplete. There is a 3rd parameter that points to a hash of environment variables.
    However, your statement is also incomplete (and consequently incorrect) since there may be other parameters, or those parameters may not be available, should an implementation choose to allow them or not provide for them, as the case may be. What DeadPlanet stated is what is guaranteed by the C standard for hosted implementations.

    EDIT:
    Quote Originally Posted by Exutus
    Can I do it in a single equation ? It could be z = ((60/BPM)/2)*1000
    Yes, you certainly could. That said, I suggest that you use more descriptive variable names instead of e, y and z.

    Quote Originally Posted by Exutus
    Can try it now, I'm at work on (lunch time)
    Can or cannot? It is best if you can compile and run your programs to get your own feedback on whether you are correct.
    Last edited by laserlight; 11-10-2009 at 10:53 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #30
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Exutus View Post
    Can I do it in a single equation ? It could be z = ((60/BPM)/2)*1000
    Yes, of course. Actually it could be just z=60/BPM/2*1000 since your parentheses already match the normal order of precedence.

    Also, there are things I haven't understood yet that you are talking about (arguments ?)
    Don't worry so much about that. Or read my previous post #19 -- this is not a serious issue here.

    However, your contention:

    Quote Originally Posted by Exutus View Post
    Hello and thanks for all your replies.
    I understand that it does not look user-friendly, and this program is for me, not for other users.
    is a little flawed: who are we then?!! Not everyone at cboard is a long distance mind reader. The more effort you put into making your code comprehensible for others, the more effort people may put into providing you with comprehensible advice (also, it is just easier for everyone -- the easier it is to understand, the easier it is to reply).

    However, I think you can be forgiven here as this is very short and simple.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  2. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 11:17 PM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM