c - 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;
}
解决方案
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.
推荐阅读
- go - 如何获取本地时区的全名?
- c# - 为什么 API 向我发送新用户的 OK 响应和 Id 但找不到用户?
- r - Stata / R中多个国家的格兰杰因果关系
- reactjs - 为空时从 API 替换图像 url
- kubernetes - 我们可以在 Kubernetes 中使用 HPA 以固定块的方式将 Pod 扩展/扩展吗?
- android - 如何将 Android TV 库添加到 Cordova 项目?
- vue.js - 未找到此依赖项:* ./src/firebase/firebase.js 中的 firebase
- reactjs - 带有令牌的Keycloak javascript适配器初始化不起作用?
- javascript - 在 Angular JS 1 上添加 html url 超链接
- java - 无法启动 kafka 服务器 (java.lang.NoSuchMethodError)