One small problem!

This is a discussion on One small problem! within the C Programming forums, part of the General Programming Boards category; Need to take in two variable ranges, 3 to 6 and 1 to 4. The program is supposed to calculate ...

  1. #1
    jfc
    Guest

    Unhappy One small problem!

    Need to take in two variable ranges, 3 to 6 and 1 to 4. The program is supposed to calculate the cubed value of the ranges (432 & 100). I have written the functions and they work fine but I can't accumulate the totals! At the moment it is returninf the cubed values for the last digit of each range (54 & 36). Probably something really easy but I'm stuck! Here is the code:

    #include<stdio.h>
    #include<conio.h>

    main()
    {

    int firsta,
    lasta, /* first range */
    firstb,
    lastb, /*second range */
    rangea,
    rangeb;

    int sum_of_cubes (int, int);

    printf("Enter first range : ");
    scanf("%d%d", &firsta, &lasta);
    printf("Enter second range: ");
    scanf("%d%d", &firstb, &lastb);

    rangea = sum_of_cubes (firsta, lasta);
    rangeb = sum_of_cubes (firstb, lastb);

    printf("first range cubed %d\nsecond range cubed %d", rangea, rangeb);

    getch();
    }
    int sum_of_cubes (int first, int last)
    {
    int number = first,
    sum = 0,
    parameter,
    total;

    int cube (int);

    while (number <= last)
    {
    /* this is the bit I'm stuck on!!
    parameter = cube(number);
    sum = parameter;
    number += 1;
    */
    }

    return sum;
    }

    cube (int item)
    {
    return item *3 *3;
    }

  2. #2
    JFK
    JFK is offline
    Registered User
    Join Date
    Oct 2001
    Posts
    15

    Angry Re: one small problem below.

    Sorry, the JFC post below was meant to be me, if anyone is kind enough to reply can they do it through this one please? Thanks.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    > return item *3 *3;
    Cube is
    item * item * item

  4. #4
    JFK
    JFK is offline
    Registered User
    Join Date
    Oct 2001
    Posts
    15

    re above:

    Thanks Salem, I've been spending so long on this that I put a 3 instead times the item to the power of 3! Doh.

    The program is still not giving me a combined total however, it's merely reporting the cube of the last digits in each range. Any ideas how to store them as a running total?

  5. #5
    JFK
    JFK is offline
    Registered User
    Join Date
    Oct 2001
    Posts
    15

    Smile Cancel the above!

    Cracked it.

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    Code:
    int sum_of_cubes(int first, int last) { 
        int number = first, 
            sum = 0;
        while(number <= last) { 
            sum += cube( number ); 
            number ++; 
        } 
        return sum; 
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Small Problem with double and integer adding?
    By Nathan the noob in forum C++ Programming
    Replies: 5
    Last Post: 03-28-2009, 04:16 PM
  2. Visual C++ small problem
    By gadu in forum C++ Programming
    Replies: 0
    Last Post: 03-10-2009, 10:45 PM
  3. Small problem with this array...
    By Merholtz in forum C Programming
    Replies: 7
    Last Post: 11-03-2008, 03:16 PM
  4. Help with a small problem (beginner)
    By piffo in forum C Programming
    Replies: 13
    Last Post: 09-29-2008, 04:37 PM
  5. Need Big Solution For Small Problem
    By GrNxxDaY in forum C++ Programming
    Replies: 8
    Last Post: 08-01-2002, 03:23 AM

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