Thread: Unsure how to het this to work

  1. #1
    Registered User
    Join Date
    Sep 2016
    Posts
    8

    Unsure how to het this to work

    I've bee working on this for awhile and I'm not sure what to do. I've tried a bunch of different ways. Some advice where I went wrong would be great.
    Last edited by The_black; 09-17-2016 at 12:20 AM.

  2. #2
    Registered User
    Join Date
    Sep 2016
    Posts
    8
    Code:
    //Pre-processor
       #include <stdio.h>
    
    
       #define SCHOLARSHIP_1000 5
       #define SCHOLARSHIP_500 10
       #define SCHOLARSHIP_250 8
    
    
    
    
    
    
    
    // Main Function
     int main(){
       //Variable Declaration
    
    
     int time = 1, num_1, num_2, num_3;
     float percentage_rate,amount_fund,amount_interest ;
    
    
     //user input
      printf("How much was in the fund last year?\n");
      scanf("%f", &amount_fund);
    
    
      printf("What was the yearly precentage rate?\n");
      scanf("%f", &percentage_rate);
    
    
      //calculate the precentage
    
    
      amount_interest = amount_fund * time * (percentage_rate/100);
    
    
    
    
    
    
      if ( amount_interest > 1000 && SCHOLARSHIP_1000 >= 5){
            num_1 = amount_interest / 1000;
            printf("%d $1000 scholarships will be awarded. \n",num_1 );}
            else if (num_1 == 0) {
            printf("0 $1000 scholarships will be awarded.\n");}
    
    
    
         if (amount_interest > 500 && SCHOLARSHIP_500 >= 10){
                num_2 = amount_interest / 500;
                 printf("%d $500 scholarships will be awarded. \n", num_2);}
                 else if (num_2 == 0) {
            printf("0 $500 scholarships will be awarded.\n");}
    
    
          if ( amount_interest > 250 && SCHOLARSHIP_250 >= 8){
            num_3 = amount_interest / 250;
               printf("%d $250 scholarships will be awarded. \n", num_3);}
               else if (num_3 ==0) {
            printf("0 $250 scholarships will be awarded.\n");}
    
    
    
               //end of main function
    
    
               return 0;
     }

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Unless you tell us what it's supposed to do, how do you expect us to help you to fix it.

    Your program compiles - what else do you want us to say?

    A better indent style might help.
    Indent style - Wikipedia, the free encyclopedia
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Sep 2016
    Posts
    8
    How much was in the fund last year? 40000
    What is the yearly percentage rate? 2
    0 $1000 scholarships will be awarded.
    1 $500 scholarships will be awarded.
    1 $250 scholarships will be awarded

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    OK, and what's the original question?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Sep 2016
    Posts
    8
    The original question is "compute the yearly interest in the Fund and determine how many $1000, $500, and $250 scholarships can be awarded." "possible, the Fund prefers to award 5 $1000 scholarships, 10 $500 scholarships, and as many $250 as they have money left for.".

  7. #7
    Registered User
    Join Date
    Sep 2016
    Posts
    8
    I rewrote it and still having issues.
    Code:
    
    //Pre-processor
    
    
    #include <stdio.h>
    #include <math.h>
    
    
    
    int main() {
    //Variable Declaration
      int  sch_1, sch_2, num_2, sch_3, num_3;
      float funds,percentage_rate, interest;
    
    
    //user input
        printf("How much was in the fund last year?\n");
        scanf("%f", &funds);
    
    
    
        printf("What is the yearly percentage rate?\n");
        scanf("%f", &percentage_rate);
    
    
      //calculate the precentage
        interest = (funds * percentage_rate)/100;
        sch_1 = interest / 1000;
        sch_2 = interest - (1000 * sch_1);
        num_2 = sch_2 / 500;
        sch_3 = interest - (500 * sch_2);
        num_3 = sch_3 / 250;
    
    
       //output
    
    
        if (sch_1 < 5) {
            printf("%d $1000 scholarships will be awarded.\n", sch_1);
        }
        else if (sch_1 >= 5){
            printf("5 $1000 scholarships will be awarded.\n");
        }
        else if (sch_1 == 0) {
            printf("0 $1000 scholarships will be awarded.\n");
        }
    
    
    
         if (num_2 < 10) {
            printf("%d $500 scholarships will be awarded.\n", num_2);
        }
        else if (num_2 >= 10) {
            printf("10 $500 scholarships will be awarded.\n");
        }
        else if (num_2 == 0) {
            printf("0 $500 scholarships will be awarded.\n");
        }
         if (num_3 >= 1) {
            printf("%d $250 scholarships will be awarded.\n", num_3);
        }
        else if (num_3 == 0) {
            printf("0 $250 scholarships will be awarded.\n");
        }
    
    
    
        return 0;
    }

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What exactly are the issues?
    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

  9. #9
    Registered User
    Join Date
    Sep 2016
    Posts
    8
    It's not outputting the correct numbers. When I've got. If I put in 195000 into funds and 5 in to percentage rate . It should be 5 for 1000, 8 for 500 and 1 for 250 but is flipping the answer for 500 and 250

  10. #10
    Registered User
    Join Date
    Sep 2016
    Posts
    8
    For 40000 funds and 2 percent it should output 0,1,1. If get it for the first one this one will crash, but if I get this one I don't get the other one.

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > the Fund prefers to award 5 $1000 scholarships, 10 $500 scholarships, and as many $250 as they have money left for.".

    Debugging your code.
    Being able to use a debugger is an essential skill, and this is a good time to start figuring out how to use it.

    Code:
    $ gcc -g -Wall main.c
    $ gdb -q ./a.out
    Reading symbols from ./a.out...done.
    (gdb) b 23
    Breakpoint 1 at 0x40069d: file main.c, line 23.
    (gdb) run
    Starting program: ./a.out 
    How much was in the fund last year?
    195000
    What is the yearly percentage rate?
    5
    
    Breakpoint 1, main () at main.c:23
    23	    interest = (funds * percentage_rate)/100;
    (gdb) n
    24	    sch_1 = interest / 1000;
    (gdb) p interest
    $1 = 9750
    (gdb) n
    25	    sch_2 = interest - (1000 * sch_1);
    (gdb) p sch_1
    $2 = 9
    (gdb) n
    26	    num_2 = sch_2 / 500;
    (gdb) p sch_2
    $3 = 750
    (gdb)
    Given that at most 5 $1000 scholarships can be awarded, I'm suspicious that the value 9 in sch_1 means you're losing interest that should be used to fund the lower values.

    The calculation of sch_2 needs to involve a value of sch_1 which is at most 5.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. unsure what this is actually for
    By Fixxxer in forum C Programming
    Replies: 3
    Last Post: 04-25-2010, 06:48 AM
  2. Need help with C. Unsure what to do next.
    By zenghoong in forum C Programming
    Replies: 1
    Last Post: 10-11-2009, 12:36 PM
  3. unsure
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 08-13-2002, 09:37 AM
  4. Little Unsure?
    By kas2002 in forum C Programming
    Replies: 8
    Last Post: 06-12-2002, 08:18 PM
  5. Help unsure
    By Joel in forum C++ Programming
    Replies: 1
    Last Post: 02-12-2002, 12:00 PM

Tags for this Thread