0

I need to know, how to save integers from stdin into array, given by first integer in line... Ehm... hope you understand. I will give you an example.

On stdin I have:

0 : [ 1, 2, 3 ]
5 : [ 10, 11, 12, 13]
6 : [ 2, 4, 9 ]
0 : [ 4, 9, 8 ]
5 : [ 9, 6, 7 ]
5 : [ 1 ]

And I need save these integers to the arrays like this:

0={1, 2, 3, 4, 9, 8}
5={10, 11, 12, 13, 9, 6, 7, 1}
6={2, 4, 9}

I absolutely don't how to do it. There is a problem, that the number of arrays(in this case - 0, 5, 6 - so 3 arrays ) can be very high and I need to work effectively with memory...So I guess i will need something like malloc and free to solve this problem, or am I wrong? The names of arrays (0, 5, 6) can be changed. Number of integers in brackets has no maximum limit.

Thank you for any help.

5
  • 1
    Yes, use malloc. Go ahead and try it. Commented Nov 27, 2016 at 19:38
  • At least try something, just get started and then upload your code if you have a problem. Commented Nov 27, 2016 at 19:41
  • fgets to read each line. Then some sort of parsing code to determine the group number, and the items. strtok and strtol could be used for the parser, but a simple for loop might be the same amount of code. For memory management, you want malloc and realloc. The latter is what allows your arrays to grow. Finally, free at the end. Commented Nov 27, 2016 at 19:42
  • suggest getting acquainted with the `realloc() function. That way, when additional space is needid in an array, it is easy to re-size the array so it has room for the additional entries. Commented Nov 29, 2016 at 11:56
  • You will need to keep track of the names of the arrays and the number of entries in each array. Commented Nov 29, 2016 at 11:57

2 Answers 2

1

I go with the assumption, this is homework, and I go with the assumption, this isn't your first homework to do, so I won't present you a solution but instead some tips that would help you to solve it yourself.

Given the input line

5 : [ 10, 11, 12, 13]

I will call "5" the "array name" and 10, 11, 12 and 13 the values to add.

  1. You should implement some system to map array names to indices. A trivial approach would be like this:

.

size_t num_arrays;
size_t * array_names;

Here, in your example input, num_arrays will end up being 3 with array_names[3] = { 0, 5, 6}. If you find a new array name, realloc and add the new array name. Also you need the actual arrays for the values:

int * * array;

you need to realloc array for each new array name (like you realloc array_names). array[0] will represent array array_names[0] here array 0, array[1] will represent array array_names[1] here array 5 and array[2] will represent array array_names[2] here array 6.

To access an array, find it's index like so:

size_t index;
for (size_t index = 0; index < num_arrays && array_names[index] != search; ++index) ;
  1. The second step is easy. Once you figured out, you need to use array[index] to add elemens, realloc that one (array[index] = realloc(array[index], new size)) and add elements there array[index][i+old_size] = new_value[i].

Obviously, you need to keep track of the number of elements in your separate arrays as well ;)

Hint: If searching for the array names take too long, you will have to replace that trivial mapping part by some more sophisticated data structure, like a hash map or a binary search tree. The rest of the concept may stay more or less the same.

Should you have problems to parse the input lines, I suggest, you open a new question specific on this parsing part.

Sign up to request clarification or add additional context in comments.

3 Comments

Ok, thanks for advices... I decided to save all my integers into 2D array and my program worked well. But only with static 2D array, so I have problem with memory... I tried to implement dynamic allocation with malloc like this: int **array1 array1 = malloc(col * sizeof(int*)); for (i = 0; i < col; i++) { array1[i] =malloc(lines* sizeof(int)); } where the "col" is umber of columns I need and "lines" is number of lines I need. But when i need to realloc (resize) this 2D array, i still have SIGSEV error. for realloc I Use this code:
array1= (int **) realloc(array1, (maxCol+1) * sizeof(int*)); array1[i] =(int*)malloc(lines * sizeof(int)); where maxCol is new number of columns I need and lines is number of lines I need... I tried to find something here, on stackoverflow, but I still have that error :/ Should I create new thread with this question?
yes, please post a new question. Giving this answer in comments is a little bit tedious. I suggest a title like »How to resize a multi dimensional array«. And then provide full source so we can give specific advise. Also, please accept this answer if it did help you.
0

In algorithmic terms, you need map (associative array) from ints to arrays. This is solved long ago in most higher level languages.

If you have to implement it manually, you have a few options:

  • simple "master" array where you store your 0, 5, 6, 1000000 and then map them to indices 0, 1, 2, 3 by doing search in for each time you have to access it (it's too time consuming when ;
  • hash table: write simple hash function to map 0, 5, 6, 1000000 (they're called keys) to values less than 1000, allocate array of 1000 elements and then make "master" array structures for each hash function result;
  • some kind of tree (e.g. red-black tree), may be a bit complex to implement manually.

Last two structures are part of programming classic and are well described in various articles and books.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.