Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

need a little help with arrays

Garrett85Garrett85 Posts: 168Member
Iv been trying for a while now to learn C++, but it seems and inpossible task. I have a few questions about the following code.

What is going on with hits[10] suddenly having letters and crap inside it like r, and i?
And whats all the dividing about (n / 10)? Or the last line of code return rand() % n; correct me if IM wrong, but than n is the functions n and not the data entered my me with cin>> correct? Anyway I fill like shooting myself trying to learn this programming language. IM in the fifth chapter of C++ without fear. The code is below.

#include
#include
#include
#include
using namespace std;

int rand_0toN1(int n);

int hits[10];

int main()
{
int n;
int i;
int r;

srand(time(NULL)); // Set seed for random numbers.

cout << "Enter number of trials to run and press ENTER: ";
cin >> n;

// Run n trials. For each trial, get a number from 0 to 9
// and then increment the corresponding element in the
// hits array.

for (i = 1; i <= n; i++)
{
r = rand_0toN1(10);
hits[r]++;
}

// Print all the elements in the hits array, along with
// the ratio of hits to the EXPECTED hits (n / 10).

for (i = 0; i < 10; i++)
{
cout << i << ": " << hits[i] << " Accuracy: ";
cout << static_cast<double>(hits[i]) / (n / 10) << endl;
}

return 0;
}

// Random 0-to-N1 Function.
// Generate a random integer from 0 to N-1.
//
int rand_0toN1(int n)
{
return rand() % n;
}


Comments

  • MT2002MT2002 Posts: 1,444Member
    : Iv been trying for a while now to learn C++, but it seems and inpossible task. I have a few questions about the following code.
    :
    : What is going on with hits[10] suddenly having letters and crap inside it like r, and i?
    [blue]
    See the comment below.[/blue]

    : And whats all the dividing about (n / 10)? Or the last line of code return rand() % n; correct me if IM wrong, but than n is the functions n and not the data entered my me with cin>> correct?
    [blue]
    Yes. The 'n' in rand_0toN1() is local to that function.
    [/blue]
    Anyway I fill like shooting myself trying to learn this programming language. IM in the fifth chapter of C++ without fear. The code is below.
    [code]
    : #include
    : #include [blue]//C++ should be [/blue]
    : #include [blue]//[/blue]
    : #include [blue]//[/blue]
    : using namespace std;
    :
    : int rand_0toN1(int n);
    :
    : int hits[10];
    :
    : int main()
    : {
    : int n;
    : int i;
    : int r;
    :
    : srand(time(NULL)); // Set seed for random numbers.
    :
    : cout << "Enter number of trials to run and press ENTER: ";
    : cin >> n;
    :
    : // Run n trials. For each trial, get a number from 0 to 9
    : // and then increment the corresponding element in the
    : // hits array.
    :
    : for (i = 1; i <= n; i++)
    : {
    [blue]// Here we choose a random number and
    // use it as an index into hits[]. So if(r==3),
    // we access hits[3]. If(r==7), we access hits[7].[/blue]
    : r = rand_0toN1(10);
    : hits[r]++;
    : }
    :
    : // Print all the elements in the hits array, along with
    : // the ratio of hits to the EXPECTED hits (n / 10).
    :
    : for (i = 0; i < 10; i++)
    : {
    : cout << i << ": " << hits[i] << " Accuracy: ";
    : cout << static_cast<double>(hits[i]) / (n / 10) << endl;
    : }
    :
    : return 0;
    : }
    :
    : // Random 0-to-N1 Function.
    : // Generate a random integer from 0 to N-1.
    : //
    : int rand_0toN1(int n)
    : {
    : return rand() % n;
    : }
    [/code]
  • sin26102003sin26102003 Posts: 5Member
    HELLO, I AM HAVING THE SAME PROBLEM WITH GENERATING COMBINATIONS, IF ANYONE HELPS U FIX THIS, PLEASE EMAIL ME AT sin26102003@yahoo.com
    thanxs


    Iv been trying for a while now to learn C++, but it seems and inpossible task. I have a few questions about the following code.
    : :
    : : What is going on with hits[10] suddenly having letters and crap inside it like r, and i?
    : [blue]
    : See the comment below.[/blue]
    :
    : : And whats all the dividing about (n / 10)? Or the last line of code return rand() % n; correct me if IM wrong, but than n is the functions n and not the data entered my me with cin>> correct?
    : [blue]
    : Yes. The 'n' in rand_0toN1() is local to that function.
    : [/blue]
    : Anyway I fill like shooting myself trying to learn this programming language. IM in the fifth chapter of C++ without fear. The code is below.
    : [code]
    : : #include
    : : #include [blue]//C++ should be [/blue]
    : : #include [blue]//[/blue]
    : : #include [blue]//[/blue]
    : : using namespace std;
    : :
    : : int rand_0toN1(int n);
    : :
    : : int hits[10];
    : :
    : : int main()
    : : {
    : : int n;
    : : int i;
    : : int r;
    : :
    : : srand(time(NULL)); // Set seed for random numbers.
    : :
    : : cout << "Enter number of trials to run and press ENTER: ";
    : : cin >> n;
    : :
    : : // Run n trials. For each trial, get a number from 0 to 9
    : : // and then increment the corresponding element in the
    : : // hits array.
    : :
    : : for (i = 1; i <= n; i++)
    : : {
    : [blue]// Here we choose a random number and
    : // use it as an index into hits[]. So if(r==3),
    : // we access hits[3]. If(r==7), we access hits[7].[/blue]
    : : r = rand_0toN1(10);
    : : hits[r]++;
    : : }
    : :
    : : // Print all the elements in the hits array, along with
    : : // the ratio of hits to the EXPECTED hits (n / 10).
    : :
    : : for (i = 0; i < 10; i++)
    : : {
    : : cout << i << ": " << hits[i] << " Accuracy: ";
    : : cout << static_cast<double>(hits[i]) / (n / 10) << endl;
    : : }
    : :
    : : return 0;
    : : }
    : :
    : : // Random 0-to-N1 Function.
    : : // Generate a random integer from 0 to N-1.
    : : //
    : : int rand_0toN1(int n)
    : : {
    : : return rand() % n;
    : : }
    : [/code]
    :

Sign In or Register to comment.