Official CS50x Thread (Free Online Harvard Programming [C, PHP, HTML, Javascript, SQL...] Courses

Data-Hawk

I have no strings on me.
Joined
May 6, 2012
Messages
8,420
Reputation
1,985
Daps
16,306
Reppin
Oasis
so if there is 4 people in the room for example

person 1 = 0
person 2 = 1
person 3 = 2
person 4 = 3

??

:sadbron: i knew i should paid attention during algebra


Edit: forget my reply. Don't want to confuse people since it seems like they haven't started coding yet.
 
Last edited:

Data-Hawk

I have no strings on me.
Joined
May 6, 2012
Messages
8,420
Reputation
1,985
Daps
16,306
Reppin
Oasis
Anyone "have" this book they recommend for beginners?
Absolute Beginner's Guide to C, Second Edition
Greg Perry


I read the book years ago... Good book, but unless you have a class that deals with C or for some specific need. I recommend learning another language ( C# , Java, Python, PHP ) most businesses aren't using C anymore.
 

Ctrl

Rookie
Joined
May 2, 2013
Messages
59
Reputation
40
Daps
211
Ok so i just finished Lecture 1 of Lesson 0 , all is simple enough brehs , but can someone explain this in another format? Its a simple algorithm to explain how to count people in a room

Now i know this is basic algebra but every time i tried to follow the county i was wrong , i kept being off by 1 or 2

@Kodie

2cqciso.png



also for those wondering Lesson 0 is two 1 hour lectures + assignments

so if there is 4 people in the room for example

person 1 = 0
person 2 = 1
person 3 = 2
person 4 = 3

??

:sadbron: i knew i should paid attention during algebra

Let N = 0
N is 0 before any counting actually starts. It's just a variable that starts out with a value of 0.

for each person in room
This basically tells the program how many times you want to do 'something'. In this case it's using the number of individual people in the room. So if you have 4 people, you're going to do 'something' 4 times.

N = N + 1
This is the 'something' that you're going to do. Again, in this case, if there are 4 people/persons in the room, we're going to do 'N = N + 1' four times.

So

For Person 1: Do N = N + 1
Again, when we created N, we gave it a value of 0. So this is going to be
0 = 0 + 1

Before we move on, it's important to note that in programming, the "=" sign doesn't mean "equals". It's an "assignment" operator, which basically means whatever is on the left gets set to the value of whatever is on the right.

So the '0' on the left is going to get set to the value of the '0 + 1' on the right (so as you can see, it's not exactly like algebra). Another way to say it is 0 "gets" 0 + 1 for short, or it might be easier to verbalize it as 0 "becomes" 0 + 1, or 0 "is" 0 + 1 (I say "is" or "gets"). It might look a little weird here, since we're substituting the real numbers in for N, but it'll make more sense when you think of assigning values to variables.

0 = 0 + 1 (0 "gets set to the value of" 0 + 1)
0 = 1 (simplified the calculation on the right side; now it means 0 gets set to 1)
1
N = N + 1 (N "gets set to the value of" N + 1)
N = 1 (N gets set to 1)
N is 1

So this was the first time we completed the N = N + 1 algorithm. If we're using the same example as before, where we said there are 4 people, we're going to be doing N = N + 1 three more times.


For Person 2: Do N = N + 1
Here N starts out as 1, which was the result from the first time we did this algorithm. So:

1 = 1 + 1 (1 gets set to the value of 1 + 1)
1 = 2 (1 gets set to 2)
2
N = N + 1 (N gets set to the value of N + 1)
N = 2 (N gets set to 2)
N is 2


For Person 3: Do N = N + 1
Here N starts out as 2, which was the result from the second time we did this algorithm. So:

2 = 2 + 1
2 = 3
3
N = 2 + 1
N = 3
N is 3


For Person 4: Do N = N + 1
Here N starts out as 3, which was the result from third time we did this algorithm. So:

3 = 3 + 1
3 = 4
4
N = 3 + 1
N = 4
N is 4


Now, let's simplify

Set up my variables:
N = 0

Do something with them:
For Person 1: Do N = N + 1
Result: N = 1

For Person 2: Do N = N + 1
Result: N = 2

For Person 3: Do N = N + 1
Result: N = 3

For Person 4: Do N = N + 1
Result: N = 4

At the end of this program, N = 4.
We've successfully counted the number of people in the room.

Hope that helps.
 

el_oh_el

Bulls On Parade...
Supporter
Joined
Aug 23, 2012
Messages
10,318
Reputation
1,920
Daps
26,059
Reppin
H-Town
Let N = 0
N is 0 before any counting actually starts. It's just a variable that starts out with a value of 0.

for each person in room
This basically tells the program how many times you want to do 'something'. In this case it's using the number of individual people in the room. So if you have 4 people, you're going to do 'something' 4 times.

N = N + 1
This is the 'something' that you're going to do. Again, in this case, if there are 4 people/persons in the room, we're going to do 'N = N + 1' four times.

So

For Person 1: Do N = N + 1
Again, when we created N, we gave it a value of 0. So this is going to be
0 = 0 + 1

Before we move on, it's important to note that in programming, the "=" sign doesn't mean "equals". It's an "assignment" operator, which basically means whatever is on the left gets set to the value of whatever is on the right.

So the '0' on the left is going to get set to the value of the '0 + 1' on the right (so as you can see, it's not exactly like algebra). Another way to say it is 0 "gets" 0 + 1 for short, or it might be easier to verbalize it as 0 "becomes" 0 + 1, or 0 "is" 0 + 1 (I say "is" or "gets"). It might look a little weird here, since we're substituting the real numbers in for N, but it'll make more sense when you think of assigning values to variables.

0 = 0 + 1 (0 "gets set to the value of" 0 + 1)
0 = 1 (simplified the calculation on the right side; now it means 0 gets set to 1)
1
N = N + 1 (N "gets set to the value of" N + 1)
N = 1 (N gets set to 1)
N is 1

So this was the first time we completed the N = N + 1 algorithm. If we're using the same example as before, where we said there are 4 people, we're going to be doing N = N + 1 three more times.


For Person 2: Do N = N + 1
Here N starts out as 1, which was the result from the first time we did this algorithm. So:

1 = 1 + 1 (1 gets set to the value of 1 + 1)
1 = 2 (1 gets set to 2)
2
N = N + 1 (N gets set to the value of N + 1)
N = 2 (N gets set to 2)
N is 2


For Person 3: Do N = N + 1
Here N starts out as 2, which was the result from the second time we did this algorithm. So:

2 = 2 + 1
2 = 3
3
N = 2 + 1
N = 3
N is 3


For Person 4: Do N = N + 1
Here N starts out as 3, which was the result from third time we did this algorithm. So:

3 = 3 + 1
3 = 4
4
N = 3 + 1
N = 4
N is 4


Now, let's simplify

Set up my variables:
N = 0

Do something with them:
For Person 1: Do N = N + 1
Result: N = 1

For Person 2: Do N = N + 1
Result: N = 2

For Person 3: Do N = N + 1
Result: N = 3

For Person 4: Do N = N + 1
Result: N = 4

At the end of this program, N = 4.
We've successfully counted the number of people in the room.

Hope that helps.
Perfect explanation. This will help when people start to deal with performing functions on arrays
 

Kodie

Pro
Joined
May 1, 2012
Messages
1,810
Reputation
100
Daps
1,525
Let N = 0
N is 0 before any counting actually starts. It's just a variable that starts out with a value of 0.

for each person in room
This basically tells the program how many times you want to do 'something'. In this case it's using the number of individual people in the room. So if you have 4 people, you're going to do 'something' 4 times.

N = N + 1
This is the 'something' that you're going to do. Again, in this case, if there are 4 people/persons in the room, we're going to do 'N = N + 1' four times.

So

For Person 1: Do N = N + 1
Again, when we created N, we gave it a value of 0. So this is going to be
0 = 0 + 1

Before we move on, it's important to note that in programming, the "=" sign doesn't mean "equals". It's an "assignment" operator, which basically means whatever is on the left gets set to the value of whatever is on the right.

So the '0' on the left is going to get set to the value of the '0 + 1' on the right (so as you can see, it's not exactly like algebra). Another way to say it is 0 "gets" 0 + 1 for short, or it might be easier to verbalize it as 0 "becomes" 0 + 1, or 0 "is" 0 + 1 (I say "is" or "gets"). It might look a little weird here, since we're substituting the real numbers in for N, but it'll make more sense when you think of assigning values to variables.

0 = 0 + 1 (0 "gets set to the value of" 0 + 1)
0 = 1 (simplified the calculation on the right side; now it means 0 gets set to 1)
1
N = N + 1 (N "gets set to the value of" N + 1)
N = 1 (N gets set to 1)
N is 1

So this was the first time we completed the N = N + 1 algorithm. If we're using the same example as before, where we said there are 4 people, we're going to be doing N = N + 1 three more times.


For Person 2: Do N = N + 1
Here N starts out as 1, which was the result from the first time we did this algorithm. So:

1 = 1 + 1 (1 gets set to the value of 1 + 1)
1 = 2 (1 gets set to 2)
2
N = N + 1 (N gets set to the value of N + 1)
N = 2 (N gets set to 2)
N is 2


For Person 3: Do N = N + 1
Here N starts out as 2, which was the result from the second time we did this algorithm. So:

2 = 2 + 1
2 = 3
3
N = 2 + 1
N = 3
N is 3


For Person 4: Do N = N + 1
Here N starts out as 3, which was the result from third time we did this algorithm. So:

3 = 3 + 1
3 = 4
4
N = 3 + 1
N = 4
N is 4


Now, let's simplify

Set up my variables:
N = 0

Do something with them:
For Person 1: Do N = N + 1
Result: N = 1

For Person 2: Do N = N + 1
Result: N = 2

For Person 3: Do N = N + 1
Result: N = 3

For Person 4: Do N = N + 1
Result: N = 4

At the end of this program, N = 4.
We've successfully counted the number of people in the room.

Hope that helps.
Great answer!
 

newarkhiphop

Moderator
Staff member
Supporter
Joined
Apr 30, 2012
Messages
37,913
Reputation
10,152
Daps
124,614
@Ctrl major props on such a detailed explanation breh , i literally spent the last 3 hours rereading your answer but it finally made sense to me, so lets say the program were to keep "counting" people the next set past 4 would be

4=4+1 correct? which would "equal" 5, which would be our next "0"

took me a minute to understand that 0 aint per se actually the numerical zero but could equal a "get set" command if need be

dunno if i even said that right :laugh:
 

dora_da_destroyer

Master Baker
Joined
May 1, 2012
Messages
65,275
Reputation
16,202
Daps
267,954
Reppin
Oakland
I signed up. :blessed:

I'm a web developer and looking to further my PHP and SQL back-end programming. Any one hit me up if you're into the web and wanna start smashing this course out with the coli brehs.

In fact any one wanna make a Coli group, including discussions about the week lectures? Let's get these certs brehs :ahh:
6 cert 6 figure coli gang :youngsabo:
 
Top