Thread: beginner problems with array elements...

  1. #1
    grnphrog
    Guest

    beginner problems with array elements...

    hey all... new programmer here....
    i am supposed to write a program that takes a number as a single variable and do a math equation with EACH number...

    example...
    int number[4]
    int main(void)
    {
    printf("enter number;
    scanf("%i", &number);
    }

    ok, no problem....
    here is the problem....

    if i enter the number 1234,
    i have to take the first digit "1" and add 7 to it...
    then i have to take the second digit and do the same this,
    and so on to the fourth digit....

    just to make sure i am getting the correct values to do my equation with i try doing the following....

    printf("%i\n", number[0];
    printf("%i\n", number[1];
    printf("%i\n", number[2];
    printf("%i\n", number[3];

    i expected to get the following result:

    1
    2
    3
    4

    instead i get....

    1234
    0
    0
    0

    when i print out the value it gives me the whole array instead of just the element....

    i need serious help...
    thanks to those who take mercy on me...
    sean

  2. #2
    Registered User
    Join Date
    Sep 2002
    Posts
    1,640
    Use Code Tags, And post the entire code plz.

  3. #3
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    You get that result because you have only one number, namely number[0] entered. To enter all 4 numbers, you should ask all those numbers.

    Like:

    Code:
    #include <stdio.h>
    
    int main ()
    {
        int number[4];
    
        printf("enter number");
        scanf("%d", &number[0]);
        printf("enter number");
        scanf("%d", &number[1]);
        printf("enter number");
        scanf("%d", &number[2]);
        printf("enter number");
        scanf("%d", &number[3]);
    	
        printf ("%d %d %d %d\n", number [0], number [1], number [2], number [3]);
    	
        return 0;
    }
    To perform an operation on each element of the array, you could use a loop, like this:

    Code:
    int i;
    int number [4];
    
    for (i = 0; i < 4; i++)
    {
        number [i]  = number [i] + 1;
    }

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    scanf does not just read one digit with a "%i" read. Perhaps you should use %c, or a width specifier. I'd just do something like:
    Code:
    x = getchar( );
    if ( isdigit( x ) )
        number[counter] = x - '0';
    Toss that into a loop or whatever...

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

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Good old scanf() will take the input 1234 as one number, as you've seen. If you want to process them one at a time, I suggest using fgets() to get a string and work with that instead.


    [edit]Darn, beat. I guess quzah ain't on the phone atm.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Shifting elements in an Array
    By mmarab in forum C Programming
    Replies: 5
    Last Post: 12-10-2007, 12:11 PM
  2. count elements in an array
    By whichet in forum C Programming
    Replies: 9
    Last Post: 11-25-2007, 08:05 AM
  3. Array problems
    By lzhaol in forum C++ Programming
    Replies: 6
    Last Post: 02-26-2006, 02:52 PM
  4. Template Array Class
    By hpy_gilmore8 in forum C++ Programming
    Replies: 15
    Last Post: 04-11-2004, 11:15 PM
  5. Array elements values not being assigned
    By Sardien in forum C Programming
    Replies: 4
    Last Post: 02-11-2002, 01:45 PM