Problem Set 1 : Introducing C

The C program you write is called Source Code. A compiler takes C source code and translates that code into machine language. Computers are made up of nothing more than thousands of electrical switches that are either on or off. Therefor, computers must ultimately be given instructions in binary. The prefix bi means two And the two states of electricity are called binary states. It’s much easier to use a C compiler to convert your C programs into 1s and 0s that represent internal on and off switch setting than for you to do it yourself.

The Compiling process :

pseudocode
The pseudocode and source code are done by you. Then, the compiler takes the source code and give a value which is called an object code.
Example :
– Writing a program that says “hello”

Before start the programs, be sure that you have cs50 appliance in your computer. Open up your gedit and let’s do this.
Not sure where to begin? See the hints below.
a. Pseudocode

  • Start Program
  • Print “hello”
  • Quit

b. Compiling

  • Source code in C :

hello
c. Object Code
hello binary

By the way, how to compile your program? Great question!

You will want to call ‘make’. You can compile your program by typing :make hello
Then if you can see the below, it means your program is compiled.make hello2

Or, you will want to call ‘clang’ by typing :

clang

If you’re done with this, let’s run the program by typing : run hello

And, viola! “hello” is printed!

run hello 2


Now, let’s talk about Conditions and Boolean expression in C.
These points are very important to understand since the Problem Set 1 will give you some basics of programming in C which will more complex in the next psets ahead.

1. Conditions :

a. If Statement

conditon

As you can see above the program said that if you have “a number less than zero”, your program should print a message such as “You picked a negative number!” and if you picked a number greater than zero, your program won’t print the message. That means your “condition” is going perfectly.

b. If – else statement

condition2

c. If – else if – else statement

condition3

d. If – if statement

condition4

Can you guess how those conditions work ?

2. Boolean Expressions

boolean

Combining Boolean Expressions :

combine boolean

3. Loops

a. For Loops (initialization, condition, update)

for-loop
Prints “This was CS50″ five times –  How it works? First, initialize variable(s) in this case ” int i “. Then check the condition ” i < 5 ” , after that update variable(s) ” i++ ” then the code inside the body will execute ” prints This was CS50 ” five times. If false exit the loop.

b. Do- While Loop
do-whileloop

Reprompts until user enters a positive number – How it, works? First, we declare variable(s), then execute code in the body “Please enter a positive number” then check the condition “enter < 1” if it is true, the code inside the body will execute if false, the loop will exit.

c. While Loop
while-loop

Counts down from 15 to 0 – How it works? First, declare variable(s) “int count = 15” then check the condition “count >= 0” if it is true, it will execute code in the body. If false, exit the loop.

4. Numerical Variables

  1. Int
  2. Float
  3. Long long
  4. Double

Watch the section : https://www.youtube.com/watch?v=fUhiHTHtYa4


Now, time to tackle Problem Set 1 :
a. Mario
b. Greedy

Mario

mar

Write in a file called mario.c in your ~/Dropbox/pset1 directory. A program that recreates half-pyramid using hashes(#) for blocks.

1. Prompt the user for half-pyramid’s height, a non-negative integer no greater than 23. If the user fails to provide a non-negative integer no greater than 23, you should re-prompt for the same again. With the help of do-while loop.

int n;
 do
 {
      n=GetInt();
 }
 while(n is invalid);

2. Draw half pyramids, you will want to call printf and one or more for loop. As the pyramid is in right align, you will want to print spaces aswell. Find the pattern!

Example :
Height = 8;
First Row = 7 spaces , 2 hashes
Second Row = 6 spaces, 3 hashes
Third Row = 5 spaces, 4 hashes
………………
n row : how many spaces?
How many hashes?

mario

Pseudocode :
For every row :
– print spaces
– print hashes
– print new line
Using for loop?
Keep in mind that the structure of for loop is should be like :

 for(initialization; condition; update)
 {
     //code inside
 }
 //code outside
for(int i = 0; i<height; i++)
 {
     print spaces
     print hashes
     print new line
 }
 //code outside

How about using while – loop? This is computer science, so many ways to solve this problem set. Your while-loop should be like :

 initialization;
 while(condition)
 {
   // code inside
   update;
 }

You should compile and run your program. Then, you’ll complete Mario. We first check for user input, and identify the pattern for each row from zero to n minus one , we print the spaces and hashes according to the pattern. With that, you’ll have a pyramid!

Greedy

Read the spec first  ” http://d2o9nyf4hwsci4.cloudfront.net/2014/x/psets/1/pset1/pset1.html#_time_for_change “. And let’s tackle greedy!
1. Prompt the user input for amount of change
2. Always use largest coin possible
3. Keep track of coins used
4. Print the final amount of coins

First, prompt the user input :

  • The amount must make sense
    Keep in mind that the amount should be a non-negative number and larger than zero.
  • What values are accepted?
  • The input is a value in dollars.
  • Use floating point imprecision.
  • How to convert dollars to cents? Use round() function. In the terminal type : man round.

Pseudocode :

  1. Get amount in dollars
  2. While(quarters can be used)
    increase count
    amount decrease by a quarter
  3. While(dimes can be used)
    increase count
    amount decrease by a dime
  4. While(etc)
    ….etc
  5. Print number of coins used

Now, compile and run the program!
jharvard@appliance (~/Dropbox/pset1) : make greedygreedy
Congrats, You’re done greedy.c!

This was Problem Set 1.

Advertisements

This was CS50 !

pset0-bg

Hello, world! Welcome to my site. Actually, this is called by “cs50book”. Perhaps, some people wondering, what’s that ? OK, I wanna pin the official website of this course that said : “CS50 is Harvard College’s introduction to the intellectual enterprises of computer science and the art of programming for majors and non-majors alike, with or without prior programming experience. An entry-level course taught by David J. Malan, CS50 teaches students how to think algorithmically and solve problems efficiently. Topics include abstraction, algorithms, data structures, encapsulation, resource management, security, software engineering, and web development. Languages include C, PHP, and JavaScript plus SQL, CSS, and HTML. Problem sets inspired by real-world domains of biology, cryptography, finance, forensics, and gaming. As of Fall 2013, the on-campus version of CS50 was Harvard’s second-largest course.”

Ok, so I hope above was clear enough to explain what CS50 is. Anyway, you guys should take this course like I did. If you guys can’t be able either enter or attend this course on campus, you will want to take it via online. Here is the link ” https://www.edx.org/course/introduction-to-computer-science-harvardx-cs50x#.VHq9pGevMdE .” This would be your amazing experience ever!

Well, guys…  What I want you to know is, this site will providing “how to do these” I mean, the psets. What is pset? Pset is a problem set which tailored by lecturer that you have to do, it was just like an “exam.” In every week, this course will have a problem set that you have to solve. Long story short, if you get stuck on problem set, you can read my post because I will walk you through it like : the algorithm, pseudocode, or something else that will be your questions about cs50.

I took CS50! And it was incredible experience I ever had.