C Massive loop

Instead of iterating over each element of the array, is it possible to quote only elements that have assignments?

In the following example, I would like to skip only three elements, not a loop through each element of the array. What are my options? I hate looping through thousands of elements when only a small one is assigned based on a specific logic.

main() { int i, intArray[10000]; intArray[334] = 30; intArray[563] = 50; intArray[989] = 90; for (i = 0; i < 10000; i++) { printf("%d\n", intArray[i]); } } 

Thanks for reading the post. Sorry if this is a re-recording. I would not find a similar question on the forum.

+4
source share
3 answers

Only indirectly:

 #include <stdio.h> int main(void) { int i, intArray[10000]; int active[10000]; int n_active = 0; intArray[334] = 30; active[n_active++] = 334; intArray[563] = 50; active[n_active++] = 563; intArray[989] = 90; active[n_active++] = 989; for (i = 0; i < n_active; i++) printf("%d\n", intArray[active[i]]); return 0; } 

Or, more succinctly, but not more clearly:

 #include <stdio.h> int main(void) { int i, intArray[10000]; int active[10000]; int n_active = 0; intArray[active[n_active++]=334] = 30; intArray[active[n_active++]=563] = 50; intArray[active[n_active++]=989] = 90; for (i = 0; i < n_active; i++) printf("%d\n", intArray[active[i]]); return 0; } 

Both of these programs will suffer if there are several assignments to the same index (this index will be stored in the active array twice). Be that as it may, it also does not check the overflow of the active array (but this should not be a problem, the hypothesis is that only a few lines are filled), and the indexes are stored in the order that they are presented - not in key order. All these defects can be fixed, but take more code (this should probably be a function or two).

+6
source

You can do something like this

 # include <stdio.h> int totalElements = 0; struct { int index, data; } Data[10000]; void addElement(int index, int data) { Data[totalElements].data = data; Data[totalElements++].index = index; } main() { int i; addElement(334, 30); addElement(563, 50); addElement(989, 90); for (i = 0; i < totalElements; i++) { printf("%d %d\n", Data[i].data, Data[i].index); } } 

Exit

 30 334 50 563 90 989 

It also suffers from the same limitations that Jonathan Leffler mentioned.

EDIT

 # include <stdio.h> int totalElements = 0; struct { int index, currentElement = 0, data[100]; } Data[10000]; void addElement(int index, int data) { int i; for (i = 0; i < totalElements; i++) { if (Data[i].index == index) { Data[i].data[Data[i].currentElement++] = data; return; } } Data[totalElements].data[Data[totalElements].currentElement++] = data; Data[totalElements++].index = index; } main() { int i, j; addElement(334, 30); addElement(334, 40); addElement(563, 50); addElement(563, 60); addElement(989, 80); addElement(989, 90); for (i = 0; i < totalElements; i++) { for (j = 0; j < Data[i].currentElement; j++) { printf("%d %d\n", Data[i].index, Data[i].data[j]); } } } 

Exit

 334 30 334 40 563 50 563 60 989 80 989 90 

Using this idea, you can overcome the limitations mentioned by Jonathan Leffler.

+1
source

Perhaps you could use a different data structure, such as a linked list. Each node in the list can have two int values, one can be an index, and the other can be a value. The linked list will only contain pointers that have been assigned (and you could also have value == 0 if it is somehow different from a normal, unassigned index).

Another alternative would be to use something like a dictionary structure. There are probably vocabulary implementations for C - I would say that if C ++ is available, perhaps you should use it instead (unless you are specifically trying to learn or are limited to C) - C ++ has many data types available directly from boxes.

0
source

Source: https://habr.com/ru/post/1501037/


All Articles