Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.
But with everything getting smaller these days, it's usually a nano-raptor, which any cat, or even a semi-attentive parrot[1], can easily dispatch. In the old days, a VAX-raptor was a thing to be feared.


[1] as long as it's not stunned or pining for the fjords.

Fair point. I understand that, these days, even a broom and dust-pan can deal with most raptor outbreaks.
 
Would you mind sharing the result of your other intermediate homework assignments - grade wise that is.

well we got 3 of our lab assignments back so far, 10/10 on each one.
and we had 2 homework assignments - 19/20 for my first one and 20/20 on my second one!

@mobilehaathi -- thank you :eek:
 
hi i need help again. i hit a road block.

i have a question that says "Write a single C++ statement that will print a random number from the set {4,9,14,19,24,29,34}

So i totally thought i could do this.

i figured it would be this:

Code:
for (int x = 4; x <=34; x+=5)
cout << (rand()%6+1) * 5 << endl;

and apparently thats not it but um WHY NOT? thanks
 
hi i need help again. i hit a road block.

i have a question that says "Write a single C++ statement that will print a random number from the set {4,9,14,19,24,29,34}

So i totally thought i could do this.

i figured it would be this:

Code:
for (int x = 4; x <=34; x+=5)
cout << (rand()%6+1) * 5 << endl;

and apparently thats not it but um WHY NOT? thanks

Let's do this step by step.

First, you need some random number. You call rand () for that. That's fine.

Next, you need a random number with seven different values. rand () % 6 won't do that, it will only give you six different values 0 to 5. So find an expression that will give you at random a value from 0 to 6.

Next find an expression that will give you at random one of the values 0, 5, 10, 15, 20, 25, 30.

Then find an expression that will give you at random one of the values 4, 9, 14, 19, 24, 29, 34.

Then print _one_ of these random values.
 
Let's do this step by step.

First, you need some random number. You call rand () for that. That's fine.

Next, you need a random number with seven different values. rand () % 6 won't do that, it will only give you six different values 0 to 5. So find an expression that will give you at random a value from 0 to 6.

Next find an expression that will give you at random one of the values 0, 5, 10, 15, 20, 25, 30.

Then find an expression that will give you at random one of the values 4, 9, 14, 19, 24, 29, 34.

Then print _one_ of these random values.

okay slightly confused on how to do the last three things you mentioned. im dumb. :confused:
 
hi i need help again. i hit a road block.

i have a question that says "Write a single C++ statement that will print a random number from the set {4,9,14,19,24,29,34}

So i totally thought i could do this.

i figured it would be this:

Code:
for (int x = 4; x <=34; x+=5)
cout << (rand()%6+1) * 5 << endl;

and apparently thats not it but um WHY NOT? thanks

What output do you get? I don't understand what you want to achieve with that for loop. But the random number should be written as:

(rand()%7 +1)*5 -1

rand()%5 gives you a random number from the set (0,1,2,3,4,5,6). With +1 and *5 you get a number from the set (5,10,15,20,25,30,35), and then you subtract 1.

I hope I didn't mess up :)
 
What output do you get? I don't understand what you want to achieve with that for loop. But the random number should be written as:

(rand()%7 +1)*5 -1

rand()%5 gives you a random number from the set (0,1,2,3,4,5,6). With +1 and *5 you get a number from the set (5,10,15,20,25,30,35), and then you subtract 1.

I hope I didn't mess up :)

Um i just thought that since it had to be from 4 to 34 in increments of 5 id need a for loop to show that. oops.

edit: when i try that, i just keep getting 4. should it be different each time i run it or what?
 
okay slightly confused on how to do the last three things you mentioned. im dumb. :confused:

Well, if you have one of the values 0, 1, 2, 3, 4, 5, or 6, then which operation would change this to one of the values 0, 5, 10, 15, 20, 25 or 30?
 
Well, if you have one of the values 0, 1, 2, 3, 4, 5, or 6, then which operation would change this to one of the values 0, 5, 10, 15, 20, 25 or 30?

so if i do rand%7+1 it would be a number ranged from 1 to 7, correct?
so *5 and then thatll be 5, 10, 15, 20, 25, 30, 35 and then i want 4, 9, 14, 19, 24, 49, 35 so i should just subtract one.

SO to answer my original question...


cout << (rand%7+1)*5-1 << endl; ? as was already posted, but want to make sure i understand the logistics behind it
 
(rand()%7)*5 + 4
would also work, and it's one operation less :)


Or:

int numbers[] = {4,9,14,19,24,29,34};

cout << numbers[rand%7] << endl;


edit: It would be more elegant to replace %7 with % length(numbers). But I don't know if this is easy to get in C.
 
Last edited:
(rand()%7)*5 + 4
would also work, and it's one operation less :)


Or:

int numbers[] = {4,9,14,19,24,29,34};

cout << numbers[rand%7] << endl;


edit: It would be more elegant to replace %7 with % length(numbers). But I don't know if this is easy to get in C.

this is a C++ class haha
 
this is a C++ class haha

Ah well, then you should implement a class that is instantiated with a list of numbers, and a method that returns one random element of that list when called.

Others might see this differently, but as long as you don't use object orientation (classes and inheritance), C and C++ are the same for me.
 
Should be satisfied with, not counting the desire of calling 'srand':

Code:
std::cout << (((rand() % 7) * 5) + 4) << std::endl;

... as a statement ends with ';', or a compund statement with:

Code:
srand(time(NULL)), std::cout << (((rand() % 7) * 5) + 4) << std::endl;

... under an interpretation of the question.

Now, how do you get the length of an array in C/C++? Is this even a sensible question to ask in C/C++, given that all arrays have to be initialized with a fixed length?

Which question are you referring? Her Home work assignement or yours?

Code:
int arrray[] ={ 4, 9, 14, 19, 24, 29, 34 };

std::cout << "Length of 'array' is: " << (sizeof(array) / sizeof(array[0])) << std::endl;

The array can be added to at edit time by simply adding additional entries. No need to specify and modify a dimension value is required.

robvas version is problematic in that if the array type is changed it will require a corresponding change (a two for one) in the code. Greater chance of error than letting the compiler do the work.
 
Last edited:
Which question are you referring? Her Home work assignement or yours?

Code:
int arrray[] ={ 4, 9, 14, 19, 24, 29, 34 };

std::cout << "Length of 'array' is: " << (sizeof(array) / sizeof(array[0])) << std::endl;

My question. Sorry, I shouldn't mess around so much in this thread. Thanks for answering it!
 
int somearray[50];
sizeof(somearray*sizeof(int));

I think that works

Yup -

Code:
void main() {
 char somearray[25];

 printf("%d", sizeof(char) * sizeof(somearray));
}

Output:
25

Nope.

It works in that particular case, because sizeof(char) is 1. Try it with an array of ints and you'll see that expression doesn't work.

I'll leave it as an exercise to figure out the correct expression. Just remember this: sizeof() returns a value measured in bytes.

So it's just like any other unit of measurement where you have to convert from measurements into counts. E.g. if you have 10 meters of string, and it takes 14 inches to tie up a package, how many packages can you tie?
 
Nope.

It works in that particular case, because sizeof(char) is 1. Try it with an array of ints and you'll see that expression doesn't work.

I'll leave it as an exercise to figure out the correct expression. Just remember this: sizeof() returns a value measured in bytes.

So it's just like any other unit of measurement where you have to convert from measurements into counts. E.g. if you have 10 meters of string, and it takes 14 inches to tie up a package, how many packages can you tie?

My mistake, it should have just been sizeof(array)

For some reason I was thinking sizeof(array) would return the amount of elements and not the size in bytes. So you'd have to times that by the element's size.

http://codepad.org/23cGeSCA
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.