Question

Hello I am having a hard time getting arrays to work in functions. I keep getting this error called C2664 "void ranNumPerm_10(int)' : cannot convert argument 1 from 'int [10]' to 'int'" I don't understand how to fix it...

Function ranNumPerm_10(int):

void ranNumPerm_10(int bubble_1[])
{
    int oneRandno;
    int haveRand[ARRAY_SIZE_1] = { 0 };

    for (int i = 0; i < ARRAY_SIZE_1; i++)
    {
        do
        {
            oneRandno = rand() % ARRAY_SIZE_1;
        } while (haveRand[oneRandno] == 1);
        haveRand[oneRandno] = 1;
        bubble_1[i] = oneRandno;
    }
    return;
}

This program is a unfinished program that will use bubble, selection, insertion sort algorithms. I just can't seem to populate arrays yet. I am trying to make a function that will do that by being a "random number permutation generator" so that every number is random and no number repeats its self. I could use some help in getting this code to work and solving error C2664.

FULL CODE:

#define _CRT_SECURE_NO_WARNINGS
#define ARRAY_SIZE_1 10
#include <stdio.h>
#include <stdlib.h>


void ranNumPerm_10(int bubble_1);

int main(void)
{
    //Declarations
    int bubble_1[ARRAY_SIZE_1];

    //Bubble population @ 10 
    ranNumPerm_10(bubble_1);
    for (int i = 0; i < ARRAY_SIZE_1; i++)
    {
        printf("%d\n", bubble_1[i]);
    }

    printf("Array population test...\n");
    return 0;
} 
    void ranNumPerm_10(int bubble_1[])
{
    int oneRandno;
    int haveRand[ARRAY_SIZE_1] = { 0 };

    for (int i = 0; i < ARRAY_SIZE_1; i++)
    {
        do
        {
            oneRandno = rand() % ARRAY_SIZE_1;
        } while (haveRand[oneRandno] == 1);
        haveRand[oneRandno] = 1;
        bubble_1[i] = oneRandno;
    }
    return;
}
Was it helpful?

Solution

The declaration and definition of ranNumPerm_10 have conflicting signatures. At the top of the full code, you declare it as void (int), but then the definition is void (int []).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top