PDA

View Full Version : C language help!



Deagle
02-12-2006, 04:48 AM
Hey, doing a little assignment for my C programming class. I've done about half or 2/3 of it but is now stuck. Can anyone here that's knowledgable in C help me figure out why my program is not giving me a correct output? I'll paste it here:

/* A program that calculates the accumulated amount give initial amount, annual interest rate and years of compounding */

float calc_acc_ini(float initial_amount,float annual_interest); /* Given initial amount Return accumulated amount */

float calc_acc_amt(float acc_amount, float annual_interest, int years); /* Given accumulated amount Return final accumulated amount */

#include <stdio.h>
main()
{ float initial_amount; /* initial amount in account */
float annual_interest; /* annual interest rate */
float acc_amount; /* accumulated amount in account */
int years; /* years of compounding */
float final_acc_amount; /* final amount in account */

/* Enter intitial amount, annual interest rate and years of compounding */
printf("Enter initial amount: ");
scanf("%f", &initial_amount);
printf("Enter annual interest rate: ");
scanf("%f", &annual_interest);
printf("Enter years of compounding: ");
scanf("%d", &years);

while(initial_amount > 0) /* looping */
{
/* Calculate accumlated amount */
final_acc_amount = calc_acc_amt(acc_amount,annual_interest,years);
/* Show accumulated amount */
printf("Accumulated amount: %4.2f $ \n", final_acc_amount);
printf("Enter initial amount: ");
scanf("%f", &initial_amount);
printf("Enter annual interest rate: ");
scanf("%f", &annual_interest);
printf("Enter years of compounding: ");
scanf("%f", &years);
}
}
/* A function that calculates the accumulated amount */
float calc_acc_ini(float initial_amount, float annual_interest)
{ float acc_amount;
acc_amount = initial_amount + initial_amount * annual_interest;
return acc_amount;
}

/* A function that calculates the final accumulated amount */
float calc_acc_amt(float calc_acc_ini, float annual_interest, int years)
{ float a;
while(years > 0)
{ a = calc_acc_ini + calc_acc_ini * annual_interest;
years = years - 1;
}
return a;
}

saphalline
02-12-2006, 04:47 PM
How far is it getting? What is displaying or not displaying?

Deagle
02-12-2006, 05:01 PM
Ah a reply. :D Well I started fixing it again and it only shows the accumulated amount for 1 year. Example:
Enter initial amount: $100
Enter interest rate: .005
Enter years of compounding: 5
Accumulated amount: $100.5
It's as if the year is not taken into account at all. :confused:
This led me to believe that the function calc_acc_ini works but the calc_acc_amt does not. The point I'm trying to make is how do I loop it so that it takes the acc_amount for the 1st year and keeps on calculating until the number of years have been met.(Not sure if that makes sense to anyone) :(

saphalline
02-12-2006, 10:35 PM
Oh, duh! I see the problem now! Man, I hate it when those things slip by... :rolleyes: Try this:

/* A function that calculates the final accumulated amount */
float calc_acc_amt(float calc_acc_ini, float annual_interest, int years)
{
float a = calc_acc_ini;

while(years > 0)
{
a = a * (1. + annual_interest);
years = years - 1;
}

return a;
}

You see, the way you have it now, the loop runs for the number of years entered. But in every iteration, your float a var is being set to the same thing over and over. You need it to accumulate.

The other way to do it is using a for loop. That will decrease run-time because you know the loop has to run <years> times. A for loop will eliminate the need to do a complex conditional check at the beginning of each iteration and thus cleans up branch prediction problems. Like this:

/* A function that calculates the final accumulated amount */
float calc_acc_amt(float calc_acc_ini, float annual_interest, int years)
{
float a = calc_acc_ini;

for( int i=0; i<years; i++ )
{
a = a * (1. + annual_interest);
}

return a;
}

There's also the matter of shortcuts in the code that work with most standard C/C++ compilers. The lines:

a = a * (1. + annual_interest);
years = years - 1;

could be shortened to:

a *= 1. + annual_interest;
years--;

Also, in order to prevent memory leaks, it's a good idea to give initial values to any new vars being declared. Setting all your floats to "0." will help with this. Such as:

float initial_amount = 0.; /* initial amount in account */

It should be "0" for integers or other ordinal types, and "0." for floats. Other classes have their own methods for initializing various states, but I'm guessing classes come later. ;)

Also, for single-line comments, you don't need /* and then */ to close it. You can just do //. Like this:

// initial amount in account

No closing flags needed. That should get you started...

Deagle
02-12-2006, 11:28 PM
I tried your code but it still doesn't work. Also in the mean time I've clean it up a bit and now it looks like this:

float calc_acc_ini(float init_amount,float annuall_interest); /* Given initial amount
Return accumulated amount */
float calc_amount(float ini_amount,float annual_interest,int years);


#include <stdio.h>
main()
{ float ini_amount; /* initial amount in account */
float annual_interest; /* annual interest rate */
float acc_amount; /* accumulated amount in account */
int years;
float c;

/* Enter intitial amount, annual interest rate and years of compounding */
printf("Enter initial amount: $");
scanf("%f", &ini_amount);
printf("Enter annual interest rate: ");
scanf("%f", &annual_interest);
printf("Enter years: ");
scanf("%f", &years);

while(ini_amount > 0) /* looping */
{
/* Calculate accumlated amount */
c = calc_amount(ini_amount,annual_interest,years);
/* Show accumulated amount */
printf("Accumulated amount: $%4.2f \n", c);
printf("Enter initial amount: $");
scanf("%f", &ini_amount);
printf("Enter annual interest rate: ");
scanf("%f", &annual_interest);
printf("Enter years: ");
scanf("%f", &years);

}
}
/* A function that calculates the accumulated amount */
float calc_acc_ini(float init_amount, float annuall_interest)
{ float acc_amt;
acc_amt = init_amount + init_amount * annuall_interest;
return acc_amt;
}
/* A function that calculates the accumulated amount */
float calc_amount(float ini_amount, float annual_interest, int years)
{ float c;
while (years > 0)
{ c = calc_acc_ini(ini_amount,annual_interest);
years = years - 1;
return c;
}
}
It still only gives me acc_amt for 1 year. :mad:

Deagle
02-13-2006, 02:22 AM
Yay after I had a tip from a friend and tweaked it a lil' it works. Thanks for the help Sap. Hope it's not too late to turn it in... :(

saphalline
02-13-2006, 02:27 PM
Glad you got it working then. What does your final code look like?

Deagle
02-13-2006, 07:52 PM
Turns out it was late so -10% but oh well. :(
For it to work all I had to do was make a new integer, a and let it = 1. Then while(a <= years). Looks like this:

/* A function that calculates the accumulated amount */
float calc_acc_amt(float acc_amount, float annual_interest, int years)
{ int a = 1;
while(a <= years)
{ acc_amount = acc_amount + acc_amount * annual_interest;
a = a + 1;
}
return acc_amount;
}

Ha spend a whole day and finally got it. It was worth it though because now I understand how functions and loops work and is more prepare for midterm this Thursday. :eek: