-3

I keep getting errors talking about double pointers and i keep changing between dereferences and pointer but just keep getting errors. i don't need the address i just need to pass the address to the functions.

#include <iostream>
#include <stdlib.h> //need for rand()
using namespace std;

//GLOBAL CONSTANTS
#define ARRAYSIZE 100
#define RANDOMNUMBERS 6

//Function Declarations
void fillUpArray(int *[]);
void freqOfData(int *[], int *[]);
void outPutFreq(int *[]);

//Main function
int main()
{
   //Build arrays and make them so they are the right size 
   int dataArray[ARRAYSIZE] = {};
   int freqArray[RANDOMNUMBERS] = {}; //frequencies need to be 0 to start
   int* dataPointer = &dataArray;
   int* freqPointer = &freqArray;

   //fill up the big data array
   fillUpArray(dataPointer);

   //find the frequencies and put them in the frequency array
   freqOfData(dataPointer, freqPointer);

   //send the frequencies to be outputted
   outPutFreq(freqPointer);

   //end the program
   return 0;
}

//User Defined Functions
void fillUpArray(int *array[])
{
    for(int i = 0; i < ARRAYSIZE; i++)
    {
        *array[i] = rand() % 6 + 1;
    }
}

void freqOfData(int *data[], int *frequency[])
{
    //walk through the entire array
    for(int i = 0; i < ARRAYSIZE; i++)
    {
        //find out what value is in this spot and add one to that frequency
        //a switch(case) is better than if statements but if you don't know it don't worry about it

        if(*data[i] == 1) 
        {
            *frequency[0] = *frequency[0] + 1;
        }
        else if(*data[i] == 2)
        {
            *frequency[1] = *frequency[1] + 1;
        }
        else if(*data[i] == 3)
        {
            *frequency[2] = *frequency[2] + 1;
        }
        else if(*data[i] == 4) 
        {
            *frequency[3] = *frequency[3] + 1;
        }
        else if(*data[i] == 5) 
        {
            *frequency[4] = *frequency[4] + 1;
        }
        else if(*data[i] == 6)
        {
            *frequency[5] = *frequency[5] + 1;
        }
        else
        {
            //never should go here <-- means there is mistake in our code
        }
    }
}

void outPutFreq(int *output)
{
    cout << "The Frequency of 1 in the Array is " << output[0] << endl;
    cout << "The Frequency of 2 in the Array is " << output[1] << endl;
    cout << "The Frequency of 3 in the Array is " << output[2] << endl;
    cout << "The Frequency of 4 in the Array is " << output[3] << endl;
    cout << "The Frequency of 5 in the Array is " << output[4] << endl;
    cout << "The Frequency of 6 in the Array is " << output[5] << endl;
}

Edit: I apologize for being "off-topic". This function is suppose to have a main() with two arrays that calls 3 methods. These method populate one of these arrays with random data (1 to 6). The second method calculates the frequency of the occurrences of the numbers 1 - 6. The last method will take this array of the frequencies and output it in the format "The Frequency of # in the Array is ##".

6
  • 2
    int* dataPointer = &dataArray; Nov 13, 2014 at 22:47
  • 1
    Use an std::array, pass it by reference.
    – Ben
    Nov 13, 2014 at 22:51
  • The errors were actually in the function calls. i'm coming for C just trying to help explain it to a friend and seems as if i've forgotten. will have to look up std::array real quick Nov 13, 2014 at 22:53
  • original i was just passing &dataArray and &freqArray directly to the functions but try to make explicit points Nov 13, 2014 at 22:54
  • I'd better read a good C book or tutorial instead :). Just a quick hint: array name is its address. Nov 13, 2014 at 22:54

1 Answer 1

1

Your code doesn't compile. Here the issues:

  • int* is a pointer to an int,
  • int* is also a pointer to an array of int.
  • You obtain the address of an array int a[...]; simply by using a. No &shall be used. But if you really prefer this notation, you could write &a[0].
  • A parameter int *a[] refers to a 2D array
  • To pass an simple array to a function, declare the parameter either as int* or as int[] but not the two together. In the called function as an array, without *

Example:

void fillUpArray(int array[])
{
    for (int i = 0; i < ARRAYSIZE; i++)
    {
        array[i] = rand() % 6 + 1;
    }
}

If you implement all these corrections the programm compiles and even works !

Now beside this, and not directly related to your question:

  • I'd strongly advise to consider using vectors instead of arrays. It's less error prone and more flexible.
  • consider declaring constants as const variables. #define should be avoided nowadays.
1
  • 1
    Thank you! One of my friends was asking for help and its been about 2 years since I wrote C code (and i know this is c++). I appreciate the help. Also the hints are great. I do try to have coding etiquette or whatever it is called. I was always taught to use #defines so it is interesting that everything is const. I am going to have to do some research about those two updates. Nov 14, 2014 at 16:08

Not the answer you're looking for? Browse other questions tagged or ask your own question.