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.
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.
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; }
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.
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
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.
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.".
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; }
What exactly are the issues?
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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
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.
> 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.
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.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)
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.