# Beginner code assistance

This is a discussion on Beginner code assistance within the C Programming forums, part of the General Programming Boards category; Hello all! I'm self learning C and I am supposed to be creating a table with compounding interest for 5 ...

1. ## Beginner code assistance

Hello all!

I'm self learning C and I am supposed to be creating a table with compounding interest for 5 years and for 5 levels of interest rates. I've been following an eBook and have been stuck on this Array area for a bit just trying to get my head around it.

The code I've got below just shows values of '100.00' for each "cell". Could someone please point out as to why this is?

It's most likely a really beginner/noob question, but either I just don't fully have my head around it yet or I'm too close to the task. Anyway, my code is below.

EDIT: Initial investment = \$100.

Code:
```#include <stdio.h>
#define NUM_RATES ((int) (sizeof(value) / sizeof(value[0])))
#define INITIAL 100
int main(void)
{
int i, rate, year, num_years;
double value[5];

printf("Enter interest rate [%%]: ");
scanf("%d", &rate);
printf("Enter number of years: ");
scanf("%d", &num_years);

printf("\nYears");
for (i = 0; i < NUM_RATES; i++){
printf("%6d%%", rate + i);
value[i] = INITIAL;
}

printf("\n");

for (year = 1; year <= num_years; year++){
printf("%3d   ", year);
for (i = 0; i < NUM_RATES; i++){
value[i] += (rate + i) / 100 * value[i];
printf("%7.2f", value[i]);
}
printf("\n");
}

return 0;
}```

2. Eh, this is a little backwards:
Code:
`#define NUM_RATES ((int) (sizeof(value) / sizeof(value[0])))`
It would have been better to write:
Code:
`#define NUM_RATES 5`
then write:
Code:
`double value[NUM_RATES];`
Anyway, the problem is that the expression (rate + i) / 100 involves integer division. You could force floating point division by changing it to: (rate + i) / 100.0

3. Thanks for the quick reply.
That seems so much clearer. Yeah that NUM_RATES part was given in the eBook as well as the answer section ?? I read somewhere that it is done that if you need to make changes to the array size, then all you need to change is just 'double value[X]'. Any validity in this?

This is a quote from the eBook I'm reading:
"We can also use 'sizeof' to measure the size of an array element, such as a[0]. Dividing the array size by the element size gives the length of the array".

Thanks for the help with that expression.

Also...just one more thing. Why is it that at line 17, that that piece of code needs to be in that for loop?

4. Originally Posted by akust0m
Yeah that NUM_RATES part was given in the eBook as well as the answer section ?? I read somewhere that it is done that if you need to make changes to the array size, then all you need to change is just 'double value[X]'. Any validity in this?
Yes, but the problem is that that method only works when you are actually dealing with an array. If you pass the array to a function, you would be passing a pointer to its first element, hence the method will not work (you get the sizeof the pointer instead of the array), even though it looks like it would. The worst part is that because it looks like it would work (NUM_RATES is obviously the number of elements of the array), you get a bug that is more difficult to spot.

5. I appreciate your response greatly, unfortunately my knowledge isn't up to a level where I fully understand that just yet.

I have changed a couple of things, would this be better:

Code:
```#include <stdio.h>
#define NUM_RATES 5
#define INITIAL 100
int main(void)
{
int i, rate, year, num_years;
double value[NUM_RATES];

printf("Enter interest rate [%%]: ");
scanf("%d", &rate);
printf("Enter number of years: ");
scanf("%d", &num_years);

printf("\nYears");
for (i = 0; i < NUM_RATES; i++){
printf("%6d%%", rate + i);
value[i] = INITIAL;
}

printf("\n");

for (year = 1; year <= num_years; year++){
printf("%3d   ", year);
for (i = 0; i < NUM_RATES; i++){
value[i] += (rate + i) / 100.0 * value[i];
printf("%7.2f", value[i]);
}
printf("\n");
}

return 0;
}```

6. One of the odd things about C (also rather wonderful), is that (and believe me, it's true), when you pass an array to a function, the array "degrades" to just a pointer. So IF your program used sizeof() on such an array, outside the function that created the array, the result would be (and this is the odd part), that sizeof() would tell you that the entire array was just the size of a pointer! (a char array would be the size of a char pointer, an int array would be the size of an int pointer, etc.).

The advantage of doing this, is that it allows C to run considerably quicker. The disadvantage of C doing this, is that it mystifies beginners, at first.

Welcome to the forum, btw! You should tell us what problems you are having with your program. That saves us a tremendous amount of time.

7. If that works for you, then it should be fine

Of course, you could do a little more, e.g., input validation.