[TOC] ## 2024xCS50 Week 1 - C ### Psets --- #### Mario problem: print hash pyramid below(less-comfy problem) "o": space "#" : hashes <p class = "text-center"># o o #</p> <p class = "text-center"># # o o # #</p> <p class = "text-center"># # # o o # # #</p> <p class = "text-center"># # # # o o # # # #</p> <p class = "text-center"># # # # # o o # # # # #</p> 1. User enter a number to define how tall is the pyramid. 2. Print the pyramid. ##### Thought: 1. Start from left half and right half pyramid. A left/right pyramid is made of " "(space) and "#"(hash symbol). Figure out relation between numbers of spaces and hashes per layer, turn it into statements. ``` For the right pyrammid: // In the nth layer, print n's hashes in nth layer For the left pyramid, print spaces first and followed by hashes: // In the nth layer, print (HEIGHT - n -1) number of spaces // print n number of hashes ``` 2. Combine these two pyramids together. Don't forget the two spaces between the pyramids, since we want to print exactly the same pattern shown above. Write a full pesudocode in higher level cites and finish all the statementes (For more information about the process of designing peudocode, you may see more from the book-Code Complete: Steve McConnell, Ch.9) Here's my attempt: ``` // Ask user: "How tall is pyramid? " // get HEIGHT(total layers) using get_int() from cs50.lib // Starts the printing from layer n = 0 to layer height // In nth layer, print number of (HEIGHT - n - 1)'s spaces // In nth layer, print number of n's hashes // In nth layer, print 2 spaces as the gap between left and right pyramids // In nth layer, print number of n's hashes // print "\n" and move to the next printing line ``` -->Translate to higher level description: ``` // for(layer = 0 to HEIGHT) // for(number = 0 to (HEIGHT - height -1)) // print a space // End of for the second for-loop // Initialze a variable j = 0; // print j's hashes, // repeat process from the last line, while (j < layer + 1) // End of the do-while loop // print two spaces // Initialize a variable k = 0; // print k's hashes // repeat this process while(k < layer + 1) // End of the do-while loop // print "\n" and go to the next line // End of the first for-loop ``` 3. Inplemente it and using debug tool if it didn't work out first time. #### Cash tbd #### Credit In this problem, you'll need to inplement an algorithm to check if user's entering card number is valid. If it's valid, print out its card type: VASA, AMEX or MasterCard. If it's not valid, print out message: "INVALID". ##### Thought: There're three functions need to be finished, one is check_card(), one is check_length() and the other is checksum(). Let's start from the main function(): ``` // Ask user the number of credit card by printing : "Number: " // If check_sum() passed Luhn's algorithm, then check its card number length // If passed card number length test, check which card is should be // Else if didn't pass card number test, print out message: "INVALID" // Else if didn't passed check_sum(), print out message: "INVALID" ``` And here's demonstration for check_sum() and check_card(): 1. check_sum(): In check_sum(), you'll need to understand how Luhn's algorithm operates first. * First, look up all digit's number. * Second, multiply even digit's number by two. * Third, add it them up. Notice that if result of number added is above 2 digits, seperate it into 2 number, and added them up again. * 4th, add to other left number which wern't be multiplied by 2. * Finally, get the sum's last digit. If it's zero, then it passed the Luhn's algorithm. ``` // Declare a variable checksum to record the last digit in final step // Declare a variable n to record current digit number seraching through // Declare a variable sum to calculate sum from step1 to step4 // for( index = 0 to cardLength) // [block1]-get n //if(index == cardLength -1) // The last digit n of card number is the remainder of 10 by card number // Else // n is equal to : (Number from index+1 to index) - (number of index), then devided by 10 //[block2]-multiply even digit bt 2 // if (n is at even digit position) // multipy n by 2 // [block3]-added n up to calculate sum // if (n is more than 2 digits) // sum = (first digit of n) + (last digit of n) // else (n has only one digit) // sum = n + previus sum // End of for-loop // Calculate the last digit of sum, which is the variavle checksum //return checksum ``` 2. check_card(): ``` // Declare each card's criteria in some arrays containing the two starting number of AMEX, MasterCard, VISA // Declare a variable to record inputing number's first two digits number // Declare a status variable to identify which kind of card it is // for(checkIndex = 0 to elements number of AMEX array) // If (inputStartNum == checkIndex'th element in AMEX array) // set status variable to 1 // End of if statement // End of for-loop // and so as the other cards..... // write switch cases according to the value of status variable ``` --- ### Additional practice #### 1. Debug Understand error message #### 2. Half Data type practice #### 3. Prime Practice using conditional statement: for loop ---