C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 09-16-2008, 11:59 AM   #1
Registered User
 
Join Date: Sep 2008
Posts: 7
Exclamation Output required

Hello all,

Well i wanted a output of a prorgram to look like this, i tried but can't figured out the logic, can anyone plesae help me out here with this

1
232
34543
4567654
567898765
internet_bug is offline   Reply With Quote
Old 09-16-2008, 12:02 PM   #2
and the Hat of Ass
 
Join Date: Dec 2007
Posts: 731
Effort required.
rags_to_riches is online now   Reply With Quote
Old 09-16-2008, 12:08 PM   #3
Registered User
 
Join Date: Sep 2008
Posts: 7
yes mate i tried from my side, but needed some expert here
internet_bug is offline   Reply With Quote
Old 09-16-2008, 12:16 PM   #4
Its hard... But im here
 
swgh's Avatar
 
Join Date: Apr 2005
Location: England
Posts: 1,466
One way to tackle it is to think about the shape first. You know its a triangle so its going to need for loops, more than likely nested loops. Within each increment of the inner loop calculate/print the number.

If you are having trouble, think about the pescudo code first, or plan it out on paper.
__________________
I'm just trying to be a better person - My Name Is Earl
swgh is offline   Reply With Quote
Old 09-16-2008, 12:28 PM   #5
Registered User
 
Join Date: Sep 2008
Posts: 7
thankz for your suggesetion M8, i have figured it out that there will be nested looping in this, but can't get to the logic of looping
internet_bug is offline   Reply With Quote
Old 09-16-2008, 01:24 PM   #6
Registered User
 
Join Date: Sep 2008
Posts: 21
These types of things are interesting. Look at this table:

(0 represents space or padding)

000010000
000232000
003454300
045676540
567898765

What do we need to know. We need to know how much padding or space to provide, how many true digits to print, and what those digits should be.

1. The "row" is going to go from 1 to 5;
2. The "pad" is going to go from 4 to 0;
3. The maximum value in the row is equal to the total number of digits printed.

Look at this table:

row|pad|max
1 4 1
2 3 3
3 2 5
4 1 7
5 0 9

What can we learn:

1. pad = 5 - row;
2. max = (2 * row) -1;

Here is how to do it, step by step.

Code:
/* Outer loop */
for(row = 1; row <= 5; ++ row){

/* The pad loop. notice that as row increments the number of spaces printed decrements */

  for(pad = row; pad < 5; ++ pad)
    printf(" ");

/* Now we are going to print the digits. This requires two loops. One that increments to "max", and one decrements back to "row */

  max = (2 * row) -1;
  for(col = row; col <= max; ++ col)
    printf("%d",col);
  for(col = max - 1; col >= row; -- col)
    printf("%d",col);

/* Finish with a newline */
  printf("\n");

}
rmetcalf is offline   Reply With Quote
Old 09-16-2008, 01:31 PM   #7
Banned
 
master5001's Avatar
 
Join Date: Aug 2001
Location: Visalia, CA, USA
Posts: 3,699
Ok... well think of the math involved a little more. Think about the fact that the first number is going to be centered and the space on each side is going to be equal (which isn't necessarily as relavant in this project since you only need to write spaces before your data, not the spaces afterward.
master5001 is offline   Reply With Quote
Old 09-16-2008, 01:39 PM   #8
Registered User
 
Join Date: Sep 2008
Posts: 7
Smile

Quote:
Originally Posted by rmetcalf View Post
These types of things are interesting. Look at this table:

(0 represents space or padding)

000010000
000232000
003454300
045676540
567898765

What do we need to know. We need to know how much padding or space to provide, how many true digits to print, and what those digits should be.

1. The "row" is going to go from 1 to 5;
2. The "pad" is going to go from 4 to 0;
3. The maximum value in the row is equal to the total number of digits printed.

Look at this table:

row|pad|max
1 4 1
2 3 3
3 2 5
4 1 7
5 0 9

What can we learn:

1. pad = 5 - row;
2. max = (2 * row) -1;

Here is how to do it, step by step.

Code:
/* Outer loop */
for(row = 1; row <= 5; ++ row){

/* The pad loop. notice that as row increments the number of spaces printed decrements */

  for(pad = row; pad < 5; ++ pad)
    printf(" ");

/* Now we are going to print the digits. This requires two loops. One that increments to "max", and one decrements back to "row */

  max = (2 * row) -1;
  for(col = row; col <= max; ++ col)
    printf("%d",col);
  for(col = max - 1; col >= row; -- col)
    printf("%d",col);

/* Finish with a newline */
  printf("\n");

}
tons of thankz M8, for explaining in such an easy manner, great work
internet_bug is offline   Reply With Quote
Old 09-16-2008, 01:41 PM   #9
Registered User
 
Join Date: Sep 2008
Posts: 7
Smile

Quote:
Originally Posted by master5001 View Post
Ok... well think of the math involved a little more. Think about the fact that the first number is going to be centered and the space on each side is going to be equal (which isn't necessarily as relavant in this project since you only need to write spaces before your data, not the spaces afterward.
great info M8, i will surely try this one and let u know
internet_bug is offline   Reply With Quote
Reply

Tags
output

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
output display in screen required imagetvr C++ Programming 9 08-19-2008 09:55 PM
More digits in output, particular digits output, output in *.txt Ene Dene C++ Programming 4 11-30-2005 04:44 PM
help with output s_ny33 C Programming 20 09-19-2005 11:51 PM
Formatting output into even columns? Uncle Rico C Programming 2 08-16-2005 05:10 PM
Output problems with structures Gkitty C Programming 1 12-16-2002 05:27 AM


All times are GMT -6. The time now is 01:56 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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