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

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

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

#1 aychdogg  Icon User is offline

  • New D.I.C Head

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

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

Posted 05 October 2012 - 11:32 AM

Hey I need some help with this. I need to write a C code in which I am to use only one printf and only one for loop. That's it. No nested for loops or other loops. Please help

Output should be similar to

*****
*****
*****

which means 5 asterisks printed in a line then a next line and this repeats 3 times.

This post has been edited by Salem_c: 05 October 2012 - 12:19 PM
Reason for edit:: adjust font size - 7 is too much

Is This A Good Question/Topic? 0
  • +

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

#2 no2pencil  Icon User is offline

  • Head MFIC
  • member icon

Reputation: 5069
  • View blog
  • Posts: 26,456
  • Joined: 10-May 07

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

Posted 05 October 2012 - 11:33 AM

Please provide what you have started so we can actually help. Just giving us your assignment (in an incredibly annoyingly large font) doesn't allow us to help you with what you are having trouble with.
Was This Post Helpful? 0
  • +
  • -

#3 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 05 October 2012 - 11:46 AM

View Postno2pencil, on 05 October 2012 - 11:33 AM, said:

Please provide what you have started so we can actually help. Just giving us your assignment (in an incredibly annoyingly large font) doesn't allow us to help you with what you are having trouble with.


Oh I'm really sorry about the font it's just that i wanted to be noticed as I need this urgent. Here is what I have made with nested loops. Now this works fine but I actually need to use only one for loop. That is the only confusing part. Is it really possible in C ? and if yes then how?

My Code:
#include <stdio.h>

main () {
     
int i, j;

i = 1;
j = 1;

for (i = 1;i <= 3;i++) {
 for (j = 1; j <= 5; j++){
    printf("*");
}
    printf ("\n");
  
}
getch();
}



Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3177
  • View blog
  • Posts: 9,634
  • Joined: 05-May 12

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

Posted 05 October 2012 - 12:06 PM

I think that you have been mesmerized by nested for loops that you forget that printf() can take more than one character.
for(int i = 0; i < 3; i++)
    printf("aychdogg");



So what happens when your replace you alias with some other string that somehow includes 5 asterisks?
Was This Post Helpful? 0
  • +
  • -

#5 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 05 October 2012 - 12:13 PM

View PostSkydiver, on 05 October 2012 - 12:06 PM, said:

I think that you have been mesmerized by nested for loops that you forget that printf() can take more than one character.
for(int i = 0; i < 3; i++)
    printf("aychdogg");



So what happens when your replace you alias with some other string that somehow includes 5 asterisks?


Do you really think I would ask such a thing if it was that easy to just write 5 asterisks myself and then loop them thrice. I know how to do that but the limitations I have been given are confusing because I don't know if it is really possible.

Let me make myself more clear like this

1. Only one printf in the form printf ("*") is allowed which should be printed 5 times like *****
2. Only one for loop should loop this thrice so that I get this pattern

*****
*****
*****
Was This Post Helpful? 0
  • +
  • -

#6 no2pencil  Icon User is offline

  • Head MFIC
  • member icon

Reputation: 5069
  • View blog
  • Posts: 26,456
  • Joined: 10-May 07

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

Posted 05 October 2012 - 12:19 PM

View Postaychdogg, on 05 October 2012 - 03:13 PM, said:

Do you really think I would ask such a thing if it was that easy to just write 5 asterisks myself and then loop them thrice.

A little bit of advice my friend : tone it down.

You open the topic with the idea that louder gets you faster results. It will actually cause members to just ignore the topic.
You didn't print your code, we had to ask for it. Waste of our time, & a waste of your time waiting.
A member with years of experience in your field is trying to help you with an elementary project, & your tone reads that he's an idiot.

You are doing all of the things you shouldn't do, to receive good, quality help. What you are doing is what causes members to think "forget this guy", & not offer any help at all. You must remember we are under no obligation to assist you.
Was This Post Helpful? 1
  • +
  • -

#7 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3177
  • View blog
  • Posts: 9,634
  • Joined: 05-May 12

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

Posted 05 October 2012 - 12:43 PM

In your original post, all you stated as the restriction to be where:

View Postaychdogg, on 05 October 2012 - 11:32 AM, said:

I need to write a C code in which I am to use only one printf and only one for loop. That's it. No nested for loops or other loops.


It was only on post #5 that you elaborated that the printf() also has restriction on what the format string could be:

View Postaychdogg, on 05 October 2012 - 12:13 PM, said:

1. Only one printf in the form printf ("*") is allowed


Interestingly in post #5 you also stated:

View Postaychdogg, on 05 October 2012 - 12:13 PM, said:

2. Only one for loop should loop this thrice


So does this mean that this restriction goes away?

View Postaychdogg, on 05 October 2012 - 11:32 AM, said:

No nested for loops or other loops.

Was This Post Helpful? 0
  • +
  • -

#8 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 05 October 2012 - 12:44 PM

Well I did not intend on sounding rude or anything I apologize if you felt that way. I just want someone of your level on this topic to give a beginner some help.
Was This Post Helpful? 0
  • +
  • -

#9 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 05 October 2012 - 12:50 PM

View PostSkydiver, on 05 October 2012 - 12:43 PM, said:

In your original post, all you stated as the restriction to be where:

View Postaychdogg, on 05 October 2012 - 11:32 AM, said:

I need to write a C code in which I am to use only one printf and only one for loop. That's it. No nested for loops or other loops.


It was only on post #5 that you elaborated that the printf() also has restriction on what the format string could be:

View Postaychdogg, on 05 October 2012 - 12:13 PM, said:

1. Only one printf in the form printf ("*") is allowed


Interestingly in post #5 you also stated:

View Postaychdogg, on 05 October 2012 - 12:13 PM, said:

2. Only one for loop should loop this thrice


So does this mean that this restriction goes away?

View Postaychdogg, on 05 October 2012 - 11:32 AM, said:

No nested for loops or other loops.


Hey thanks for your time and well I'm sorry for the wrong beggining I'm just new to this so I hope you understand. Anyways the person who gave this project said that he wants me to use only one printf ("*") and only one for loop to get this output

*****
*****
*****

from what I understood of this I made a for loop which is

#include <stdio.h>

main () {
     
int i, j;

for (i = 1;i <= 5;i++) {
    printf("*");
}
getch();
}


It prints three asterisks just fine but now I need these five asterisks to be repeated thrice like the output i gave above. I could use nested for loop for this or a number of other solutions but the restriction of using only one for loop has put me into trouble. I hope you can get me out of this.
Was This Post Helpful? 0
  • +
  • -

#10 no2pencil  Icon User is offline

  • Head MFIC
  • member icon

Reputation: 5069
  • View blog
  • Posts: 26,456
  • Joined: 10-May 07

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

Posted 05 October 2012 - 12:52 PM

I would add a conditional if/else & if your count is within five, print a new line '\n'. Thus you print fifteen asterisks in your for loops, & within the loop count to five, print the '\n', & reset your counter.

Or loop 3 times, & use two counters within the loop.
Was This Post Helpful? 1
  • +
  • -

#11 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 05 October 2012 - 12:59 PM

View Postno2pencil, on 05 October 2012 - 12:52 PM, said:

I would add a conditional if/else & if your count is within five, print a new line '\n'. Thus you print fifteen asterisks in your for loops, & within the loop count to five, print the '\n', & reset your counter.

Or loop 3 times, & use two counters within the loop.


hhmm well yes if/else conditionals work fine too. Can you give me a little snippet example without using the if/else? I don't know if im logically correct on this but like combining two for loops into one somehow?
Was This Post Helpful? 0
  • +
  • -

#12 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2836
  • View blog
  • Posts: 9,740
  • Joined: 08-August 08

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

Posted 05 October 2012 - 01:08 PM

Have you learned about recursive functions yet? That would make this easy.
Was This Post Helpful? 0
  • +
  • -

#13 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3177
  • View blog
  • Posts: 9,634
  • Joined: 05-May 12

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

Posted 05 October 2012 - 01:12 PM

Actually, the part that has me puzzled is how he is going to print out the '\n' at the end of each line without using printf(), unless he is also allowed to use printf("\n") or allowed to use putchar('\n'). But then if he is allowed to use putchar(), he can even do the assignment/challenge without even using printf(), and use putchar() all the way.
Was This Post Helpful? 1
  • +
  • -

#14 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 05 October 2012 - 01:14 PM

No :( I do php so I know it can be done and all but I don't have a clue how to do it in C. I think the teacher only wants us to confirm that it can't be done like this as a practice. I don't know. I'm confused.
Was This Post Helpful? 0
  • +
  • -

#15 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3177
  • View blog
  • Posts: 9,634
  • Joined: 05-May 12

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

Posted 05 October 2012 - 01:16 PM

Say something like this pseudo code which doesn't even need an else. Just needs an if:
for i = 1..15 do:
{
    put out a '*'
    if (i is divisible by 5)
        put out a '\n'
}


Was This Post Helpful? 2
  • +
  • -

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