• Some users have recently had their accounts hijacked. It seems that the now defunct EVGA forums might have compromised your password there and seems many are using the same PW here. We would suggest you UPDATE YOUR PASSWORD and TURN ON 2FA for your account here to further secure it. None of the compromised accounts had 2FA turned on.
    Once you have enabled 2FA, your account will be updated soon to show a badge, letting other members know that you use 2FA to protect your account. This should be beneficial for everyone that uses FSFT.

C++ question...

CBlakeNS

Gawd
Joined
Dec 29, 2002
Messages
808
I have started with an array that defines 1000 elements.

Inside the array I need to assign the elements to have a random number between 0 and 1000.

Do you guys have any pointers in how to do so?

I really appreciate it.
 
I'd use the C rand() function in a for loop. There may be better ways depending on the platform you're using.
 
I dont understand how I would do that...

for (int i = 0; i < size; i++){
randomNumber = 1 + rand()% 1000;
}

and if that was right how would I incorporate it into the array?
 
I dont understand how I would do that...

for (int i = 0; i < size; i++){
randomNumber = 1 + rand()% 1000;
}

and if that was right how would I incorporate it into the array?


If that rand() function works properly, just say

for (int i = 0; i < size; i++){
arrayName = lowerBound + (upperBound * rand());
}

in your example i think you wanted a number between 0 and 1000, so
lowerBound would be 0, upperBound would be 1000
 
Here is my code so far, but I cant get it to work....

//Array

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <stdlib.h>

using namespace std;

const int size = 1000;

int randomNumber;
int largestValueSoFar;

int ar[size] = {};

int main () {

for (int i = 0; i < size; i++){
ar = 1 + (1000 * rand());
}

int listMaximum(const int A[], int size){
while (size >= 1);
int largestValueSoFar = A[0];

for (int i = 1; i <size; ++i){
if (A > largestValueSoFar)
largestValueSoFar = A;
return largestValueSoFar;
}

cout << "The maximum value of the array is: " << largestValueSoFar << endl;



return 0;
}
 
first you implement an array with 1000 elements

then you make a for loop that goes from 0 - 999.

then you call each individual array element using the variable in the for loop.

then you set the value with the rand() function.

i can't remember the syntax as i've not coded for a while, but should look sort of like this:

#include <stdlib.h>
int main(){
int array[1000];
for(int i = 0; i < 1000; i++){
array = rand() % 1000;
}
return 0;
}

...or something like that.

EDIT: oh, you want between 0 - 1000. that is rand() % 1001.
 
Here is my code again, it prints out the random numbers from A[702] - A[1000]: How come it wont show 0 - 1000?

//Array

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <stdlib.h>

using namespace std;

const int size = 1001;

int ar[size];

int main () {

srand((unsigned int) time(0));

for (int i = 0; i < size; i++){
ar = 1 + rand() % 1001;
}

for (int i = 0; i < size; i++){
cout << " A[" << i << "] = " << ar << endl;
}



return 0;
}
 
You've got several mistakes in your code. Perhaps you should grab a book on C++, or take a programming class.
 
Back
Top