THIS IS IN C CODE!! You need to develop an algorithm to convert the provided val
ID: 3602585 • Letter: T
Question
THIS IS IN C CODE!!
You need to develop an algorithm to convert the provided value to a
32-bit value that would display correctly on the seven segment display. You are required to only return
the necessary information without displaying leading zeros. For example, if value = 5, then the function would return the seven-segment conversion that would display 5 and not 05.
/* The function below takes two arguments: value which is the value in decimal
* to be displayed by the seven-segment displays and a character array that have the encoding
* to display the provided value in decimal on the seven-segment displays. The provided value
* can range from 0 - 999. The seven-segment displays must output the entire range correctly.
*/
unsigned char table[10] = { 0x3f, 0x06, 0x5b, 0x2F, 0xff, 0x7f, 0xff, 0x7f, 0xff, 0x7f}; -- these values map to a 7segment hex display. the values are 0,1,2,3,4,5,6,7,8,9. So when you give the function below a number, for example 11, you would just need to return "0x0606" which is two 1's in the above array.
TO DO:
*****************************************************************
int HEX3to0_display (int value, unsigned char * table)
{
/* YOU NEED TO COMPLETE THIS CONVERSION FUNCTION. */
}
**********************************************************
Explanation / Answer
// --------- CODE WITH TEST CASE --------//
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/*
Algorithm :
Convert input number to string
switch over each element in input string.
Find appropriate hex code in the table, convert the hex value to string and concatenate to the output string
Return output string in converted format
*/
int HEX3to0_display (int value, unsigned char * table)
{
int i;
//
char in_buff[4], out_buff[6];
//
char temp[10];
//
// clear the buffers before storage
memset(in_buff, '', sizeof(in_buff));
//
memset(out_buff, '', sizeof(out_buff));
//
// this holds converted values from hex/integers to char*
memset(temp, '', sizeof(temp));
//
// convert the integer to char array
sprintf(in_buff, "%d", value);
//
// loop over each character in the converted input string
for(i = 0; i < strlen(in_buff); i++)
{
//
switch(in_buff[i])
{
//
case '1':
//
memset(temp, '', sizeof(temp));
//
// for 1 in input number, corresponding hex value is at table[1]
// get corresponding hex code from the table and store the converted string to out_buff
itoa(table[1], temp, 16);
//
strcat(out_buff, temp);
//
break;
//
case '2':
//
memset(temp, '', sizeof(temp));
//
itoa(table[2], temp, 16);
//
strcat(out_buff, temp);
//
break;
//
case '3':
//
memset(temp, '', sizeof(temp));
//
itoa(table[3], temp, 16);
//
strcat(out_buff, temp);
//
break;
//
case '4':
//
memset(temp, '', sizeof(temp));
//
itoa(table[4], temp, 16);
//
strcat(out_buff, temp);
//
break;
//
case '5':
//
memset(temp, '', sizeof(temp));
//
itoa(table[5], temp, 16);
//
strcat(out_buff, temp);
//
break;
//
case '6':
//
memset(temp, '', sizeof(temp));
//
itoa(table[6], temp, 16);
//
strcat(out_buff, temp);
//
break;
//
case '7':
//
memset(temp, '', sizeof(temp));
//
itoa(table[7], temp, 16);
//
strcat(out_buff, temp);
//
break;
//
case '8':
//
memset(temp, '', sizeof(temp));
//
itoa(table[8], temp, 16);
//
strcat(out_buff, temp);
//
break;
//
case '9':
//
memset(temp, '', sizeof(temp));
//
itoa(table[9], temp, 16);
//
strcat(out_buff, temp);
//
break;
//
case '0':
//
memset(temp, '', sizeof(temp));
//
itoa(table[0], temp, 16);
//
strcat(out_buff, temp);
//
break;
}
}
//
return (int)strtol(out_buff, NULL, 16); // convert the output string to integer
}
//
int main()
{
//
unsigned char table[10] = { 0x3f, 0x06, 0x5b, 0x2F, 0xff, 0x7f, 0xff, 0x7f, 0xff, 0x7f};
//
int code, val = 55;
//
code = HEX3to0_display(val, table);
//
printf("Input : %d, Generated Code : %d, in hex : %x", val, code, code);
//
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.