You got this :) 1 CSE-1310 Review Unix Commands Listing Files: Cleaning the Terminal: Directory Magic: File Magic: C Compiler Commands 3 3 3 3 4 4 Data Types 5 printf and scanf Statements 5 5 6 7 printf scanf What is & and when/why use it? Functions Declaring/Defining Functions Function Prototypes Using Functions in main 8 8 9 9 Reading Error Messages 10 If-Statements (Conditional Statements) 10 11 11 If-Else Else-If Switch-Case Statements 12 Arrays 13 13 13 13 14 14 14 Declaring an Array: Accessing Elements in an Array: Modifying Elements in an Array: Strings: Declaring Strings: Reading in Strings Opening a File: 15 15 For-Loops, While Loops 16 Files You got this :) For-Loops While-Loops Do-While Loops Operators, Order of Operations, and other Misc. Order of Operations: Math Operators: Logical/Boolean Operators: Casting Advanced File I/O Input Output 2 16 17 18 18 18 19 19 20 21 21 22 Command Line Arguments 22 Base Conversions 24 24 25 28 What are Bases? How do you convert from base 10 to other bases? How do you convert from other bases to base 10? You got this :) 3 Unix Commands Listing Files: ls = list files ls -l = list files with additional details for each file ls -a = shows all files, including hidden files Cleaning the Terminal: clear = clears the console, so your eyes don't hurt after seeing so many errors Directory Magic: mkdir “dir” = create a directory called “dir” mkdir "homework_folder" cd “dir” = change working1 directory into “dir” cd .. = change to previous directory cd - = undo's previous directory change cd homework_folder2 pwd = displays the current working directory pwd >> ~/Desktop/homwork_folder rmdir “dir” = deletes/removes a directory named “dir” rmdir "homework_folder" 1 The directory / file you’re currently viewing. Ex: if you open your file explorer and go to the downloads folder, then that’s your working directory 2 If the directory name has a space, then surround the file name with quotation marks You got this :) 4 File Magic: cat ”file1” = prints all the file contents of “file1” at once cat "sushomework.txt" >> Good morning :) more ”file1” = prints a ‘page’ worth of “file1”, and prompts you to press enter key to print next page of that file3 (so if the file is super long, opt to use more instead of cat) rm “file1” = removes/deletes a file called “file1” rm "sushomework.txt" touch “file1” = quickly makes a file named “file1” and/or updates the time-stamp touch "sushomework.txt" mv “file1” “file2” = renames and/or moves “file1” to “file2” mv "sushomework.txt" "sushomworkrenamed.txt" cp “file1” “file2” = makes a copy of “file1” and calls the copy “file2” cp "sushomework.txt" "sushomeworkcopy.txt" C Compiler Commands gcc “program.c” = compiles your C source code with the name “program.c” (This will produce an output file with the name a.out gcc "whatCodingAssignment.c" ./a.out = will run the code that is compiled (if you only used gcc “program.c”) gcc-o “C_program.out” “program.c” = compiles your C source code with the name “program.c” and produces and output file with the name “C_program.out” gcc-o "whatProgram.out" "whatCodingAssignment.c" ./”C_program.out” = will run the code that is compiled in the file with the name “C_program.out” 3 I don’t feel like giving an example for this You got this :) 5 Data Types There are many data types in C: int (integer) // integers are 4 bytes float (floating point) // float are 4 bytes double (twice the size of a float) // doubles are 8 bytes char (a single character) // characters are 1 byte These are the primitive data types in C. Characters are unique in that they can hold integer values too, and when they are printed out the integer value is correlated to an ASCII value, and it’ll print the character related to that ASCII value. When declaring data types you state what type it is, then give it a name, and you can initialize a value. int x = 15; Above, we declare an integer type with the name ‘x’ and it holds a value 15. Another thing you can do with the primitive data types is to create an array. To do that, you just add [] after the name. int x[] = {“15, 16, 17”};4 printf and scanf Statements printf Let’s dissect a simple printf statement: printf("I am %d years old now!", 17); Whenever C sees a valid printf specifier, it finds the variable associated with it and replaces the specifier with that value %d = this printf specifier represents a decimal value [no decimal places] (integer). %f5 = this printf specifier represents a floating-point number (float/double) 4 5 More info on arrays page 12 %e is for scientific notation, and %g uses the shortest representation: %e or %f. If you want to print out a double, then use %lf, which stands for ‘long float’. You got this :) 6 %c = this printf specifier represents a single character %s = this printf specifier represents a character array AKA a string of characters with a NULL terminator at the end Another example: printf("%d %d %d", 17, 23, 58); // prints out: 17 23 58 Prints out those numbers in order. You can replace those numbers there with variables: int x = 15; float y = 69.01; char c = 'c'; char str = "String"; printf("%d, %f, %c, %s", x, y, c, str); // prints out: 15, 69.01, c, String Please note that order matters too, so the first thing after the quotes needs to match the first format specifier (In the example below, the first thing MUST be an integer, then a float, then a character, and finally a string [AKA char array w/ NULL at the end]) int x = 15; float y = 69.01; char c = 'c'; char str = "String"; printf("%d, %f, %c, %s", x, y, c, str); // prints out: 15, 69.01, c, String scanf Instead of printing out things, the scan function takes in the values specified by the parameter: int x; printf("give me a number: "); scanf("%d", &x);6 printf("%d", x); /* assume that this is what the terminal shows: */ >> give me a number: 15 >> 15 ⭐If you input ‘a’, or something that is not a number, C will store it 6 An ampersand is needed when scanning into a variable You got this :) 7 as 0 What is & and when/why use it? & means “to get the address of” the thing next to it int x; printf("Location of x in memory = %p", &x); // %p is the format specifier for addresses What &x means is that it will get the address of x in memory Why does scanf use it? scanf uses the ‘&’ because of how functions work in C. Pretty much, you cannot really change the value of ‘x’ if you just give scanf ‘x’. BUT if you give scanf the address of ‘x’, or the location of ‘x’, then scanf will be able to change the value of ‘x’. A little confusing but think of it like this: - You call 911, and you say who you are, but hang up. They know who you are, but they aren't able to find you because, well, where do they go? They don’t know your location at all, so they scramble or give up. That is the same as scanf("%d", x); - Now this time, you call 911 and you state your location of where you need help. This is much more useful to them, because then they are able to locate you and properly send in aid in a timely manner. That is what this is doing scanf("%d", &x); When do you use the ‘&’? ‘&’ is used for almost any data type EXCEPT strings (or really arrays too). int x; float y; char c; char str[20]; scanf("%d", scanf("%f", scanf("%c", scanf("%s", &x); &y); &c); str); // str IS the address You got this :) 8 Why are strings an exception? It’s because the name of an array IS the address. There’s no point in doing ‘&str‘ because then you are being too specific/redundant. Remember, for scanf to do its job, it needs to know the address of the variable to properly modify it. Also, the name of an array IS the address. Functions We’ve seen functions before. scanf and printf are functions themselves! Declaring/Defining Functions To declare functions, you need to give it a return type, a name, and parameters. Return types, which are the first word, specify if the function has a “return” statement and what data type will be returned. The name is the next word when declaring a function. And finally anything in the () are known as arguments/parameters. int returnZero() { return 0; } The function defined above has an integer return type, so it must have a return statement that returns an integer value. The name is “returnZero”, and it has zero parameters. void someNoReturnFunction(int integerValue, char string[]) { printf("%d\n7", integerValue); printf("%s\n", string); } The function defined above has a void, or no return type, so it WILL NOT have a return statement. The name is “someNoReturnFunction”, and it has two parameters in this specific order, an integer first and then a character array. 7 \n is a new line character; it’s like pressing the enter key while editing a document You got this :) 9 Function Prototypes So when defining functions, the first line of the function is known as a function definition. A function prototype is pretty much that line of the function definition followed by a single semicolon. int returnZero(); void someNoReturnFunction(int integerValue, char string[]); The code above are examples of two function prototypes. Using Functions in main So when using functions in main, all you need to do is call the name of the function, and provide the proper parameters. int main() { int x = returnZero(); // This will assign x to whatever the function returnZero returns char str[] = "Hello! Remember you got this :D"; someNoReturnFunction(x, str); // This execute the code that is inside someNoReturnFunction } In the code above, “returnZero()” will return 0, and then the value of x = 0. The function “someNoReturnFunction(x, str)” will execute the code inside. The parameters passed in the () MUST be in the order of what is in the function definition. So the first value passed into the function MUST be an integer as stated from the definition, AND the second value MUST be a character array. Note that the names of the variable do not need to be the same as what was in the definition AND you do not need to specify what data type is it. You got this :) 10 Reading Error Messages Fun fact: if you divide a number by 0, C will throw an error: printf("%d", 15 / 0); /*****************OUTPUT******************** main.c:2:23: warning: division by zero [-Wdiv-by-zero] printf("%d", 15 / 0); ^ ...Program finished with exit code 0 ********************************************/ Let’s dissect that error message: main.c:2:23: - main.c is the file name - 2 is the line where the warning is located - 23 is the character index (starting at 1) where the warning is located. - warning: division by zero [-Wdiv-by-zero] is the description of the error. Usually C will tell you exactly what you did wrong on that line and suggests how to fix the error. C also points to where in the line that error is at, which you can see under the warning line in the output. If-Statements (Conditional Statements) “If this then that” is basically what an if-statement is. Let’s look at an example of one: if (3 < 15) // 3 < 15 is the condition { printf("code inside these curly brackets are executed if the condition is true"); } If statements must include a conditional statement (statement that evaluates to true or false8) inside the parenthesis. 8 In C, there technically is no true and false; as long as the statement evaluates to an integer-decimal variable type (short, int, long), then C will interpret statements that evaluate to 0 as false, and any other number as true. You got this :) 11 If-Else If the condition is false, then C will execute code in the else-block9. Else-if’s always go before the else-statement in order from top to bottom. if (3 > 15) { printf("this is true"); } else { printf("This is the else block."); } When this is run, it’ll print out This is the else block. Else-If If the initial condition is false, then C will evaluate the next if-condition: if (3 > 5) { printf("This initial statement is true"); } else if (3 > 4) { printf("This second condition is true"); } else if (4 > 3) { printf("The third condition is true"); } else { printf("I'm going to cry"); } Basically: if a value = 0, then it is considered false. If it’s a non-zero, including negative numbers, then C considers that true 9 A block of code is code that is surrounded by curly brackets You got this :) 12 Switch-Case Statements Switch-case statements functions similarly to the if / else-if blocks I typed out earlier. The only differences are: ● at the end of the case, if you don’t include a break statement10 then C will continue executing all cases after the matching case ● The parameter of the switch-case statement must be an integer value ● The cases are basically “if [parameter] is equal to [case number], then [execute code] ● It’s faster than the if-else if chain Let’s look at an example: int x = 10; switch(x) { case 5: printf("this parameter is a 5"); break; case 10: printf("this parameter is a 10"); case 15: printf("this also executes because there's no break statement to end off case 10"); default: printf("the default case is like the else block in an ifelse statement"); } This code outputs code from 10, 15, and the default case. 10 Breaks outside of the block of code that C is currently in. Ms. Tiernan only wants you to use breakstatements inside of switch-case statements You got this :) 13 Arrays Arrays are a collection of elements. Declaring an Array: int name[15]; Typically, you declare an array with its datatype, name, and size (in this case it’s 15). You can also declare and assign values to an array on the spot: int name2[]11 = {1, 2, 3, 4, 5}; int name3[2] = {1, 2, 3, 4, 5}; If you give that above array a size of 2 (shown in integer array name3), then from elements past index 212 in the assignment (numbers inside the curly brackets) don’t exist in C’s eyes. That means that If I tried to print out the element in position 3 integer array name3, then C will return a 0, meaning that the element doesn’t exist. Accessing Elements in an Array: To access elements in an array, you need the array name, followed by square brackets with the index of the element you’re searching for inside the brackets. For example, retrieving the element in position 3 of integer array name2 would return a 3. printf("%d", name2[2]]); //prints out 3 Modifying Elements in an Array: To set an element in an array to another element, then do this: name2[0] = 69; //now the elements in name2 are: {69, 2, 3, 4, 5} 11 12 You can still give the size in the bracket, and C will prioritize that size first. Position 2; arrays are zero-indexed, meaning that the first element in an array is at position zero. You got this :) 14 Strings: Strings aren’t a datatype in C; they’re just an array of characters, with the last element of the array being and EOF13 character. Declaring Strings: char string[15] = "hi" //string[0] = 'h', string[1] = 'i', string[2] = EOF character Reading in Strings You can read in a string using scanf, but there’s a difference: you don’t need an ampersand (&) char str[50]; printf("give me a word, with no space in-between: "); scanf("%s", str); printf("%s", str); /* What Terminal Sees: * > give me a word, with no space in-between: sleep_deprivation > sleep_deprivation *************************/ 13 End-of-File You got this :) 15 Files In order to use functions regarding files, you need the #include <stdlib.h> header Opening a File: Whenever you open a file in C, you can choose whether to read in values or to write out values.14 FILE *fileName; fileName = fopen("homework.txt", "r"); The first parameter is the file name, complete with file extension, while the second is the mode in which you want to interact with the file While reading in values, you can use the function feof() function to check if we’ve reached the end of the file. Let’s look at this example code: #include <stdlib.h> #include <stdio.h> #define MAX 100 // replaces ALL instances of 'MAX' with 100 int main() { FILE * in; in = fopen("example.txt", "r"); //opens file to read (second parameter "r") while (!feof(in)) //feof() function checks if you've reached end of file { char str[MAX]; fscanf(in15, "%s", str); printf("%s ", str); } } 14 There’s actually more but those are generally the most important ones You need the FILE * as the first parameter in the fprintf function. Thanks to sha4wsh4nk for pointing that out! 15 You got this :) 16 example.txt: hello there everyone how is everyone's day? today is my birthday! This code reads in a line of example.txt, and prints out the line For-Loops, While Loops For-Loops For loops are used to repeat a block of code a certain number of times. Let’s look at an example of a for-loop and try to break it down. int size = 6; int arr[size] = {1, 2, 3, 4, 5, 6}; int sum = 0; for(int i = 0; i < size; i++) { sum = sum + arr[i]; } printf("sum: %d\n", sum); What the code above does is that it prints out the sum of all the elements in array arr by iterating through all the elements in the array: int i = 0; = your starting point. A variable is usually declared and assigned16 to a value. The value must be a integer-decimal, but can also be a variable whose datatype is an integer-decimal 16 If you already have a variable that’s declared, then you would just assign in the first parameter of the for-loop You got this :) 17 ● You can also set i = someVariable as long as someVariable is the same / smaller integer-decimal datatype as variable i i < size = This is your end condition. Every time C repeats the code in the for-loop it checks if that end condition is still false, and will keep repeating until the end-statement is true. i++ = modifies the variable i so that it’ll reach the endpoint. In that example we started from 0 and worked up until i is no longer less than size. However, we can make another array that iterates backwards from the last to first element in the array: // continuing from the previous code snippet // why can I be declared here again? Because of scope17 for(int i = size - 1; i >= 0; i--) { printf("%d ", i); } This block of code prints out each element in the array, starting from the last index to the first index. While-Loops While loops are used to repeat a block of code until the given condition is no longer true.18 Let’s print out the sum of all elements in the array again, but with a while-loop: int size = 5; int i = 0; int sum = 0; int arr[] = {1, 2, 3, 4, 5, 6}; while(i < size) sum +=19 arr[i++20];21 17 Scope is the concept that variables only exist within the code of block that they’re created in (typically in-between {}). For example, in for-loops, the variable i is created inside of the for-loop block, so that means that you can’t reference that variable outside of that for-loop. 18 Honestly, it’s up to preference whether you would like to use a for-loop or while-loop to repeat blocks of code; each always has its advantages, and the more you code the more you’ll know when to use each. I can’t really put in words when to use each. In the wise words of Hoai: “if you are doing something at the end of the loop, for loop is probably better” 19 The same as sum = sum + arr[i++]; 20 This calls arr[i] first, and then increments i by 1. 21 If your repetition / if-structure’s body is only one line then you don’t need to surround it with curly brackets. You got this :) 18 printf("%d", sum); Do-While Loops A do-while loop is similar to a while-loop, but C executes the block of code before checking the given condition: int a = 0; int b = 0; do { a++; } while (a < 10); while (b < 10) { b++; } printf("a: %d\t22b: %d\n", a, b); // prints out: a: 10 b: 10 Although a and b have the same value in the end, the way they got there was slightly different. Operators, Order of Operations, and other Misc. Order of Operations: 1. post-increment / decrement, calling a function (), usage of [ ] in arrays, casting 2. pre-increment / decrement, unary + -23, ! operator, & (address of), sizeof() function 3. *, /, % 4. +, - (math) 5. Relational operators ( <, > , <=, >=, ==, !=) 6. Boolean operators (&&, ||) 7. Assignment (=, +=, -=, *=, /=, %=) 22 23 This is a tab character in C Unary - is making a variable its opposite value. EX: int i = 5; i = -i; and now the value of i is -5 You got this :) 19 8. commas Math Operators: ● Your typical math operators: ○ adding (+) ○ subtracting (-) ○ multiplying (*) ○ dividing (/) ● Modulus (%): the remainder of the division of two integer-decimal numbers ○ ex: printf(“%d”, 15 % 4) prints out 3, because that’s the remainder of the division of 15 by 4. ● Increment (++): increasing an integer-decimal by 1 ○ post-increment: i++ ○ pre-increment: ++i24 ● Decrement (--): decreasing an integer-decimal by 1 ○ post-decrement: i-○ pre-decrement: --i Logical/Boolean Operators: ● AND (&&) ○ Will compare the two values. If they are both true (so both values are not 0) it will return a 1 (true), if not return a 0 (false) ● OR (||) ○ Will compare the two values and check if only one of them is true (so not a 0). If one of the values are true, then it will return a 1, if both are false, then it will return 0 ● EQUAL TO (==) ○ Will compare two values and checks if they are the same. If they are the same then it returns a 1, otherwise returns a 0 24 Why post / pre-increment? Post-increment returns the variable before increasing that variable’s value by 1, while pre-increment increases the value first, and then returns the variable. This applies to pre / postdecrement also You got this :) 20 Casting Casting is converting one datatype to another. It’s best practice to only cast from smaller-sized datatypes to larger datatypes in order to avoid overflow25. Here’s an example of when to use casting: int a = 3; int b = 4; double q = (double)3 / 4; //or you can do 3 / (double)4, and it'd still work the same //if I were to print out variable q it would return 0.75 Typically, with integer-division it would chop off all numbers after the decimal point, so if you were to print out a / b it would output 0. double x = 3.8; int rounded = (int)(x + 0.5); // returns x but rounded to the nearest whole number (chops off decimal points) THIS IS THE END OF THE FIRST MIDTERM REVIEW. BEYOND PAGE 20 IS THE MATERIAL FOR THE REST OF THIS COURSE 25 Smaller datatype can’t support as big of a number as the bigger datatype, so it won’t convert correctly You got this :) 21 Advanced File I/O26 Input Another way of opening a file in C is to store the name in a variable and calling that variable whenever you create the file: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 #include <stdlib.h> #include <stdio.h> #define MAX_SIZE 128 int main() { char *fileName = "brandon-is-cool.txt"; FILE *inFile = fopen(fileName, "r"); if (inFile != NULL) { printf("All is well! File \"%s\" opened without errors!\n", fileName); printf("Here's what's in the file:\n"); char line[MAX_SIZE]; while (!feof(inFile)) { fgets(line, 64, inFile); sscanf(line, "%[^\n]", line); printf("%s", line); } } else printf("Something whack happened opening file \"%s\"\n", fileName); } 6: I stored the name of the file name so that I could use that name later on. 9: If a file can’t be opened, then C will set that variable’s value to NULL 17: This format specifier captures every character up to the newline character \n 26 Refer back to page 15 if you need a refresher on basic File I/O You got this :) 22 Output The second parameter in the fprintf() function is how you want to open the file. So far, we’ve only talked about one of them, which is r, and stands for read only. Here’s some other modes that fprintf() supports: ● � r is read only ● � w is write only Command Line Arguments In C, whenever you compile and run the code (refer to page 3 as a refresher for command line arguments) You pass arguments to it. Here’s an how that would go: >> .\[c_file_name] [argument_1] [argument 2] You can add more than one argument, just make sure to separate them by spaces. A detailed example is shown below of how this would work: Let’s have a text file called fun.txt Minh (17): Sometimes he describes the taste of food as Holidays Erika (15): Pebble Brain go brrr And let’s break down the code that’s on the next page: 6: The header for the main function is a little different. The header this time has an integer which is the number of parameters passed to the main function when the file is run (int argc), and a string array (char *argv[]) which contains each argument. 11: Loops through each index in argv and prints out each element in that array. 14: This for-loop iterates through each argument in argv. 21: This checks if the argument is an openable file 24: This loops through each line in each argument, assuming that each argument is a file passed to it27. 27 i = 1 because the 0th argument is the path of the C file being run You got this :) 23 Let’s have a C file called cmd-args-example.c 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 #include <stdio.h> #include <stdlib.h> #include <string.h> #define S_MAX 128 int main(int argc, char *argv[]) { printf("Number of arguments: %d\n", argc); // print out all the parameters: for (int i = 0; i < argc; i++) printf("args[%d]: %s\n", i, argv[i]); FILE *inFile; // loop through each argument in argv for (int i = 1; i < argc; i++) { char *fName = argv[i]; inFile = fopen(fName, "r"); printf("\nWhat's in file %s?\n", fName); // reads file if it can be successfully opened: if (inFile != NULL) { char line[S_MAX]; while (!feof(inFile)) { //reading in a line of input: fgets(line, S_MAX, inFile); //printing out line: printf("%s", line); } } printf("\n"); } } You got this :) 24 Here’s how you would run the code via terminal (after compiling): ⭐Pay attention to the blue text: that’s how you pass arguments into your C file⭐ 3-4: Result of the for-loop in line 11 from cmd-args-example.c 7-8: Result of the loops starting at lines 21 & 24 in cmd-args-example.c 1 2 3 4 5 6 7 8 PS ~\marshie\coding-review ./cmd-args-example fun.txt Number of arguments: 2 args[0]: ~\marshie\coding-review\cmd-args-example.exe args[1]: fun.txt What's in file fun.txt? Minh (17): Sometimes he describes the taste of food as Holidays Erika (15): Pebble Brain go brrr Base Conversions What are Bases? The current system we are all familiar with is base 10 Where we count from 0 to 9 and then make a new place (ten’s) and start counting up again. (8 -> 9 -> 10 -> 11 -> 12, etc.) Computers on the other hand use something called base 2. Where you bounce between two numbers (0 and 1) and if you want to continue counting, you make a new place. (0 -> 1 -> 10 -> 11 -> 100 -> 101 -> 110, etc.) There are also other base systems like octal (0-7) and hexa (0-15). You probably can figure out how octal (base 8) counts, but what about hexadecimal (base 16)? It works like normal, you start at 0 and keep counting up! So the order will look like this: 0 -> 1 -> 2 -> 3… 8 -> 9 But what do you do after 9? Well to keep things consistent and cram our normal two You got this :) 25 digit numbers in one place, we resort to using the alphabet! Therefore, after 9 in hex is ‘A’ (I think lowercase works too idk). Then you keep going until you hit 15 (‘F’) and after that you start a new place. So it’ll be like: 9 -> A (10) -> B (11) -> C (12) -> D (13) -> E (14) -> F (15) -> 10 (16) -> 11 (17)… 1A (26) -> 1B (27) Whatever is in parenthesis is the base 10 equivalent value! How do you convert from base 10 to other bases? So we get the gist of the bases, how do you actually convert from one to another? Lets go ahead and start off by figuring out how to convert from base 10 to other bases! So lets convert this number, 3010 to base 2! Step 1: Divide the number to convert by the base you want to convert to, and ignore all decimal values (It’s integer division) > So in this case, we will divide 30 by 2! This will get us a quotient of 15 Step 2: Now MOD (modulo) the number to convert by the base you want to convert to > Therefore we will do 30 MOD 2! This should get us with a remainder of 0 Step 3: Put your remainder in the next lowest place value for your answer. > In this case, we don’t have an answer set up yet for 3010 to base 2, therefore our recent remainder we got from step 2 will go in the “one’s” place. (Technically it is not the one's place since it is not base 10, but whatever). Our current answer after step 3 is: 0 Step 4: Check to see if you have a quotient > 0. If you do, then jump to step 1 and repeat the process with the new quotient! Essentially, if you got a quotient > 0, do steps 1-4 until you get a quotient of 0. Recall that our quotient is 15, therefore we gotta go to step 1 again. The (2) indicates this is our second pass through Step 1 (2): Divide new quotient by base to convert to. So we do 15/2 = 7 (we drop decimals) Step 2 (2): Take the MOD of new quotient by the base to convert to You got this :) 26 15 % 2 = 1 Step 3 (2): Put the remainder in the next lowest place (“ten’s). Recall that our current answer is 0, so the next remainder will sit to the LEFT of that number. Current answer = 10 Now we go back to step 4, check our quotient (7), and 7 > 0 is true, so we do it again Step 1 (3): New quotient/new base 7/2 = 3 Step 2 (3): New quotient % new base 7 % 2 = 1 Step 3 (3): Put remainder to the next lowest place. (So to the LEFT of the previous remainder) Current answer = 110 Step 4 (3): Check the quotient [3] from step 1, 3 > 0 True, so repeat Step 1 (4): 3/2 = 1 Step 2 (4): 3%2 = 1 Step 3 (4): Current answer = 1110 Step 4 (4): Check quotient [1], 1 > 0 True, repeat Step 1 (5): 1/2 = 0 Step 2 (5): 1%2 = 1 Step 3 (5): Current answer = 11110 Step 4 (5): Check quotient [0], 0 > 0 False, STOP So now that the quotient = 0, we have our final answer! Therefore, 3010 = 111102! Below will be a condensed version of converting 23610 to base 16 It follows the format of (Original base/quotient) / (new base) = (quotient) R(remainder) 236/16 = 14 R12 -> C (recall that values over 10 become letters with A = 10) Current answer = C 14/16 = 0 R14 -> E Current answer = EC You got this :) 27 Quotient = 0, STOP! 23610 = EC16 - side note: if you write your work like I showed earlier, you can just write you answer from bottom number to the top writing it from left to right For example: R1 R2 R0 R9 Answer will be 9021 !DISCLAIMER! You do not stop the process when the remainder = 0, you stop when the quotient is equal to 0 Here are some additional examples: Convert 48310 to base 8 483/8 = 60 R3 60/8 = 7 R4 7/8 = 0 R7 48310 = 7438 Convert 105610 to base 16 1356/16 = 84 R12 (Which will be represented as C) 84/16 = 5 R4 5/16 = 0 R5 135610 = 54C16 Convert 12110 to base 2 121/2 = 60 R1 60/2 = 30 R0 30/2 = 15 R0 15/2 = 7 R1 7/2 = 3 R1 You got this :) 3/2 = 1 R1 1/2 = 0 R1 28 12110 = 11110012 How do you convert from other bases to base 10? So now that you know how to convert base 10 to any base, what about the other way around? Well it’s quite simple luckily. You just need to set it up a little bit and then add. Let’s go ahead and prove that 111102 = 3010! Step 1: So from the rightmost digit (whatever is in the “one’s” place), we multiply its value by the base to the power of 0 > So in this case, the rightmost digit is 0 and the base is 2, so we multiply 0 * 20. > Thus, for now we have this: (0*20) Step 2: Then we add the next digit to the left (“ten’s”) multiplied by the value of the base to the power 1 (Oh hey that’s +1 power from the last step!) > This means we will add (1*21) to our previous setup > Now we have this: (1*21) + (0*20) Step 3: Then add the next digit to the left, but this time multiplied by the value of the base to the power of 2 (do you see a pattern yet?) > So that gives us this: (1*22) + (1*21) + (0*20) Step 4: I think you know what to do now lol. Keep repeating this until you get to the leftmost digit. Add one to the power as you travel further left > Which should net us this: (1*24) + (1*23) + (1*22) + (1*21) + (0*20) > Which equals: 16 + 8 + 4 + 2 + 0 = 3010 Let's get some more practice! (and these problems should look familiar) Convert EC16 to base 10 - Recall that C = 12, and E = 14 > So rightmost place has a C (12), and we are in base 16, therefore the first thing to do is to do C * 160, which is the same as (12*160) remember you always start with ^0 first!! > Now moving onto the next place, it has an E (14). Now we multiply E*161, which is the You got this :) 29 same as (14*161) > Now if we look to the left again, there is nothing, thus we have finished setting up all the values to add together. All that is left is to simplify and add! (14*161) + (12*160) = 224 + 12 = 23610 The rest will be condensed examples: Convert 7438 to base 10 (7*82) + (4*81) + (3*80) = 448 + 32 + 3 = 48310 Convert 54C16 to base 10 (5*162) + (4*161) + (C*160) [remember C represents 12] (5*162) + (4*161) + (12*160) = 1280 + 64 + 12 = 135610 Convert 11110012 to base 10 (1*26) + (1*25) + (1*24) + (1*23) + (0*22) + (0*21) + (1*20) = 64 + 32 + 16 + 8 + 0 + 0 + 1 = 12110