# plz send me the solution of the problem.

Discussion in 'C Programming' started by wahid, Jan 9, 2010.

1. ### wahidGuest

Write a program that will have an integer
number input, and show the digits seperately.
(Assume that no number with more than 3
digits will be present in the input)

like :
• Enter a Number: 942
• The first digit of the number is : 9
• The second digit of the number is : 4
• The third digit of the number is : 2

wahid, Jan 9, 2010

2. ### Doug MillerGuest

In article <>, wahid <> wrote:
>Write a program that will have an integer
>number input, and show the digits seperately.
>(Assume that no number with more than 3
>digits will be present in the input)
>
>like :
>=95 Enter a Number: 942
>=95 The first digit of the number is : 9
>=95 The second digit of the number is : 4
>=95 The third digit of the number is : 2

to him.

Or do your own fucking homework.

Doug Miller, Jan 9, 2010

On Jan 9, 9:39 am, (Doug Miller) wrote:
> In article <..com>, wahid <> wrote:
>
> >Write a program that will have an integer
> >number input, and show the digits seperately.
> >(Assume that no number with more than 3
> >digits will be present in the input)

>
> >like :
> >=95 Enter a Number: 942
> >=95 The first digit of the number is : 9
> >=95 The second digit of the number is : 4
> >=95 The third digit of the number is : 2

>
> to him.
>
> Or do your own fucking homework.

Okay, it took me an entire 45 seconds to come up with a solution. What
do I win?

4. ### osmiumGuest

wahid wrote:

> Write a program that will have an integer
> number input, and show the digits seperately.
> (Assume that no number with more than 3
> digits will be present in the input)
>
> like :
> • Enter a Number: 942
> • The first digit of the number is : 9
> • The second digit of the number is : 4
> • The third digit of the number is : 2

In elemntary school you learned how to divide integers and then later on you
discovered the wonders of "long division"; with integers, C follows the
rules from the earlier years. Use that hint to write your program.

osmium, Jan 9, 2010
5. ### Eric SosmanGuest

On 1/9/2010 12:29 PM, wahid wrote:
> Write a program that will have an integer
> number input, and show the digits seperately.
> (Assume that no number with more than 3
> digits will be present in the input)
>
> like :
> • Enter a Number: 942
> • The first digit of the number is : 9
> • The second digit of the number is : 4
> • The third digit of the number is : 2

#include <stdio.h>
#include <stdlib.h>
int main(void) {
const char *fmt = "The first digit of the number is : %d\n"
"The second digit of the number is : %d\n"
"The third digit of the number is : %d\n";
int number;
if (scanf("%d", &number) != 1)
return EXIT_FAILURE;
switch (number) {
case 0: printf(fmt, 0, 0, 0); break;
case 1: printf(fmt, 0, 0, 1); break;
/* ... left as an exercise for the student ... */
case 999: printf(fmt, 9, 9, 9); break;
default: return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}

Note: A C90 compiler might reject this program on the grounds
that it has too many case labels. The C99 Standard raised the
limit, and will not reject it for that reason. Try to find a C99-
conforming compiler, just for safety's sake.

--
Eric Sosman
lid

Eric Sosman, Jan 9, 2010
6. ### Antoninus TwinkGuest

On 9 Jan 2010 at 17:29, wahid wrote:
> like :
> â€¢ Enter a Number: 942
> â€¢ The first digit of the number is : 9
> â€¢ The second digit of the number is : 4
> â€¢ The third digit of the number is : 2

#include <stdio.h>

#define S1 "The "
#define S2 " digit of the number is : %d\n"

int main(void)
{
int d, rv = 0;
fputs("Enter a Number: ", stdout);
fflush(stdout);
if(scanf("%d", &d) == 1 && d >=0 && d <= 999)
printf(S1 "first" S2 S1 "second" S2 S1 "third" S2, d/100, d%100/10, d%10);
else {
fputs("Invalid input: needed a non-negative integer with <= 3 digits\n",
stderr);
rv = 1;
}
return rv;
}

Antoninus Twink, Jan 9, 2010
7. ### SeebsGuest

On 2010-01-09, wahid <> wrote:
> Write a program that will have an integer
> number input, and show the digits seperately.
> (Assume that no number with more than 3
> digits will be present in the input)

> like :
> ? Enter a Number: 942
> ? The first digit of the number is : 9
> ? The second digit of the number is : 4
> ? The third digit of the number is : 2

I'm surprised you're getting this as homework. It's surprisingly
difficult for homework to assign to someone who apparently has never
made any effor towards learning C. Anyway, here's something
you might try:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <limits.h>
#include <stddef.h>
#include <math.h>

struct {
double lo, close;
int d;
} digits[] = {
{ 6.80183905339058814121, 6.80239476332431092231, '\x39' },
{ 6.68398653227400885157, 6.68461172766792710576, '\x38' },
{ 6.55036579410553621017, 6.55108033504340436792, '\x37' },
{ 6.39609597446756872330, 6.39692965521614631541, '\x36' },
{ 6.21360759808860851905, 6.21460809842219141785, '\x35' },
{ 5.99021376520632919949, 5.99146454710798170851, '\x34' },
{ 5.70211441755550385579, 5.70378247465620091816, '\x33' },
{ 5.29581423632991832307, 5.29831736654803631126, '\x32' },
{ 4.60015764416454686625, 4.60517018598809180219, '\x31' },
{ 9, 9, '\x30' },
{ 4.49423862528080952927, 4.49980967033026502122, '\x39' },
{ 4.37575702166028612794, 4.38202663467388120466, '\x38' },
{ 4.24132675257074609476, 4.24849524204935935501, '\x37' },
{ 4.08597631255158422192, 4.09434456222210041432, '\x36' },
{ 3.90197266957464483639, 3.91202300542814596085, '\x35' },
{ 3.67630067190707610081, 3.68887945411393625150, '\x34' },
{ 3.38439026334577430788, 3.40119738166215546116, '\x33' },
{ 2.97041446556970090143, 2.99573227355399085425, '\x32' },
{ 2.25129179860649530909, 2.30258509299404590109, '\x31' },
{ 9, 9, '\x30' },
{ 2.14006616349627076801, 2.19722457733621956422, '\x39' },
{ 2.01490302054226466666, 2.07944154167983574766, '\x38' },
{ 1.87180217690159134136, 1.94591014905531323187, '\x37' },
{ 1.70474809223842527217, 1.79175946922805495731, '\x36' },
{ 1.50407739677627416697, 1.60943791243410028180, '\x35' },
{ 1.25276296849536805667, 1.38629436111989057245, '\x34' },
{ 0.91629073187415510660, 1.09861228866810978211, '\x33' },
{ 0.40546510810816438486, 0.69314718055994528623, '\x32' },
{ -0.69314718055994528623, 0.00000000000000000000, '\x31' },
{ 9, 9, '\x30' },
{ NAN, NAN, '\x00' }
};

char *names[] = { "first", "second", "third", "oops", "shit", 0 };

int
main(void) {
int n;
double d;
int i;
int hit = 0;
int di = 0;
printf("Enter a Number: ");
fflush(stdout);
if (scanf("%d", &n) != 1) {
fprintf(stderr, "Shyeah right.\n");
exit(EXIT_FAILURE);
}
d = sqrt(n);
if (d != d) {
fprintf(stderr, "**** you too buddy.\n");
exit(EXIT_FAILURE);
}
if ((d = log(n)) > 6.9068) {
exit(EXIT_FAILURE);
}

for (i = (d >= 0 ? 0 : (hit = 1, 20)); digits.lo == digits.lo; ++i) {
if (d > digits.lo) {
printf("The %s digit of the number is %c.\n",
names[di++], digits.d);
d = log(exp(d) - exp(digits.close));
hit = 2;
}
if (digits.lo == 9) {
if (hit == 1) {
printf("The %s digit of the number is %c.\n",
names[di++], digits.d);
}
if (hit) {
hit = 1;
}
}
}
return 0;
}

.... cut here...

Hope this helps!

-s
--
Copyright 2010, all wrongs reversed. Peter Seebach /
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!

Seebs, Jan 9, 2010
8. ### sfuerstGuest

On Jan 9, 10:23 am, "osmium" <> wrote:
> wahid wrote:
> > Write a program that will have an integer
> > number input, and show the digits seperately.
> > (Assume that no number with more than 3
> > digits will be present in the input)

>
> > like :
> > • Enter a Number: 942
> > • The first digit of the number is : 9
> > • The second digit of the number is : 4
> > • The third digit of the number is : 2

>
> In elemntary school you learned how to divide integers and then later on you
> discovered the wonders of "long division"; with integers, C follows the
> rules from the earlier years.  Use that hint to write your program.

Division? Who needs that?

#include <stdio.h>

int main(void)
{
unsigned n, x, y, m = 429496729;
char t[] = {48, 49, 50, 50, 51, 51, 52, 53, 53, 54, 55, 55, 56, 56,
57, 48, 23, 59, 41};
char *p = 16 + t;

printf("Enter a number less than 1000\n");
scanf("%u", &n);

x = (n >> 1) + (n >> 2);
y = (n >> 1) + (n >> 3) + (n >> 6) - (n >> 10) + (x >> 11) - (n >>
16);
x += x >> 4;
x += x >> 8;
y += y >> 20;
x += x >> 16;
y >>= 6;
*p++ = ((m * n) >> 28)[t];
x >>= 3;
*p++ = ((m * ((n > 9 + x * 10) + x)) >> 28)[t];
*p = ((m * ((n > 99 + y * 100) + y)) >> 28)[t];

printf("The first digit of the number is %c\n", *p--);
printf("The second digit of the number is %c\n", *p--);
printf("The third digit of the number is %c\n", *p);

return 0;
}

sfuerst, Jan 9, 2010
9. ### wahidGuest

thank you .

wahid, Jan 10, 2010

On Jan 9, 6:53 pm, pete <> wrote:
> wahid wrote:
> > Write a program that will have an integer
> > number input, and show the digits seperately.
> > (Assume that no number with more than 3
> > digits will be present in the input)

>
> > like :
> > Enter a Number: 942
> > The first digit of the number is : 9
> > The second digit of the number is : 4
> > The third digit of the number is : 2

>
> /* BEGIN new.c */
>
> #include <stdio.h>
>
> #define MAX             999
> #define str(x)          # x
> #define xstr(x)         str(x)
>
> int main(void)
> {
>      int d;
>      char array[sizeof xstr(MAX)];
>      char *string[] = {"first","second","third"};
>
>      fputs("Enter an integer from 0 to " xstr(MAX) ": ", stdout);
>      fflush(stdout);
>      if (fscanf(stdin, "%d", &d) == 1 && d >= 0 && MAX >= d) {
>          sprintf(array, "%d", d);
>          for (d = 0; array[d] != '\0'; ++d) {
>              printf("The %-6s digit of the number is : %c\n",
>                  string[d], array[d]);
>          }
>      } else {
>          puts("Try again.");
>      }
>      return 0;
>
> }
>
> /* END new.c */
>
> --
> pete

Not to sound like a dick. But whatever happened to waiting for the
person to post an attempted solution before posting a (correct) one?

11. ### Keith ThompsonGuest

> On Jan 9, 6:53Â pm, pete <> wrote:
>> wahid wrote:
>> > Write a program that will have an integer
>> > number input, and show the digits seperately.
>> > (Assume that no number with more than 3
>> > digits will be present in the input)

>>
>> > like :
>> > Enter a Number: 942
>> > The first digit of the number is : 9
>> > The second digit of the number is : 4
>> > The third digit of the number is : 2

>>
>> /* BEGIN new.c */

[code snipped]
>>
>> --
>> pete

>
> Not to sound like a dick. But whatever happened to waiting for the
> person to post an attempted solution before posting a (correct) one?

You're assuming pete's solution is correct. Maybe it is, maybe it
isn't, but I doubt that wahid knows enough to be able to tell -- and I
for one am not going to tell him which of the posted solutions, if
any, are actually correct.

--
Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Keith Thompson, Jan 10, 2010
12. ### Antoninus TwinkGuest

On 10 Jan 2010 at 20:11, Keith Thompson wrote:
> You're assuming pete's solution is correct.

I don't think he's assuming anything of the sort. What makes you think
he hasn't checked through pete's solution and *found* it to be correct?

> Maybe it is, maybe it isn't,

> I doubt that wahid knows enough to be able to tell -- and I for one am
> not going to tell him which of the posted solutions, if any, are
> actually correct.

You've missed the boat here Keith - correct solutions to all the posted
problems have already been supplied, and you can bet your life that if
any of them are incorrect, then there will be a whole horde of people
falling over themselves to trip me up and pick holes in my code if
they're there - that's the "peer review" that you love so much in
action!

Antoninus Twink, Jan 10, 2010
13. ### Keith ThompsonGuest

pete <> writes:
[snip]
> I don't care if OP wants to copy homework solutions
> and learn nothing.

I suggest that you should care. Surely if you want to find little
programming puzzles for yourself, you can do so without helping
someone cheat.

--
Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Keith Thompson, Jan 11, 2010

On Jan 10, 4:36 pm, pete <> wrote:
> Antoninus Twink wrote:
> > On 10 Jan 2010 at 20:11, Keith Thompson wrote:

>
> >>You're assuming pete's solution is correct.

>
> > I don't think he's assuming anything of the sort. What makes you think
> > he hasn't checked through pete's solution and *found* it to be correct?

>
> >>Maybe it is, maybe it isn't,

>

>
> >>I doubt that wahid knows enough to be able to tell -- and I for one am
> >>not going to tell him which of the posted solutions, if any, are
> >>actually correct.

>
> > You've missed the boat here Keith - correct solutions to all the posted
> > problems have already been supplied, and you can bet your life that if
> > any of them are incorrect, then there will be a whole horde of people
> > falling over themselves to trip me up and pick holes in my code if
> > they're there - that's the "peer review" that you love so much in
> > action!

>
> That's pretty much it.
>
> When I post untested code
> I always get caught
> And that's what I'm looking for.
>
> My criteria for doing a homework problem
> is that it be somewhat amusing and not too hard.
>
>  From time to time an OP has asked questions about my posted code,
> and then I've explained it to the best of my ability
> and I suppose the OP was able to pick up something about C.
>
> I don't care if OP wants to copy homework solutions
> and learn nothing.
>
> --
> pete

That was untested code?! Uh..... here is what I get...

[cdalten@localhost oakland]\$ gcc -Wextra -Wall new.c -o new
[cdalten@localhost oakland]\$ ./new
Enter an integer from 0 to 999: 321
The first digit of the number is : 3
The second digit of the number is : 2
The third digit of the number is : 1
[cdalten@localhost oakland]\$ ./new
Enter an integer from 0 to 999: 8888888
Try again.
[cdalten@localhost oakland]\$ ./new
Enter an integer from 0 to 999: 45
The first digit of the number is : 4
The second digit of the number is : 5
[cdalten@localhost oakland]\$ ./new
Enter an integer from 0 to 999: 2
The first digit of the number is : 2
[cdalten@localhost oakland]\$

I guess that both you and I have vastly different views on what
"untested code" is.

15. ### scatteredGuest

On Jan 10, 8:37 pm, Keith Thompson <> wrote:
> pete <> writes:
>
> [snip]
>
> > I don't care if OP wants to copy homework solutions
> > and learn nothing.

>
> I suggest that you should care.  Surely if you want to find little
> programming puzzles for yourself, you can do so without helping
> someone cheat.
>

I agree in general but in this case I suspect that if a professor saw

#define str(x) # x
#define xstr(x) str(x)

in an intro to programming class then that professor would strongly
suspect that the student has obtained that code from somewhere else
(unless the textbook covered stringizing macros - which seems beyond
the scope of an intro text), especially if the student is as weak a
wahid seems to be. Such a professor could quickly confirm that
suspicion by asking wahid a simple question or two. What I would
really have a problem with would be posting code that wahid could
plausibly turn in as his own.

scattered, Jan 11, 2010
16. ### Richard BosGuest

pete <> wrote:

> I don't care if OP wants to copy homework solutions and learn nothing.

Let me suggest for your consideration that you should, if only because
he might either get a Green Card and be employed to be your cow-orker,
or get employed where he is to write some software which you will come
(possibly by necessity) to rely on.

Richard

Richard Bos, Jan 11, 2010
17. ### Eric SosmanGuest

On 1/11/2010 1:15 PM, Richard Bos wrote:
> pete<> wrote:
>
>> I don't care if OP wants to copy homework solutions and learn nothing.

>
> Let me suggest for your consideration that you should, if only because
> he might either get a Green Card and be employed to be your cow-orker,
> or get employed where he is to write some software which you will come
> (possibly by necessity) to rely on.

Still worse, you may be assigned to clean up the mess
he makes ...

--
Eric Sosman
lid

Eric Sosman, Jan 11, 2010
18. ### Charlton WilburGuest

>>>>> "C" == Chad <> writes:

C> Not to sound like a dick. But whatever happened to waiting for
C> the person to post an attempted solution before posting a
C> (correct) one?

Posting a possibly correct solution -- I only skimmed it -- that is more
rigorous and thorough than the homework plagiarist likely has the
experience to be, and is written in idiomatic C that is likely to be
well above the homework plagiarist's level of ability seems like another
good solution to the problem.

Charlton

--
Charlton Wilbur

Charlton Wilbur, Jan 12, 2010
19. ### Keith ThompsonGuest

Charlton Wilbur <> writes:
>>>>>> "C" == Chad <> writes:

>
> C> Not to sound like a dick. But whatever happened to waiting for
> C> the person to post an attempted solution before posting a
> C> (correct) one?
>
> Posting a possibly correct solution -- I only skimmed it -- that is more
> rigorous and thorough than the homework plagiarist likely has the
> experience to be, and is written in idiomatic C that is likely to be
> well above the homework plagiarist's level of ability seems like another
> good solution to the problem.

Assuming that the instructor is sufficiently competent to recognize
that the submitted code is beyond the student's abilities. If he
gets enough help cheating, he might be able to fool the instructor
just because the instructor will never have seen any code the
student actually wrote.

--
Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Keith Thompson, Jan 12, 2010
20. ### Charlton WilburGuest

>>>>> "KT" == Keith Thompson <> writes:

KT> Charlton Wilbur <> writes:

>> Posting a possibly correct solution -- I only skimmed it -- that
>> is more rigorous and thorough than the homework plagiarist likely
>> has the experience to be, and is written in idiomatic C that is
>> likely to be well above the homework plagiarist's level of
>> ability seems like another good solution to the problem.

KT> Assuming that the instructor is sufficiently competent to
KT> recognize that the submitted code is beyond the student's
KT> abilities. If he gets enough help cheating, he might be able to
KT> fool the instructor just because the instructor will never have
KT> seen any code the student actually wrote.

Well, if the instructor is that incompetent, the student is not going to
learn much either way.

Charlton

--
Charlton Wilbur

Charlton Wilbur, Jan 13, 2010