hello.c Code:
#include <cs50.h>
#include <stdio.h>
int main(void)
{
string name = get_string("Whats your name? ");
printf("hello, %s\n", name);
}
mario.c Code (ausgewählt: the harder one):
#include <cs50.h>
#include <stdio.h>
int Left_blocks(int quantity);
int Right_blocks(int quantity);
int Space(int quantity);
int main(void)
{
int height;
do
{
height = get_int("Whats the height? "); // Asking user for height:
}
while (height <= 0 || height > 8); // go back (line 7) if height is 0 or below (negative number)
// same goes if its above 8
for (int row = 0; row < height; row++) // the row
{
Space(height - (row + 1)); // to illustrate the left pyramide with spaces in between that
// decreases based on height and row
Left_blocks(row); // passing the current postion of the row
printf(" "); // spaces between the pyramide
Right_blocks(row); // passing the current postion of the row
printf("\n"); // the column
}
}
int Left_blocks(int quantity)
{
for (int column = 0; column < quantity + 1; column++) // row starts with 0 so +1 for quantity
{
printf("#");
}
return 0;
}
int Right_blocks(int quantity)
{
for (int column = 0; column < quantity + 1; column++) // row starts with 0 so +1 for quantity
{
printf("#");
}
return 0;
}
int Space(int quantity)
{
for (int space = 0; space < quantity; space++)
{
printf(" ");
}
return 0;
}
credit.c Code (ausgewählt: the hardest task on Problem Set 1 - Optional but chosen!):
#include <cs50.h>
#include <stdio.h>
long Checksum(long number, int cardID);
int main(void)
{
int cardID;
long number = get_long("Number: ");
if (number > 999999999999999 &&
number < 9999999999999999) // MasterCard with 16 digits could also be a VISA
{
// if the given number starts with 51, 52, 53, 54 or 55
if (number > 5099999999999999 && number < 5599999999999999)
{
cardID = 1;
Checksum(number, cardID);
}
else if (number > 3999999999999999 && number < 4999999999999999)
{
cardID = 2;
Checksum(number, cardID);
}
else
{
printf("INVALID\n");
}
}
else if (number > 999999999999 && number < 9999999999999) // VISA with 13
{
// if the given number starts with 4 (and having 13 digits) or starting with 4 (and having
// 16 digits)
if (number > 3999999999999 && number < 4999999999999)
{
cardID = 2;
Checksum(number, cardID);
}
}
else if (number > 99999999999999 && number < 999999999999999) // AMEX with 15 digits
{ // if the given number starts with 34
if ((number > 339999999999999 && number < 349999999999999) ||
(number > 369999999999999 && number < 379999999999999))
{
cardID = 3;
Checksum(number, cardID);
}
else
{
printf("INVALID\n");
}
}
else
{
// if non of the above ablies e.g. a phone number
printf("INVALID\n");
}
}
long Checksum(long number, int cardID)
{
long division = 0;
long origin_division = 0;
long current_digit = 0;
long next_digit = 0;
long multiply;
long first_product_digit = 0;
long second_product_digit = 0;
long sum = 0;
long origin_number = 0;
origin_number = number;
// second to last digit
division = number / 10; //=400360000000001
number = division; // updating number
// printf("Cutting Number (current): %li\n", number);
current_digit = division % 10; //=1
// printf("getting digit (current): %li\n", current_digit);
multiply = current_digit * 2; // 8*2= 16
// printf("multiplying digit (current): %li\n", multiply);
// what if the current digit was containg two digits?
if (multiply > 9) // if the number after multiplying has more than 2 digits:
{
second_product_digit =
multiply %
10; // gets me the second digit (2) from 12 (second digit from 12 is 2) / example 10 = 0
// printf("it has two digits (current), here is the second digit: %li\n",
// second_product_digit);
first_product_digit =
multiply / 10; // gets me the first digit (1) from 12 ("" is 1) / example 10 = 1
// add them together
// printf("it has two digits (current), here is the first digit: %li\n",
// first_product_digit);
current_digit = first_product_digit +
second_product_digit; // with the exmaple 12: 1+2=3 / example 10= 1+0
}
else
{
current_digit = multiply;
// printf("No other digit so setting current with the value: %li\n", current_digit);
}
// printf("---------------------------------------------");
while (number > 0)
{
division = number / 100; //=40036000000000 ""... =40036
number = division; // updating number
// printf("Cutting Number (next): %li\n", number);
next_digit = division % 10; //=0 ... =6
// printf("getting digit (next): %li\n", next_digit);
multiply = next_digit * 2; //=0 = =12
// printf("multiplying digit (next): %li\n", multiply);
origin_division = origin_number % 10;
// printf("getting digit from origin number: %li\n", origin_division);
origin_number = origin_number / 100;
// printf("cutting digit from origin number: %li\n", origin_number);
if (origin_number < 9)
{
sum += origin_number;
}
sum = sum + origin_division;
// printf("adding the origin digits %li\n", sum);
if (multiply > 9) // if the number after multiplying has more than 2 digits:
{
second_product_digit = multiply % 10; // gets me the second digit (2) from 12 (second
// digit from 12 is 2) / example 10 = 0
// printf("it has two digits (current), here is the second digit: %li\n",
// second_product_digit);
first_product_digit =
multiply / 10; // gets me the first digit (1) from 12 ("" is 1) / example 10 = 1
// printf("it has two digits (current), here is the first digit: %li\n",
// first_product_digit);
// add them together
next_digit = first_product_digit +
second_product_digit; // with the exmaple 12: 1+2=3 / exaple 10= 1+0
}
else
{
next_digit = multiply;
// printf("No other digit so setting current with the value: %li\n", next_digit);
}
// current digit is the sum of the next
// printf("adding current and next digit: %li + %li\n", current_digit, next_digit);
current_digit =
current_digit +
next_digit; // adding the current digit (2) with the next one (0 and later 12 aka. 3)
}
// printf("\ngoing to check if current digit (%li) + sum (%li) modulo 0\n",current_digit,
// sum);
if ((current_digit + sum) % 10 == 0)
{
if (cardID == 1)
{
printf("MASTERCARD\n");
}
else if (cardID == 2)
{
printf("VISA\n");
}
else if (cardID == 3)
{
printf("AMEX\n");
}
}
else
{
printf("INVALID\n");
}
return 0;
}