0

I have been asked to write a code that receives a type of data without being specific about what type (example, int, float, double ect..) and its certainly a number. The code is simple, find a number in the array and return it's location in the array. This is the code i wrote:

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int findselectnum(int num, int array[], int size)
{
    int i = 0;
    for (i = 0; i <= size; i++)
    {
        if (num == array[i])
            return i;
    }
    return EOF;
}
int main()
{
    int array[] = { 1, 3, 5, 10, 88, 21, 100, 77, 0, 11, 2 }, num = 0, result = 0, size = 0;
    printf("Enter a number you wish to find\n");
    scanf("%d", &num);
    size = sizeof(array);
    result = findselectnum(num, array, size);
    if (result != EOF)
        printf("The number %d is in array[%d]\n", num, result);
    else
        printf("The number does not exists on the array");
}

As you can see, i can only use the int type, so numbers that are float, or double, wont get the same result. So how can i use all types of data? Is there some kind of a structure for that? Thanks in advance.

Cykapath
  • 27
  • 5
  • So you have a list of numbers of known type and you get a character sequence as input which represents a number in any syntax. You only know that the syntax is one of those used to specify literal numbers for e.g. initialising variables of unknown type in C code? – Yunnosch Jan 17 '19 at 10:29
  • You need a parser, e.g. like the one in C, which find if a token is a int, or a float, or also an array. You need to define rules. Take C as example. But if you use it in real world, probably you should use existing tools (e.g. JSON) – Giacomo Catenazzi Jan 17 '19 at 10:29
  • If yes, take the input as what it is, a sequence of characters, aka a string. Then try to parse it as one of each of the possible number format. Start with the more complex ones, e.g. float in scientific notation, then continue to the easier ones, e.g. int. When none of the parsings succeed, complain to user that it is not a known format. – Yunnosch Jan 17 '19 at 10:30
  • @Yunnosch I have a list of numbers in different types, for example: array[ 1, -3, 3.25, -5.0] ect. But i cannot declare a specific type, it needs to be "universal", the array itself cannot be only from type int. – Cykapath Jan 17 '19 at 10:35
  • 1
    All of them should scan as float, think. Is there any sample input which does NOT scan as float? – Yunnosch Jan 17 '19 at 10:49
  • On the other hand, any input which actually NEEDS float format cannot possibly be found as a value in an int array, can it? – Yunnosch Jan 17 '19 at 10:50
  • Please manually mock up a sample dialog between the program and a user. What is the programs prompt, what does the user enter, what should the program output? – Yunnosch Jan 17 '19 at 10:51
  • You'll need to create your own structure with all possible types within. Assign a suitable item in this structure with proper value, add this structure to an array. – montonero Jan 17 '19 at 10:51
  • @montonero I do not think so. It is not necessary to store input values which cannot possibly be found in the to-be-searched int array. – Yunnosch Jan 17 '19 at 10:52
  • One problem coming up after the immediate topic is the fact that comparing floats for identity is problem-ridden... – Yunnosch Jan 17 '19 at 10:53
  • @Yunnosch Since it's not clear what is a OP's final goal it's hard to predict what is really necessary. But if he'll need just find specified number within input data then the structure is not needed, agree. – montonero Jan 17 '19 at 10:56
  • @montonero I think the instructions are not clear, but in general, i need to find any type of number in the array, in the code i wrote i used int for the array and the rest of the numbers to simplify things but that's not the goal – Cykapath Jan 17 '19 at 11:47
  • Have you covered `union` and `struct` yet? If your array was a union or struct with members for each possible type, then you could try to interpret the user's input as either int, float, double, etc., then search the array for the corresponding type/value. – lockcmpxchg8b Jan 17 '19 at 12:49
  • @lockcmpxchg8b It is familiar to me, i'll look into it. – Cykapath Jan 17 '19 at 13:22
  • @Yunnosch I think i'll use float like you stated, simplifies everything, thanks. – Cykapath Jan 17 '19 at 16:27
  • You might want to comment on the answers below, in case they are not suitable, to explain why. Otherwise you might get comments concerning not accepting any answer. I think for most readers it is still not clear what you want to achieve/solve/ask. I only have my guesses confirmed by you accepting my proposal... – Yunnosch Jan 17 '19 at 16:37
  • However, search for "comparing float for identity", to get information on a problem you very likely will encounter soon. – Yunnosch Jan 17 '19 at 16:37

2 Answers2

0

You can use a void* type instead of the int[] type.

It is the type used by the memcpy() function so that it can copy any type of data.

Then you have to cast your void* argument to compare it with the variable of the type you want.

Personally, I wouldn't use C to perform this kind of task.

C++ is maybe a bit more appropriate since it natively handles such mechanisms (templates): https://en.wikipedia.org/wiki/Template_(C%2B%2B)

  • *Then you have to cast your void\* argument to compare it with the variable of the type you want.* No. That would violate both [strict aliasing](https://stackoverflow.com/questions/98650/what-is-the-strict-aliasing-rule) and perhaps platform alignment restrictions. – Andrew Henle Jan 17 '19 at 13:47
  • @Bastien It's only with C, can't use anything else. – Cykapath Jan 17 '19 at 16:25
0

I have been asked to write a code that receives a type of data without being specific about what type (example, int, float, double ect..) and its certainly a number.

So how can i use all types of data?

You can't. More specifically, the CPU itself can't. To compare 2 numbers, the CPU has to know what size the numbers are, if the numbers are integer or floating type, and (if it's not just comparing for equality) if the number is signed or not.

There are 3 ways around this:

a) Code duplication (including templates in C++ where the compiler automatically generates N versions of the code for N types, and using macros in C to do similar).

b) Only support the largest type (e.g. maybe double in C) and convert everything to the largest type so that you only need one function for one type.

c) Abuse variable arguments and have run-time branches based on a "what type is it" argument (and increased risk of programmer mistakes); like int findselectnum(char *data_type, int size, ...). This mostly ends up being code duplication in disguise (e.g. you'd duplicate the loop in each case of a switch(data_type)).

Depending on the actual situation; I'd use a mixture of the first 2 options - e.g. find out why the software has been so poorly designed that it needs to do the same work on multiple different types, then try to change the design so it only needs to care about a small number of different types; then have a small number of different functions for the types that are "unavoidably necessary" (e.g. maybe an int findselectnum_int(int num, int array[], int size) and a separate int findselectnum_double(double num, double array[], int size); with nothing for float or short or whatever because they were avoidable).

Community
  • 1
  • 1
Brendan
  • 35,656
  • 2
  • 39
  • 66