Help passing array to main function

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#include <iostream>
#include <fstream>
#include <string>
#include<ctime> //for rand and srand
#include<cstdlib> //for time function
#include<iomanip> //Formatting
#include<cmath> //Pow function
#include<vector> // to display Game results properly
double populateArray(double* arr, int);
double probability[]={};
using namespace std;
double main2();

int sides =0;
double totalweight =0;
void rollTime(int x, double y ,double *arr);
int main()
{

    int switch2=0;
    int switch3 =0;
    int choice=0;
    do {

        cout << "Choose an option:\n\n";
        cout << "Option: 1: Enter sides and weights for a die \n";
        cout << "Option: 2: Specify output file \n";
        cout << "Option: 3: Simulate rolls \n";
        cout << "Option: 4: Exit\n\n";
        cout << "Your choice: ";
        cin >> choice;
        if(choice == 1)
        {
            main2();
            switch2=1;
        }
        else if(choice == 2)
        {
         string filename;
            ofstream outputFile;//steam output file
             cout<<"What is the name of the file would you like to output to?";
            // /Users/toddi/CLionProjects/untitled1/save.txt
             cin >> filename;
             switch3=1;
        }


        else if(choice == 3 && switch2==0 || switch3==0)
        {
            cout<<"You must select options 1 and 2 first; cancelling… \n";
        }
        else if(choice == 3 && switch2==1 && switch3 ==1)
        {
            cout<<"Simulate rolls\n";
            cout<<sides<<"\n";
            cout<<totalweight<<"\n";
            double* arr = new double[sides];
            rollTime( sides, totalweight, arr);
        }
    }while(choice!=4);


    return 0;
}
double main2()
{
    srand((unsigned)time(0));//seed random



    cout<<endl;
    cout<<"Enter the number of sides ( >= 2 ): ";
    cin>>sides;



    double* arr = new double[sides];
    populateArray(arr,sides);
    //for ( int i = 0; i < sides; i++)
    //{
        //cout <<"The weight of side "<< i +1 <<" is : "<<*(arr + i)<<endl;
    //}
    for ( int i = 0; i < sides; i++)
    {
       totalweight = totalweight + *(arr + i);
    }
   // cout <<"The total weight of sides "<< totalweight <<endl;



   // outputFile.open(filename);//open outputfile

   // outputFile.close();
   // cout << "\nData written to"<< filename <<"\n";
   return *arr;
}
double populateArray(double* arr,int x)
{
    for ( int i = 0; i < x; i++)
    {

        cout << "Enter the weight for Side "<<i+1<<" ( > 0 ): ";
        cin>>*( arr + i );


        //shifts the address

    }
    return *arr;

}
void rollTime(int x, double y, double *z)
{
    vector <double> boundary4;
    //cout<< "You have "<< x<<" sides.\n";
    //cout<<"Your total weight is "<<y<<"."<<endl;
    for( int i = 0; i < x; i++)
    {
        double prob = (*(z+i)/y)*100;
        //cout<<"The probability of landing on side "<< i +1 << " is "<<prob<<endl;
        probability[i]=prob;

    }
    //for( int i = 0; i < x; i++)
    //{
    //    cout<<"The probability of landing on side "<< i +1 << " is "<<probability[i]<<endl;
   // }

    double boundary1 =0;
    for( int i = 0; i < x; i++)
    {
        boundary1 +=  probability[i] ;
       // cout<<"The upper boundary for  landing on side "<< i +1 << " is "<<boundary1<<endl;
        boundary4.push_back(boundary1);
    }
   // for( int i = 0; i < x; i++)
   // {
   //cout<<"The upper boundary for  landing on side "<< i +1 << " is "<<boundary4[i]<<endl;
   // }
   // double random2 = (rand()%100)+.01;
    //cout<<"You rolled a for your first try "<<setprecision(4)<<" "<< random2<<" lets find out where that falls.\n";
   // int result = 0;
   // for ( int i = 0; i < x; i++)
   // {
     //   if (boundary4[i] >= random2)
     //   {
     //       result = i+1;
     //       break;
    //    }
  //  }
   // cout<<"you fell on side "<<result<<endl;
    cout<<"how many times would you like to roll with me?"<<endl;
    int rollwithme =0;
    cin>>rollwithme;
    cout<<"you want to roll "<<rollwithme<<" times. Lets go.\n";
    int result3[x];
    for (int i = 0; i <= x; ++i)
    {
        result3[i]=0;
    }
    for ( int i = 0; i < rollwithme; i++)
    {
        double random3 = (rand()%100)+.01;
        for ( int i = 0; i < x; i++)
        {
            if (boundary4[i] >= random3)
            {
                result3[i] = result3[i] + 1;
                break;
            }
        }
    }
    for ( int i = 0; i < x; i++)
    {
        cout<<"you rolled side "<<i+1<<" "<<result3[i]<<" times with an expected probability of "<<probability[i]<<"\n";
    }
}
Last edited on
at line 58 the function will not take the array that we already declared with the other function and it is saying it is undeclared when i bring it out of the scope of the other function.
I'd recommend you start by eliminating all of those global variables, pass the variables to and from the functions that need them.

Next statically allocated arrays must use compile time constants for their sizes and the size must be greater than 0.

Lastly for now, be very important, use meaningful variable and function names and avoid single letter variable names except when used as loop index values. Single letter variable names combined with global variables make the program difficult to debug because the program logic is being obscured.


yeah. i've been fighting with it for about 12 hours. Only made it worse.
Okay, so explain exactly what you mean by:
at line 58 the function will not take the array that we already declared with the other function

What array are you talking about, and what other function are you talking about?

and it is saying it is undeclared

What exactly is saying what exactly is undeclared?

when i bring it out of the scope of the other function.

Again what exactly is that "other" function.

If you're getting compile/linker errors, post the error messages, all of them, exactly as they appear in your development environment.

I've managed to workaround this solution with vectors which is probably a much more difficult way of accomplishing this project. I can't post the full solution here. However I did PM you jib. Thanks for your advice.
Last edited on
Topic archived. No new replies allowed.