CS50

Problem set 1

This pset is still simple but it took me hours to finish. There are 4 files in this set: Hello, Water, Mario, and Greedy. If you are taking the course and still working on this pset, be aware that copying my code for your submission is considered unreasonable academic dishonesty. Please be reminded of this term at the beginning of every problem set in CS50.

Else, help me point out any problem (even smallest) with my code. I find it quite long and not really smart. If you have any better idea, please lighten me! Thanks!

Ok then.

This is hello.c

#include <stdio.h>

int main(void)
{

printf(“hello, world\n”);

}

So easy, huh? 😀

This is water.c

#include <stdio.h>
#include <cs50.h>

int main(void)
{

printf(“minutes: “);
int x = GetInt();

int y = 12*x;

printf(“bottles: %i\n”, y);

}

Still easy, only one simple multiplication.

This is mario.c

#include <stdio.h>
#include <cs50.h>

int main(void)
{

int height, rows, spaces, hashes;

do
{
printf(“height: “);
height = GetInt();
}
while (height<0 || height>23);

for (rows = 1; rows <= height; rows++)
{

for (spaces = (height-rows); spaces>0; spaces–)
{

printf(” “);

}

for (hashes = 1; hashes <= (rows +1); hashes++)
{

printf(“#”);

}
printf(“\n”);

}

return 0;

}

Combined usage of printf()  and  for and do-while loops. A little more complicated, huh? 🙂

And this is greedy.c

#include <stdio.h>
#include <cs50.h>
#include <math.h>

int main(void)
{

float change = 0;
int cent = 0;
int quarter = 0;
int dime = 0;
int nickel = 0;
int penny = 0;
int leftover = 0;
int totalcoin = 0;

do
{

printf (“O hai! How much change is owed?\n”);
change = GetFloat();

}
while (change < 0);

cent = round (change*100);

quarter = cent/25;
leftover = cent%25;

dime = leftover/10;
leftover = leftover%10;

nickel = leftover/5;
penny = leftover%5;

totalcoin = quarter + dime + nickel + penny;
printf (“%i\n”, totalcoin);

}

And that’s my answer to pset1-CS50.

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