-1
int multiplicationMethod(int k, int n)
{

    if(n > SIZE || n < SIZE)
    {
        printf("IndexOutOfBounds Error. Please Try different slot number.");
    }
    else
    { 
         int N = rand();
         float A = N/RAND_MAX;

         int hashedValue = n * ((int)((k * A%1)));
         HashTable[hashedValue] = k;

         return hashedValue;
    }

}

int hashedValue = n * ((int)((k * A%1))); is being tagged with a pointer error expression must have an integral type I don't understand this though because the * is used with the post NOT prefix operation. Please help. Thanks!

UpAndAdam
  • 4,515
  • 3
  • 28
  • 46
  • 3
    Are you sure it's not about the `%` operator which here appears to take a float as input? E: there are more bugs besides that. – harold Aug 04 '23 at 21:32
  • 2
    You need fmod for floats see - https://stackoverflow.com/questions/6102948/why-does-modulus-division-only-work-with-integers – Dave S Aug 04 '23 at 21:39
  • 4
    `N/RAND_MAX` does not do what you think it does – harold Aug 04 '23 at 22:50
  • 2
    `if(n > SIZE || n < SIZE)` Are you sure about that condition? – Bob__ Aug 04 '23 at 23:17
  • 2
    Tip: The "compound" operations in the problematic statement led you to assume there was a "pointer" problem... Before turning to SO, you could break apart that expression into intermediate results to find the reason for yourself. In fact, if you did this, you would soon see that `A` will always be `0.0`... There's a lot that's wrong here... – Fe2O3 Aug 05 '23 at 01:59

3 Answers3

3

k * A%1

You can't do that. A is a float; and % only takes integer arguments.

This is an embarrassment to C. There's no good for this restriction anymore. Just auto-call fmod when you see that operator.

Should read k * fmod(A, 1)

As pointed out in comments, you need to change float A = N/RAND_MAX; to float A = N/(float)RAND_MAX; for your code to work; otherwise A will always be 0.

Joshua
  • 40,822
  • 8
  • 72
  • 132
0
srand((unsigned int)time(NULL));
float A = (float)(rand()) / (float)(RAND_MAX);
int B = 1;
int hashedValue = (int)((n* (k* (modf(A,&B)))));
HashTable[hashedValue] = k;

sreand() generates a random seed to generate a random number upon each subsequent call to method. Rand int generation in rand() method originally produces an int but is typecasted to a float and then divided by an incredibly large float. The result of this is a random float between 0 and 1.

The next process is relatively self-explanatory. However, it is important to note that modf parses the float a and the memory address to B.

  • 1
    This is posted as an answer, but it isn't clear to me how it answers your original question of _"why is C confusing my multiplication operations with pointer assignment?"_ Also: _"...note that `modf` parses the float `A` and the memory address to `B`"_ is at least unclear. `modf` stores the integral part of `A` in the object pointed to by `B` and returns the fractional part of `A`. – ad absurdum Aug 05 '23 at 22:29
-3

As DaveS stated you need to use fmod() rather than the % modulo operator when trying to work out the modulus of a float.

Here is your example with some tweaks. Big thing I have to point out is that you need to be very careful with your naming conventions it is easy to use one letter characters but outside of a loop it will affect the readability of your code. Also method names should be descriptive multiplicationMethod gives no real information as to what the method actually does.

#include <iostream>
#include <unordered_map>
#include <math.h>
const int SIZE = 20;
class TestClass
{
    std::unordered_map<int, std::string> hashTable;
public:
    int multiplicationMethod(int value,int slotNum)
    {
        if(slotNum > SIZE)
        {
            printf("IndexOutOfBounds Error. Please Try different slot number.");
        }
        else
        { 
            int N = rand();
            float A = N/RAND_MAX;
    
            int hashedValue = slotNum * ((int)(value * fmod(A,1)));
            hashTable[hashedValue] = value;
            printf(std::to_string(hashedValue).c_str());
            return hashedValue;
        }
    }
};


int main() 
{
    TestClass t;
    t.multiplicationMethod(4,4);
}