asterisks printed by only one printf and one for loop in the form

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

37 Replies - 2987 Views - Last Post: 12 October 2012 - 11:29 PM Rate Topic: -----

#31 Salem_c  Icon User is online

  • void main'ers are DOOMED
  • member icon

Reputation: 1678
  • View blog
  • Posts: 3,180
  • Joined: 30-May 10

Re: asterisks printed by only one printf and one for loop in the form

Posted 09 October 2012 - 11:24 AM

View Postaychdogg, on 09 October 2012 - 04:23 PM, said:

See this was what I wanted all along and I had a bit of idea about this but there was some error.

You know, posting your latest effort, errors and all, is what we're all about.

So what is YOUR latest effort?
Was This Post Helpful? 1
  • +
  • -

#32 aychdogg  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 05-October 12

Re: asterisks printed by only one printf and one for loop in the form

Posted 10 October 2012 - 04:14 AM

Well guys ofcourse I did mention I am new to this so I'm trying to learn more and more stuff. From what I got from Salem_c's code was that he used "(perline+1)*lines" so that we can get all the asterisks at once e.g 15. Then he used "[i%(perline+1)==perline]" later to seperate 5 asterisks and get the remainder. I hope I'm correct on that?
Was This Post Helpful? 0
  • +
  • -

#33 aychdogg  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 05-October 12

Re: asterisks printed by only one printf and one for loop in the form

Posted 10 October 2012 - 04:23 AM

View Postno2pencil, on 09 October 2012 - 10:45 AM, said:

The op clearly doesn't understand the exercise or how to code it. You guys are basically doing this for him. That's why he didn't answer yes. He doesn't know.


Sir, I respect you replying to my post and all and I did mention I'm new to this so I think you need to spare me that constant nag. Ofcourse I don't know all this otherwise I wouldn't have asked for a barely 5,6 lines of code. You guys are pros so you know stuff in details while I'm here to learn. I didn't get to reply to Jim's post #22 because I saw Salem_c
s post later. I know what he was talking about though. Salem_c's code actually helped me and I tried learning whatever concept he has used. I have googled each concept to learn it so by the time i get judged on my level of programming skills I'll have enough knowledge on this and i don't have to worry about my instructor being totally daft.
Was This Post Helpful? 0
  • +
  • -

#34 Salem_c  Icon User is online

  • void main'ers are DOOMED
  • member icon

Reputation: 1678
  • View blog
  • Posts: 3,180
  • Joined: 30-May 10

Re: asterisks printed by only one printf and one for loop in the form

Posted 10 October 2012 - 04:38 AM

> Well guys ofcourse I did mention I am new to this so I'm trying to learn more and more stuff.
Well you see, learning how to program is rather different than learning magic tricks.

Your post #3 was perfectly adequate solution for anyone learning C. In fact, it's perfectly adequate in almost every context you can imagine - if you just take the requirement to print *** in a square, and ignore the completely stupid limitation of "use one loop".

And like all magic tricks, it's obvious when you see it, and rather pointless when you understand it. Ultimately, it doesn't help you solve any other problem.

Now imagine you were to give your code to someone else (exceedingly likely in professional programming). But now the requirement has changed to put a + in each corner. With your code at post #3, it's a relative simple task that anybody could achieve.

With my cryptic 'fun' code, you're basically screwed for figuring out how to do it. The most obvious (and sane) thing to do would be to delete it and do it properly with an obvious nested loop.
Was This Post Helpful? 2
  • +
  • -

#35 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3576
  • View blog
  • Posts: 11,126
  • Joined: 05-May 12

Re: asterisks printed by only one printf and one for loop in the form

Posted 10 October 2012 - 04:42 AM

View Postaychdogg, on 10 October 2012 - 04:14 AM, said:

From what I got from Salem_c's code was that he used "(perline+1)*lines" so that we can get all the asterisks at once e.g 15.


Try again. perline == 5, lines == 3. So (perline+1)*lines == (5+1)*3 == 6*3 == 18. 18 != 15.

View Postaychdogg, on 10 October 2012 - 04:14 AM, said:

Then he used "[i%(perline+1)==perline]" later to seperate 5 asterisks and get the remainder. I hope I'm correct on that?


If that is used for getting the remainder then the following should print out 2:
int i = 7;
int perline = 5;
printf("%d", i%(perline+1)==perline);


But somehow it prints out 0.
Was This Post Helpful? 0
  • +
  • -

#36 jimblumberg  Icon User is offline

  • member icon


Reputation: 4075
  • View blog
  • Posts: 12,578
  • Joined: 25-December 09

Re: asterisks printed by only one printf and one for loop in the form

Posted 10 October 2012 - 06:24 AM

The purpose of my question in post #22 was to ascertain the actual requirements. If you can print a variable instead of a constant character, it is much easier to complete this task using only one loop. By adding an if or if/else statement to the loop it is very easy to print your "box" using a single loop and single printf(). And I agree with Salem_c, using his cryptic code is not the way to accomplish this objective.

Jim
Was This Post Helpful? 0
  • +
  • -

#37 aychdogg  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 05-October 12

Re: asterisks printed by only one printf and one for loop in the form

Posted 10 October 2012 - 10:29 AM

alright. thanks.
Was This Post Helpful? 0
  • +
  • -

#38 MasterUvPuppetz  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 1
  • Joined: 12-October 12

Re: asterisks printed by only one printf and one for loop in the form

Posted 12 October 2012 - 11:29 PM

um.. can i ask for a c++ equivalent for this exercise?

Thanks in advance,

Was This Post Helpful? -2
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3