notes1

notes1 - Lecture 1 • Syllabus class webpage • Class...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Lecture 1 • Syllabus / class webpage • Class questions • Last name A-K: email [email protected] • Last name L-Z: email [email protected] • Getting an engineering computer account • PuTTY/SSH • Homework 0 • Class notes password • Academic Misconduct Policy Lecture 2 • Reading for this week: Chap 1 and Chap 2 • Today’s lecture • Introduction to C Programming • A simple C program: Printing a line of text • Debugging Introduction to C What will we program a computer to do using C? • • • • • • Print output Accept input Arithmetic operations (+, -, *, /, %) Decision making (example: if x > 10, print “x is greater than 10”) Logic (&&, ||) Repetition statements and looping – while – fo r Steps in Wrinting/Executing a Program 1. Write / edit program using an editor program (i.e. vi, emacs). All C programs should end in .c 2. Compile / debug program 3. Execute compiled program A Simple Program in C % vi intro.c 1 2 3 4 5 6 7 8 9 10 11 /* A simple Program in C */ #include <stdio.h> /* function main begins program execution */ int main() { printf( "Welcome to ENGR 3!\n" ); return 0; /* indicate that program ended successfully */ } /* end function main */ Welcome to ENGR 3! Line 1: Comments 1 2 3 4 5 6 7 8 9 10 11 /* A simple Program in C */ #include <stdio.h> /* function main begins program execution */ int main() { printf( "Welcome to ENGR 3!\n" ); return 0; /* indicate that program ended successfully */ } /* end function main */ • Comments in C are written in the form /* … */ • Anything between the comment indicators is ignored by the compiler Line 2: #include <stdio.h> 1 2 3 4 5 6 7 8 9 10 11 /* A simple Program in C */ #include <stdio.h> /* function main begins program execution */ int main() { printf( "Welcome to ENGR 3!\n" ); return 0; /* indicate that program ended successfully */ } /* end function main */ • Lines beginning with # are processed by preprocessor before program is compiled. • This line tells computer to include contents of the standard input/output header. Virtually all programs start with this line. Line 5: int main() 1 2 3 4 5 6 7 8 9 10 11 /* A simple Program in C */ #include <stdio.h> /* function main begins program execution */ int main() { printf( "Welcome to ENGR 3!\n" ); return 0; /* indicate that program ended successfully */ } /* end function main */ • This line is a part of every C program. • All programs are composed of functions. The parentheses after main indicate that main is a function. Lines 6 and 11 1 2 3 4 5 6 7 8 9 10 11 /* A simple Program in C */ #include <stdio.h> /* function main begins program execution */ int main() { printf( "Welcome to ENGR 3!\n" ); return 0; /* indicate that program ended successfully */ } /* end function main */ • Left brace, {, begins the body of every function. Right brace, }, ends each function. Line 7: printf 1 2 3 4 5 6 7 8 9 10 11 /* A simple Program in C */ #include <stdio.h> /* function main begins program execution */ int main() { printf( "Welcome to ENGR 3!\n" ); return 0; /* indicate that program ended successfully */ } /* end function main */ • Instructs computer to print to output the string inside the quotes. • \n = newline, \t = tab (backslash is an escape character) • Every statement must end with a semicolon (;) Some Common Escape Sequences • • • • • \n = newline \t = tab \a = alert (sound the system bell) \\ = Backslash. Insert a backslash character in a string. \” = Insert a double quote character in a string. Line 9: return 0; 1 2 3 4 5 6 7 8 9 10 11 /* A simple Program in C */ #include <stdio.h> /* function main begins program execution */ int main() { printf( "Welcome to ENGR 3!\n" ); return 0; /* indicate that program ended successfully */ } /* end function main */ • Included at the end of every main function (exits the function). In some cases, the compiler may give you a warning if you don’t include this statement. Compile / Execute a Program 1 2 3 4 5 6 7 8 9 10 11 /* A simple Program in C */ #include <stdio.h> /* function main begins program execution */ int main() { printf( "Welcome to ENGR 3!\n" ); return 0; /* indicate that program ended successfully */ } /* end function main */ % gcc intro.c % a.out OR % gcc –o intro.out intro.c % intro.out Debugging • Debugging refers to ridding your program of errors (both syntax and logic errors) • For syntax errors, try to comment out lines and then add them back in. • Logic errors must be determined by examining the output of the program. • Your book lists many common programming errors. Make sure to read these. C Program #2: Adding 2 Integers • Input (read-in) two integer values through the keyboard • Compute the sum of the values • Print the result Desired output (see Fig. 2.5 in book): % prog2.out Enter first integer: 23 Enter second integer: 12 Sum is 35 % C Program #2: Adding 2 Integers 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 /* Add 2 integers */ #include <stdio.h> /* function main begins program execution */ int main() { int integer1; /* first number to be input by user */ int integer2; /* second number to be input by user */ int sum; /* variable in which sum will be stored */ printf( “\nEnter first integer: " ); /* prompt */ scanf( “%d”, &integer1 ); /* read an integer */ printf( “Enter second integer: " ); /* prompt */ scanf( “%d”, &integer2 ); /* read an integer */ sum = integer1 + integer2; /* assign total to sum */ printf( “Sum is %d\n\n”, sum ); /* print sum */ return 0; /* indicate that program ended successfully */ } /* end function main */ C Program #2: Adding 2 Integers • Lines 7-9: – Definition of variables – int means the variables are integers (i.e. -6, 0, 2, 23,…) – These lines could be combined into 1: • int integer1, integer2, sum; • Line 12: scanf( “%d”, &integer1 ); – scanf obtains a value from the user (usually from the keyboard) – This scanf statement has 2 arguments, “%d” and &integer1 • First argument indicates type of data that should be input (%d = decimal integer) • &integer1 – location in memory to store variable • & can be confusing – for now, just remember to include it with variable name in scanf statements • Line 17: arithmetic operation, assigns value to sum • Line 18: – Note differences from previous printf statement – Calculations can also be performed inside printf statements • Printf( “Sum is %d\n\n”, integer1 + integer2 ); Variables • • • • Each variable has a name, data type, and a value The name and data type must be declared at beginning of function The data type must be specified when the integer is declared A few common data types: • • • • int – used to define an integer float – used to define floating point numbers (#’s with a decimal point) double – same as float, only stores more digits after decimal char – defines characters (i.e. ‘a’, ‘x’, ‘X’, …) • Modifiers – short, long • short int • long double Keywords • Keywords have special functions in C and are not to be used as identifiers, such as variable names. • Keywords for C are listed in Fig. 2.15: Arithmetic in C • Arithmetic calculations – Use * for multiplication and / for division – Integer division truncates remainder • 7 / 5 returns 1 – Modulus operator (%) returns the remainder • 7 % 5 returns 2 • Order of Operation – Same as you learned in math classes • Use parentheses when necessary – Example: Find the average of three variables a, b, and c • Incorrect: a + b + c / 3 • Correct: ( a + b + c ) / 3 Arithmetic in C • Arithmetic Operators: (Fig. 2.9) • Order of Operation: (Fig. 2.10) C Program: Arithmetic 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include <stdio.h> /* function main begins program execution */ int main() { int int1, int2, int3, int4, int5; float flt1, flt2; int1 int2 flt1 flt2 int3 int4 = = = = = = 5/2; 5.0/2; 5/2; 5.0/2; 7+3*2+1; (7+3)*(2+1); continued next page C Program: Arithmetic 16 17 18 19 20 21 22 23 24 25 printf( printf( printf( printf( printf( printf( "\nint1 "\nint2 "\nflt1 "\nflt2 "\nint3 "\nint4 is is is is is is %d\n", int1 ); %d\n", int2 ); %f\n", flt1 ); %f\n", flt2 ); %d\n", int3 ); %d\n\n", int4 ); return 0; /* indicate program ended successfully */ } /* end function main */ Typecasting • Allows you to perform data type conversions • Example: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 /* typecasting */ #include <stdio.h> int main() { int i=3, j=4, k=3.4*3; float m=4, p=3.4*3; printf( printf( printf( printf( printf( printf( } "\nj/i (integer division) is %d.\n", j/i ); "j/i (float division) is %f.\n", (float)j/i ); "m/i (float division) is %f.\n", m/i ); "m/i (integer division) is %d.\n", (int)m/i ); "k is %d.\n", k ); "p is %f.\n\n", p ); return 0; Decision Making: the if statement • Every executable statement in C either performs an action (i.e. performs a calculation, prints output,…) or performs a decision • if statement – Simple example of performing a decision – If a condition is true, then the body of the if statement is executed – Conditions in if are formed by using equality operators and relational operators Decision Making: the if statement • Equality and Relational Operators: (Fig. 2.12) Standard algebraic equality operator or relational operator Equality operators = ≠ Relational operators > < >= <= > < >= <= x>y x<y x >= y x <= y x is greater than y x is less than y x is greater than or equal to y C equality or relational operator Example of C condition Meaning of C condition == != x == y x != y x is equal to y x is not equal to y x is less than or equal to y C Program: Decision Making 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 /* Decision Making: using the if statement */ #include <stdio.h> /* function main begins program execution */ int main() { int num1, num2; /* declare variables */ printf( "\nEnter two integers, and I will tell you\n" ); printf( "the relationship they satisfy: " ); scanf( "%d%d", &num1, &num2 ); /* read two integers */ if ( num1 == num2 ) { printf( "%d is equal to %d\n", num1, num2 ); } /* end if */ if ( num1 != num2 ) { printf( "%d is not equal to %d\n", num1, num2 ); } /* end if */ C Program: Decision Making continued 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 if ( num1 < num2 ) { printf( "%d is less than %d\n", num1, num2 ); } /* end if */ if ( num1 > num2 ) { printf( "%d is greater than %d\n", num1, num2 ); } /* end if */ if ( num1 <= num2 ) { printf( "%d is less than or equal to %d\n", num1, num2 ); } /* end if */ if ( num1 >= num2 ) { printf( "%d is greater than or equal to %d\n", num1, num2 ); } /* end if */ return 0; /* indicate that program ended successfully */ } /* end function main */ Algorithms • Basic strategy (recipe) for getting a program to perform it’s functions (coach’s playbook). Consists of: – Actions to be performed – Order in which these actions are executed • Pseudocode – Statements written in plain English which state what you want the program to do • Flow charts: good way to develop an algorithm – Diamonds: decision symbols – Rectangles: action symbols – Ovals: beginning or end of program or section of code Algorithms • Before writing a program: – Have a thorough understanding of the problem – Carefully plan an approach for solving it • While writing a program: – Know what programming tools are available – Use good programming principles ...
View Full Document

{[ snackBarMessage ]}

Ask a homework question - tutors are online