# Thread: Array problem

1. ## Array problem

Hi, my first post and I'm excited to be part of this community,

How would you get the first two Elements in a char array, as well as Uppercase them? Like getting just LI from Linux.
Variable name is char name[MAX_NAME_LENGTH]

And, how can you get the last 4 digits from a 8 number lenght integer? Like 5678 from 12345678?
Variable name is unsigned long int phoneNumber

Thanks,

Matt

2. Do you know how to access any members of an array? If so, it shouldn't be that hard to figure out how to get the first two, should it?
Code:
foo = array[ elementnumber ];
Remember that arrays begin their indexing at 0, and not 1. Thus, if your array size is 10, you have accessable elements 0 - 9.

You could convert the number to a string, and then look at the last four digits. Or, you could use some math operators, such as the modulus operator to in effect mask off the last four digits. Or you could use division. For example, and I'm doing this because it's funner than simply using modulus, you could do this:
Code:
int foo = 12345679;
int bar = foo / 10000;
int baz = bar - foo * 10000;

printf( "baz is %d.\n", baz );
See, I could just show you the easy way, but this is funner. Anyway, that looks about right. If it's off, blame mixed drinks.

Quzah.

3. Damn mixed drinks...

Code:
int baz = bar - foo * 10000;
should be
Code:
int baz = foo - bar * 10000;

4. Originally Posted by bithub
Damn mixed drinks...
Thanks. I'll take out my wrath on this one here.

"Damn you, mixed drink!" *glug glug* "Damn you!"
Originally Posted by Quzah
See, I could just show you the easy way, but this is funner. Anyway, that looks about right. If it's off, blame mixed drinks.
Or maybe that was just part of the "funner" I was making it!

Quzah.

5. Thanks for the help,

I have managed to get the two elements from the array, however how do you put them together?

Also, the integer one is pumping out the wrong numbers, and is way off.

How do you do it with mod, as I have three different numbers to process:
46315531 -> 5531
46315021 -> 5021
46315547 -> 5547

6. You're probably not doing it right then...
Code:
#include<stdio.h>
int main( void )
{
int foo[] = { 1234567, 2345678, 3456789, 0x0 };
int bar, baz, x;

for( x = 0; foo[ x ]; x++ )
{
bar = foo[ x ] / 10000;
baz = foo[ x ] - bar * 10000;
printf("baz = %d\n", baz );
}

for( x = 0; foo[ x ]; x++ )
{
printf("foo[ x ] %% 10000 = %d\n", foo[ x ] % 10000 );
}

return 0;
}
Quzah.

Popular pages Recent additions