Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C Array loop through

Tags:

arrays

c

Instead of looping through each element of an array is it possible to loop through only elements which have assignments?

In the following example I would like to loop through only three elements instead of looping through each element in the array. What are my options ? I hate to loop through thousands of elements when only handful from them are assigned based on certain 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]);
    }
}

Thank you for reading the post. Sorry if it a re-post. I would not find similar question in the forum.

like image 600
Me Unagi Avatar asked Mar 22 '23 12:03

Me Unagi


2 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's more than one assignment to the same index (that index will be stored in the active array twice). As it stands, it also doesn't check for overflow of the active array (but that shouldn't be a problem; the hypothesis is that only a few of the rows are populated), and the indexes are stored in the order that they're presented — not in key order. All these defects can be fixed, but take more code (it would probably need to be a function or two).

like image 155
Jonathan Leffler Avatar answered Apr 06 '23 06:04

Jonathan Leffler


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);
   }
}

Output

30 334
50 563
90 989

This also suffers the same limitations 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]);
      }
   }
}

Output

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

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

like image 23
thefourtheye Avatar answered Apr 06 '23 06:04

thefourtheye