Thread: how do we transfer arrays content

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    42

    how do we transfer arrays content

    Hi,
    I have an array that have a string of nine number in it. I would like to know the code that would allow me to take for example the two first and two last number of this string and put them in a new array. Then I would use strcpy to have 2 array (with the 2 first and 2 last number). Finally, I would like to add them together and add each number together.

    Code:
     987654321 --> is my initial array
     9  8  2 1 --> my four number that I took from my first array
     9  8  2 1 --> the exact copy of those four number
    18 16  4 2 --> the addition of those numbers (grouped in the array)
    1+8+1+6+4+2 = 22 --> 22 is the result I seek
    thanks, It's really great having some help when we need it
    Last edited by nevrax; 03-26-2007 at 02:00 PM.

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Are you sure you're dealing with strings? Is your array of type int or char?

    You can always transfer one array to another with a simply loop, strcpy() for strings, or memcpy() for other types.
    Last edited by MacGyver; 03-26-2007 at 02:01 PM.

  3. #3
    Registered User
    Join Date
    Mar 2007
    Posts
    42
    Yes, I am dealing with string and I didn't decided if I should make the content int or char. I'm really new in this and I'm learning by myself most of the time so if you have any suggestion, don't hesitate. What I want is to enter nine number 987654321 for example and do some operation on them with string.
    Last edited by nevrax; 03-26-2007 at 02:05 PM.

  4. #4
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Strings in C are just char arrays that have a null terminator char (ie. '\0'). If you just want an array of numbers, then use int arrays.

    If you just want the first two numbers and last two numbers put into another array, you could make have a separate array of size 4, and just copy the elements out manually.

    Code:
    copiedarray[0] = originalarray[0];
    copiedarray[1] = originalarray[2];
    copiedarray[2] = originalarray[origsize-2];
    copiedarray[3] = originalarray[origsize-1];
    You would have to have the variable origsize defined as an int and set to the size of originalarray.

  5. #5
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    I would strongly suggest you read an array tutorial.

    If you have an array named x, to add the first element with the second one and store it in a variable called result, you would do something like this:

    Code:
    result = x[0] + x[1];
    Just adapt it to whatever you're doing.

  6. #6
    Lean Mean Coding Machine KONI's Avatar
    Join Date
    Mar 2007
    Location
    Luxembourg, Europe
    Posts
    444
    I'm gonna explain a little more in detail since I already saw your other thread and you seem motivated but you're lacking the basics. As you already know, C has several "types" for variables, each of which serve a very specific purpose. A number is generally of type "int", which is the short form for integer. An integer is used for mathematical operations (in its pure form, there are other possibilities for it too) and you can use every imaginable mathematical operation on it.

    Here's an example:
    Code:
    int number1 = 243;
    int number2;
    
    number2 = number1 + 156;
    number1 = number2 * 2;
    An array in C is a continuous memory space that hold several items of the same type. You can access the elements seperately by specifying its index within the array.

    In the following I'll define an array of integers and do various operations with it:

    Code:
    int array[3] = {100, 3, 25};
    array[0] = array[0] + array[1];
    array[2] = array[2] / 5;
    Now you need to understand that the possibility to access different elements only works in arrays, it isn't possible to access a specific decimal place within a number like that. If you want to access a decimal place in an integer, you first need to convert it into an array of characters (a string):

    Code:
    int i = 987654321;
    char buf[512];
    snprintf(buf, sizeof(buf), "%d", i);
    with this code you're actually printing the number into a string, which is an array of characters. Now you are able to access each decimal place just like in the example above.

    The first digit "9" would be inside buf[0] etc...

    Now, unfortunately there's another problem, namely that the digits are converted to their ASCII value and since the ascii code for 9 is something different than 9, you can't apply the mathematical operations on them just like that.

    You have chosen by pure chance a very difficult task which requires quite a bit of understanding

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need Help With 3 Parallel Arrays Selction Sort
    By slickwilly440 in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2005, 10:47 PM
  2. Header File Question(s)
    By AQWst in forum C++ Programming
    Replies: 10
    Last Post: 12-23-2004, 11:31 PM
  3. Do I have a scanf problem?
    By AQWst in forum C Programming
    Replies: 2
    Last Post: 11-26-2004, 06:18 PM
  4. Creation of Menu problem
    By AQWst in forum C Programming
    Replies: 8
    Last Post: 11-24-2004, 09:44 PM
  5. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 09:24 PM