More Solutions

R

Romram

I was asked to submit atleast 5 solutions for the following problem:

By Replacing/Adding or deleting only one character from the following
code snippet, make it print tttttttttttttttttttt (20 times)

#include<stdio.h>

int main()
{
int k, j=20;
for(k=0;k<j;k--)
printf("t);
printf("\n");
return 0;
}

I came up with 3 solutions which were

1. instead of for(k=0;k<j;k--) write for(k=0;k<j;j--)

2. instead of for(k=0;k<j;k--) write for(k=0;-k<j;k--)

3.instead of for(k=0;k<j;k--) write for(k=0;k+j;k--)

Can anyone suggest me 2 more solutions?
 
I

Ico

Romram said:
I was asked to submit atleast 5 solutions for the following problem:

By Replacing/Adding or deleting only one character from the following
code snippet, make it print tttttttttttttttttttt (20 times)

#include<stdio.h>

int main()
{
int k, j=20;
for(k=0;k<j;k--)
printf("t);
printf("\n");
return 0;
}

I assume you forget the closing quote in the first printf(). Please make
sure that the code you post actually is correct and compiles.
1. instead of for(k=0;k<j;k--) write for(k=0;k<j;j--)

2. instead of for(k=0;k<j;k--) write for(k=0;-k<j;k--)

3.instead of for(k=0;k<j;k--) write for(k=0;k+j;k--)

Can anyone suggest me 2 more solutions?

instead of for(k=0;k<j;k--) write for(k=0;k<j;j-- )
instead of for(k=0;k<j;k--) write for(k=0;k<j;j-- )

et cetera. If this is not considered a correct answer, the question
should have been more precise.
 
R

Romram

instead of for(k=0;k<j;k--) write for(k=0;k<j;j-- )
instead of for(k=0;k<j;k--) write for(k=0;k<j;j-- )

et cetera. If this is not considered a correct answer, the question
should have been more precise.

I meant 5 different or unique solutions. Does it make the question
clear now?
 
I

Ico

Ico said:
I assume you forget the closing quote in the first printf(). Please make
sure that the code you post actually is correct and compiles.


instead of for(k=0;k<j;k--) write for(k=0;k<j;j-- )
instead of for(k=0;k<j;k--) write for(k=0;k<j;j-- )

Ehm, no those are wrong ofcourse, I changed *and* inserted, which is
against the rules. I should sit on my hands before I type.
 
I

Ico

Romram said:
I was asked to submit atleast 5 solutions for the following problem:

By Replacing/Adding or deleting only one character from the following
code snippet, make it print tttttttttttttttttttt (20 times)

#include<stdio.h>

int main()
{
int k, j=20;
for(k=0;k<j;k--)
printf("t);
printf("\n");
return 0;
}

I came up with 3 solutions which were

1. instead of for(k=0;k<j;k--) write for(k=0;k<j;j--)
2. instead of for(k=0;k<j;k--) write for(k=0;-k<j;k--)
3.instead of for(k=0;k<j;k--) write for(k=0;k+j;k--)

Can anyone suggest me 2 more solutions?

I have reason to believe you have already found the only three possible
solutions. I couldn't think of any more than the ones you already
stated, so I have just tried a brute-force search of all possible
replaces/additions/deletions of all possible characters at all possible
locations.

I'm interested in any other solutions that I might have missed.

Ico
 
J

Joe Wright

Romram said:
I was asked to submit atleast 5 solutions for the following problem:

By Replacing/Adding or deleting only one character from the following
code snippet, make it print tttttttttttttttttttt (20 times)

#include<stdio.h>

int main()
{
int k, j=20;
for(k=0;k<j;k--)
printf("t);
printf("\n");
return 0;
}

I came up with 3 solutions which were

1. instead of for(k=0;k<j;k--) write for(k=0;k<j;j--)

2. instead of for(k=0;k<j;k--) write for(k=0;-k<j;k--)

3.instead of for(k=0;k<j;k--) write for(k=0;k+j;k--)

Can anyone suggest me 2 more solutions?

Clearly some sort of homework. The canonical for loop ..

for (k = 0; k < j; ++k)

... otherwise the loop almost infinite. The snippet is broken in at least
two places and cannot be fixed with one change (as far as I can tell).

Please compile these things into small programs before posting here.
 
P

Pedro Graca

Ico said:
I have reason to believe you have already found the only three possible
solutions. I couldn't think of any more than the ones you already
stated, so I have just tried a brute-force search of all possible
replaces/additions/deletions of all possible characters at all possible
locations.

I'm interested in any other solutions that I might have missed.

add a single `-' to for(k=0;k<j;k--): for(k=0;-k<j;k--)
________^________
 
P

Pedro Graca

Ico said:
I have reason to believe you have already found the only three possible
solutions. I couldn't think of any more than the ones you already
stated, so I have just tried a brute-force search of all possible
replaces/additions/deletions of all possible characters at all possible
locations.

I'm interested in any other solutions that I might have missed.

My last post was a repeated solution. Sorry.


If you don't mind UB (decrementing k past INT_MIN) or using an
unprototyped function or waiting for the program to go through all the
negative values of the int type ...

instead of printf("t"); write pruntf("t"); and link with a library that
defines

int pruntf(char const * t) {
static int n = 20;
if (n) {
--n;
printf("%s", t);
}
return 0;
}
 
R

Romram

instead of printf("t"); write pruntf("t"); and link with a library that
defines

int pruntf(char const * t) {
static int n = 20;
if (n) {
--n;
printf("%s", t);
}
return 0;
}


You can change only one character in the code..... adding a library is
way out of the question..
 
I

Ico

Romram said:
I was asked to submit atleast 5 solutions for the following problem:

By Replacing/Adding or deleting only one character from the following
code snippet, make it print tttttttttttttttttttt (20 times)

#include<stdio.h>

int main()
{
int k, j=20;
for(k=0;k<j;k--)
printf("t);
printf("\n");
return 0;
}

I came up with 3 solutions which were

1. instead of for(k=0;k<j;k--) write for(k=0;k<j;j--)
2. instead of for(k=0;k<j;k--) write for(k=0;-k<j;k--)
3.instead of for(k=0;k<j;k--) write for(k=0;k+j;k--)

Can anyone suggest me 2 more solutions?

It's been a few days now, did you found any alternatives yet ?

Ico
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,174
Messages
2,570,940
Members
47,485
Latest member
Andrewayne909

Latest Threads

Top