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. #1
    Registered User
    Join Date
    Aug 2012
    Posts
    3

    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. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,308
    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
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Aug 2012
    Posts
    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. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,308
    Quote 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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Aug 2012
    Posts
    3
    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. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,308
    If that works for you, then it should be fine

    Of course, you could do a little more, e.g., input validation.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C code, MD5 hash, if statements assistance
    By letmein in forum C Programming
    Replies: 5
    Last Post: 09-14-2010, 10:25 AM
  2. Hello,i need assistance in completing the code
    By toader in forum C++ Programming
    Replies: 1
    Last Post: 06-22-2009, 04:32 AM
  3. Code Assistance
    By Nezmin2 in forum C Programming
    Replies: 12
    Last Post: 12-19-2008, 12:26 AM
  4. Needing Assistance;Unreachable code in function main()
    By Mehtabyte in forum C++ Programming
    Replies: 5
    Last Post: 11-19-2008, 02:27 PM
  5. C code assistance please
    By lotus in forum C Programming
    Replies: 7
    Last Post: 05-25-2002, 03:29 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21