Problem getting the sum of random numbers

I have this code that generates four random numbers
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
void random(int a[][4])
{
  srand(time(NULL));
  
  int f[4];
  int s[4];

  int c, b, i;

  // sum of random numbers
  int sum = 0;
    
  file2 << "4 Random Numbers " << "\n";

  for( i = 0; i < 4; i++)
  {
    f[i] = rand() % 7;
    s[i] = rand() % 4;

    if((f[i] == 4) && (s[i] == 3))
    {
      b = rand() % 7;
      c = rand() % 4;
            
      // function that checks for duplicate random numbers
      check(b, c, a);
    }
    else if((f[i] == 5) && (s[i] == 2) || (f[i] == 5) && (s[i] == 3) )
    {
      b = rand() % 7;
      c = rand() % 4;

      check(b, c, a);
    }
    else if((f[i] == 6) && (s[i] == 1) || (f[i] == 6) && (s[i] == 2) ||
            (f[i] == 6) && (s[i] == 3))
    {
      b = rand() % 7;
      c = rand() % 4;

      check(b, c, a);
    }
    else
    {
      file2 << a[f[i]][s[i]] << " ";
      sum += a[f[i]][s[i]]; // i think the problem is here
    }          
  }

file2 << "\n";

cout << "Sum " << sum;
  
file2 << "\n";
}


Problem is it does not display the correct sum of numbers.

Any tips on how to solve this?
I'm having trouble reading that, could you elaborate on what you're trying to do? how do you know what they should add up to if they are random?

you should only seed srand once, at the start of main.
What exactly is sum supposed to be? Is it a problem that sum is not updated in the if statement, only in the else part.
Last edited on
I generate four random numbers and store them in array f and s.
1
2
3
 
f[i] = rand() % 7;
s[i] = rand() % 4; 


I then use f and s as index to array a. The reason why I know that the sum is wrong because I store the random numbers to a file

 
file2 << a[f[i]][s[i]] << " ";


And yes I only seed once
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
void random(int a[][4])
{
  srand(time(NULL)); // if you call this function twice, you've seeded more then once.
  
  int f[4]; //some badly named array of random ints from 0-6
  int s[4]; //another one from 0-3

  int c, b, i; // why is b and c necessary? and why i if it only exists within the scope of your for loop?
  //I think local variables take presedence, so declaring i shouldn't be a problem as you don't use it anywhere outside of a for loop

  // sum of random numbers
  int sum = 0;
    
  file2 << "4 Random Numbers " << "\n";

  for( i = 0; i < 4; i++)
  {
    f[i] = rand() % 7;
    s[i] = rand() % 4;

    if((f[i] == 4) && (s[i] == 3)) // why can't f be 4 and s be 3?
    {
      b = rand() % 7;
      c = rand() % 4;
            
      // function that checks for duplicate random numbers
      check(b, c, a); // what happens if it finds them? why do you need b and c?
    }
    else if((f[i] == 5) && (s[i] == 2) || (f[i] == 5) && (s[i] == 3) ) // again
    {
      b = rand() % 7;
      c = rand() % 4;

      check(b, c, a); // x2
    }
    else if((f[i] == 6) && (s[i] == 1) || (f[i] == 6) && (s[i] == 2) ||
            (f[i] == 6) && (s[i] == 3)) // *cough*
    {
      b = rand() % 7;
      c = rand() % 4;

      check(b, c, a); // x3
    }
    else
    {
      file2 << a[f[i]][s[i]] << " "; // so dimension one is 0-6 at index i, and dimension two is 0-3 at index i, and index i is 0-3, a[0-6[0-3]][0-3[0-3]]
      sum += a[f[i]][s[i]]; // i think the problem is here
      //well what does the array a hold at these indexes?
    }          
  }

file2 << "\n";

cout << "Sum " << sum;
  
file2 << "\n";
}


see if you can answer some of those, it'd help to know what the heck you're trying to accomplish.

also I just noticed, if this is the first run through then if f[0] is 4 and s[0] is 3, they remain undeclared, yet down there you use them as indexes, not good
Last edited on
I only call the function once

why is b and c necessary?
b and c is necessary to store the random numbers if it falls from one of the conditions above.

why can't f be 4 and s be 3?
the conditions above such as if((f[i] == 4) && (s[i] == 3)) and etc. does not contain any values.

what happens if it finds them?
if b and c are duplicate numbers it calls the check() recursively otherwise it displays the value.

well what does the array a hold at these indexes?
the values of array a

what do you mean by
f[0] is 4 and s[0] is 3, they remain undeclared
but you aren't doing anything with b and c except checking if they are duplicates, if they aren't you display them, but you don't assign them to f or s, so imagine if i is 0, and it generates f[0] = 4 and s[0] = 3, now it gets stuck in that if statement which assigns b and c a new value, then checks if b and c are equal, if they are I still don't understand what it does, nothing? and if they aren't it outputs them to cout or a file.

the thing is, if f[0] = 4 and s[0] = 3 then none of the other else if checks will run, meaning it then skips to calculating f[1] and s[1] without assigning f[0] and s[0] with any value at all.

I don't understand what you mean by the conditions not containing any values.

as for the values of the array a, what are they exactly? that's a key part of it, its a two dimensional array, the first dimention is passed without limit and the second is passed with a limit of 4, is it even legal to do that? and then at the end you're looking for these values by using the random numbers as indexes for them.

Can I please see the code that assigns values to array a, the code for the check function, and any other function that the check function calls, along with an explanation of what you hope the sum will be and what the program is trying to do.

edit:

nevermind, they aren't unassigned, they just don't get added to the sum
Last edited on
do you have any suggestions?
I can't offer any because I don't know what you are trying to do

Can I please see the code that assigns values to array a, the code for the check function, and any other function that the check function calls, along with an explanation of what you hope the sum will be and what the program is trying to do.
Last edited on
never mind I already got it.

Thanks anyways :)
Topic archived. No new replies allowed.