Finding Squares of N random integers

Hi, I just started programming a few weeks ago, so I'm and having some trouble with loop structures and random number generators. Sorry if the question seems tedious, our prof hasn't taught us rand() yet or how to use it. This is the assignment we've been given:

Requirement:
Write a program that displays squares of N random integers, where the integers are between 0 and 20.

Specifications:
1. The program will print a greeting message that describes its mission.
2. The user will be prompted to enter a value for N (between 0-100). You must validate the
input.
3. The program will assign a random integer using rand() to some variable.
4. It will print the variable and its square (see sample output)
5. The program will continue to print each random number and its square.

Hint:
 Use must use a for loop to get full marks for this question.
 Use rand()%n to generate a number between 0 and n-1

*I was getting help from a friend, but this is as far as we got, I can't figure out where to go from here or what I need to change if I've done something wrong.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <string>
#include <cmath>
using namespace std;

int main()
{[output][/output]   
    int N = 0;
    
    cout<<"Please enter a number between 0 - 100 to generate random number squares: "<<endl;
    cin>>N;
    
    if (!( N > 0 && N < 100))
    {
        cout<<"Invalid entry, please enter a number between 0 - 100: ";
        cin>>N;
    }
    
    for (int S=0; S <= N; S++)
    {
        int P = 0;
        P = rand()%21;
        P = pow(P,2);
    }
    
}
closed account (o3hC5Di1)
Hi there,

You need to seed the random number generator. This page has a good example and explanation:
http://www.cplusplus.com/reference/cstdlib/rand/?kw=rand

Documentation for srand(): http://www.cplusplus.com/reference/cstdlib/srand/

Hope that helps, please do let us know if you have any further questions.

All the best,
NwN
Topic archived. No new replies allowed.