-
February 28th, 2014, 03:57 PM
#1
Not sure why values are changing when I'm passing arrays and array sizes
Hi, I'm working on a homework assignment that asks me to roll two die a user given number of times, find the roll sums, and a few other things. I'm working on it one module at a time and I'm running into two big problems so far.
The first problem is that my int variable rolls changes to a number within the random number generator range of numbers after I run rolldie. I got around this by making a const equal to the user entered value of rolls just so that I could continue developing the program.
My second problem is that the values of the arrays resultsOne[] and resultsTwo[] are changed after running findsum(). I have no idea why this is happening and I even tried passing them as const, but that changed nothing. If anyone could point me in the right direction, I will be forever grateful!
We just started learning about passing arrays to functions, so there might be something big that I'm missing.
Thanks!
Code:
#include <iostream>
#include <cstdlib>
using namespace std;
void rolldie(int resultsOne[], int sizeOfresultsOne, int resultsTwo[], int sizeOfresultsTwo);
void findsum(int resultsOne[], int sizeOfresultsOne, int resultsTwo[], int sizeOfresultsTwo, int tossSums[], int sizeOftossSums);
int main()
{
srand(time(NULL));
int rolls = 0;
int resultsOne[] = {};
int resultsTwo[] = {};
int tossSums[] = {};
cout << "Enter number of tosses : ";
cin >> rolls;
const int roll = rolls;
rolldie(resultsOne, rolls, resultsTwo, roll);
cout << resultsOne[0] << endl;
cout << resultsTwo[0] << endl << endl;
findsum(resultsOne, roll, resultsTwo, roll, tossSums, roll);
cout << resultsOne[0] << endl;
cout << resultsTwo[0];
cin.sync();cin.ignore();
return 0;
}
void rolldie(int resultsOne[], int sizeOfresultsOne, int resultsTwo[], int sizeOfresultsTwo)
{
for(int i = 0; i < sizeOfresultsOne; i++)
{
resultsOne[i] = 1 + (rand() % 6);
resultsTwo[i] = 1 + (rand() % 6);
}
for(int i = 0; i < sizeOfresultsTwo; i++)
{
resultsTwo[i] = 1 + (rand() % 6);
}
}
void findsum(int resultsOne[], int sizeOfresultsOne, int resultsTwo[], int sizeOfresultsTwo, int tossSums[], int sizeOftossSums)
{
cout << sizeOfresultsOne;
for (int i = 0; i < sizeOfresultsOne; i++)
{
tossSums[i] = resultsOne[i] + resultsTwo[i];
}
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|