Beginner that is fighting with code

I can compile this code, but when i try to execute it i encounter an error, any ideas to fix it?

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
/*#include <fstream> 
#include <iostream>
using namespace std;

//uses loop to randomly generate value into each element of array
double genValues(double* dblArray)
{      
       int i;
       for (i = 0; i < 100; i++)
           dblArray[i] = (rand() % 50 + 1);
       
}

//counts how many a times a number is stored in array and prints our for each //number
double countValues(double* dblArray)
{
       int i;
       int addValue = 0;
       
       for (i = 0; i < 100; i++)
           if (dblArray[i] == (rand() % 50 + 1))
              {
                    addValue++;
                    cout<< dblArray[i] << "repeats itself "<<addValue<<" times."<<endl;
              }
              
              //return addValue;
}
       
       

int main()
{
    double* dblArray[100];
    
    genValues(dblArray[100]);
    
    countValues(dblArray[100]);
    
    return 0;
}*/
Last edited on
1) everything is commented =P

2) line 34 makes an array of pointers, not an array of doubles. Assuming you want an array of doubles, use double dblArray[100]; instead

3) why are you using doubles for this array? rand returns an integer, and you shouldn't use == with doubles anyway becaues doubles are an approximation.

4) lines 36 and 38 go out of bounds on the 'dblArray' array. If you make an array of 100 elements, then only elements 0-99 are valid. When passing a pointer to the array as a function parameter, leave out the brakets:
genValues( dblArray ); // note, no [brakets]

5) Your genValues function has a 'double' return type, but doesn't return anything (your compiler should be erroring at you). Either return something, or change it to a void.

6) Ditto for countValues, although it looks like you might want countValues to return an int (not a double).
Last edited on
Wow, thanks. Actually almost working now, but the program always outputs the same answer when it should always be different because of rand. you guys have any idea why because everything seems to be right to me.

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
/*#include <fstream> 
#include <iostream>
using namespace std;

void genValues(int dblArray[])
{      
       int i;
       for (i = 0; i < 99; i++)
           dblArray[i] = (rand() % 50 + 1) ;
       
}

void countValues(int dblArray[])
{
       int i;
       int addValue = 0;
       
       for (i = 0; i < 99; i++)
           if (dblArray[i] == (rand() % 50 + 1))
              {
                    addValue++;
                    cout<< dblArray[i] << " repeats itself "<<addValue<<" times."<<endl;
              }
}
       
       

int main()
{
    int dblArray[100];
    
    genValues(dblArray);
    
    countValues(dblArray);
    
    return 0;
}
    
*/
Last edited on
include ctime, and then make sure you srand(time(0))
Wow can't believe i missed that key thing, must not be my day. Everything is working now, but my teacher still says it's wrong do you guys see anything i'm missing?

because it seems sometimes it gives me a cout message that is the same as previous one.

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
/*#include <fstream> 
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;

void genValues(int dblArray[])
{      
       int i;
       for (i = 0; i < 99; i++)
           dblArray[i] = rand() % 50 + 1 ;
           
       
}

int countValues(int dblArray[])
{
       int i;
       int addValue = 0;
       
       for (i = 0; i < 99; i++)
           if (dblArray[i] == rand() % 50 + 1)
              {
                    addValue++;
                    cout<< dblArray[i] << " repeats itself "<<addValue<<" times."<<endl;
              }
}
       
       

int main()
{
    srand(time(0));
    
    int dblArray[100];
    
    genValues(dblArray);
    
    countValues(dblArray);
    
    return 0;
}
*/
Last edited on
countValues isn't counting the values, it's doing something else-checking against random numbers.
Topic archived. No new replies allowed.