Thread: Array problem

  1. #1
    KDE Fanboy
    Join Date
    Aug 2005
    Location
    Toowoomba, Qld, AU
    Posts
    2

    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. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Damn mixed drinks...

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

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote 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!"
    Quote 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.
    Hope is the first step on the road to disappointment.

  5. #5
    KDE Fanboy
    Join Date
    Aug 2005
    Location
    Toowoomba, Qld, AU
    Posts
    2
    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. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array problem
    By TomBoyRacer in forum C++ Programming
    Replies: 3
    Last Post: 04-08-2007, 11:35 AM
  2. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  3. Replies: 6
    Last Post: 02-15-2005, 11:20 PM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Need desperate help with two dimensional array problem
    By webvigator2k in forum C++ Programming
    Replies: 4
    Last Post: 05-10-2003, 02:28 PM