Thread: Need some help with this small code

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    33

    Need some help with this small code

    Hi,

    I just started learning c and i was if anyone could i can print the number of runs from the code below at the end instate of after each run.

    Thanks!!

    Code:
    #include <stdio.h>      //printf
    #include <string.h>     //strcmp
    int main(){
     int x,y,runs,best=0,count=0;
     while ( scanf("%d", &x) == 1 )
      {
         if(x!=NULL)
          {
               runs++;
               printf("%s"," ");
               printf("%s","(");
               printf("%d", x);
               printf("%s",")");
           }
         printf("%d Number runs: ", runs);
         
      }
    }
    
    /*
    sample input:
    
    3  2 2 2  1 1 2 2
     Sample output:
    
     (3) (2 2 2) (1 1 2 2)
     Number runs: 3
     
    */
    
    
    Instate i get this output:
    
    (3)Number runs:  1(222)Number runs:  2(1122)Number runs: 3
    Last edited by axe; 01-05-2006 at 07:28 PM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by axe
    Hi,

    I just started learning c and i was if anyone could i can print the number of runs from the code below at the end instate of after each run.
    Thanks!!1 - x will never be NULL. NULL is a macro usually reserved for pointer comparison. You should just check to see if it's zero. In theory, they're the same thing, but there are minor differences between the two.

    2 - Just increment count inside your if check.
    3 - Print the number of runs (count) when you're at the bottom.
    4 - A function that is set to return a non-void type should always have a return statement. It's just good form.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    The big problem you have at the moment is than scanf() doesn't tell you how many spaces it skipped over from one int to the next.

    If you're looking at that level of detail in a string, then you should start by reading a whole line into a buffer using say fgets. Then you can parse that string looking at integers, counting significant spaces or whatever.

  4. #4
    Registered User
    Join Date
    Jan 2006
    Posts
    33
    Code:
    #include <stdio.h>      //printf
    #include <string.h>     //strcmp
    int main(){
     int x,y,runs,best=0,count=0;
     while ( scanf("%d", &x) == 1 )
      {
         if(x!=NULL)
          {
               runs++;
               printf("%s"," ");
               printf("%s","(");
               printf("%d", x);
               printf("%s",")");
           }
         printf("%d Number runs: ", runs);
         
      }
    }
    
    /*
    sample input:
    
    3  2 2 2  1 1 2 2
     Sample output:
    
     (3) (2 2 2) (1 1 2 2)
     Number runs: 3
     
    */
    
    
    Instate i get this output:
    
    (3)Number runs:  1(222)Number runs:  2(1122)Number runs: 3

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by axe
    Code:
    #include <string.h>     //strcmp
    You aren't using strcmp, or any string related functions for that matter.
    Quote Originally Posted by axe
    Code:
    int main(){
     int x,y,runs,best=0,count=0;
    You never use y.
    Quote Originally Posted by axe
    Code:
         if(x!=NULL)
    Stop comparing integers to NULL.
    Quote Originally Posted by axe
    Code:
          {
               runs++;
    You never initialize runs. This means you can't be sure what value it's going to contain before you start using it in your current code.
    Quote Originally Posted by axe
    Code:
               printf("%s"," ");
               printf("%s","(");
               printf("%d", x);
               printf("%s",")");
           }
    This just says:

    Always print a space. Then always print a (, then always print the value of x, then always print another ).

    Why don't you simply do this instead:
    Code:
    printf(" (%d)", x );
    Quote Originally Posted by axe
    Code:
      }
    }
    You still aren't returning from main. As good practice, you should always return something from functions which return non-void.

    Now how about starting to listen to what people tell you, eh?


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Jan 2006
    Posts
    33
    Code:
    Hi quzah,
    
    I think i am now starting to listen and thank. I still have a problem with the output though. I need the output to print the "Number of runs: 4" just once; for intance if i input :
    
    23 2234 435 5
    i need it to output:
    (23) (2234) (435) (5) Number of runs: 4
    
    instate it outputs:
    
    (23)Number of runs:  1 (2345)Number of runs:  2 (345)Number of runs:  3 (5)Number of runs: 4
    
    
    #include <stdio.h>      //printf
    int main(){
     int x,runs=0;
     while ( scanf("%d", &x) == 1 )
      {
         runs++;
         if(x!=0)
          {
               printf(" (%d)", x);
           }
           printf("Number of runs:  %d",runs);
       }
       return 0;
    }

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Move the line where you print out the number of runs outside of the loop. That way it only prints it once.


    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User
    Join Date
    Jan 2006
    Posts
    33
    Code:
    Hi Quzah,
    
    I tried the way you instructed does not print the number of runs.
    
     
    #include <stdio.h>      //printf
    int main(){
     int x,runs=0;
     while ( scanf("%d", &x) == 1 )
      {
         runs++;
         if(x!=0)
          {
               printf(" (%d)", x);
           }
           
       }
       printf("Number of runs:  %d",runs);
       return 0;
    }

  9. #9
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Code:
    goto post3;
    Last edited by Dave_Sinkula; 01-05-2006 at 10:35 PM.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  10. #10
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Quote Originally Posted by axe
    Code:
    Hi Quzah,
    
    I tried the way you instructed does not print the number of runs.
    
     
    #include <stdio.h>      //printf
    int main(){
     int x,runs=0;
     while ( scanf("%d", &x) == 1 )
      {
         runs++;
         if(x!=0)
          {
               printf(" (%d)", x);
           }
           
       }
       printf("Number of runs:  %d",runs);
       return 0;
    }
    instead
    Code:
    #include <stdio.h>      //printf
    int main()
    {
        int x,runs=0,ch;
        printf("Enter a number ( Q to quit)\n?");
        while ( scanf("%d", &x) == 1 )
        {
            runs++;
            if(x!=0)
            {
                printf(" (%d)", x);
            }
        printf("\nEnter a number ( Q to quit)\n?");
        }
       printf("Number of runs:  %d",runs);
       while((ch=getchar())!='\n' && ch != EOF);
       getchar();
       return 0;
    }
    
    /* my output
    Enter a number ( Q to quit)
    ?123
     (123)
    Enter a number ( Q to quit)
    ?456
     (456)
    Enter a number ( Q to quit)
    ?789
     (789)
    Enter a number ( Q to quit)
    ?369
     (369)
    Enter a number ( Q to quit)
    ?q
    Number of runs:  4
    ssharish2005

  11. #11
    Registered User
    Join Date
    Jan 2006
    Posts
    33
    Code:
    Thank you ssharish2005!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  2. True ASM vs. Fake ASM ????
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 04-02-2003, 04:28 AM
  3. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  4. Replies: 4
    Last Post: 01-16-2002, 12:04 AM