Generate Random Numbers

Hello Everyone, I am new to C++, this is my first class ever. I have a homework assignment to write the below code. I am stuck because I need it to look like:

The range for this row is: 1 to 10: 10
The range for this row is: 11 to 20: 17 14
The range for this row is: 21 to 30: 22 29 22
The range for this row is: 31 to 40: 40 34 34 38
The range for this row is: 41 to 50: 50 50 45 41 47
The range for this row is: 51 to 60: 51 58 58 56 51 57
The range for this row is: 61 to 70: 68 62 62 66 62 61 69
The range for this row is: 71 to 80: 75 71 77 76 79 73 77 78
The range for this row is: 81 to 90: 84 86 83 90 86 82 89 82 84
The range for this row is: 91 to 100: 98 92 93 97 99 93 93 98 94 94


But instead my code is looking like this:
The range for this row is: 1 to 10: 1 9 2 9 8 9 1 2 6 5
The range for this row is: 11 to 20: 17 15 11 16 11 19 11 16 14 19
The range for this row is: 21 to 30: 27 27 27 30 29 28 27 29 25 25
The range for this row is: 31 to 40: 35 38 35 36 38 32 36 38 34 32
The range for this row is: 41 to 50: 42 42 48 45 47 48 43 50 43 48
The range for this row is: 51 to 60: 58 51 55 55 53 53 54 59 53 60
The range for this row is: 61 to 70: 65 67 67 69 62 66 63 70 63 66
The range for this row is: 71 to 80: 71 77 79 80 71 75 78 73 74 72
The range for this row is: 81 to 90: 90 84 85 84 90 87 88 85 87 81
The range for this row is: 91 to 100: 96 92 97 95 92 91 100 96 100 93


Below is my code, can someone please advise where I am going wrong?


#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int randNum(int a, int b) {
return rand()%(b-a+1)+a;
}

int main () {
// Part 1: Complete the code
srand(time(0));
int first=0, last=9;
for (int r = 1; r <= 100; r+=10) {
cout<<"The range for this row is: "<< r <<" to "<< r+9 <<":\t";
for(int c = 1; c <= 10 ; c++) {
cout << randNum(first+r,last+r)<<" ";
}
cout << endl;
}
for(int c = 1; c <= 10 ; c++) {

This line controls how many columns are printed. Since you are looping from 1 to 10, you will print 10 columns.


Ask yourself how many columns you want to print for a given row. Note: the answer is not always 10.
I understand what you mean but I do not know how to fix that.
Can you give me a clue or help me out further?
Like I said I never took computer programming before.
The number of "columns" you need to output is related to the "row" it's on.

Row 1: output 1 number
Row 2: output 2 numbers
Row 3: output 3 numbers
etc.

Now how can you change the loop condition c <= 10 to do that?
I tried
c <= r
c <= r-9
c <= r-10
c <= 10-r

but I am still not getting it
Like I said I never took computer programming before.


It's less a problem of computer programming and more a problem of thinking logically. A lot of programming problems are more about logic than about actual code.

fg109 hinted more at what I was getting at:

Row 1: output 1 number
Row 2: output 2 numbers
Row 3: output 3 numbers


See the pattern? So the number of columns to print depends on what row you're on.

The next question is: How do you know what row you're on?


EDIT:
I tried [...] but I am still not getting it


Don't just plug random things into your program and hope it will do what you want. That never works.

To solve this problem you have to think about the logic for it.
Last edited on
I got it thank you.
I used
c <= r/10+1
You can do a counter if you wish.

1
2
3
4
5
6
7
8
9
10
11
12
int counter = 1;
 
for(10 times)
{
        
        for(counters value amount of times)
        {
                //type that shit
        }
        
        counter++;
}
Topic archived. No new replies allowed.