I need to generate 3 different random numbers and print out the largest of the 3.

The random number needs to be between 1-100 inclusive.
My attempt is below....

1)I don't understand why everytime I run my program it gives the same answer as the MAX? Isn't srand(time(0)); suppose to generate different random numbers everytime?

2) If i wanted to generate a random number from 40-120 inclusive would I write it like this? rand()%80+40

3) Unrelated to random numbers, If I wanted to simply print out the time on the computer, how would I do that?

Code:
```#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main(void) {

int num1, num2, num3, max;
srand(time(0));

num1=rand()%100+1;
num2=rand()%100+1;
num3=rand()%100+1;

if(num1>num2 && num1>num3)
num1=max;
else if(num2>num1 && num2>num3)
num2=max;
else
num3=max;

printf("%d", max);

system("PAUSE");
return 0;
}```

2. Originally Posted by matthayzon89
I don't understand why everytime I run my program it gives the same answer as the MAX?
Unfortunately, you mixed up the left hand side and right hand side of your assignments, e.g.,
Code:
`num1=max;`
should be:
Code:
`max = num1;`
Originally Posted by matthayzon89
2) If i wanted to generate a random number from 40-120 inclusive would I write it like this? rand()%80+40
Almost: there are 120-40+1=81 integers in the range [40, 120], hence you should use rand() % 81.

Originally Posted by matthayzon89
3) Unrelated to random numbers, If I wanted to simply print out the time on the computer, how would I do that?
That depends on how exactly you want the time to be represented. Look into the functions from <time.h> for ideas.

3. Originally Posted by laserlight

Almost: there are 120-40+1=81 integers in the range [40, 120], hence you should use rand() % 81.
How does the computer know to start from 40 ? wouldn't rand()%81 be capable of generating numbers below 40? Also why are you adding 1?

Maybe it will be easier for me to understand if you can explain how I come up with the first and second number?
rand()%<**FIRSTNUMBER**>+<**SECOND NUMBER**>

4. Originally Posted by matthayzon89
How does the computer know to start from 40?
Sorry, I was just correcting the range, not the start point as that is correct. That is, it should be: rand() % 81 + 40.

Originally Posted by matthayzon89
Also why are you adding 1?
Consider a contiguous sequence of integers: 10, 11, 12. How many numbers are there in the sequence? 12-10=2, or 12-10+1=3?

5. You have done a very sill mistake my friend

you have writtten
num1=max;

it should be
max=num1;

Same with num2 and num3.

just note down these mistakes and correct your code and you will get the required output.

6. For generating numbers between 40 and 120 use this

(rand()%120)+40

or
you can use this one also
random(81)+40

7. Originally Posted by Mr. AB
For generating numbers between 40 and 120 use this

(rand()%120)+40
That is wrong as the range will then be [40, 160).

Originally Posted by Mr. AB
you can use this one also
random(81)+40
Maybe, maybe not, since this random function is non-standard.

8. Can anyone please explain why this code is NOT printing out i=0 and sum=0? (it runs and does not print out anything)

The while loop is NOT executed b/c the semi colon.
sum = sum+i; <- is executed independent of the while loop so 0+0=0 <- for sum.

My logic:
The printf should print out the 'i=0' from when it was declared and sum=0 from the explanation above.

My logic#2:
Does this have anything to do with the boolean expressions are 0 and 1 and 0=FALSE and 1=TRUE and since it values out to 0, then nothing prints out? IF it was any other value besides zero then something would print out? <-- Is that correct?

Code:
```int i=0, sum=0;
while (i<10);
sum = sum+i;
printf("i=%d,sum=%d\n",i,sum);```

9. Originally Posted by matthayzon89
Can anyone please explain why this code is NOT printing out i=0 and sum=0? (it runs and does not print out anything)

The while loop is NOT executed b/c the semi colon.

Here's an exercise: start with the number 0. While 0 is less than 10. Jump.

Keep jumping.

Come on, you can do it... jump!

If you stopped jumping, then you have failed to follow my instructions, because 0 is always less than 10, so you must jump forever

Originally Posted by matthayzon89
sum = sum+i; <- is executed independent of the while loop so 0+0=0 <- for sum.
No, it is never executed, because it is never reached. It can only be reached when the value of i is not less than 10, but the value of i is always 0, and 0 is always less than 10.

Originally Posted by matthayzon89
The printf should print out the 'i=0' from when it was declared and sum=0 from the explanation above.
Likewise, this line is never reached due to the infinite loop.

10. Originally Posted by laserlight

Here's an exercise: start with the number 0. While 0 is less than 10. Jump.

Keep jumping.

Come on, you can do it... jump!
Lol laserlight, I love how you combine teaching C with a healthy lifestyle.

11. Originally Posted by laserlight

No, it is never executed, because it is never reached. It can only be reached when the value of i is not less than 10, but the value of i is always 0, and 0 is always less than 10.

Likewise, this line is never reached due to the infinite loop.

How come the printf and sum=sum+1; are never reached?

I thought that if there is a while loop with a semi colon after the condition then the loop does not run, and basically everything after it runs normally b/c of that semi colon simply 'stop' the loop b4 it started.

For example if the while loop was not there it would print out everything normally...right?
Or if there was a curly brace after sum=sum+i then it would also print...right?

I know how loops work but my question is more about the semi colons relation ship and how it effects the loop, its very very confusing when it comes to if statements and loops (especially when our prof tries to trick us and purposely does not indent anything, or puts a semi colon in a random place, and we need to predict whats going to happen WITHOUT a computer )

12. Originally Posted by matthayzon89
I thought that if there is a while loop with a semi colon after the condition then the loop does not run, and basically everything after it runs normally b/c of that semi colon simply 'stop' the loop b4 it started.
No, that means that the loop has an empty body. But the condition of the loop can still be executed. In this case, you have an infinite loop because the condition of the loop is always true.

13. So basically the semi colon has no impact on the code? It executes the while as if the semi colon wasnt there?

Is this true for all cases (assuming the condition is true)?

14. That code is not going to print anything because the while will loop for ever but won't do anything because of the ; following it. The 1st printf IS NOT inside the while. Also, if x = 100 for example the while will exit, printf("hello world") would happen and then printf("Boat") will also happen. I'll let you figure out why that is.

15. Originally Posted by matthayzon89
So basically the semi colon has no impact on the code? It executes the while as if the semi colon wasnt there?
No. The semicolon itself constitutes a single statement which becomes the entire loop body. Thus when the loop runs, it does nothing besides check to loop condition over and over.
If you remove the semicolon then whatever was after that forms the loop body.

How come the printf and sum=sum+1; are never reached?
There is no such line in that program. Make sure you use a big enough font to distinguish i from 1.