Can you merge arrays?

This is a discussion on Can you merge arrays? within the C Programming forums, part of the General Programming Boards category; i have a file that contains one thousand numbers 73167176531330624919225119674426574742355349194934 96983520312774506326239578318016984801869478851843 85861560789112949495459501737958331952853208805511 12540698747158523863050715693290963295227443043557 66896648950445244523161731856403098711121722383113 62229893423380308135336276614282806444486645238749 30358907296290491560440772390713810515859307960866 70172427121883998797908792274921901699720888093776 65727333001053367881220235421809751254540594752243 52584907711670556013604839586446706324415722155397 53697817977846174064955149290862569321978468622482 ...

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    17

    Can you merge arrays?

    i have a file that contains one thousand numbers
    73167176531330624919225119674426574742355349194934
    96983520312774506326239578318016984801869478851843
    85861560789112949495459501737958331952853208805511
    12540698747158523863050715693290963295227443043557
    66896648950445244523161731856403098711121722383113
    62229893423380308135336276614282806444486645238749
    30358907296290491560440772390713810515859307960866
    70172427121883998797908792274921901699720888093776
    65727333001053367881220235421809751254540594752243
    52584907711670556013604839586446706324415722155397
    53697817977846174064955149290862569321978468622482
    83972241375657056057490261407972968652414535100474
    82166370484403199890008895243450658541227588666881
    16427171479924442928230863465674813919123162824586
    17866458359124566529476545682848912883142607690042
    24219022671055626321111109370544217506941658960408
    07198403850962455444362981230987879927244284909188
    84580156166097919133875499200524063689912560717606
    05886116467109405077541002256983155200055935729725
    71636269561882670428252483600823257530420752963450

    i'm scanning the file using fscanf and inserting each line into a char array in which it will be easier for me to manipulate later on. however, if do fscanf again it will overwrite the array.

    so i was thinking that if i have each line inserted into an array and combines all those arrays into one big array, it would be easier for me to complete the task.

    is there anything like that?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    That would be called "a two-dimensional array".

  3. #3
    Registered User
    Join Date
    Aug 2009
    Posts
    17
    i know i could use a two dimensional array but if i could somehow combine it all into one big array it would be so much easier for me.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    A two-dimensional array is one (big) array, yes.

  5. #5
    Registered User
    Join Date
    Aug 2009
    Posts
    17
    i'm trying to Find the greatest product of five consecutive digits in the 1000-digit number. so i'll be looking at each index and doing this with a 1D array is easier. I'll use the 2D array if that's my last resort and I don't want to simply change the file so it'll be one long line that contains 1,000 digits. I was thinking that it was possible to fscanf one line of file into an array and fscanf the file again so it'll be in the same array for example
    i do fscanf in a file that contains
    684681
    123456
    .
    what i want to do is fscanf it, so the array will contain 684681 and fscanf it again so the new array will become 684681123456.
    is this possible? if not i guess ill go with the 2D array.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    If you're trying to read into a character array, then you're going to have difficulties with the \0 characters that are always there. But fortunately you can overwrite them:
    Code:
    char Bigarray[1000];
    for (i = 0; i < 1000; i += 100) {
        scanf("%s", Bigarray+i);
    }
    (Edit: This assumes that you know how many digits are on each line. If not, you'll want to use the more fancy parts of scanf, like %n.)

  7. #7
    Registered User
    Join Date
    Aug 2009
    Posts
    17
    yea, i got that down but is there no way to combine multiple 1D arrays into one giant 1D array? i could do it manually with a for loop but maybe i'm just trying to find an easy way out. guess i'll have to make a 2D array and go from there.

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Have you considered using mem copy or mem move?

    I haven't used mem copy or move for array's before, but I see no reason not to. Each is just a contiguous block of memory, so it should be no problem.

    If you need to delete certain char's or digits though, then a for loop would seem better.

    There is no need to make a 2D array for this. (and I wouldn't).

  9. #9
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Just don't use scanf. It is simple

    Code:
    char c;
    int i = 0;
    char array[1000];
    while ((c = getc()) != EOF)
      if (c != '\n') array[i++] =c;

  10. #10
    Registered User
    Join Date
    Aug 2009
    Posts
    17
    wow i'm so stupid, i completely forgot u could get a single char at a time. i was thinking you could only read one line at a time!! i completely forgot !!! argh!!! i feel so stupid!!!!

  11. #11
    Registered User
    Join Date
    Apr 2009
    Posts
    187
    also if its very big memory though and its bigger than what you expect you can just declare the variable for holding on the heap and allocate it and if its more then realloc and free in the end

  12. #12
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    You're not thinking about the clever way to solve the problem. You only need to store 5 characters at once in memory.
    As you read in a char, you place it in a circular buffer. You then update a running product that is divided by the number coming out and multiplied by the number going into the buffer. If this product exceeds your current maximum product, update the maximum.

    E.g. your buffer may initially hold 31415, and your running product is therefore 3x1x4x1x5 = 60.

    Your next digit is say 9, and the circular buffer head is at position zero. So you take your 60, divide it by 3, giving 20, the multiply that by 9 giving 180 and replace the number in the buffer, giving 91415. You replace your max total with the 180. You'll notice that 9x1x4x1x5 correctly matches the updated running product but that you don't have to multiply all 5 numbers together each time.

    Next number is say 2 and your circular buffer head is at position 1. You divide the 180 by 1 and multiply it by 2, giving 360. The buffer now holds 92415 and your max is 360
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    fgets + strcat = merged arrays.


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

  14. #14
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    The important thing to undesrtand here though is that all arrays in C is one big array. If you point a pointer to the beginning of a 2D array then you can use it as a 1D array

  15. #15
    Registered User
    Join Date
    Aug 2009
    Posts
    17
    Quote Originally Posted by iMalc View Post
    You're not thinking about the clever way to solve the problem. You only need to store 5 characters at once in memory.
    As you read in a char, you place it in a circular buffer. You then update a running product that is divided by the number coming out and multiplied by the number going into the buffer. If this product exceeds your current maximum product, update the maximum.

    E.g. your buffer may initially hold 31415, and your running product is therefore 3x1x4x1x5 = 60.

    Your next digit is say 9, and the circular buffer head is at position zero. So you take your 60, divide it by 3, giving 20, the multiply that by 9 giving 180 and replace the number in the buffer, giving 91415. You replace your max total with the 180. You'll notice that 9x1x4x1x5 correctly matches the updated running product but that you don't have to multiply all 5 numbers together each time.

    Next number is say 2 and your circular buffer head is at position 1. You divide the 180 by 1 and multiply it by 2, giving 360. The buffer now holds 92415 and your max is 360
    AHHH that's so true!!! When I first thought u could only get one line at a time, I thought i would have to break up each line but with fgetc it gets the job done. yea my way was pretty bleh... Your way is so elegant and so damn efficient compared to what I was trying to do. Thanks man.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to merge two arrays recursevly..
    By transgalactic2 in forum C Programming
    Replies: 117
    Last Post: 01-11-2009, 03:47 PM
  2. Need Help With 3 Parallel Arrays Selction Sort
    By slickwilly440 in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2005, 09:47 PM
  3. Do you know...
    By davejigsaw in forum C++ Programming
    Replies: 1
    Last Post: 05-10-2005, 10:33 AM
  4. How to merge 2 arrays?
    By planet_abhi in forum C Programming
    Replies: 3
    Last Post: 02-15-2003, 11:23 PM
  5. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 08:24 PM

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