Help with Homework problem

I'm having a problem with this homework problem which I'm sure will end up being a duh moment, but I'm stuck. I know I'm missing the else statement I just haven't been able to figure it out.

The program is a company giving scholarships in the amounts of 1000, 500 and 250. The funds need to be awards

The awards need to be split to go into each amount if theres enough funds to. Ex. 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.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41

//included libraries
#include <stdio.h>
#include <math.h>

//main function
int main() {


    int cost1, cost2, cost3, num1, num2, scholarship_amount;

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

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


    scholarship_amount = (num1 * num2/100);

{
        if (scholarship_amount >= 1000)
        cost1 = scholarship_amount/1000;
        printf ("\n%d $1000 scholarships will be awarded.\n", cost1);


        if (scholarship_amount >= 500)
        cost2 = scholarship_amount/500-cost1;
        printf("%d $500 scholarships will be awarded.\n", cost2);


        if (scholarship_amount >= 250)
        cost3 = scholarship_amount/250;
        printf ("%d $250 scholarships will be awarded.\n\n", cost3);
        }


return 0;

}
If statements that have more than one function require braces.
The main problem is that you're not subtracting the cost needed for scolarships before going on checking if there is money for more.
Is this heading in the right direction, or am I completely off here

1
2
3
4
5
6
        if (scholarship_amount >= 1000)
        cost1 = scholarship_amount/1000;
        if(scholarship_amount-1000);
        printf ("\n%d $1000 scholarships will be awarded.\n",cost1)
        else if(scholarship_amount =< 1000);
         printf("0 $1000 scholarships will be awarded.\n" ,cost1);


Sorry, this is only my 3rd code ever so I'm a little confused.
The only correct lines are the first 2.

The problem is that you don't have a graps of what you have to do. Get a sheet of paper and perform the math to solve the problem, then translate that into code.

Try to use better variable names so you don't get confused when using them for operations. Also use better indentation and put brackets after ifs to separate the logic.

I'd do it like this.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
const int BEST_SCHOLARSHIP_COST = 1000;
const int MEDIUM_SCHOLARSHIP_COST = 500;
const int WORST_SCHOLARSHIP_COST = 250;

int funds;
int bestScholarshipAwarded = 0;
int averageScholarshipAwarded = 0;
int worstScholarshipAwarded = 0;
// ...
if(funds >= BEST_SCHOLARSHIP_COST)
{
    bestScholarshipAwarded = funds / BEST_SCHOLARSHIP_COST;
    // Etc
}
// bestScholarshipAwarded is initialized to 0, so even if there are less than 1000$ 
// and the if is not executed it will still print that there will be 0 scholarships awarded
printf ("\n%d $1000 scholarships will be awarded.\n", bestScholarshipAwarded);
Last edited on
I went back and worked on the math and now I think the concept is showing but I'm still missing something.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
//main function
int main() {

    const int BEST_SCHOLARSHIP_COST = 1000;
    const int MEDIUM_SCHOLARSHIP_COST = 500;
    const int WORST_SCHOLARSHIP_COST = 250;

    int funds;
    int bestScholarshipAwarded = 0;
    int mediumScholarshipAwarded = 0;
    int worstScholarshipAwarded = 0;
    int amount, interest, total, remaindingbest, remaindingmedium;

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

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

    funds = (amount * interest/100);


    if (funds >= BEST_SCHOLARSHIP_COST)

    {
        bestScholarshipAwarded = funds / 5000;
        printf("%d $1000 scholarships will be awarded.\n", bestScholarshipAwarded);
        remaindingbest = funds - bestScholarshipAwarded
    }

    if (funds >= MEDIUM_SCHOLARSHIP_COST)

 {
        mediumScholarshipAwarded = (funds - remaindingbest)/500;
        printf("%d $500 scholarships will be awarded.\n", mediumScholarshipAwarded);
        remaindingmedium = funds - remaindingbest;

    }

     if (funds >= WORST_SCHOLARSHIP_COST)
     {

        worstScholarshipAwarded = (funds - remaindingmedium)/250;
        printf("%d $250 scholarships will be awarded.\n", worstScholarshipAwarded);

    }
return 0;

}
You're missing the check for the maximum of scholarships awardable. If you have $15000 you can award 15 best scholarships, but you want a maximum of 5, so check how many you can affor and if it's more that 5 set the amount to 5
1
2
3
4
5
6
7
8
9
if (funds >= BEST_SCHOLARSHIP_COST)
{
    bestScholarshipAwarded = funds / BEST_SCHOLARSHIP_COST; // or you can write 1000, it's just for the sake of clarity
    if(bestScholarshipAwarded > 5)
    {
        bestScholarshipAwarded = 5;
    }
    // ...
}

I suppose the division on line 26 is a typo since 34 and 43 are correct.

The remainding variables are not needed, because if you award scholarships their cost can be immediately subtracted from the funds. And using them needlessly complicates the logic.
It's also dangerous to use uninitialized variables. If 500 <= funds < 1000 holds true the operations for the best scholarships will not be performed and remaindingbest will not be calculated. This means that, when line 34 executes, its value is an unpredictable random value, making the program fail. Same thing for remaindingmedium

Remember that xxxxScholarshipAwarded variables are the amount of sholarships, so to get the total cost you have to multiply by the cost of a single scholarship.
Okay you lost me on this part...So should I removing the remaindingbest and remaindingmedium?

Also would this program make more since with modules?
It would be easier to code if you just use the funds variable.
funds = funds - bestScholarshipAwarded * BEST_SCHOLARSHIP_COST;
Or in the short form
funds -= bestScholarshipAwarded * BEST_SCHOLARSHIP_COST
This way you subtract 5000 from the initial 15000, and in the second calculation you start with 10000 and divide that by the cost of a medium scholarship.

Also would this program make more since with modules?

What do you mean?
Okay here is where I'm at now with the suggestion, but I'm still not figuring out how to only have the amount only 5 to 1000 10 to 500 and the rest go to 250.

1
2
3
4
5
6
7
8
9
10
11
12
13
if (funds = funds - bestScholarshipAwarded * BEST_SCHOLARSHIP_COST);
        {bestScholarshipAwarded = (funds / BEST_SCHOLARSHIP_COST);
        printf("%d $1000 scholarships will be awarded.\n", bestScholarshipAwarded);

    if (funds >= MEDIUM_SCHOLARSHIP_COST)
        {mediumScholarshipAwarded = funds / MEDIUM_SCHOLARSHIP_COST;
        printf("%d $500 scholarships will be awarded.\n", mediumScholarshipAwarded);
        funds = funds - mediumScholarshipAwarded * MEDIUM_SCHOLARSHIP_COST;}

        if (funds >= MEDIUM_SCHOLARSHIP_COST);
        {worstScholarshipAwarded = funds / WORST_SCHOLARSHIP_COST;
        printf("%d $250 scholarships will be awarded.\n", worstScholarshipAwarded);}
        funds = funds - worstScholarshipAwarded * WORST_SCHOLARSHIP_COST;}
Don't put everything inside the first if. The calculations need to be run independently from the first one.
Why did you change the first condition? It was correct before, and it is correct in #2 and #3.
I wrote how to check the max amount of scholarships 4 posts ago.
Here is what I have now... i'm just having problems with the smaller numbers now.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
//main function
int main() {

    const int BEST_SCHOLARSHIP_COST = 1000;
    const int MEDIUM_SCHOLARSHIP_COST = 500;
    const int WORST_SCHOLARSHIP_COST = 250;

    int funds;
    int bestScholarshipAwarded = 0;
    int mediumScholarshipAwarded = 0;
    int worstScholarshipAwarded = 0;
    int remainder = 0;
    int remainingmedium = 0;
    int amount, interest, remainingbest;

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

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

    //formula
    funds = (amount * interest/100);

    // Checking the if funds are more than or equal to BEST
    if (funds >= BEST_SCHOLARSHIP_COST) {
        bestScholarshipAwarded = funds / BEST_SCHOLARSHIP_COST;
        if(bestScholarshipAwarded > 5){
        remainder = bestScholarshipAwarded - 5;
            bestScholarshipAwarded = 5;
            remainingbest = BEST_SCHOLARSHIP_COST * remainder;
            printf("%d $1000 scholarships will be awarded.\n", bestScholarshipAwarded);
        }else{
            remainder = bestScholarshipAwarded % 1;
            bestScholarshipAwarded -= remainder;
            remainingbest = BEST_SCHOLARSHIP_COST * remainder;
            printf("%d $1000 scholarships will be awarded.\n", bestScholarshipAwarded);
        }


    // taking the remainder of the Math above and checking to see if it is
    // more or equal to Medium and the else is if it is more or equal to Worst
    if(remainingbest >= MEDIUM_SCHOLARSHIP_COST){
        mediumScholarshipAwarded = remainingbest / MEDIUM_SCHOLARSHIP_COST;
        if(mediumScholarshipAwarded > 10){
        remainder = mediumScholarshipAwarded - 10;
            mediumScholarshipAwarded = 10;
            remainingmedium = MEDIUM_SCHOLARSHIP_COST * remainder;
            printf("%d $500 scholarships will be awarded.\n", mediumScholarshipAwarded);
        }else{
            remainder = mediumScholarshipAwarded % 1;
            mediumScholarshipAwarded -= remainder;
            remainingmedium = MEDIUM_SCHOLARSHIP_COST * remainder;
            printf("%d $500 scholarships will be awarded.\n", mediumScholarshipAwarded);
        }
            worstScholarshipAwarded = remainingmedium / WORST_SCHOLARSHIP_COST;
            printf("%d $250 scholarships will be awarded.\n", worstScholarshipAwarded);
        }else{
            worstScholarshipAwarded = remainingmedium / WORST_SCHOLARSHIP_COST;
            printf("%d $250 scholarships will be awarded.\n", worstScholarshipAwarded);
        }

    // Checking if funds are more than or equal to MEDIUM but obivously less then BEST
    }else if(funds >= MEDIUM_SCHOLARSHIP_COST){
        mediumScholarshipAwarded = funds / MEDIUM_SCHOLARSHIP_COST;
        if(mediumScholarshipAwarded > 5){
            mediumScholarshipAwarded -= 5;
            remainder = mediumScholarshipAwarded % 2;
            mediumScholarshipAwarded -= remainder;
            remainingmedium = MEDIUM_SCHOLARSHIP_COST * remainder;
            printf("%d $500 scholarships will be awarded.\n", mediumScholarshipAwarded);
        }else{
            remainder = mediumScholarshipAwarded % 1;
            mediumScholarshipAwarded -= remainder;
            remainingmedium = MEDIUM_SCHOLARSHIP_COST * remainder;
            printf("%d $500 scholarships will be awarded.\n", mediumScholarshipAwarded);
        }

    // taking the remainder of the Math above and checking to see if it is
    // more or equal to Worst
        if (remainingmedium >= WORST_SCHOLARSHIP_COST) {
            worstScholarshipAwarded = remainingmedium / WORST_SCHOLARSHIP_COST;
            printf("%d $250 scholarships will be awarded.\n", worstScholarshipAwarded);
        }

    //Lastly seeing if it is more or equal to WORST
    }else{
        worstScholarshipAwarded =  funds / WORST_SCHOLARSHIP_COST;
        printf("%d $250 scholarships will be awarded.\n", worstScholarshipAwarded);
    }


    return 0;

}
You're still failing to get the logic right, and honestly it's getting a bit hard to explain the mistakes through the forum.
I didn't want to write the code for you because homework is supposed to teach you this stuff, but I'll do it and hopefully you will not just turn it in without reading and understaing it.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
int main()
{
	// These constants are not really important.
	// They make the code more clear but you can manually
	// write the values every time they are needed.
	const int BEST_SCHOLARSHIP_COST = 1000;
	const int MEDIUM_SCHOLARSHIP_COST = 500;
	const int WORST_SCHOLARSHIP_COST = 250;

	int amount = 0;
	printf("How much was in the fund last year?\n");
	scanf("%d", &amount);

        // If interest is integer then interest/100 will be 0 unless interest is greater than 100
	float interest = 0.0;
	printf("What is the yearly percentage rate?\n");
	scanf("%f", &interest);

	int funds = amount * interest / 100;

	// I'll use these names since they are shorter...
	int bsa = 0; // bestScholarshipsAwarded
	int msa = 0; // mediumScholarshipsAwarded
	int wsa = 0; // worstScholarshipsAwarded

	// Check if there is enough money for at least one scholarship
	if(funds >= BEST_SCHOLARSHIP_COST)
	{
		// Calculate how many scholarships can be awarded with the current funds
		bsa = funds / BEST_SCHOLARSHIP_COST;

		// If the amount is more than we want to award... 
		if(bsa > 5)
		{
			// ...set the amount to the maximum
			bsa = 5;
		}

		// Subtract from the funds the total cost of the scholarships we can award
		funds -= bsa * BEST_SCHOLARSHIP_COST;
	}

	// If you put the printf() inside the 'if' it won't print
	// anything if there areless than $1000
	// It doesn't really matter though
	printf ("%d $1000 scholarships will be awarded.\n", bsa);

	// When you get at this points there are two cases:
	// 1) scholarships were already awarded so the total funds are diminished
	// 2) there wasn't enough money to award a higher-class scholarship
	// so the funds are unchanged

	// In either case the formula doesn't care about that, it only needs to know
	// how many funds there are in order to check if there are enough to pay for
	// its kind of scholarship
	if(funds >= MEDIUM_SCHOLARSHIP_COST)
	{
		msa = funds / MEDIUM_SCHOLARSHIP_COST;
		if(msa > 10)
		{
			msa = 10;
		}
		funds -= msa * MEDIUM_SCHOLARSHIP_COST;
	}
	printf ("%d $500 scholarships will be awarded.\n", msa);

	// As you can see the logic is the same in all the cases,
	// only with different numbers
	if(funds >= WORST_SCHOLARSHIP_COST)
	{
		wsa = funds / WORST_SCHOLARSHIP_COST;

		// No maximum in the third case, so no 'if' needed

		funds -= wsa * WORST_SCHOLARSHIP_COST;
	}
	printf ("%d $250 scholarships will be awarded.\n", wsa);


	printf("There are $%d remaining\n", funds);

	// End the program
	return 0;
}

If you don't understand something feel free to ask, but please make an honest effort to understand it.
Last edited on
Topic archived. No new replies allowed.