Thread: Homework Help, simple program, unsure newbie :(

  1. #1
    Registered User Aslan14's Avatar
    Join Date
    Mar 2009
    Posts
    18

    Question Homework Help, simple program, unsure newbie :(

    Hi everyone. I read all the "before you post, know this" stuff, so I hope I get it right!

    First, this is a homework that I have been stuck on for a month. I have a serious mental block when it comes to programming for some reason I did numerous Goggle searches but found nothing. I found a similar program on here, but I didn't want to copy theirs, for obvious reason. I would really like to learn how to program in C. Plus, the one that is posted was really buggy.

    The instructions are as followed;
    "HW2 computes a list of conversions from miles per hour to feet per second. The list should be from 0 to 80 mph with an increment the user inputs. After the prompt 'input increment:' is given, your program should display the answer on the screen. Output should be %5.0f %5.1f."

    Second, I wrote a program is a fine program in itself, it converts mph to fps very nicely. BUT it is incorrect because it does not do it in "user inputted increments." I didn't do what was asked, and I have been avoiding this program ever since.

    This is where I am stuck! I don't even know where to start with the increments.

    I know I need to have the user input an increment from 0 to 80, then the program should print out a list of conversions in the specific increment.

    Should I use "increment" as declared variable? Then use it in some sort of equation that the program executes when the user assigns "increment" a value?

    I just need a push in the right direction I think.

    I will include the original program, forgive me if the code tags suck, I am very new at all of this.
    Code:
    #include <stdio.h>
    #define SCALE_FACTOR (1.46666667f)
    
    int main(void)
    {
           
    float mph, fps;
    
            printf("Enter mph: ");
            scanf("%f", &mph);
    
                    fps = mph * SCALE_FACTOR;
    
            printf("Feet per second equivalent: %5.1f\n", fps);
    
    return 0;
    }
    Any input from the experts would be greatly appreciated! (I would go to my professors office hours, but it is a holiday and he is currently out of town)

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Aslan14
    I know I need to have the user input an increment from 0 to 80, then the program should print out a list of conversions in the specific increment.
    Actually, no. The user might input an increment such as 5, then your program will loop 0, 5, 10, ..., 75, 80, converting each of these values.
    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

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You're in BIG trouble now, Aslan14! (needed to clean my glasses! )

    You can't just come in here, read and follow the homework policy, show real effort in your description of the problem, AND use code tags!

    This is unheard of!!

    Welcome to the Forum, Aslan14!


    So first, add a message telling the user to input an interval, and get that.

    Then use a for loop to give your output, and include the interval, and a % operator:

    Say interval is 10:
    Code:
    int interval=10;
    for(i=0;i<80(whatever is right); i++) {
      if(i % interval==0)
        print your data here
    }
    if you don't want the data to be printed on the first loop through (when i is zero, then add the part in green):
    Code:
    if(i % interval==0 && i)
    Last edited by Adak; 11-11-2010 at 06:24 PM.

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by Adak View Post
    Say interval is 10:
    Code:
    int interval=10;
    for(i=0;i<80(whatever is right); i++) {
      if(i % interval==0)
        print your data here
    }
    I think something like the following would be much better (clearer and more efficient):
    Code:
    for (i = 0; i < 80(whatever is right); i += interval) {
        print your data here
    }

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I'm "selling" the mod operator, today. No idea why!

    Aslan, listen to anduril, OK?
    Last edited by Adak; 11-11-2010 at 06:25 PM.

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Adak View Post
    You're in BIG trouble now, Asian14!
    Daymn Adak... you like to scare the bejeebers outa me....

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I'm getting punchy from messing around with sorting algoriithms, too much.

    Found a good one though!

  8. #8
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    It's Aslan not Asian LOL. Let me close the racist door there for you before the draft gets in.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  9. #9
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Aslan probably left it open...he's on the move.

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I told you I was getting punchy -

    apologies, Aslan!!

  11. #11
    Registered User Aslan14's Avatar
    Join Date
    Mar 2009
    Posts
    18
    Hello again everyone! I just wan to say I got a good chuckle from your replies!

    Sorry for the absence, my computer took a nose dive (damn HPs)! This is what I have, but I apparently am not doing the "for" loop right.

    Code:
    #include <stdio.h>
    #define SCALE_FACTOR (1.46666667f)
    
    int main(void)
    {
            int i = 0;
            float fps, increment;
    
            printf("Enter increment between 0 and 80: ");
            scanf("%f", &i);
    
    {
    for (i = 0; i <== 80; i+= increment )
    }
    
    {
            fps = i * SCALE_FACTOR;
            printf("Feet per second equivalent: %5.2f\n", fps);
    }
    return 0;
    }

    I first tried using mph instead of i, so it looked like;
    Code:
     int mph = 0
    But that didn't seem to be working, so I went with 'i".

    The errors I am getting are;
    hw02.c: In function `main':
    hw02.c:17: error: syntax error before '=' token
    hw02.c:17: error: syntax error before ')' token

    Any suggestions?

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    <== should be <=
    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

  13. #13
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Aslan14 View Post
    Hello again everyone! I just wan to say I got a good chuckle from your replies!

    Sorry for the absence, my computer took a nose dive (damn HPs)! This is what I have, but I apparently am not doing the "for" loop right.

    Code:
    #include <stdio.h>
    #define SCALE_FACTOR (1.46666667f)
    
    int main(void)
    {
            int i = 0;
            float fps, increment;
    
            printf("Enter increment between 0 and 80: ");
            scanf("%f", &i);
    
    {
    for (i = 0; i <== 80; i+= increment )
    }
    
    {
            fps = i * SCALE_FACTOR;
            printf("Feet per second equivalent: %5.2f\n", fps);
    }
    return 0;
    }

    I first tried using mph instead of i, so it looked like;
    Code:
     int mph = 0
    But that didn't seem to be working, so I went with 'i".

    The errors I am getting are;
    hw02.c: In function `main':
    hw02.c:17: error: syntax error before '=' token
    hw02.c:17: error: syntax error before ')' token

    Any suggestions?
    You are correct... your for loop is not going to work as the syntax errors have indicated.
    First <== is not a valid token in C.
    Second your bracketing is all wrong.

  14. #14
    Registered User Aslan14's Avatar
    Join Date
    Mar 2009
    Posts
    18

    Thumbs up Yay!!

    After fixing the brackets and the relational operator (how did I miss those?), I was getting a really really weird output!
    Code:
     "Feet per second equivalent: -3149642752.00"
    I remember seeing somewhere in my book that this is bad... LOL
    So, had to do a little internet research because I figured out I was missing an expression to add the increment to the mph!

    Thanks for everyone's help! (you may hear more from me, I am really no good at programming!)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. c program that accepts and executes commands?
    By Cimposter in forum C Programming
    Replies: 3
    Last Post: 09-30-2009, 02:58 PM
  2. Inheritance and program structure planning please help a newbie
    By ninjacookies in forum C++ Programming
    Replies: 1
    Last Post: 10-23-2005, 12:18 PM
  3. Newbie! Help With Two Simple C++ Programs.
    By slickwilly440 in forum C++ Programming
    Replies: 4
    Last Post: 09-06-2005, 08:38 AM
  4. Need help with simple, simple program.
    By LightsOut06 in forum C Programming
    Replies: 5
    Last Post: 09-01-2005, 08:31 PM
  5. simple frontend program problem
    By gandalf_bar in forum Linux Programming
    Replies: 16
    Last Post: 04-22-2004, 06:33 AM