Counting from 0 to 99

In C ++, which of the following solutions is more reliable and reliable for counting from 0 to 99 and storing each iteration in variables for dozens and some places? And how can any method be improved to make it as fast as possible and not resource intensive?

typedef int (*IntFunction) (int* _SegmentList);

int display1SegmentPinNums[] = {...pin numbers...};
int display2SegmentPinNums[] = {...other pin numbers...};

// Then I have some functions that display a number to 7-segment displays. They each return an integer 1 and have a parameter of (int* _SegmentList), as per the type definition above

// An array of all the functions
IntFunction displayFunctions[10] = {display_0, display_1, display_2, display_3, display_4, display_5, display_6, display_7, display_8, display_9};

// Solution 1
for (int tens = 0; tens < 10; tens++)
{
    for (int ones = 0; ones < 10; ones++)
    {
        displayFunctions[tens](display1SegmentPinNums);
        displayFunctions[ones](display2SegmentPinNums);
    }
}

// Solution 2
for (int i = 0; i < 100; i++)
{
    ones = (i % 10);
    tens = ((i - ones) / 10);

    displayFunctions[tens](display1SegmentPinNums);
    displayFunctions[ones](display2SegmentPinNums);
}

Edit: I have included a slightly simplified version of my complete code. Hope this helps a better answer. This is for the Arduino BTW project, with 7-segment displays and an attempt to make a stopwatch.

+4
source share
4 answers

, tens ones 9 , , .

, , :

  • 1:11 , 121 , 110 , 200
  • 2: 1 , 101 , 100 , 200 , 200 (modulo ), 100

:

  • , 1 .
  • , , ​​ , .., . , , , .

:

( ..), , , . , , , 1 2 .

+5

1 , , 2 .

, 1 , . .

+2

: , , - . , , .

: , , , , , unit test. - , , ( , -).

, , , , arduino. , , , . , , , . , , .

, . , .

+1

, , , , ( ).

, 3 , 4 .

j ( ), 4.

, "" "", .

, , , , !

0

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


All Articles