H192 Midterm 1 Review Tom Zajdel
Declaring variables Need to specify a type when declaring a variable. Can declare multiple variables in one line. int x, y, z; float a, b, c; Can also initialize in same line (assign initial value) float radius = 5.0, area; What is in variable area?
Types Type Description Example int integer int a = 5; long long integer long a = 500; float Floating decimal point float a = 4.2; double long float double a = 5000.25; char character char a = 5 ; char a = A ;
Precision Precision deals with how much information a variable stores Precisions: int < float < double Int < long If an assignment puts a value of high precision into a lower precision variable, information is lost. Decimal truncation most common example int b = 5.99; What is the value in b?
Arithmetic Math operators: + addition - subtraction * multiplication / division % remainder (modulus) Other operators & address reference operator * used to declare pointers
Arithmetic The operand with highest precision is used, the other operand is made more precise A float has greater precision than an int. int x = 5; float y = 7.5, z; z = 5 + 7.5; -> converted to 5.0 + 7.5 z = 12.5;
Arithmetic Be careful with integer division 1/2 =? 1.0/2 =? 1/2.0 =? 1.0/2.0 =? Can use explicit type casting to solve problem 1 / (float) 2 1/2.0 Lets programmer explicitly change the value s type
Arithmetic How do you use exponents? #include <math.h> ^ Use this header before the main function Gives you access to pow function pow (x, n) = x n pow (2, 3) =?
More arithmetic num *= 2.0 num = num * 2.0; count++ count = count + 1; Know difference between count++ and ++count? Prove it! Assume x = 10, and y = 12, z = 0, all are ints; z = x - ++y; What does z equal? y? Why?!
Problems int v = 5, w, x =.99, y = 6.1, z; float a = 2.5, b = 3, c = 2/5, d, e; z = a * (y * x) + v + a; /* z =? */ d = pow(b, x) * a; /* d =? */ e = c + (y % v) / 2.0; /* e =? */
printf printf( Output string, a, b, c, ) Puts output to screen Takes an output string and any number of variable values to be printed Each variable to be outputted is indicated in the output string float = %f int = %d or %i char = %c
printf int x = 10; float y = 4.5323235232; char z = C ; printf( x = \ny = \nz =,,, ); What will this code print?
printf Answer: printf( x = %d\ny = %f\nz = %c, x, y, z); The code prints: x = 10 y = 4.5323235232 z = C How do you clean up the decimal? Instead of %f, use %.2f to format to two decimal places
scanf scanf( format string, &a, &b, &c, ) Gets user input Needs to get the format of input, and variable addresses where the values will go Addresses use the & address of or reference of operator Example format string %f%f%d%d Two floats, followed by two ints
scanf int x; float z; printf( Please enter an integer > ); scanf(, ); z = x / 2.0; printf( Your number is now, ); What will this code do?
scanf Answers int x; float z; printf( Please enter an integer > ); scanf( %d, &x); z = x / 2.0; printf( Your number is now %f, z);
File I/O fscanf and fprintf take input from files, and print input to files, respectively Must declare a FILE pointer. To do that, use the pointer * FILE * input, output; Once declared, initialize with fopen
File I/O fopen( name, r/w ) Opens a file with called name r for reading (input) w for writing (output) Pass this function to your FILE* variables input = fopen( data.dat, ); output = fopen( results.txt, );
File I/O What if file does not exist? input = fopen( data.dat, r ); Assume that data.dat does not exist! fopen returns NULL, so input == NULL Safest to check if fopen was successful before moving on if (input == ) printf( \nfile failed to open! );
File I/O fprintf(output, output string, a,b,c, ) fscanf(input, format string, &a, &b, &c, ) Note the similarities to printf and scanf as mentioned before. These functions take the additional FILE* parameters created using fopen
Let s take a compiling break Source: xkcd
if/else if (conditional) Check the conditional. If the condition is true, run the line of code immediately after the if. Otherwise, check the next else if or else. if (x _ b) printf( x is less than b! ); else printf( x is not less than b! );
if/else Can use compound conditions to combine them ( condition1 && condition2)? (condition1 condition2)? Choices for conditionals == < > >= <=!= Why shouldn t you use = in a comparison?
if/else To run more than one statement after an if, create a { functional block! } int tom = 1, awesome = 1; if (tom == awesome) { printf ( Tom is awesome! ); tom = 5; printf( Tom is no longer awesome! Bwahaha! ); }
switch/case Uses either an int or a char as a switch Selects the right case to run can be either conditional or value char letter; scanf( %c,&letter); switch(letter) { case A : case a : printf( A! ); break; } default: printf( Not A! );
switch/case Note: program keeps running all code under the case, until it reaches a break or the end of the switch. Be careful to only run cases you want! default is the case that occurs if all else fails
if/else switch/case if (number == 5) { do this code; } else if (number == 10) { do this code; } else { do this code; } switch ( ) { case ( ): do this code; ; } case ( ): do this code; ; default: do this code;
if/else switch/case if (number == 5) { do this code; } else if (number == 10) { do this code; } else { do this code; } switch (number) { case 5: do this code; break; } case 10: do this code; break; default: do this code;
Loops - while while (condition) { do this code } Keeps looping while condition is true
Loops - while char answer = y ; while ( ) { } printf( Do you still think Tom is awesome? > ); scanf(, ); Finish the loop!
Loops - while char answer = y ; while (answer == y ) { } printf( \ndo you still think Tom is awesome? > ); scanf( %c, &y);
Loops while What if we were afraid this would be an infinite loop? Use a counter. Let s say we want to run five times max. Finish the code! char answer = y ; int counter = 0; while (answer == y && ) { printf( \ndo you still think Tom is awesome? > ); scanf( %c, &y); ; }
Loops do while Runs at least once, then checks conditional after the code char answer; do { printf( Do you still think Tom is awesome? > ); scanf(, ); } while (answer == y ) ; //<-- Note the semicolon!!
Loops - for A definite loop Defines the following Initial counter value Looping condition Counter increment int k; for (k = 0; k < 20; k++) //This loops how many times? { CODE // What would I put here } // to print numbers 1 to 20?
Arrays An array is a line of values of the same data type, stored contiguously in memory int arr[7]; //Creates an array of integers of // length 7, i.e. 7 integers arr[0] arr[1] arr[2] arr[3] arr[4] arr[5] arr[6]
Arrays int arr[ ] = {0, 1, 2, 3, 4, 5, 6}; //Initializing arrays char name[ ] = { F, E, H, 1, 9, 2 }; printf( arr[1] = \n name[5] =,, ); Finish the code! What does it display?
Arrays Can initialize array with a loop int array[10], i; for (i = 0; i < 10; i++) { array[i] = i * 2; } What is in array[ ] after this code runs?
Arrays Finish following loop to print values in array. Use array as defined in last slide, which is an array of ints with length of 10. int k; for ( ; ; ) { printf( ); }
Arrays Write a loop to display the information in name char name = { F, R, E, U, E, L, E, R }; for ( ; ; ;) { printf( ); }
Arrays Let s get input data from a file, using fscanf. The file s name is data.dat and it contains 20 floats, each on a new line. input = fopen(, ); float numbers[ ]; for (int i = 0; ; ) { fscanf( ); }
Functions You can write your own functions! Each function has 3 components Name Return type Parameter list, of names and types For example, double pow(double x, double n) pow takes two doubles and returns a double (which can usually be manipulated as floats)
Functions Write a function prototype at the top of your program (above main) Contains all info about function, it s like defining a variable before using it float min(float a, float b); This will be our function prototype. Let s define our function under main
Functions #include <stdio.h> float min(float a, float b); int main() { } float min(float a, float b) { } //FUNCTION PROTOTYPE //FUNCTION DEFINITION
Functions We want to return the smaller of two input values. Define the function! float min(float a, float b) { }
Functions #include <stdio.h> float min(float a, float b); int main() { float number = min(5.0, 6.0); //WHAT WENT INTO number? } float min(float a, float b) { if (a < b) return a; else return b; }
Functions Note a and b only live inside the function min. They cannot be used outside the function. Variable values may also be passed into a function. float x = 1.0, y = 2.0, z; z = min(x, y); NOTE: a and b are not usable in this segment they are only defined in min!
Functions Let s write our own power function, ipow. It will take a float and an int. If power = x n, x is the float, and n is the int. Return an the result as an int. What is the function s prototype?
Functions The prototype is int ipow(float x, int n); Now, define the function. HINT: x n = x * x * x n times int ipow( float x, int n) { for ( ; ; ) } return ;
Flowcharts Start/End START END General action process
Flowcharts Print to screen Number of students is, students printf( Number of students = %d, students);
Flowcharts Decision structure (if, switch) A<B? F T if (A < B) statement; else other_statement;
Flowcharts Indefinite loop while index <= limit n y while (index <= limit) looping_statement;
Flowcharts Definite loop k = 0 k++ <= limit n y for (k = 0; k <= limit; k++) looping statement
Thank you! Best of luck tomorrow!