-2

I have a keypad that I use to enter digits into a microcontroller (atmega32 avr). The user will be able to enter 1, 2 or 3 digits, because I defined the input range will be [0-999]. Each of those digits will be stored in a position of an array of integers.

So, if my array is defined as int d[3] and the user enters, in sequence, 2, 6, 9, I'll have d[0] == 2, d[1] == 6 and d[2] == 9. Now I want to make an integer from those digits {2, 6, 9}: value == 269. How can I do that?

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
Aashkanpasha
  • 39
  • 1
  • 7
  • C is not C++. Please choose the appropriate tag for the code you are writing and [edit] your Q. Also format it using markdown. – D. Ben Knoble Aug 03 '19 at 12:30
  • Thanks. Because number can be between 0-999 it doesnt work for all. If operator enter 3 and 8 only. Then we have y=380 not 38 by this way. Am i right?? – Aashkanpasha Aug 03 '19 at 12:33

3 Answers3

1

The following answer should be applicable for any language. This would be fairly simple, I would advise you to format the values in your array a little differently. I think it would be easier if the operator enters a single-digit number store it like so: {0, 0, 5}. If it was a double-digit number store it like so: {0, 5, 9}. Finally, if the operator enters a three-digit number store it like so: {5, 9, 2}.

Finally after you format the array you can use the following logic to get the number. For example if the array is arr = {5, 9, 8}; then you could do the following to get it as a number:

arr[0] * 100 + arr[1] * 10 + arr[2]

That would work if you follow the formatting advice I gave above.

  • Thanks dear. You mean operator enter 0 0 5 instead if 5??? I know it makes problem easy but this is why boss eployee me only and i couldnt say i cant do that lets enter 3 digits. – Aashkanpasha Aug 03 '19 at 12:55
  • You could do that, but for a better user experience, you will want the user to enter just 5, and then in your code pad it with zeroes when entering it into the array – Abhinav Vemulapalli Aug 03 '19 at 12:59
  • Does that make sense Aashkanpasha? – Abhinav Vemulapalli Aug 03 '19 at 13:06
  • I think my error is another where. Keypad () scans my keypad.....t=keypad(); ...... so when i want to l[i]=t is wrong I want to make a aray by my int numbers t. What should i do? – Aashkanpasha Aug 03 '19 at 14:37
  • I'm sorry I'm not familiar with the exact methods and such so you'll have to consult the documentation of what you are using. You can also ask another question regarding this. If I answered your original question about how to make an integer number from array elements please approve the answer. Hope this helps and sorry I couldn't be of more help! – Abhinav Vemulapalli Aug 03 '19 at 14:39
  • @Aashkanpasha, please provide code about Keypad() or start a new question. We can't help you if you don't show any code – abhivemp Aug 03 '19 at 15:37
  • @AbhinavVemulapalli answers your question. Your error is a little specific. Please elaborate – abhivemp Aug 03 '19 at 15:37
  • @Aashkanpasha: the basic algorithm if you want to write a loop is `total = total*10 + digit`. Then you can stop at the first non-digit. If you have an array of `int` already, you can't tell the difference between `1` and `100` unless you use some sentinel value. – Peter Cordes Aug 08 '19 at 01:44
0

You can create the number iteratively by multiplying the digit with its corresponding unit's power.

Example: {2, 6, 9} can be expressed as ((2 * 10^2) + (6 * 10^1) + (9 * 10^0))

#include <stdio.h>
#include <stdlib.h>


long power_10 (int exp)
{
    long res = 1;
    int  i;  

    for (i = 1; i <= exp; i++) {
        res = res * 10; 
    }   
    return res;
}

int main()
{
    int   arr[] = {2, 6, 9}; 
    int   i = 0;
    int   len = sizeof(arr)/sizeof(arr[0]);
    long  num = 0;

    for (i = 0; i < len; i++) {
        num += arr[i] * power_10((len - 1 - i));    
    }   
    printf("%ld\n", num);
}
Sumit Trehan
  • 3,985
  • 3
  • 27
  • 42
0

You have two problems to solve, right?

  • Problem #1: detect when the input is completed
  • Problem #2: convert the array of digits into a single integer value

I can see two ways of solving problem #1:

  1. Finish the input with a sentinel. - Does your keypad have something like an Enter Key?
  2. Finish the input with a timeout. - Start a timer after each reading. An overflow can indicate that user finished input.

In both cases you can count how many digits the user informed before the sentinel/timeout.


Now we just have to solve problem #2:

If you stored your digits in the array starting at index zero, from the most significant digit to the least significant digit, then the solution will be something like this:

// note: n_digits and d come from the solution of the first problem

int sum = 0;
for (int i = 0; i < n_digits; ++i) {
    sum = sum*10 + d[i];
}

// do whatever you need with the result
printf("%d\n", sum);
rrd
  • 175
  • 7