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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s