How to Sum a Pascal Triangle

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Dec 2007
Posts: 17
Reputation: cherryteresa is an unknown quantity at this point 
Solved Threads: 0
cherryteresa cherryteresa is offline Offline
Newbie Poster

How to Sum a Pascal Triangle

 
0
  #1
Dec 3rd, 2008
Hi there. I had to make a code to print out a Pascal Triangle. But I'm not getting it to display properly.

For example, if the user types in 4, then it will print:
1
1
1 2
1 3 3

But it should print
1
1 1
1 2 1
1 3 3 1


And the last thing I'm trying to figure out is how to make it show a sum of the numbers.
So it should add:
.. 1
.. 1 + 1
.. 1 + 2 + 1
+ 1 + 3 + 3 + 1
.. 15 <-----------and display this as the sum.

Any help is appreciated. Thank you!

#include <stdlib.h>
#include <stdio.h>
#include <fstream>
#include <iostream>
#include <ctype.h>  // I know some of these may be unnecessary 
#include <string> // but our teacher wants us to include
#include <iomanip> // these in all our code.
#include <math.h> 
using namespace std;
int sum, i, size;
void inputSize();
void sumNumbers();
 
void main(int size) 
{
	int sum=0;
	cout<<"Pascal Triangle:\n";
	inputSize();
}

void inputSize()
{
	cout<< "Enter the amount of rows (between 1 and 9) you would like in the triangle."<<endl;
	cin >>size;
	cout<<"\n";	

	if (size > 0 && size <10)
	{	
		int *rowA = new int[1];
		rowA[0] = 1;
		cout <<setw(6)<<rowA[0] <<endl;
 
		for (int x = 1; x < size; x++) 
		{
			int *rowB = new int[x+1];
			rowB[0] = rowA[0];
			for (int y = 1; y < x; y++)
				rowB[y] = rowA[y-1] + rowA[y];
				rowB[x] = rowA[x-1];
														 
			delete [] rowA;
			rowA = rowB;
 
			for (int y = 0; y < x; y++)
			cout <<setw(6)<< rowB[y];
			cout <<"\n";
		}
		delete [] rowA;

		sumNumbers();
	}

	else
	{ cout<< "Number is not between 1-9.\n"<<endl; }
}
		
void sumNumbers()                           
{
	for (int x=0; x<size; x++)
	{
		for (int y=0; y<size; y++)
		{
			sum +=y;  // This is totally wrong, 
                }                         // I can't figure out how to 
		cout<<sum<<"\n"<<endl; //make a sum of the numbers
	}
}


Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 2,039
Reputation: Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice 
Solved Threads: 139
Team Colleague
Rashakil Fol's Avatar
Rashakil Fol Rashakil Fol is offline Offline
Super Senior Demiposter

Re: How to Sum a Pascal Triangle

 
1
  #2
Dec 3rd, 2008
You can figure out your first problem just by looking at your results. You're not printing the last 1 of each row. Why? Well, you're stopping too early in your loop that prints out the row's numbers.

For your second problem, if you temporarily added a few cout statements that showed the numbers you're adding, you'd see your problem very quickly.

You might not have to go through this effort -- I'm sure some shmuck will come along to tell you exactly what's wrong with your code.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 17
Reputation: cherryteresa is an unknown quantity at this point 
Solved Threads: 0
cherryteresa cherryteresa is offline Offline
Newbie Poster

Re: How to Sum a Pascal Triangle

 
0
  #3
Dec 3rd, 2008
Thanks. I actually just now figured out how to make the triangle print properly. I kept trying to add +1 in different places before. And then I realized, woops - I should have made it <= (less than or equal to) instead of < (less than)
for (int y = 0; y <= x; y++)

But I'm still having issues with the sum.
Last edited by cherryteresa; Dec 3rd, 2008 at 4:33 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 1,903
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 273
ddanbe's Avatar
ddanbe ddanbe is offline Offline
Posting Virtuoso

Re: How to Sum a Pascal Triangle

 
1
  #4
Dec 3rd, 2008
You might observe that each row in the triangle is equal to 2 raised to n. Where n starts at zero.
Your example : 2^0+2^1+2^2+2^3=15
I wonder why those binaries always pop up when we talk computer and why Pascal is also a computerlanguage.?.?.?
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 17
Reputation: cherryteresa is an unknown quantity at this point 
Solved Threads: 0
cherryteresa cherryteresa is offline Offline
Newbie Poster

Re: How to Sum a Pascal Triangle

 
0
  #5
Dec 4th, 2008
You're right, I was thinking about how all this math ties together. It's crazy to think about sometimes!

Okay, so thank you for pointing out the power thing. I think this is where I got stuck before. I'm trying to figure out how to raise something to the power. We didn't go over this in class and all the things I could find online showed it in printf form and not cout. And so I'm having trouble figuring out how to do it. I think basically what I need to do is:

for (int x=0; x<size; x++)
	{
		sum += pow (2, x); - // trying to figure 
                                               //  out how to 
                                               //translate this to code.
                                              // 2 to the x power

		for (int y=0; y<size; y++)
		{

		}
		cout<<sum<<"\n"<<endl;
	}
}

I'm going to keep searching.
Last edited by cherryteresa; Dec 4th, 2008 at 1:48 pm.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,809
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: How to Sum a Pascal Triangle

 
1
  #6
Dec 4th, 2008
Originally Posted by cherryteresa View Post
You're right, I was thinking about how all this math ties together. It's crazy to think about sometimes!

Okay, so thank you for pointing out the power thing. I think this is where I got stuck before. I'm trying to figure out how to raise something to the power. We didn't go over this in class and all the things I could find online showed it in printf form and not cout. And so I'm having trouble figuring out how to do it. I think basically what I need to do is:

for (int x=0; x<size; x++)
	{
		sum += pow (2, x); - // trying to figure 
                                               //  out how to 
                                               //translate this to code.
                                              // 2 to the x power

		for (int y=0; y<size; y++)
		{

		}
		cout<<sum<<"\n"<<endl;
	}
}

I'm going to keep searching.
pow (int, int) doesn't exist.

http://www.cplusplus.com/reference/c...cmath/pow.html

Make one or both a double:
  1. pow (2.0, x); // or pow (2.0, (double) x);

pow is from cmath, which you have as math.h. You may want to change those #include statements to include C++ libraries (cstdlib, cctype, cmath, cstdio) rather than the .h files. I'm not sure whether the compiler cares or not (probably not), but at least it makes clear that you are using C++ to the reader.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 1,903
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 273
ddanbe's Avatar
ddanbe ddanbe is offline Offline
Posting Virtuoso

Re: How to Sum a Pascal Triangle

 
0
  #7
Dec 4th, 2008
Originally Posted by VernonDozier
pow (int, int) doesn't exist.
Quite right, but it might that some C++ compilers implicitly convert an int to a double.
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 17
Reputation: cherryteresa is an unknown quantity at this point 
Solved Threads: 0
cherryteresa cherryteresa is offline Offline
Newbie Poster

Re: How to Sum a Pascal Triangle

 
0
  #8
Dec 4th, 2008
Oh, thank you guys all so much. I got it to work using the "pow (2.0, x)" method (using Visual C++ Express, just fyi).

#include <stdlib.h>
#include <stdio.h>
#include <fstream>
#include <iostream>
#include <ctype.h> 
#include <string>
#include <iomanip>
#include <math.h>
using namespace std;
int sum, i, size;
void inputSize();
void sumNumbers();
 
void main(int size) 
{
	int sum=0;
	cout<<"Pascal Triangle:\n";
	inputSize();
}

void inputSize()
{
	cout<< "Enter the amount of rows (between 1 and 9) you would like in the triangle."<<endl;
	cin >>size;
	cout<<"\n";	

	if (size > 0 && size <10)
	{	
		int *rowA = new int[1];
		rowA[0] = 1;
		cout <<setw(6)<<rowA[0] <<endl;
 
		for (int x = 1; x < size; x++) 
		{
			int *rowB = new int[x+1];
			rowB[0] = rowA[0];
			for (int y = 1; y < x; y++)
				rowB[y] = rowA[y-1] + rowA[y];
				rowB[x] = rowA[x-1];
														 
			delete [] rowA;
			rowA = rowB;
 
			for (int y = 0; y <= x; y++)
			cout <<setw(6)<< rowB[y];
			cout <<"\n";
		}
		delete [] rowA;

		sumNumbers();
	}

	else
	{ cout<< "Number is not between 1-9.\n"<<endl; }
}
		
void sumNumbers()
{
	for (int x=0; x<size; x++)
	{
		sum += pow (2.0, x);

		for (int y=0; y<size; y++)
		{
		}
	}
	cout<<"\nThe sum of the values: "<<sum<<"\n"<<endl;
}
Last edited by cherryteresa; Dec 4th, 2008 at 7:57 pm.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC