Why is this simple output so difficult to code in C

In summary, the conversation discusses the purpose of the forum and the rules regarding providing complete answers to problems. It also includes a discussion on how to print a specific pattern using loops in a programming language.
  • #1
camel-man
76
0
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?
 
Technology news on Phys.org
  • #2
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.
 
Last edited by a moderator:
  • #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. :-)
 
  • #4
trueo said:
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.
 
  • #5
trueo said:
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.
 
  • #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.
 
  • #7
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.
 
  • #8
uart said:
Five "printf"s anyone?
Or one :bugeye:.
 
  • #9
Timo said:
Or one :bugeye:.
Yep. Or two or three, depending on how much you like or dislike long lines of source code. Take your pick. :smile:

It a funny question this one. How do you get these five lines of output, no other information or parameters given. :rofl:
 
  • #10
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:

https://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.
 

1. Why is coding simple output in C difficult?

Coding simple output in C can be difficult because the language is low-level and requires a lot of manual memory management. This means that even simple tasks like printing output to the screen require more code and attention to detail compared to higher-level languages.

2. What makes C output different from other languages?

C output is different from other languages because it is a procedural language, meaning that instructions are executed in a specific order. This can make it more challenging to write code for simple output as the programmer must have a clear understanding of the program's flow and structure.

3. How does C handle simple output compared to other programming languages?

C handles simple output differently from other programming languages because it requires the explicit use of formatting specifiers, such as %d for integers and %f for floating-point numbers. This can be confusing for beginners who are used to the more user-friendly approach of other languages.

4. Is there a way to make coding simple output in C easier?

There are several ways to make coding simple output in C easier. One approach is to use libraries or frameworks that provide higher-level functions for output, such as printf() in the standard library. Another way is to use a code editor or IDE that has features like auto-completion and syntax highlighting, which can aid in writing and debugging C code.

5. What are some common errors when coding simple output in C?

Some common errors when coding simple output in C include using the wrong formatting specifier, forgetting to include necessary header files, and not allocating enough memory for the output. Other errors can occur due to syntax mistakes or logic errors in the code. It is crucial to thoroughly test and debug the code to catch these errors.

Similar threads

  • Programming and Computer Science
Replies
1
Views
505
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
4
Views
941
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
29
Views
1K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
6
Views
1K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
9
Views
1K
  • Programming and Computer Science
Replies
0
Views
412
Back
Top