首页 > 解决方案 > c variables and equation

问题描述

Updated code and question thanks to Sankalp Bhamare : The updated code is not getting the expected value in variable twoHundredFiftyScholarship.

When using the debugger I can see that the only value that is incorrect is for variable twoHundredFiftyScholarship.

The expected value for variable twoHundredFiftyScholarship when entering the sample run #1 should be 1. The value I get is 3.

same thing with for sample run #2. The expected value is 8. The value I get is 48.


Sample Run #1

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.

Sample Run #2

How much was in the fund last year?

1200000

What is the yearly percentage rate?

1

5 $1000 scholarships will be awarded.

10 $500 scholarships will be awarded.

8 $250 scholarships will be awarded.

The expected value for variable twoHundredFiftyScholarship when entering the sample run #2 should be 8. The value I get is 48.

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


int main(){

    double fundAmount;
    int yearlyInterestRate;
    double yearlyInterest;
    double remainingScholarship;
    int thousandScholarships = 0;
    int fiveHundredScholarship = 0;
    int twoHundredFiftyScholarship = 0;


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

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

    yearlyInterest = fundAmount*yearlyInterestRate/100.0;
    remainingScholarship = yearlyInterest;
    thousandScholarships = remainingScholarship/1000.0;
    fiveHundredScholarship = remainingScholarship/500.0;
    twoHundredFiftyScholarship = remainingScholarship/250.0;

    if(thousandScholarships > 5){
        thousandScholarships = 5;

        remainingScholarship -= thousandScholarships*1000.0;}


    if(fiveHundredScholarship > 10){
        fiveHundredScholarship = 10;

        remainingScholarship -= fiveHundredScholarship*500;}



        remainingScholarship -= twoHundredFiftyScholarship*250;



        printf("%d $1000 scholarships will be awarded.\n",thousandScholarships);
        printf("%d $500 scholarships will be awarded.\n",fiveHundredScholarship);       
        printf("%d $250 scholarships will be awarded.\n",twoHundredFiftyScholarship);



    system("pause");

    return 0;
}

标签: cvariablesequation

解决方案


Question 1: How many variables are needed for this problem?

That question makes no sense. Imagine you have one variable declared as int x; then you could code

int x1 = x;
int x2 = x*1;
int x3 = x+0;
int x4 = x|0;

then all of x, x1, x2, x3, x4 contain the same integer value and could be used interchangeably.

Imagine also that you have two variables and code

int x= something();
int y= x+1;

then (if y is not changed later) you could substitute occurrences of y with x+1.

Be sure to read more about the C programming language (notably Modern C) and How to debug small programs.


推荐阅读