input

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • mitchellpal@gmail.com

    #1

    input

    guys i need help here, what do i do to my code inorder to prompt the
    user to enter any 10 integer values then display them in ascending
    order?

    #include<stdio. h>
    #define SIZE 10

    main()
    {
    int i;
    int pass;//pass counter
    int hold;//
    long a[SIZE]={12,451,56,205 ,450,78,90,67,4 5,100};


    //printf("Enter 10 integer values\n\n");

    printf("\nEleme nts in original array\n\n");

    for(i=0;i<SIZE; i++){
    printf("%4d",a[i]);
    }

    printf("\n\n");

    //pass counter
    for(pass=0;pass <SIZE;pass++) {

    for(i=0;i<SIZE-1;i++){

    if(a[i]>a[i+1]){
    hold=a[i];
    a[i]=a[i+1];
    a[i+1]=hold;
    }//endif
    }//end inner for
    }//end outer for

    printf("Element s in ascending order\n\n");

    for(i=0;i<SIZE; i++){
    printf("%4d",a[i]);
    }//endfor
    printf("\n\n");
    }
    the program is working as it is...... my email address is real too.

  • Vladimir S. Oka

    #2
    Re: input


    mitchellpal@gma il.com wrote:[color=blue]
    > guys i need help here, what do i do to my code inorder to prompt the
    > user to enter any 10 integer values then display them in ascending
    > order?
    >
    > #include<stdio. h>
    > #define SIZE 10
    >
    > main()
    > {
    > int i;
    > int pass;//pass counter
    > int hold;//
    > long a[SIZE]={12,451,56,205 ,450,78,90,67,4 5,100};
    >
    >
    > //printf("Enter 10 integer values\n\n");
    >
    > printf("\nEleme nts in original array\n\n");
    >
    > for(i=0;i<SIZE; i++){
    > printf("%4d",a[i]);
    > }
    >
    > printf("\n\n");
    >
    > //pass counter
    > for(pass=0;pass <SIZE;pass++) {
    >
    > for(i=0;i<SIZE-1;i++){
    >
    > if(a[i]>a[i+1]){
    > hold=a[i];
    > a[i]=a[i+1];
    > a[i+1]=hold;
    > }//endif
    > }//end inner for
    > }//end outer for
    >
    > printf("Element s in ascending order\n\n");
    >
    > for(i=0;i<SIZE; i++){
    > printf("%4d",a[i]);
    > }//endfor
    > printf("\n\n");
    > }
    > the program is working as it is...... my email address is real too.[/color]

    Your program is working by sheer luck! You use an int as a temporary
    storage to swap two longs. This may not give you the results you expect
    (think what happens if long is wider than int, and your numbers exceed
    MAXINT).

    To prompt user, use printf() to output the prompt, fgets() (NOT gets())
    to collect the line entered, and sscanf() to parse it for number
    entered. Wrap this in a nice loop, and you're all set...

    Cheers

    Vladimir

    PS
    My e-mail address is real as well, but that's highly off-topic in
    c.l.c. ;-)

    Comment

    • mitchellpal@gmail.com

      #3
      Re: input

      let me try this.......than ks

      Comment

      • Vladimir S. Oka

        #4
        Re: input

        mitchellpal@gma il.com wrote:
        [color=blue]
        > let me try this.......than ks[/color]

        Try what?

        Please quote what you're replying to. There's going to be people
        who can't see the original post. If using Google:

        "If you want to post a followup via groups.google.c om, don't use
        the broken "Reply" link at the bottom of the article. Click on
        "show options" at the top of the article, then click on the
        "Reply" at the bottom of the article headers." - Keith Thompson
        More details at: <http://cfaj.freeshell. org/google/>

        Cheers

        Vladimir

        Comment

        • Emmanuel Delahaye

          #5
          Re: input

          mitchellpal@gma il.com a écrit :[color=blue]
          > guys i need help here, what do i do to my code inorder to prompt the
          > user to enter any 10 integer values then display them in ascending
          > order?[/color]

          In some loop, use a fgets() to accept a line of text and strtol() to
          convert the text to a number. Store the values in some array.

          Then, sort the array and display it.

          Do your best an post your code if you are stuck.

          --
          A+

          Emmanuel Delahaye

          Comment

          Working...