I am trying to 1: Calculate the sum of the square numbers leading up to a user entered integer. For ex, if the user inputted 3, the function should calculate 1*1+2*2+3*3 = 14
And 2: Make a triangle (downwards first but then I have to figure out upwards too) The user has to tell it the character to use (eg, &) as well as the # of rows in the triangle. It would look something like this:
&&&
&&
&
(Similar to 3, except 3 would look like
&
&&
&&&)
Update: My current progress is below, but when the triangles are used the downwards one just prints like this
&&&
&&&
&&&
in the correct number of rows. The upwards triangle just doesn't work. When I try to enter the character, everything disappears off the console for the output.
And for the sum of squares, it kind of works, but it outputs "The sum is" for every calculation. So if 3 was entered, it would say "The sum of the squares is 9" "The sum of the squares is 18" The sum of the Squares is 27"
instead of "The sum of the squares is 14"
The code below includes the functions but does not include the main or header files (all the main file does is tell it to run) or the #include's. And I know it's probably really wrong but I've been trying for a while and this is the best i've gotten for these.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
|
// Function 1
using namespace std;
void squares (){
double n = 0.0;
double sum = 0.0;
int i = 0;
cout << " \n\n Please enter the value of n: ";
cin >> n;
while (i<=n){
i++;
}
sum +=pow(n,2.0);
cout << "\n\n The sum of the squares is: " << sum <<endl;
}
void printDownTriangle(){
char x;
int r = 0;
int i = 0;
cout << " \n\n What character would you like to print?";
cin >> x;
cout << "\n\n How many rows in the triangle? ";
cin >> r;
while (i < r){
int j = 1;
while (j < r){
cout << x;
j++;
}
cout << endl;
i--;
}
}
void printUpTriangle(){
char x;
int r = 0;
int i = 0;
cout << " \n\n What character would you like to print?";
cin >> x;
cout << "\n\n How many rows in the triangle? ";
cin >> r;
while (i < r){
int j = 1;
while (j < r){
cout << x;
j++;
}
cout << endl;
i++;
}
}
|