1

for my assignment we need to create a code that generates

  1. n number of random numbers (from 0-100) and store them in an array
  2. then we have to ask the user a question if they want to see the numbers in the array
  3. then ask the user if they want to see the numbers in the array in order from 0-100
  4. then ask the user if they want to see the histogram of the data set
  5. finally, ask the user if they want to see the number of data in the array, the max, the min, the standard deviation, average, variance.

I am however, finding it hard for the program to display the numbers of the array if the user says y the question (AKA step 2)

Any help is great

int main()
{

    int numb=0;
    int *newar=NULL;
    char response;
    cout << "enter integer:";
    cin >> numb;

    newar = new int [numb];


    for(int i=0;i<numb;i++)
    {
        newar[i]=rand() % 101;

        cout << "Do you want to see array? (Y/N) :";
        cin>> response;
        if (response != 'n')
        {
            cout<<newar[i]<<" ";
        }
    }
    return 0;
}
6
  • What exactly is your problem - "finding it hard for the program to display the numbers" is not a useful description. Perhaps if you tried indenting your code you would see more clearly what your current logic is. Commented Jan 23, 2017 at 2:37
  • Move that question outside the loop. If answer is yes make another loop and print the numbers in the array. Commented Jan 23, 2017 at 2:37
  • i apologize. this is what my output looks like Please enter a positive integer:54 Do you want to see the content of the data set being generated? (Y/N) :y 41 Do you want to see the content of the data set being generated? (Y/N) :y 65 Do you want to see the content of the data set being generated? (Y/N) : So as you can see it keeps asking the question over and over again and not showing the numbers of the array. Commented Jan 23, 2017 at 3:09
  • If i try to move the question outside the loop, it gives me an error message saying "i" is not defined Commented Jan 23, 2017 at 3:11
  • forget about i, make a new loop with its own counter. Commented Jan 23, 2017 at 3:18

1 Answer 1

1

First fill the array:

for(int i  =0; i<numb; i++)
{
    newar[i] = rand() % 101;
}

And then prompt the user:

cout << "Do you want to see array? (Y/N) :";
cin >> response;

If yes, make a new loop:

if (response != 'n')
{
    for(int j = 0; j < numb; j++) // (you could use i as well, it would be another i
    {
        cout << newar[j] << " ";
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.