-2

I am having trouble figuring out a way to "cut off" the program from putting more than 5 $1,000 scholarships and 10 $500 scholarships. How would I cut it off after 5 and 8 from what I already have? My code is below the problem


One division of Programmers for a Better Tomorrow is their Scholarship Endowment Fund. They provide yearly scholarships to students who need a hand in amounts of 1000, 500, and 250 dollars. The money for these scholarships comes from interest made on previous donations and investments.

You will create a program to compute the yearly interest in the Fund and determine how many $1000, $500, and $250 scholarships can be awarded. For example, if the Fund had 500,000 dollars in it on September 30th 2016 and the yearly interest rate was 3 percent then the Fund will have 515,000 dollars in it at the end of this September. This gives them $15,000 to disburse as scholarship money.

If possible, the Fund prefers to award 5 $1000 scholarships, 10 $500 scholarships, and as many $250 as they have money left for. With $15,000 the Fund can award 5 $1000 scholarships, 10 $500 scholarships, and 20 $250 scholarships. Your program should print this information for the user.

If that is not possible, the Fund will award as many $1000 and $500 scholarships as they can.

For example, if they had $4,750 they would award 4 $1000 scholarships, 1 $500 scholarship, and 1 $250 scholarship.

Input Specification

  1. The amount of money in the fund, n, as of one year ago where n is greater than or equal to 0. (n may include decimal places)
  2. The yearly percent rate, p, as an integer where p is greater than zero.

Output Specification
Output the result using the format below:

X $1000 scholarships will be awarded.
Y $500 scholarships will be awarded.
Z $250 scholarships will be awarded

My code so far:

#include <stdio.h>
#include <math.h>


//main function
int main() {

        int ten, five, twofive, leftovers_ten, leftovers_five, scholarship_money;
        float fund, interest;

printf("How much was in the fund last year?\n");
scanf("%f", &fund);

printf("What is the yearly percentage rate?\n");
scanf("%f", &interest);

    scholarship_money = fund * (interest / 100);

    {
    if(ten < 5) {
    ten = scholarship_money / 1000;
    printf("%d $1000 scholarships will be awarded.\n", ten);

    }
    else {
        ten = 5;
        printf("5 $1000 scholarships will be awarded.\n");
    }
    }

        leftovers_ten = scholarship_money - (ten * 1000);
    {

    if(five < 10) {
    five = leftovers_ten / 500;
    printf("%d $500 scholarships will be awarded.\n", five);

    }

    else {
            five = 10;
        printf("10 $500 scholarships will be awarded.\n");
    }
    }

    leftovers_five = leftovers_ten - (five * 500);

    twofive = leftovers_five / 250;
    printf("%d $250 scholarships will be awarded.\n", twofive);

    return 0;
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • Like [this homework assignment](http://cboard.cprogramming.com/c-programming/171235-can-someone-help-cant-get-workings.html)? This is one of numerous money problems posted here, the newbie mistake is to work in floating point. – Weather Vane Sep 16 '16 at 23:08
  • 1
    @Weather Vane Money has trouble should one work in any data type. [various issues](http://stackoverflow.com/a/32214586/2410359) – chux - Reinstate Monica Sep 16 '16 at 23:13
  • @chux good answer but did your crystal ball say our local savings interest rate would drop to 0.5%? – Weather Vane Sep 16 '16 at 23:16
  • "having trouble figuring out a way to "cut off" the program from putting more than 5 $1,000 ..." is not descriptive enough. Detail your problem, post the inputs used, outputs seen and expected outputs. – chux - Reinstate Monica Sep 16 '16 at 23:18
  • @chux I would like to find out how to make the max number of $1000 scholarships 5 and the max number of scholarships 8 like in the problem with what I currently have – Olivia Garcia Sep 16 '16 at 23:21
  • You have repeated your goal which is partially solved with `ten = scholarship_money / 1000; if (ten > 5) ten = 5`, yet have not provided inputs used, outputs seen and expected outputs. – chux - Reinstate Monica Sep 16 '16 at 23:26
  • You have not followed on what you did with the the top scholarship money. You have already deducted it, before you do `leftovers_five = (ten * 1000) - (five * 500);` which should be `leftovers_five = leftovers_ten - (five * 500);` – Weather Vane Sep 16 '16 at 23:28
  • I just updated my code. It is working in some cases, but not all to get me the correct amount of scholarships. Do you see anything wrong with it that would hinder me from getting the correct output of scholarships? – Olivia Garcia Sep 16 '16 at 23:47
  • You must limit the number of each type of scholarship *before* you deduct the money. `ten = scholarship_money / 1000; if (ten > 5) { ten = 5; }`. As you were already advised by chux a half hour ago. – Weather Vane Sep 16 '16 at 23:54
  • I reposted my code with if else statements but I still do not get the right answers. This is due at midnight eastern so I'm a little panicky. Sorry. Please help me – Olivia Garcia Sep 17 '16 at 00:37

1 Answers1

0

The simplest tool for debugging is to just print the intermediate results out. That way you would have found out what the commenters tried to tell you. If I may take the liberty to give you a short sketch:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

// ALL CHECKS OMMITTED!

/*
If possible, the Fund prefers to award 5 $1000 scholarships, 10 $500 scholarships, and as many $250 as they have money left for. With $15,000 the Fund can award 5 $1000 scholarships, 10 $500 scholarships, and 20 $250 scholarships.

If that is not possible, the Fund will award as many $1000 and $500 scholarships as they can.

Input Specification 
  1. The amount of money in the fund, n, as of one year ago where n is greater than
     or equal to 0. (n may include decimal places)
  2. The yearly percent rate, p, as an integer where p is greater than zero.

Output Specification

  Output the result using the format below:
      X $1000 scholarships will be awarded.
      Y $500 scholarships will be awarded.
      Z $250 scholarships will be awarded
*/

int main()
{

  int ten, five, twofive, interest;
  int res;
  float fund, leftovers_ten, leftovers_five, scholarship_money;

  printf("How much was in the fund last year?\n");
  // scanf returns the number of elements it had read
  res = scanf("%f", &fund);
  if (res != 1) {
    // just bail out here for simplicity
    fprintf(stderr, "Input for fund incorrect\n");
    exit(EXIT_FAILURE);
  }
  // 1. The amount of money in the fund, n, as of one year ago where n is greater than
  //    or equal to 0. (n may include decimal places)
  if (fund < 0.0) {
    fprintf(stderr, "Fund must be bigger than or equal to zero but is %f\n",
            fund);
    exit(EXIT_FAILURE);
  }

  printf("What is the yearly percentage rate?\n");
  res = scanf("%d", &interest);
  if (res != 1) {
    // just bail out here for simplicity
    fprintf(stderr, "Input for interrest incorrect\n");
    exit(EXIT_FAILURE);
  }
  // 2. The yearly percent rate, p, as an integer where p is greater than zero.
  if (interest <= 0) {
    fprintf(stderr, "Interest must be bigger than zero but is %d\n", interest);
    exit(EXIT_FAILURE);
  }

  // some of the casts that have been added for clarity are redundant
  scholarship_money = fund * (1.0 + (float) interest / 100.0);
  printf("scholarship_money:  %.20f\n", scholarship_money);
  ten = (int) floor(scholarship_money / 1000.0);
  // the Fund prefers to award 5 $1000 scholarships...
  if (ten > 5) {
    scholarship_money = scholarship_money - (5000.0);
    ten = 5;
  }
  printf("ten:                %d\n", ten);

  leftovers_ten = scholarship_money - (ten * 1000.0);
  printf("leftovers_ten:      %.20f\n", leftovers_ten);
  five = (int) floor(leftovers_ten / 500.0);
  // ... 10 $500 scholarships ...
  if (five > 10) {
    leftovers_ten = leftovers_ten - (5000.0);
    five = 10;
  }
  printf("five:               %d\n", five);

  leftovers_five = scholarship_money - (float) (ten * 1000 + five * 500);
  printf("leftovers_five:     %.20f\n", leftovers_five);
  // ... and as many $250 as they have money left for
  twofive = (int) floor(leftovers_five / 250.0);
  printf("twofive:            %d\n", twofive);

  printf("%d $1000 scholarships will be awarded.\n", ten);
  printf("%d $500 scholarships will be awarded.\n", five);
  printf("%d $250 scholarships will be awarded.\n", twofive);
  printf("Sum:  %d\n", ten * 1000 + five * 500 + twofive * 250);
  printf("Rest: %.20f\n",
         scholarship_money - (float) (ten * 1000 + five * 500 + twofive * 250));

  exit(EXIT_SUCCESS);
}

One of your biggest problems is, that you use floating point for money operations, but I'll leave that to you (or your Prof., who may or may not point these problems out later).

There are still a lot of other ways this code can fail, you need to stuff all of these holes!

deamentiaemundi
  • 5,502
  • 2
  • 12
  • 20