HI can give suggesstion

Discussion in 'C Programming' started by aslamhenry@yahoo.com, Sep 18, 2007.

  1. Guest

    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;
    }
    , Sep 18, 2007
    #1
    1. Advertising

  2. In article <>,
    <> 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
    Walter Roberson, Sep 18, 2007
    #2
    1. Advertising

  3. Mark Bluemel Guest

    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.
    Mark Bluemel, Sep 18, 2007
    #3
  4. Guest

    On Sep 18, 11:07 pm, Mark Bluemel <> wrote:
    > 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
    , Sep 18, 2007
    #4
  5. In article <>,
    <> wrote:
    >On Sep 18, 11:07 pm, Mark Bluemel <> 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
    Walter Roberson, Sep 18, 2007
    #5
  6. Guest

    On Sep 19, 6:34 am, -cnrc.gc.ca (Walter Roberson)
    wrote:
    > In article <>,
    >
    > <> wrote:
    > >On Sep 18, 11:07 pm, Mark Bluemel <> 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
    , Sep 19, 2007
    #6
  7. On 18 Sep, 15:59, 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
    Nick Keighley, Sep 19, 2007
    #7
  8. CBFalconer Guest

    Nick Keighley wrote:
    > 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
    CBFalconer, Sep 19, 2007
    #8
  9. U Guest

    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
    U, Sep 20, 2007
    #9
  10. U wrote:

    > 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
    John J. Smith, Sep 25, 2007
    #10
  11. user923005 Guest

    On Sep 24, 4:06 pm, John J. Smith <> wrote:
    > U wrote:
    > > 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:


    Please tell me that this is a gag post.

    > /* BEGIN bar.c */
    >
    > #include <stdio.h>
    > #include <stdlib.h>
    > #include <string.h>
    >
    > void main()


    1.25b: What's the right declaration for main()?
    Is void main() correct?

    A: See questions 11.12a to 11.15. (But no, it's not correct.)

    > {
    > char number_buffer[4096];
    > long number;
    > printf("please any key in 5 digit : ");
    > fflush(stdin);


    12.26: How can I flush pending input so that a user's typeahead isn't
    read at the next prompt? Will fflush(stdin) work?

    A: fflush() is defined only for output streams. Since its
    definition of "flush" is to complete the writing of buffered
    characters (not to discard them), discarding unread input
    would
    not be an analogous meaning for fflush on input streams.

    There is no standard way to discard unread characters from a
    stdio input stream, nor would such a way necessarily be
    sufficient, since unread characters can also accumulate in
    other, OS-level input buffers. You may be able to read and
    discard characters until \n, or use the curses flushinp()
    function, or use some system-specific technique. See also
    questions 19.1 and 19.2.

    References: ISO Sec. 7.9.5.2; H&S Sec. 15.2.

    > fflush(stdout);
    > gets(number_buffer);


    12.23: Why does everyone say not to use gets()?

    A: Unlike fgets(), gets() cannot be told the size of the buffer
    it's to read into, so it cannot be prevented from overflowing
    that buffer. As a general rule, always use fgets(). See
    question 7.1 for a code fragment illustrating the replacement
    of
    gets() with fgets().

    References: Rationale Sec. 4.9.7.2; H&S Sec. 15.7 p. 356.

    > 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 */
    user923005, Sep 25, 2007
    #11
  12. CBFalconer Guest

    "John J. Smith" wrote:
    > U wrote:
    >> wrote:
    >>
    >> [
    >> looking for solution to the following problem
    >>|
    >>| how to make these program huhu
    >>|
    >>| please any key in 5 digit : 56789


    This joker is a troll. He keeps reposting this and ignoring
    answers. So ignore him.

    --
    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
    CBFalconer, Sep 25, 2007
    #12
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. senthil
    Replies:
    0
    Views:
    614
    senthil
    Nov 21, 2003
  2. grocery_stocker
    Replies:
    10
    Views:
    620
    Keith Thompson
    May 25, 2005
  3. HI can give suggesstion

    , Sep 18, 2007, in forum: C Programming
    Replies:
    0
    Views:
    322
  4. Jon

    Suggesstion for standard upgrade

    Jon, Oct 31, 2010, in forum: C Programming
    Replies:
    3
    Views:
    228
    Seebs
    Oct 31, 2010
  5. Mohammad Khan

    GUI .. Ruby .. Suggesstion

    Mohammad Khan, May 13, 2004, in forum: Ruby
    Replies:
    3
    Views:
    108
    Richard Dale
    May 14, 2004
Loading...

Share This Page