#Why can't I return this array from a function?

143 messages · Page 1 of 1 (latest)

worldly zinc
#

`#include <iostream>
#include <random> //For random int
#include <fstream> //For files
using namespace std;

//Global constants
const int RANGE = 99;

//Prototypes

void diceCounter(int diceCounter[], int RANGE);

int main() {

//Open file

ofstream outfile;
outfile.open("histogram.txt");

//Array for dice
int diceOutputs[RANGE] = {};
diceCounter(diceOutputs, 99);

}

void diceCounters(int diceCounter[]){

int dice1 [RANGE] = {};
int dice2 [RANGE] = {};
int totalDice [RANGE] = {};

for (int i = 0; i < RANGE ; i++){

    //Dice1

    random_device dice1Roller;
    uniform_int_distribution<int> dice1Range(1,6);
    dice1[i] = dice1Range(dice1Roller);

    //Dice2

    random_device dice2Roller;
    uniform_int_distribution<int> dice2Range(1,6);
    dice2[i] = dice2Range(dice2Roller);

    diceCounter[i] = dice1[i] + dice2[i];

}

}`

drowsy cypressBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

stuck lagoon
#

The rules are complicated, but in short, when you move around a c-array, you only move around a pointer to the first element.
In this case, since the array is stored on the stack, the memory is "destroyed" once the function returns, so the pointer you return points to nothing

#

To solve this (the c++ way), use std::array

#

To solve this the c way, use a output parameter (pass in an array as an argument, then write the output directly to that)

worldly zinc
#

Nor did he say that there was an array header

crude lynx
#

You can use a template for passing in the array to the function

void diceCounters(std::array<int, size> diceCounter) { }
quartz surge
#

The prototype doesn't actually match the function. The name is off by one letter and a parameter is missing. The function that you called doesn't actually have a definition. Also, the missing parameter can't be used anyway, because RANGE has to be const and it is already defined as a const in the global scope.

crude lynx
worldly zinc
# quartz surge The prototype doesn't actually match the function. The name is off by one letter...

So I took your suggestions and whatnot, but I'm having an issue. How come the function diceCounter doesn't change the array value in diceOutput?

`void diceCounter(int diceOutput[]){
int diceCounter[] = {};
int dice1[] = {};
int dice2[] = {};

for (int i = 0; i < 100 ; i++){

    //Dice1

    random_device diceRoller;
    uniform_int_distribution<int> dice1Range(1,6);
    dice1[i] = dice1Range(diceRoller);

    //Dice2

    random_device dice2Roller;
    uniform_int_distribution<int> dice2Range(1,6);
    dice2[i] = dice2Range(dice2Roller);

    diceOutputs[i] = dice1[i] + dice2[i];

    cout << diceOutputs [i] << endl;
}

}`

worldly zinc
#

I haven't worked with templates

crude lynx
#

basically allows you to operate with generic types

quartz surge
worldly zinc
worldly zinc
#

`int main() {

//Open file

ofstream outfile;
outfile.open("histogram.txt");

//Write a program that will roll a pair of 6-sided dice 100 times, then produce a histogram showing the distribution of totals.
//Keep in mind that the lowest possible score is 2, and the highest is 12. Use an array to keep track of the number of rolls for each die total.

//Array for diceOutputs

int diceOutputs[RANGE] = {};
diceCounter(diceOutputs);

}

void diceCounter(int diceOutput[]){ //Function to roll dice
int diceOutputs[RANGE] = {};
int dice1[RANGE] = {};
int dice2[RANGE] = {};

for (int i = 0; i < 100 ; i++){

    //Dice1

    random_device diceRoller;
    uniform_int_distribution<int> dice1Range(1,6);
    dice1[i] = dice1Range(diceRoller);

    //Dice2

    random_device dice2Roller;
    uniform_int_distribution<int> dice2Range(1,6);
    dice2[i] = dice2Range(dice2Roller);

    diceOutput[i] = dice1[i] + dice2[i];

}

}

void numberCheck(int diceOutputs[]){ //Check for values
int two = 0, three = 0, four = 0, five = 0, six = 0, seven = 0;
int eight = 0, nine = 0, ten = 0, eleven = 0, twelve = 0;

for (int value : diceOutputs){
    if (value == 2)
        two += 1;
    else if (value == 3)
        three += 1;
    else if (value == 4)
        four += 1;
    else if (value == 5)
        five += 1;
    else if (value == 6)
        six += 1;
    else if (value == 7)
        seven += 1;
    else if (value == 8)
        eight += 1;
    else if (value == 9)
        nine += 1;
    else if (value == 10)
        ten += 1;
    else if (value == 11)
        eleven += 1;
    else if (value == 12)
        twelve += 1;
}

}`

#

This is my current code. Im having issues at the second function with the array again

#

mainly at

#

for (int value : diceOutputs)

#

says that "cannot build range expression with array function parameter 'diceOutputs' since parameter with array type 'int[]' is treated as pointer type 'int *"

#

I don't really know what to do with that bc I have not dealt with pointers

drowsy cypressBOT
#
int main() {
  // Open file

  ofstream outfile;
  outfile.open("histogram.txt");

  // Write a program that will roll a pair of 6-sided dice 100 times, then
  // produce a histogram showing the distribution of totals. Keep in mind that
  // the lowest possible score is 2, and the highest is 12.  Use an array to keep
  // track of the number of rolls for each die total.

  // Array for diceOutputs

  int diceOutputs[RANGE] = {};
  diceCounter(diceOutputs);
}

void diceCounter(int diceOutput[]) {  // Function to roll dice
  int diceOutputs[RANGE] = {};
  int dice1[RANGE] = {};
  int dice2[RANGE] = {};

  for (int i = 0; i < 100; i++) {
    // Dice1

    random_device diceRoller;
    uniform_int_distribution<int> dice1Range(1, 6);
    dice1[i] = dice1Range(diceRoller);

    // Dice2

    random_device dice2Roller;
    uniform_int_distribution<int> dice2Range(1, 6);
    dice2[i] = dice2Range(dice2Roller);

    diceOutput[i] = dice1[i] + dice2[i];
  }
}

void numberCheck(int diceOutputs[]) {  // Check for values
  int two = 0, three = 0, four = 0, five = 0, six = 0, seven = 0;
  int eight = 0, nine = 0, ten = 0, eleven = 0, twelve = 0;

  for (int value : diceOutputs) {
    if (value == 2)
      two += 1;
    else if (value == 3)
      three += 1;
    else if (value == 4)
      four += 1;
    else if (value == 5)
      five += 1;
    else if (value == 6)
      six += 1;
    else if (value == 7)
      seven += 1;
    else if (value == 8)
      eight += 1;
    else if (value == 9)
      nine += 1;
    else if (value == 10)
      ten += 1;
    else if (value == 11)
      eleven += 1;
    else if (value == 12)
      twelve += 1;
  }
}
rav
crude lynx
#
int main() {
  // Open file

  ofstream outfile;
  outfile.open("histogram.txt");

  // Write a program that will roll a pair of 6-sided dice 100 times, then
  // produce a histogram showing the distribution of totals. Keep in mind that
  // the lowest possible score is 2, and the highest is 12.  Use an array to keep
  // track of the number of rolls for each die total.

  // Array for diceOutputs

  int diceOutputs[RANGE] = {};
  diceCounter(diceOutputs);
}

void diceCounter(int diceOutput[]) {  // Function to roll dice
  int diceOutputs[RANGE] = {};
  int dice1[RANGE] = {};
  int dice2[RANGE] = {};

  for (int i = 0; i < 100; i++) {
    // Dice1

    random_device diceRoller;
    uniform_int_distribution<int> dice1Range(1, 6);
    dice1[i] = dice1Range(diceRoller);

    // Dice2

    random_device dice2Roller;
    uniform_int_distribution<int> dice2Range(1, 6);
    dice2[i] = dice2Range(dice2Roller);

    diceOutput[i] = dice1[i] + dice2[i];
  }
}

void numberCheck(int diceOutputs[]) {  // Check for values
  int two = 0, three = 0, four = 0, five = 0, six = 0, seven = 0;
  int eight = 0, nine = 0, ten = 0, eleven = 0, twelve = 0;

  for (int value : diceOutputs) {
    if (value == 2)
      two += 1;
    else if (value == 3)
      three += 1;
    else if (value == 4)
      four += 1;
    else if (value == 5)
      five += 1;
    else if (value == 6)
      six += 1;
    else if (value == 7)
      seven += 1;
    else if (value == 8)
      eight += 1;
    else if (value == 9)
      nine += 1;
    else if (value == 10)
      ten += 1;
    else if (value == 11)
      eleven += 1;
    else if (value == 12)
      twelve += 1;
  }
}
crude lynx
#

What is the function numberCheck doing?

worldly zinc
#

The function numberCheck is checking how many values are in the array

#

The current hw assignment is a histogram

#

so I plan on checking all of these numbers and then outputting it with stars

#

`Example output:

2 **

3 *****

4 ********

5 ***********

6 **************

7 *****************

8 ***********

9 *****

10 ***

11 **

12 *`

#

I had all of this done, but only in the main function and it was pretty damn ugly

#

So I'm just rewriting all this using functions

worldly zinc
crude lynx
worldly zinc
#

??

grave lagoon
#

```cpp
int x = 1
```
becomes

int x = 1;
#

or you can chose any other language

worldly zinc
#

ohh gotcha

worldly zinc
#

Can someone give me a bit of guidance on how to deal with the numberCheck function? Specifically at for (int value : diceOutputs)

Cannot build range expression with array function parameter 'diceOutputs' since parameter with array type 'int[]' is treated as pointer type 'int *'

That's the error I get and I do not know how to combat it.

crude lynx
crude lynx
worldly zinc
crude lynx
#

it doesnt need any variables

#

it simple prints them out

#

havent tested it tho so beware

worldly zinc
crude lynx
grave lagoon
worldly zinc
# crude lynx what error

Not any error, but when I plugged in that code and then couted it in the main function, I just got like 8-12 stars. Originally I thought it corrilated to the first index position in diceOutputs, so I did diceOutput[0] but it would not corrilate to the number of stars.

worldly zinc
grave lagoon
#

void numberCheck(int diceOutputs[]) is being transformed to void numberCheck(int *diceOutputs)

#

the array in function arguments is really a lie (it's implicitly converting to a pointer type, and array decay passed the pointer to the first member)

worldly zinc
grave lagoon
#

you could do something like

template<std::size_t length> void numberCheck(int (&diceOutputs)[length])
#

and pass a reference to the array

worldly zinc
grave lagoon
worldly zinc
#

oh gotcha

#

So how come

grave lagoon
#

you would get an error that length wasn't defined

worldly zinc
#

some code like what my professor made like this

#
void makeShip(char myGrid[][GRID_SIZE], int gridSize, int shipSize)
{
    // Inputs: 2D Grid array, Grid size, ship size
    // Does: Inserts a ship of ship size randomly in the grid
    //          - Updates the grid array
    // Returns: Nothing

    cout << "makeShip: Entered" << endl;

    bool horiz = (rand() % 2); // 0 or 1 (horizontal or vertical)
    int position = (rand() % (GRID_SIZE - shipSize + 1)); // start in position 0 through 6
    if (horiz) { // horizontal ship
        int randRow = rand() % GRID_SIZE;  // choose a random row
        for (int shipPos = 0; shipPos < shipSize; shipPos++){
            myGrid[randRow][position+shipPos] = STAR;
        }
    }
    else {  // vertical ship
        int randCol = rand() % GRID_SIZE;  // choose a random column
        for (int shipPos = 0; shipPos < shipSize; shipPos++){
            myGrid[position+shipPos][randCol] = STAR;
        }
    }
}```
#

works without that?

grave lagoon
#

because they're passing the size of the arrays in

worldly zinc
#

Ohh

#

So for my code, could I do something like this

grave lagoon
worldly zinc
#
void numberCheck(int diceOutputs[], int RANGE) {  // Check for values
  int two = 0, three = 0, four = 0, five = 0, six = 0, seven = 0;
  int eight = 0, nine = 0, ten = 0, eleven = 0, twelve = 0;

  for (int value : diceOutputs) {
    if (value == 2)
      two += 1;
    else if (value == 3)
      three += 1;
    else if (value == 4)
      four += 1;
    else if (value == 5)
      five += 1;
    else if (value == 6)
      six += 1;
    else if (value == 7)
      seven += 1;
    else if (value == 8)
      eight += 1;
    else if (value == 9)
      nine += 1;
    else if (value == 10)
      ten += 1;
    else if (value == 11)
      eleven += 1;
    else if (value == 12)
      twelve += 1;
  }
}
#

or does this not work bc its not a global constant

grave lagoon
worldly zinc
grave lagoon
#

if you're doing pointer+size then you would need to do a normal for loop

grave lagoon
worldly zinc
#

Ohh okay

#

so could I do something like this

#
for (int i = 0; i < diceOutputs; i ++)
#

or would it have to be RANGE

#

or diceOutputs[RANGE]

grave lagoon
worldly zinc
#

wdym accessing?

#

like

#
for (int i = 0; i < RANGE ; i++)
  for (int value : diceOutputs [i])
grave lagoon
worldly zinc
#

Thats perfect

#

I need the int value inside of the array to check the number

grave lagoon
#

ptr[i] is the same as *(ptr+i), it's moving forward i elements and then getting the pointed to value

grave lagoon
#

why are you trying to iterate over an int?

worldly zinc
#

Just checking the value

#

What I'm trying to do is see how many numbers in diceOutputs are equal to that specific number, so in the end I can output x amount of stars for that number.

#

Its for a histogram

grave lagoon
#

inside of the for loop for i

worldly zinc
# grave lagoon then you should do switch/if
    if (value == 2)
      two += 1;
    else if (value == 3)
      three += 1;
    else if (value == 4)
      four += 1;
    else if (value == 5)
      five += 1;
    else if (value == 6)
      six += 1;
    else if (value == 7)
      seven += 1;
    else if (value == 8)
      eight += 1;
    else if (value == 9)
      nine += 1;
    else if (value == 10)
      ten += 1;
    else if (value == 11)
      eleven += 1;
    else if (value == 12)
      twelve += 1;
#

Just like this?

#

should be i

#

i just copied the last text

grave lagoon
worldly zinc
#

okay okay

#

so im going to write the final code and let you see

#

wait actually

#

wdym with int value = diceOutputs[i];

#

why would that have signifigance?

#

couldn't I just use the if else statments alone?

grave lagoon
worldly zinc
#

ohhh

#

so I'd put int value = diceOutputs [i] before the if else loop

grave lagoon
worldly zinc
# grave lagoon yes, though if else is hardly a loop
void numberCheck(int diceOutputs[]){    //Check for values
    int two = 0, three = 0, four = 0, five = 0, six = 0, seven = 0;
    int eight = 0, nine = 0, ten = 0, eleven = 0, twelve = 0;

    for (int i = 0; i < RANGE; i++){
        for (int value : diceOutputs[i]){
            if (value == 2)
                two += 1;
            else if (value == 3)
                three += 1;
            else if (value == 4)
                four += 1;
            else if (value == 5)
                five += 1;
            else if (value == 6)
                six += 1;
            else if (value == 7)
                seven += 1;
            else if (value == 8)
                eight += 1;
            else if (value == 9)
                nine += 1;
            else if (value == 10)
                ten += 1;
            else if (value == 11)
                eleven += 1;
            else if (value == 12)
                twelve += 1;
        }
    }
}```

So I couldnt do int value = diceOutputs[i] because it overshadowed the for loop int value. But I've ran into another error, and it says:
#

Invalid range expression of type 'int'; no viable 'begin' function available

grave lagoon
worldly zinc
crude lynx
grave lagoon
worldly zinc
#

Ohh, so instead of that for loop, I'd just replace it with

#

int value = diceOutputs[i];

crude lynx
#

what are you going to do with all those variables? print them out?

grave lagoon
worldly zinc
worldly zinc
#

maybe I should show the whole code rq

crude lynx
#

pepe

grave lagoon
worldly zinc
#
//Preprosser directories

#include <iostream>
#include <array>
#include <random> //For random int
#include <fstream> //For files
using namespace std;

//Global constants
const int RANGE = 99;

//Prototypes

void diceCounter(int diceOutput[]);
void numberCheck(int diceOutputs[]);

int main() {

    //Open file

    ofstream outfile;
    outfile.open("histogram.txt");

//Write a program that will roll a pair of 6-sided dice 100 times, then produce a histogram showing the distribution of totals.
//Keep in mind that the lowest possible score is 2, and the highest is 12.  Use an array to keep track of the number of rolls for each die total.

    //Array for diceOutputs

    int diceOutputs[RANGE] = {};
    diceCounter(diceOutputs);




}

void diceCounter(int diceOutput[]){ //Function to roll dice
    int diceOutputs[RANGE] = {};
    int dice1[RANGE] = {};
    int dice2[RANGE] = {};

    for (int i = 0; i < 100 ; i++){

        //Dice1

        random_device diceRoller;
        uniform_int_distribution<int> dice1Range(1,6);
        dice1[i] = dice1Range(diceRoller);

        //Dice2

        random_device dice2Roller;
        uniform_int_distribution<int> dice2Range(1,6);
        dice2[i] = dice2Range(dice2Roller);

        diceOutput[i] = dice1[i] + dice2[i];

    }
}
#
void numberCheck(int diceOutputs[], int size){    //Check for values
    int two = 0, three = 0, four = 0, five = 0, six = 0, seven = 0;
    int eight = 0, nine = 0, ten = 0, eleven = 0, twelve = 0;

    for (int i = 0; i < RANGE; i++){
        int value = diceOutputs[i];
        if (value == 2)
            two += 1;
        else if (value == 3)
            three += 1;
        else if (value == 4)
            four += 1;
        else if (value == 5)
            five += 1;
        else if (value == 6)
            six += 1;
        else if (value == 7)
            seven += 1;
        else if (value == 8)
            eight += 1;
        else if (value == 9)
            nine += 1;
        else if (value == 10)
            ten += 1;
        else if (value == 11)
            eleven += 1;
        else if (value == 12)
            twelve += 1;
    }
}```
worldly zinc
#

brb gonna shower yo

crude lynx
#

you can print in a certain format there though

worldly zinc
#

Damn you right

#

I’ll probably do that soon

worldly zinc
#

works

#

thanks yo

#

!solved