Thread: C++ Cannonball Pyramid Calculator (tetrahedron)

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    2

    C++ Cannonball Pyramid Calculator (tetrahedron)

    I am making a C++ program that will calculate how many cannonballs there are in a cannonball pyramid, or tetrahedron. (4 sided pyramid, not your typical 5 sided one), depending on how many cannonballs are on one side of the bottom layer or how many layers there are in the tetrahedron. So lets say there are 10 cannonballs on one side of the bottom layer, making this tetrahedron 10 layers tall. There would be 55 cannonballs total on the bottom layer (10+9+8+7+6+5+4+3+2+1=55) The next layer would have 45 (55-10=45) and the next 36 (45-9=36) and so on... 55+45+36+28+21+15+10+6+3+1 = 220, so there would be 220 cannonballs total in this tetrahedron.

    So I know what I want my program to do, I just don't know how to do it. The program right now will take 10 layers, or 10 cannonballs on a side, and turn it into 55 using recursion (I can use recursion or a nested loop). However, I have no idea what I am going to do to get it to find out what the next layer needs to be, then add it together eventually getting it to add all layers and getting the sum, which is 220.

    The pyramidChart() function is just a reference so you can see how many cannonballs are in a tetrahedron 1-10 layers high.

    Code:
    #include <iostream>
    
    using namespace std;
    
    int cannonBalls(int side);
    
    int main() {
    	
    	cout << "The first layer has "<< cannonBalls(10) << " cannonballs."<< endl;
    	
    	return 0;
    }
    
    int cannonBalls(int side) {
        cout << "The number of cannonballs is " << side << endl;
    	//if base case return 1
    	if(side==1) {
    		return 1;
        }
    	else {
    		//otherwise return side + fn(s-1)
    		return side + cannonBalls(side - 1);
    		
        }
    
    }
    
    	
    
    int pyramidChart() {
    	int n = 0;
    	int cannonBalls = 0;
    	cin>>n;
    	
    	switch (n) {
    		case 1:
    			cannonBalls = 1;
    			break;
    		case 2:
    			cannonBalls = 3+1;
    			break;
    		case 3:
    			cannonBalls = 6+3+1;
    			break;
    		case 4:
    			cannonBalls = 10+6+3+1;
    			break;
    		case 5:
    			cannonBalls = 15+10+6+3+1;
    			break;
    		case 6:
    			cannonBalls = 21+15+10+6+3+1;
    			break;
    		case 7:
    			cannonBalls = 28+21+15+10+6+3+1;
    			break;
    		case 8:
    			cannonBalls = 36+28+21+15+10+6+3+1;
    			break;
    		case 9:
    			cannonBalls = 45+36+28+21+15+10+6+3+1;
    			break;
    		case 10:
    			cannonBalls = 55+45+36+28+21+15+10+6+3+1;
    			break;
    		default:
    			cannonBalls = 0;
    	}
    	return cannonBalls;
    }
    Any help is appreciated! Thanks!

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    First, you need to replace those hard-coded numbers by a single generic function that just takes the side length. The triangle that makes up one layer has, for a side length of N, 1+2+3+...+N balls. This should be really easy to calculate in a loop instead of writing the calculations out. (Actually, you can do it with a simple multiplication, but that's left as an extra exercise.)

    Once you have the function, let's call it layer(n), you can calculate the number of balls in a pyramid by summing up the results of layer(1) through layer(N).
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    2
    Thanks Corn Bee. However, ignore the pyramidChart() function, I just created that so I could have a reference to what the numbers needed to add up to. Look at the function cannonBalls(int side) function. That function takes the number 10, which is the side length, and comes up with 55, which is the total number of canonballs for layer 1. I however, need to either make another function, or add something to this current function, to get 55 to 220. It's late though, I am off to bed! I'll continue thinking about what you said though, maybe I can use it with my cannonBalls function...

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    My layer() is your cannonBalls(). Now do the sum.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating a half pyramid
    By rightbrainer in forum C Programming
    Replies: 7
    Last Post: 04-21-2009, 01:29 AM
  2. Number pyramid
    By Tehy in forum C Programming
    Replies: 7
    Last Post: 05-31-2007, 09:01 AM
  3. Making a pyramid of Xs
    By Tride in forum C++ Programming
    Replies: 8
    Last Post: 12-12-2003, 05:14 PM
  4. Physics: Kind of like a cannonball...
    By Cheeze-It in forum Game Programming
    Replies: 11
    Last Post: 09-12-2001, 01:53 PM