7-segment display with printf and multidimensional array for segments

In KN Kings "C-Programming: A Modern Approach", chapter 10, exercise 7, the task is to convert a digital digit from normal digits to 7-segment digits in ASCII art, for example:

_ _ _ _ _ _ _ _ | _| _| |_| |_ |_ | |_| |_| | | | |_ _| | _| |_| | |_| | |_| 

I got a sequence for each digit, where to turn it on and off

Example:

 int digit_sequence[10][7] = { // A,B,C,D,E,F,G /* 0 */ {1,1,1,1,1,1,0} } 

Where 1 = ON, 0 = OFF

but it's hard for me to work with the function process_digit(int digit, int position) .

It’s hard for me to translate from sequence[10][7] to digits[4][MAX_DIGITS*4]

Can a gracious soul help me?

I read the seven-segment golf challenge, but although I understand this theory, it's still hard for me to convince my brain to do what I want with multiple arrays.


Ignoring the art of ASCII, the question reads:

Write a program that asks the user for a number and then displays the number using characters to simulate the effect of a seven-segment display.

...

Characters other than numbers should be ignored. Write the program so that the maximum number of digits is controlled by a macro called MAX_DIGITS, which has a value of 10. If the number contains more than this number of digits, additional digits are ignored. Tips. Use two external arrays. One of them is the segments [...] array, which stores data representing the correspondence between numbers and segments. Another digits array will be an array of characters with 4 lines (since each segmented digit has 4 characters) and MAX_DIGITS * 4 columns (the numbers are three characters wide, but a space is required between the numbers to read). Write your program in four functions: main , [...]

 void clear_digits_array(void); void process_figit(int digit, int position); void print_digits_array(void); 

clear_digits_array will store empty characters in all elements of the digit array. process_digit save the seven-segment representation of digit at the specified position in the digits array (positions range from 0 to MAX_DIGITS - 1 ). print_digits_array display the lines of an array of numbers, each of which will contain one line [...].

+4
source share
1 answer

The trick is to display where the segment is displayed.

The line number for each segment is the same.

 0: _ _ 1: |_| |_| ... 2: |_| |_| 3: 

However, the columns change to position . Each position is a mini-matrix with a width of 4 characters (3 for segments and 1 for space: '|_| ' ). Therefore, we fix the segment line and sum its column on the “mini-matrix” c (position * 4).

 0123 4567 89AB _ _ _ |_| |_| |_| ... |_| |_| |_| pos0 pos1 pos2 

There is? The code would be something like this:

 void process_digit(int digit, int position){ int i; for(i=0;i<7;i++){ if(segments[digit][i]==1) /* Has digit the i segment? */ switch(i){ case 0: digits[0][1+position*4]='_';break; case 1: digits[1][2+position*4]='|';break; case 2: digits[2][2+position*4]='|';break; case 3: digits[2][1+position*4]='_';break; case 4: digits[2][0+position*4]='|';break; case 5: digits[1][0+position*4]='|';break; case 6: digits[1][1+position*4]='_';break; } } } 

(You can choose between '-' and '_' or perhaps change some lines)

Hope this helps.

+1
source

All Articles