# Thread: beginner problems with array elements...

1. ## 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

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. Use Code Tags, And post the entire code plz.

3. 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. 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.

5. 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.

Darn, beat. I guess quzah ain't on the phone atm.