Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > HI can give suggesstion

Reply
Thread Tools

HI can give suggesstion

 
 
aslamhenry@yahoo.com
Guest
Posts: n/a
 
      09-18-2007
how to make these program huhu

please any key in 5 digit : 56789

ouput

5678 9
567 89
56 789
5 6789

take a look at my coding...why the ouput is rubbish huhuhu

#include <stdio.h>

int main(void)
{
float num;
int i, j, x ;

printf("please key in any 5 digit number:");
scanf("%f",&num);




for ( i=0 ; 5>i ; ++i){

for(j=0 ; i>j ; ++j){
printf(" ") ;
}

for( x = j+1 ; 5 >= x ; ++x){

printf("%.0f", num);
}
putchar('\n');
}

return 0;
}

 
Reply With Quote
 
 
 
 
Walter Roberson
Guest
Posts: n/a
 
      09-18-2007
In article <(E-Mail Removed). com>,
<(E-Mail Removed)> wrote:
>how to make these program huhu


>please any key in 5 digit : 56789


>ouput


>5678 9
> 567 89
> 56 789
> 5 6789


>take a look at my coding...why the ouput is rubbish huhuhu


>printf("please key in any 5 digit number:");
>scanf("%f",&num);


Don't read the number as floating point: read it as a pure string
of characters. You don't ever need to know the numeric value:
you just need to manipulate the characters the person typed in.
With the requirements you have presented, the code you will
arrive out should pretty much be able to handle input strings
such as Kv8$} and do the staggered printing, if not for the
checking that you will do to ensure that the user only entered
digits (and exactly 5 of them.)
--
There are some ideas so wrong that only a very intelligent person
could believe in them. -- George Orwell
 
Reply With Quote
 
 
 
 
Mark Bluemel
Guest
Posts: n/a
 
      09-18-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> how to make these program huhu
>
> please any key in 5 digit : 56789
>
> ouput
>
> 5678 9
> 567 89
> 56 789
> 5 6789
>
> take a look at my coding...why the ouput is rubbish huhuhu


I'll give you a hint. The processing you need would be no different if
you wanted to enter "abcde" and get
abcd e
abc de
....

This is string processing, not numeric processing.
 
Reply With Quote
 
aslamhenry@yahoo.com
Guest
Posts: n/a
 
      09-18-2007
On Sep 18, 11:07 pm, Mark Bluemel <(E-Mail Removed)> wrote:
> (E-Mail Removed) wrote:
> > how to make these program huhu

>
> > please any key in 5 digit : 56789

>
> > ouput

>
> > 5678 9
> > 567 89
> > 56 789
> > 5 6789

>
> > take a look at my coding...why the ouput is rubbish huhuhu

>
> I'll give you a hint. The processing you need would be no different if
> you wanted to enter "abcde" and get
> abcd e
> abc de
> ...
>
> This is string processing, not numeric processing.


but the question ask to do in digit take a look at the question

Write a program in C that reads any five digit number and displays the
number in two parts diagonally as shown in the user interface screen
as shown below

Please key in any 5 digit number : 56789

5678 9
567 89
56 789
5 6789


The number displayed is separated into two parts beginning with the
rightmost unit digit.The process continues untill leftmost digit is
reached

 
Reply With Quote
 
Walter Roberson
Guest
Posts: n/a
 
      09-18-2007
In article <(E-Mail Removed) .com>,
<(E-Mail Removed)> wrote:
>On Sep 18, 11:07 pm, Mark Bluemel <(E-Mail Removed)> wrote:


>> I'll give you a hint. The processing you need would be no different if
>> you wanted to enter "abcde" and get
>> abcd e
>> abc de


>> This is string processing, not numeric processing.



>but the question ask to do in digit take a look at the question


>Write a program in C that reads any five digit number and displays the
>number in two parts diagonally as shown in the user interface screen
>as shown below


Well, if you *insist* (and I really *really* think you should reconsider):


Read the number as a number, to satisfy what you feel to be
criteria that you work with a "number". Now loop 5 times, each time
forming a new number from the original number divided by 10 to the
power of the loop index (i.e., divide by 10 when the index is 1, divide
by 100 when the index is 2, divide by 1000 when the index is 3, and so on.)

Write out the new number to a character buffer, using code similar to

sprintf(tmpbuffer, "%5.*f", loopindex, modifiednumber)

The * means that the number of digits after the decimal places is to
be taken from the value of the next argument in the list, which is the
loopindex in this case. So the first loop trip would use %5.1f
and store the string with one decimal place, the second loop trip
would use %5.2f and store the string with two decimal places, etc..

Now, still inside the loop, find the decimal place in the temporary
buffer and replace it with a space. Now print out the temporary buffer
and return for the next iteration of the loop.


But I really do recommend that you recognize that just because
the problem statement says that you must read a 5 digit number,
it doesn't mean that you have to work with the input as
a numeric value. They just throw that bit about "number" in,
in order to confuse people who haven't figured out the difference
between the -value- of a number and the way it is -represented-.
--
Okay, buzzwords only. Two syllables, tops. -- Laurie Anderson
 
Reply With Quote
 
aslamhenry@yahoo.com
Guest
Posts: n/a
 
      09-19-2007
On Sep 19, 6:34 am, (E-Mail Removed)-cnrc.gc.ca (Walter Roberson)
wrote:
> In article <(E-Mail Removed) .com>,
>
> <(E-Mail Removed)> wrote:
> >On Sep 18, 11:07 pm, Mark Bluemel <(E-Mail Removed)> wrote:
> >> I'll give you a hint. The processing you need would be no different if
> >> you wanted to enter "abcde" and get
> >> abcd e
> >> abc de
> >> This is string processing, not numeric processing.

> >but the question ask to do in digit take a look at the question
> >Write a program in C that reads any five digit number and displays the
> >number in two parts diagonally as shown in the user interface screen
> >as shown below

>
> Well, if you *insist* (and I really *really* think you should reconsider):
>
> Read the number as a number, to satisfy what you feel to be
> criteria that you work with a "number". Now loop 5 times, each time
> forming a new number from the original number divided by 10 to the
> power of the loop index (i.e., divide by 10 when the index is 1, divide
> by 100 when the index is 2, divide by 1000 when the index is 3, and so on.)
>
> Write out the new number to a character buffer, using code similar to
>
> sprintf(tmpbuffer, "%5.*f", loopindex, modifiednumber)
>
> The * means that the number of digits after the decimal places is to
> be taken from the value of the next argument in the list, which is the
> loopindex in this case. So the first loop trip would use %5.1f
> and store the string with one decimal place, the second loop trip
> would use %5.2f and store the string with two decimal places, etc..
>
> Now, still inside the loop, find the decimal place in the temporary
> buffer and replace it with a space. Now print out the temporary buffer
> and return for the next iteration of the loop.
>
> But I really do recommend that you recognize that just because
> the problem statement says that you must read a 5 digit number,
> it doesn't mean that you have to work with the input as
> a numeric value. They just throw that bit about "number" in,
> in order to confuse people who haven't figured out the difference
> between the -value- of a number and the way it is -represented-.
> --
> Okay, buzzwords only. Two syllables, tops. -- Laurie Anderson


thx for the hint...but can u give me some example..i mean a bit
only....the rest i do by myself

 
Reply With Quote
 
Nick Keighley
Guest
Posts: n/a
 
      09-19-2007
On 18 Sep, 15:59, (E-Mail Removed) wrote:

> how to make these program huhu


what does "huhu" mean?


> please any key in 5 digit : 56789
>
> ouput
>
> 5678 9
> 567 89
> 56 789
> 5 6789
>
> take a look at my coding...why the ouput is rubbish huhuhu


what is the output?


> #include <stdio.h>
>
> int main(void)
> {
> float num;


don't use float to hold an integer

> int i, j, x ;
>
> printf("please key in any 5 digit number:");


fflush(stdout) to ensure output appears

> scanf("%f",&num);


scanf() is tricky to use correctly. Consider fgets()
followed by sscanf(). Check the return value of any
scanf() type call

> for ( i=0 ; 5>i ; ++i){


loops 5 times. Why not use the usual idiom i < 5?

>
> for(j=0 ; i>j ; ++j){
> printf(" ") ;
> }


outputs i spaces


> for( x = j+1 ; 5 >= x ; ++x){


??


>
> printf("%.0f", num);


what do you *think* this does?

> }
> putchar('\n');
> }
>
> return 0;
> }


if you *insist* on reading an integer you could turn
it back into characters using sprintf(). To put put the "broken"
number. On the nth line

output first n characters
output a space
output rest of chars

or

for all characters
output char
if break point
output space



--
Nick Keighley

 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      09-19-2007
Nick Keighley wrote:
> (E-Mail Removed) wrote:
>

.... snip ...
>
>> for ( i=0 ; 5>i ; ++i){

>
> loops 5 times. Why not use the usual idiom i < 5?


If you study it closely, he did

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>



--
Posted via a free Usenet account from http://www.teranews.com

 
Reply With Quote
 
U
Guest
Posts: n/a
 
      09-20-2007
(E-Mail Removed) wrote:

[
looking for solution to the following problem
|
| how to make these program huhu
|
| please any key in 5 digit : 56789
|
| ouput
|
| 5678 9
| 567 89
| 56 789
| 5 6789
|
]
<snippage>

>
> but can u give me some example..i mean a bit
> only....the rest i do by myself
>


Here you go...

/*** begin foo.c ***/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

int new_number(long **ppnumber)
{
char foo[256];
size_t bar;
*ppnumber = NULL;
fprintf(stdout, "please any key in 5 digit : "); fflush(stdout);
if(!fgets(foo, 256, stdin)) { if(feof(stdin)) return 1; else return 2; }
bar = strlen(foo)-1; if(foo[bar] == '\n') bar[foo] = '\0'; else bar++;
if(bar != 5) return 3;
for(bar = 0; bar < 5; bar++) if(!isdigit(*(foo+bar))) return 4;
if((*ppnumber = malloc(sizeof * ppnumber)) == NULL) return 5;
**ppnumber = strtol(foo, (char **)NULL, 0x0A);
return 0;
}

void delete_number(long **ppnumber)
{
free(*ppnumber);
*ppnumber = NULL;
}

void fail(int y)
{
char *f;
switch(y) {
default: return;
case 1: f = "EOF"; break;
case 2: f = "Read error"; break;
case 3:
case 4: f = "Not a 5-digit number"; break;
case 5: f = "Out of memory"; break;
}
fprintf(stderr, "\n*** ERROR: %s!!!\n\n\n", f);
exit(EXIT_FAILURE);
}

signed main(void)
{
long *pnumber;
int jupiter[5], v3, v4;

if((v3 = new_number(&pnumber)) == 0)
{
fprintf(stdout, "\nouput\n\n");
for(v3 = 0; v3 < 5; v3++) {
jupiter[5-v3-1] = *pnumber % 10 - 1;
*pnumber /= 10;
}
for(v3 = 0; v3 < 4; v3++) {
for(v4 = 0; v4 < v3; v4++)
fputc(' ', stdout);
for(v4 = 0; v4 < 5; v4++) {
fputc('1' + jupiter[v4], stdout);
if(v4 == 3-v3)
fputc(' ', stdout);
}
fputc('\n', stdout);
}
delete_number(&pnumber);
} else {
fail(v3);
}
return EXIT_SUCCESS;
}

/*** end foo.c ***/


--
U

 
Reply With Quote
 
John J. Smith
Guest
Posts: n/a
 
      09-24-2007
U wrote:

> (E-Mail Removed) wrote:
>
> [
> looking for solution to the following problem
> |
> | how to make these program huhu
> |
> | please any key in 5 digit : 56789
> |
> | ouput
> |
> | 5678 9
> | 567 89
> | 56 789
> | 5 6789
> |
> ]
> <snippage>
>
>>
>> but can u give me some example..i mean a bit
>> only....the rest i do by myself
>>

>
> Here you go...
>
> /*** begin foo.c ***/
>
> #include <stdio.h>
> #include <stdlib.h>
> #include <string.h>
> #include <ctype.h>
>
> int new_number(long **ppnumber)
> {
> char foo[256];
> size_t bar;
> *ppnumber = NULL;
> fprintf(stdout, "please any key in 5 digit : "); fflush(stdout);
> if(!fgets(foo, 256, stdin)) { if(feof(stdin)) return 1; else return 2; }
> bar = strlen(foo)-1; if(foo[bar] == '\n') bar[foo] = '\0'; else bar++;
> if(bar != 5) return 3;
> for(bar = 0; bar < 5; bar++) if(!isdigit(*(foo+bar))) return 4;
> if((*ppnumber = malloc(sizeof * ppnumber)) == NULL) return 5;
> **ppnumber = strtol(foo, (char **)NULL, 0x0A);
> return 0;
> }
>
> void delete_number(long **ppnumber)
> {
> free(*ppnumber);
> *ppnumber = NULL;
> }
>
> void fail(int y)
> {
> char *f;
> switch(y) {
> default: return;
> case 1: f = "EOF"; break;
> case 2: f = "Read error"; break;
> case 3:
> case 4: f = "Not a 5-digit number"; break;
> case 5: f = "Out of memory"; break;
> }
> fprintf(stderr, "\n*** ERROR: %s!!!\n\n\n", f);
> exit(EXIT_FAILURE);
> }
>
> signed main(void)
> {
> long *pnumber;
> int jupiter[5], v3, v4;
>
> if((v3 = new_number(&pnumber)) == 0)
> {
> fprintf(stdout, "\nouput\n\n");
> for(v3 = 0; v3 < 5; v3++) {
> jupiter[5-v3-1] = *pnumber % 10 - 1;
> *pnumber /= 10;
> }
> for(v3 = 0; v3 < 4; v3++) {
> for(v4 = 0; v4 < v3; v4++)
> fputc(' ', stdout);
> for(v4 = 0; v4 < 5; v4++) {
> fputc('1' + jupiter[v4], stdout);
> if(v4 == 3-v3)
> fputc(' ', stdout);
> }
> fputc('\n', stdout);
> }
> delete_number(&pnumber);
> } else {
> fail(v3);
> }
> return EXIT_SUCCESS;
> }
>
> /*** end foo.c ***/



Way too complicated. Also, the output does not match the template
in the OP.

OP wanted:

| please any key in 5 digit : 56789
|
| ouput
|
| 5678 9
| 567 89
| 56 789
| 5 6789

Your program:

| please any key in 5 digit : 56789
|
| ouput
|
| 5678 9
| 567 89
| 56 789
| 5 6789

Here's my take:

/* BEGIN bar.c */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>


void main()
{
char number_buffer[4096];
long number;
printf("please any key in 5 digit : ");
fflush(stdin);
fflush(stdout);
gets(number_buffer);
if(strlen(number_buffer) != 5)
goto error;
for(number=0; number<5; number++)
if(number_buffer[number] < '0' || number_buffer[number] > '9')
goto error;
number=atol(number_buffer);
printf("\nouput\n\n"
"%04li %01li\n"
" %03li %02li\n"
" %02li %03li\n"
" %01li %04li\n",
number/10, number%10,
number/100, number%100,
number/1000, number%1000,
number/10000, number%10000);
return;

error:
printf("invalid input.\n");
return;
}
/* END bar.c */

--
John J. Smith
Homework Expert
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Suggesstion for standard upgrade Jon C Programming 3 10-31-2010 05:42 PM
HI can give suggesstion aslamhenry@yahoo.com C Programming 0 09-18-2007 02:11 PM
Give us 3 minutes; we give you the whole library lib Computer Support 1 02-04-2005 03:16 AM
Give us 3 minutes; we give you the whole library lib Computer Support 0 01-27-2005 07:52 AM
GUI .. Ruby .. Suggesstion Mohammad Khan Ruby 3 05-14-2004 10:26 AM



Advertisments