couple of inputs

This is a discussion on couple of inputs within the C Programming forums, part of the General Programming Boards category; hai~ sorry to bother all of you, but i just can't figure this one out the program gets 2 lines ...

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    8

    couple of inputs

    hai~
    sorry to bother all of you, but i just can't figure this one out

    the program gets 2 lines from standard input. in the first there is only one number that decides how many numbers shall come in the second line.


    so the question is, how can you scanf() the numbers in the second line if you don't know how many of those are coming?

    btw scanf is not neccessary, but must stick to simple C functions

    many thanks ^^

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    If only there was a way to repeat a statement (or a group of statements) some fixed number of times....

  3. #3
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    602
    How sarcastic, but funny!

  4. #4
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    602
    Read up on what scanf returns upon success. This will be a big clue!

  5. #5
    Registered User
    Join Date
    Nov 2008
    Posts
    8
    right but you cannot use scanf("%d "&something) in a loop cause scanf needs an eof or enter pushed, but these numbers come at once in the same line

  6. #6
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    602
    There are a number of ways to do this. What tabstop was eluding to is that your first scanf assignment can be used to set a loop iterator for your next scanf's, for the amount you need.

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by skull_tkl View Post
    right but you cannot use scanf("%d "&something) in a loop cause scanf needs an eof or enter pushed, but these numbers come at once in the same line
    scanf does not throw away extra input, but keeps it for future use.

  8. #8
    Registered User
    Join Date
    Nov 2008
    Posts
    8
    well , maybe i am really that dumb that was my first choice but it didn't seem to work so here's the whole story:

    the program gets that 2 lines yes.
    the numbers in the second line are temperatures, and we are searching for the highest difference between two numbers that are next to each other.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int absz(int i)         // makes positive from any kind of number
    {
        if (i>=0)return i;
        else return -i;
        }
    
    int main() {
    int n,d=0,t1,t2,i;     //number,difference,temp1,temp2,i
    
        scanf("%d\n",&n);
        scanf("%d ",&t1);
        
        for(i=0; i<n-1; i++)
        {
               scanf("%d ",t2);
                         if (absz(t2-t1)>d ) d=absz(t2-t1);  // if difference here is higher than 
                                                                                // before then store
                         t1=t2;                                             // prepares next comparison
        }
        
        printf("%d\n",d);                                             //printing the highest difference
        
    	return 0;
    }

  9. #9
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    602
    I do not believe this is accomplishing what you think. You are comparing all the following values from preceeding scanfs to t1 only then simply calculating the deference. What is absz() really doing?

    Are you not needing to retain what two values show the largest difference at all? Are you really only concerned with the values that only compare to t1, as some base temperature you are measuring against?

  10. #10
    Registered User
    Join Date
    Nov 2008
    Posts
    8
    t1 is always getting a new value in the loop , so i'm comparing the previous number to the number i got right now

    i need the difference as a positive value so absz is helping in that, if absz gets a positive value, it returns it so i get positive, and if it gets negative it retuns the positive equivalent so again, i get a positive number

    and no i don't need the numbers stored, i only need the highest difference

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    And if you put the ampersand back in front of t2, does it work?

  12. #12
    Registered User
    Join Date
    Nov 2008
    Posts
    8
    aren't the simplest things those that make life so wonderful ? ^^
    works perfectly now, thanks and all hail to tabstop ^^

    are there any "thankyou" points on this forum ,or you just sustain yourselves on the knowledge that you helped some fellow who really should just read the books again...and again...?
    Last edited by skull_tkl; 11-21-2008 at 02:41 PM.

  13. #13
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Hail tabstop! *bows*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How do I validate these inputs?
    By Kyeong in forum C Programming
    Replies: 1
    Last Post: 10-19-2008, 02:20 PM
  2. checking inputs meets multiple conditions
    By 60beetle60 in forum C Programming
    Replies: 5
    Last Post: 04-19-2008, 08:25 AM
  3. I need help with validation for two inputs
    By KarrieWojo in forum C Programming
    Replies: 14
    Last Post: 12-20-2007, 10:55 AM
  4. Taking inputs at Command Line
    By Stephenf in forum C++ Programming
    Replies: 1
    Last Post: 09-28-2005, 02:33 AM
  5. Getting multiple inputs using cin
    By edk in forum C++ Programming
    Replies: 2
    Last Post: 09-13-2001, 02:34 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21