input row of integers via scanf into an array...

This is a discussion on input row of integers via scanf into an array... within the C Programming forums, part of the General Programming Boards category; that make sense? How should I go about doing this...? Essentially I want to be able to input 11 single ...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    5

    input row of integers via scanf into an array...

    that make sense? How should I go about doing this...?

    Essentially I want to be able to input 11 single digit numbers (ie from 0-9) and store them in an array. Once I have that I can start looking at the second step, which complicates things by calculating the highest number, then printing the difference between that and the printed number...

    eventually I'd like to be able to input say: 56784321908 and have it see that 9 is the highest value and thus subtract the numbers away from 9 so it prints: 43215678091.

    I'll work on that later, but right now I'm just stumped as to how to input a bunch of numbers via a scanf and put them into an array.
    Code:
    scanf( "%11d", &array[11] ) ;
    /* Initial Idea for 11 digits */
    hoping that if I then made an array:
    Code:
    int array[11]
    that might lead to something... but then got to thinking do I infact want:
    Code:
    scanf( "%1d%1d%1d%1d%1d%1d%1d%1d%1d%1d%1d", &array[11] ) ;
    ? I still can't actually seem to figure out how I get the numbers into the array... do I have to list it out like:
    Code:
    int a[11]
      a[0] = '_' ;
      a[1] = '_' ;
      a[2] = '_' ;
      a[3] = '_' ;
      a[4] = '_' ;
      a[5] = '_' ;
      a[6] = '_' ;
      a[7] = '_' ;
      a[8] = '_' ;
      a[9] = '_' ;
      a[10] = '_' ;
    ? If so what do I but between the ' 's?

    This must be a simple thing: How to input a line of single digits and save them into an array, to later be called. But this is the only idea I have so far, and most tutorials/lecture slides/text books either work with letters, and changing case, or single inputs, not a bunch of them.

    Completely stumped. I don't have a piece of code to post for corrections, I'm just trying to get my head around this idea, so any help, or points in the right direction would be ace.

    Thanks,

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >How should I go about doing this...?
    Keep it simple. scanf requires very strict formatting, but your problem doesn't provide that formatting. So what you need to do is take each digit as a character and add it individually to your array in a loop (after converting it to the integer representation of that character, of course):
    Code:
    for ( i = 0; i < 11; i++ ) {
      int ch = getchar();
    
      if ( !isdigit ( ch ) )
        break;
    
      array[i] = ch - '0';
    }
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how do you input data from a file into an array?
    By jorgejags in forum C Programming
    Replies: 5
    Last Post: 11-03-2008, 01:48 PM
  2. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  3. for loop with sort issues
    By redmondtab in forum C Programming
    Replies: 10
    Last Post: 10-09-2006, 10:36 AM
  4. Passing a 2d array by Reference
    By loko in forum C Programming
    Replies: 8
    Last Post: 07-23-2005, 06:19 AM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM

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