Making a triangle and summing using while loops

Oct 6, 2014 at 10:18pm
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++;
		}
	}
Last edited on Oct 7, 2014 at 8:30pm
Oct 6, 2014 at 10:55pm
Oct 6, 2014 at 11:06pm
Your fucntion 1 is good excpet for the fact that you messed up the while loop a little.

First, your condition while(n <= i) means that the loop will execute only if n <= i . But then, i = 0 and n is a user input which is greater than 0.
Hence your loop will never work.

Aside that, you fail to modify the variable that checks the condition.
E.g
1
2
3
4
5
while (n > 3) //n = 4
{
    cout<<"hello"<<endl;
    // n--; n will eventually become < 3 and the loop will stop.
}

this loop will go for ever coz n will always remain 4 for eternity.
The comment fixes it.


In function 2,

First, (j<i({ . Mismatched "( ".
secondly, i and j will always have a constant difference of 1 through out the while loops.

In general, your ideas are all good.

Here is a working code,

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
#include <iostream>
#include <cstdlib>
using namespace std;

int main()
{
	int n;
	int sum = 0;

	cout<<"Enter the value of n: ";
	cin>>n;

	for(int i = 1; i <= n; i++)
		sum += i*i;

	cout<<"The sum of the squares is "<<sum<<endl;
	system("pause");
	system("cls");

	char ch;
	int nRow;

	cout<<"Enter the character to use: ";
	cin>>ch;
	cout<<"Enter the number of rows: ";
	cin>>nRow;

	for(int i = 0; i < n; i++)
	{
		for(int j = i; j < n ; j++)
			cout<<ch;
		cout<<endl;
	}

	system("pause");
	return 0;
}

Oct 7, 2014 at 12:55am
Unfortunately, I do not yet know how to use a for loop.
Oct 7, 2014 at 1:06am
for loop structure
 
for(initialize, condition, increment)


while loop structure
1
2
3
4
5
initialize
while(condition)
{
  increment
}


You can use any of the two.

Exmpale.
print 1 to 10.
1
2
3
4
5
6
7
8
9
10
11
for(int i = 1; i <= 10; i++)
cout<<i<<endl;

//using while

int i = 1;
while(i <= 10)
{
    cout<<i<<endl;
    i++;
}


You see, they are the same.
Oct 7, 2014 at 1:07am
read control flow (chapter 5), from above mentioned link.
Oct 7, 2014 at 3:08pm
Thank you so much, shadowCODE !
Oct 7, 2014 at 8:28pm
But how do I get the triangle to look like a triangle rather than a square?
Oct 7, 2014 at 8:46pm
you'll need nested loop (one loop in another loop) to do that. that diagram is a classic for beginners. so try yourself. if you can - you'll remember it.
Oct 7, 2014 at 9:00pm
Hi, I made a code to print a triangle, check if works to you. If you want to watch how works step by step use debugger and see what happens.

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
# include < iostream >
using namespace std ;

int main ()
{
   int n;
  char ch; 

  cout << "Enter a number : "; // this is a height of triangle.
  cin >> n;
  cout << "Enter a character:  ";
  cin>>ch;

  for (int j = 0; j < n; ++j)    // Here, this loop is for # of rows until n = height
 {
     for (int i = 0; i < n-j-1; ++i)  // This loop is for # of columns that has to put a space
    {                                         
          cout << " ";
    }
    for (int i = 0; i < 2*j+1; ++i) // This loop is for # of columns that has to put a
   {                                            //  character.
         cout << ch ;
   }
   cout << endl ;
 }

 cin.get();
 return 0;
}


j = rows
i = columns

for example: If the height of triangle was 3. you get this:

0 0 * 0 0
0 * * * 0
* * * * *

Zeros represent the space or empty, and * the character.

Advice: :)
You have to write on a piece of paper each iteration of a loops to understand how wokrs. try this and you will undertand how works the loops in general. It works for me.
Last edited on Oct 7, 2014 at 9:44pm
Oct 7, 2014 at 9:02pm
Wait, i thought i did a nested loop. Line 27,46. How would I do a nested loop for the first one? I am so lost.
Oct 7, 2014 at 9:46pm
I edited my above comment. check it.
Topic archived. No new replies allowed.