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;
}
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.
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.