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

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

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

#16 no2pencil  Icon User is online

  • Toubabo Koomi
  • member icon

Reputation: 5189
  • View blog
  • Posts: 26,896
  • Joined: 10-May 07

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

Posted 05 October 2012 - 01:16 PM

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

#include <stdio.h>

main () {
     
int i, j;

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


...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...


My understanding is that the point of the project is to replace loop with logic. If he's already using printf, why couldn't he just use another?

Sounds to me like he needs to properly understand the project himself, before posting it here.

& my solution only adds 3 lines of code. So it's impossible to show it without showing it.
Was This Post Helpful? 1
  • +
  • -

#17 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:17 PM

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

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.


Exactly. That is the thing confusing me too plus that how can it be done with only one for loop with only one printf. I think it's just a test of logic and the teacher wanted it to be that way so thanks anyways everyone really appreciate it plus I apologize for any misunderstandings earlier. Good day.
Was This Post Helpful? 0
  • +
  • -

#18 no2pencil  Icon User is online

  • Toubabo Koomi
  • member icon

Reputation: 5189
  • View blog
  • Posts: 26,896
  • Joined: 10-May 07

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

Posted 05 October 2012 - 01:17 PM

View PostSkydiver, on 05 October 2012 - 04:16 PM, said:

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'
}




That's even shorter what I did, but similar logic.
Was This Post Helpful? 0
  • +
  • -

#19 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:39 PM

This works fine but then I am not following the restrictions as I had to use 2 printf .. one for the asterisks and the other for the next line. Moreover I had to use two loops so that first I generate 5 asterisks and then so that I repeat them thrice. Logically it is correct but as I said I had to fall out of the restrictions. Thanks to everyone who replied :)

#include <stdio.h>

main () {
     
int i, j;

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

Was This Post Helpful? 0
  • +
  • -

#20 no2pencil  Icon User is online

  • Toubabo Koomi
  • member icon

Reputation: 5189
  • View blog
  • Posts: 26,896
  • Joined: 10-May 07

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

Posted 05 October 2012 - 01:43 PM

You don't need to use two loops, & I'm fairly confident that you are confused about not using a 2nd printf.

#include <stdio.h>
int main(void) {
  int i=0, loop=0;
  while(i<15) {
    printf("*");
    (check if loops is less than 4, if true increase by 1)
    (else print new line, set loop to zero)
    }
    i++;
  }
  return 0;
}



Quote

[mrgore@purplebox ~]$ ./junk
*****
*****
*****

Was This Post Helpful? 1
  • +
  • -

#21 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3473
  • View blog
  • Posts: 10,712
  • Joined: 05-May 12

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

Posted 05 October 2012 - 02:02 PM

Or if you are really hung up about the two printf()'s here's your code with just one printf(), indenting fixed, and the correct return type for main():

#include <stdio.h>

int main() {
    int i, j;

    for (i = 1,j = 1;i <= 3;i++) {
        for (j = 1; j <= 5; j++){
            printf("*");
        }
        putchar('\n');
    }
    // OP had getch(); here
    return 0;
}


Was This Post Helpful? 0
  • +
  • -

#22 jimblumberg  Icon User is online

  • member icon


Reputation: 3993
  • View blog
  • Posts: 12,322
  • Joined: 25-December 09

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

Posted 05 October 2012 - 02:11 PM

Does it have to printf("*");? Or can it be something like:printf("%c",c);?

Jim

This post has been edited by jimblumberg: 05 October 2012 - 02:11 PM

Was This Post Helpful? 0
  • +
  • -

#23 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 1628
  • View blog
  • Posts: 3,090
  • Joined: 30-May 10

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

Posted 05 October 2012 - 02:15 PM

I'm feeling :devil2: and :ninja:
Spoiler

Was This Post Helpful? 4
  • +
  • -

#24 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 09 October 2012 - 09:23 AM

View PostSalem_c, on 05 October 2012 - 02:15 PM, said:

I'm feeling :devil2: and :ninja:
Spoiler



YOU my friend are awesome ! See this was what I wanted all along and I had a bit of idea about this but there was some error. Anyways you rock thankyou so much !
Was This Post Helpful? 0
  • +
  • -

#25 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3473
  • View blog
  • Posts: 10,712
  • Joined: 05-May 12

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

Posted 09 October 2012 - 09:36 AM

Ah, but the printf() is not of the from printf("*") which you said is a hard requirement. It is of the form printf("%c", ...).

Was the asterisk a mind game (along the lines of "there 10 people in the world who understand binary") where it actually represented a wildcard rather than a literal asterisk? If it was a wildcard then my original printf("*****") should have also been valid, by induction: "*" is a wildcard, and so is "%c", so "*%c" would be legal, then so would "**", ...
Was This Post Helpful? 0
  • +
  • -

#26 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 09 October 2012 - 10:01 AM

View PostSkydiver, on 09 October 2012 - 09:36 AM, said:

Ah, but the printf() is not of the from printf("*") which you said is a hard requirement. It is of the form printf("%c", ...).

Was the asterisk a mind game (along the lines of "there 10 people in the world who understand binary") where it actually represented a wildcard rather than a literal asterisk? If it was a wildcard then my original printf("*****") should have also been valid, by induction: "*" is a wildcard, and so is "%c", so "*%c" would be legal, then so would "**", ...


Your argument is valid but printing one asterisk here as printf("*") replaced as %c once inside and that multiplied using arithmetical operators is fine. Either way I'm printing one asterisk and then multiplying the result to be displayed multiple times so that is not a problem. The main problem was in using only one printf which prints one asterisk and then using one loop. Anyways thanks all.
Was This Post Helpful? 0
  • +
  • -

#27 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 1628
  • View blog
  • Posts: 3,090
  • Joined: 30-May 10

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

Posted 09 October 2012 - 10:23 AM

Perhaps I should have been more cryptic with
Spoiler


or even
Spoiler

Was This Post Helpful? 2
  • +
  • -

#28 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3473
  • View blog
  • Posts: 10,712
  • Joined: 05-May 12

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

Posted 09 October 2012 - 10:38 AM

View Postaychdogg, on 09 October 2012 - 10:01 AM, said:

View PostSkydiver, on 09 October 2012 - 09:36 AM, said:

Ah, but the printf() is not of the from printf("*") which you said is a hard requirement. It is of the form printf("%c", ...).

Was the asterisk a mind game (along the lines of "there 10 people in the world who understand binary") where it actually represented a wildcard rather than a literal asterisk? If it was a wildcard then my original printf("*****") should have also been valid, by induction: "*" is a wildcard, and so is "%c", so "*%c" would be legal, then so would "**", ...


Your argument is valid but printing one asterisk here as printf("*") replaced as %c once inside and that multiplied using arithmetical operators is fine. Either way I'm printing one asterisk and then multiplying the result to be displayed multiple times so that is not a problem. The main problem was in using only one printf which prints one asterisk and then using one loop. Anyways thanks all.


If that was a legal substitution, why did you not reply yes to Jim's post #22?

Where are you seeing the multiplying happening? I'm only seeing a modulus operation happening.

Anyway with the printf("%c") was legal, then this should also work:
char output[] = "*****\n*****\n*****\n";
for(int i = 0; i < strlen(output); i++)
     printf("%c", output[i]);


:devil2:
Was This Post Helpful? 0
  • +
  • -

#29 no2pencil  Icon User is online

  • Toubabo Koomi
  • member icon

Reputation: 5189
  • View blog
  • Posts: 26,896
  • Joined: 10-May 07

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

Posted 09 October 2012 - 10:45 AM

View PostSkydiver, on 09 October 2012 - 01:38 PM, said:

If that was a legal substitution, why did you not reply yes to Jim's post #22?

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.
Was This Post Helpful? 2
  • +
  • -

#30 jimblumberg  Icon User is online

  • member icon


Reputation: 3993
  • View blog
  • Posts: 12,322
  • Joined: 25-December 09

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

Posted 09 October 2012 - 10:53 AM

I agree with you on that. But unless the instructor is totally daft they probably won't accept any of Salem_c devilish obfuscated code as being written by the OP.

Jim

This post has been edited by jimblumberg: 09 October 2012 - 10:54 AM

Was This Post Helpful? 0
  • +
  • -

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