pas triangle problem...

This is a discussion on pas triangle problem... within the C++ Programming forums, part of the General Programming Boards category; Hi it's me again now i have another problem i can't figure out what's wrong with my code.. It work's ...

  1. #1
    Registered User
    Join Date
    May 2006
    Location
    in your head
    Posts
    49

    pas triangle problem...

    Hi it's me again now i have another problem i can't figure out what's wrong with my code.. It work's well with lower numbers like 4, 5 but above five the triangle is messed up need help here's the code..I can't get what's wrong with my code????.....

    Code:
    #include <iostream>
    
    using namespace std;
    int main()
    {
      int n;
      cout << "Input num\n";
      cin >> n;
      cin.ignore();
      for (int y = 0; y < n; y++)
      {
        int k = 1;
        
        cout.width(n-y);
        for (int x = 0; x <= y; x++)
        {
          cout << k << " ";
          k = k * (y - x) / (x + 1);
          
        }
        cout << "\n";
      }
      cout << "\n";
      
      cin.get();
    }
    tnx...

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    Each digit of a number is a character in the output, so a two digit number will take up two digits instead of one. That is why your output looks funny (I assume that's your problem since the code works fine otherwise for me).

    I think the output is fine the way it is, but if you want to change it, you'll have to figure out how many characters the largest value will be and make sure each value printed uses that many characters. For example, for a starting point of 5, the largest value is 10. When you print out the one-digit numbers, they should all be "01" or " 1" so that they all take up two characters.

  3. #3
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,236
    Code:
    #include<iostream>
    using namespace std;
    typedef unsigned int uint;
    int main(){
    	uint pos[2]={0,0},ar[200][200];
    	while(pos[0]<1 || pos[1]>pos[0]|| pos[1] <1){
    		cout<<"row: ";
    		cin>>pos[0];
    		cout<<"column: ";
    		cin>>pos[1];
    	}
    
    	for(int i =0 ; i<pos[0];i++) ar[i][0]=ar[i][i]=1;
    	
    	for(uint i=2;i<=pos[0];i++)
    		for(uint j=1;j<i;j++)
    				ar[i][j]=ar[i-1][j]+ar[i-1][j-1];
    		
    
    	cout<<ar[pos[0]-1][pos[1]-1]<<endl;
    	
    	cin.get();cin.get();
    
    	return 0;
    }
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  4. #4
    Registered User
    Join Date
    May 2006
    Location
    in your head
    Posts
    49
    ahhhh okey understood tnx for the hints guys i really appreciate it

  5. #5
    Registered User
    Join Date
    May 2006
    Location
    in your head
    Posts
    49
    Quote Originally Posted by Daved
    Each digit of a number is a character in the output, so a two digit number will take up two digits instead of one. That is why your output looks funny (I assume that's your problem since the code works fine otherwise for me).

    I think the output is fine the way it is, but if you want to change it, you'll have to figure out how many characters the largest value will be and make sure each value printed uses that many characters. For example, for a starting point of 5, the largest value is 10. When you print out the one-digit numbers, they should all be "01" or " 1" so that they all take up two characters.
    Btw how can i take up two characters like you said "01" instead of 1 how can i do that 01 thing...... tnx....and sorry for being such a noob....

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    There are different ways. You could just figure out how many digits the number has and add the 0's manually. Or you could use io manipulators and functions like width() to set the appropriate size of the output to the same number of digits each time and perhaps setfill to make sure it fills the extras with 0's. Try playing around with those ideas to see if you can figure out a way.

  7. #7
    Registered User
    Join Date
    May 2006
    Location
    in your head
    Posts
    49
    Thank you very much

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 07:54 PM
  2. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM
  3. Replies: 5
    Last Post: 11-07-2005, 11:34 PM
  4. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 09:37 AM
  5. beginner problem
    By The_Nymph in forum C Programming
    Replies: 4
    Last Post: 03-05-2002, 05:46 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21