New Reply

Why is this simple output so difficult to code in C

 
Share Thread
Nov25-12, 10:22 PM   #1
 

Why is this simple output so difficult to code in C


1
1 2 1
1 2 1 2 1
1 2 1 2 1 2 1
1 2 1 2 1 2 1 2 1

I am stumped on how to do this, is it really as easy as it looks?
PhysOrg.com science news on PhysOrg.com

>> Leading 3-D printer firms to merge in $403M deal (Update)
>> LA to give every student an iPad; $30M order
>> CIA faulted for choosing Amazon over IBM on cloud contract
Nov25-12, 11:08 PM   #2
 
Recognitions:
Gold Membership Gold Member
Mod note: Deleted the portion that shows the answer to the problem in post #1.
Trueo, I see you are fairly new to the forum so perhaps you don't realize it, but the POINT of this forum is not to spoon-feed full answers to problems but to help people learn how to get their own answers by figuring out where they are having difficulty and giving them some help to get them over the next hump.

That is, we are not here to show how knowledgeable we are, we are here to help others get more knowledgeable.
Nov25-12, 11:31 PM   #3
 
I just think people visiting PF are mostly students who are different from (advanced) employees; and I am not showing off with what I know, it is just a mini program, which doesn't build me into any person. :-)
Nov25-12, 11:37 PM   #4
 
Recognitions:
Gold Membership Gold Member

Why is this simple output so difficult to code in C


Quote by trueo View Post
I just think people visiting PF are mostly students who are different from (advanced) employees; and I am not showing off with what I know, it is just a mini program, which doesn't build me into any person. :-)
You are still missing the point.
Nov25-12, 11:56 PM   #5
 
Mentor
Quote by trueo View Post
I just think people visiting PF are mostly students who are different from (advanced) employees; and I am not showing off with what I know, it is just a mini program, which doesn't build me into any person. :-)
Physics Forums rules do not permit providing complete answers, especially when the original poster has not shown any work.
Nov26-12, 01:47 PM   #6
 
It's too long since I last programmed in C, so you'll have to make do with some pseudo-code. I guess you don't just want to output your example, but any kind of figure like this:

1
1 2 1
1 2 1 2 1
. . .
n ones and (n-1) twos alternating

The first thing I see here is that you want n rows, so you need some kind of loop:

for i = 1 to n do {
. . .
}

Those three dots must produce line number i, which is made up of i ones and (i-1) twos. Forget the twos for the moment, and you still need to print i ones -- with another loop:

for j = 1 to i do {
print "1"
}

Now if you can work out how to get those two loops (one for printing n lines, the other for printing i ones) to work together, all you need to add is the twos in between the ones.
Nov27-12, 06:24 AM   #7
 
Recognitions:
Science Advisor Science Advisor
Five "printf"s anyone?

OP probably should have been a bit more specific about exactly what was the problem if s/he wants a more specific answer.
Nov27-12, 01:29 PM   #8
 
Quote by uart View Post
Five "printf"s anyone?
Or one .
Nov27-12, 10:54 PM   #9
 
Recognitions:
Science Advisor Science Advisor
Quote by Timo View Post
Or one .
Yep. Or two or three, depending on how much you like or dislike long lines of source code. Take your pick.

It a funny question this one. How do you get these five lines of output, no other information or parameters given.
Nov30-12, 12:12 AM   #10
 
Mentor
Many posts, which took this thread far beyond its original intent as a simple homework-help request, have been moved into a new thread here:

http://www.physicsforums.com/showthread.php?t=655808

Please continue that discussion in the new thread. I apologize if I lost any posts in the process of transplanting them.
New Reply

Similar discussions for: Why is this simple output so difficult to code in C
Thread Forum Replies
FORTRAN- Same source code compiled with different compilers gives different output Programming & Comp Sci 6
Can someone look over my C++ code - Simple short code! Engineering, Comp Sci, & Technology Homework 4
Output of CAMB code Cosmology 3
Help with Simple C++ code please :) Programming & Comp Sci 20
simple code Brain Teasers 42