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!