Copying an array of ints to a float, and vis versa

This is a discussion on Copying an array of ints to a float, and vis versa within the C Programming forums, part of the General Programming Boards category; Hi, I'm having a little trouble trying to figure this out, mostly because I don't really understand bitwise operators. Let's ...

  1. #1
    kpw
    kpw is offline
    Registered User
    Join Date
    Aug 2001
    Posts
    9

    Copying an array of ints to a float, and vis versa

    Hi, I'm having a little trouble trying to figure this out, mostly because I don't really understand bitwise operators.

    Let's say we have a integer array num with 10 elements filled with single digit numbers:
    num[10]

    num[0]: 1 num[6]: 9
    num[1]: 2 num[7]: 3
    num[2]: 6 num[8]: 0
    num[3]: 7 num[9]: 1
    num[4]: 0
    num[5]: 4

    So the number would be 1267049301.

    If I had two of these types of arrays, I need to do arithmetic on them, like multiplying, addition, subtraction, ect. The only way I can see doing this is to make them floats, then do the arithmetic, and then change that number back into that array of integers. Now my question is, how would I do this? And if there's a easier way of doing this, I'd like to know that too.

    Thanks for any help.

  2. #2
    Registered User jasrajva's Avatar
    Join Date
    Oct 2001
    Posts
    99
    why do want to store the num in an int array you may as well store it in a long int and add subtract etc.

    if however yr problem is to store and manipulate the number in an array
    then you can
    a) write a fn to add two arrays by adding two corresponding digits and if a carry is generated then carry it to the next number etc
    once this is done multiplication shouldnt be too difficult

    b) you can run a loop through the array
    and multiply the place value with the digit and add the result to another variable that is of the type long int or float the manipulate it and
    run another while loop dividing the result by succesive powers of 10 and putting the remainder back into the array at each stage

  3. #3
    kpw
    kpw is offline
    Registered User
    Join Date
    Aug 2001
    Posts
    9
    Thanks for the response jv. I'm still a little confused though, could you maybe write out some pseudo or real code example of what you were saying. I'd really appreciate it. Thanks.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    What they're getting at is this:

    int array1[5] = { 1,2,3,4,5 };
    int array2[5] = { 1,2,3,4,5 };
    int array3[5] = { 0 };

    You cannot just add these like you would:

    array 3 = array1 + array 2 //not valid

    If these were single integers:

    int a1 = 12345, a2 = 12345 ,a3 =0;

    a3 = a1+ a2; //valid

    They're saying: if you want to us arrays as you describe above, you'll have to write your own math functions:

    add( int array[], int size1, int array2[], int size2 )
    { go through each element, add them, carry the >10 repeat }

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

  5. #5
    kpw
    kpw is offline
    Registered User
    Join Date
    Aug 2001
    Posts
    9
    Heh, and that's where I'm having trouble. Doing the arithmetic on the integer arrays. Could you show me an example of how I could go about doing this?

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21