









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!










Basic Tools for Building Algorithms Atomic Data
Rules for Variable Identifiers in C A sequence of letters, digits, and the special character _ (underscore). A letter or the underscore character must be the first character of an identifier. The C language is case-sensitive. This means that first and First are two different identifiers. Atomic Data Types in C Numbers
Associativity (left to right) 10 + 3 + 7 = 20 10 – 3 + 7 = 14 15 / 5 * 2 = 6 Assignment Operator / Assignment Statement general form: variable = expression; The expression can be a constant or a variable. int x, y; x = 5; y = x; x = 6; The expression can be an arithmetic expression. x = y + 1; y = x * 2; x = x + 10; Assignment compatibility: There must be compatible types on both sides of the assignment operator or an error will occur. int x; double y; char c; c = ‘9’; /* ok / c = ‘10’; / error / c = 9; / ok in C, not all languages though / x = y; / y is truncated to fit into x / y = x; / ok / x = 5 + 3.2; / truncates the result to 8 / y = 5 + 3.2; / ok / x = 10/4; / x is 2, this is integer division, no remainder / y = 10/4; / y is 2.0 / y = 10/4.0; / y is 2.
For pointers, assignment means one of the following depending on how it is used: (1) Creating a new variable. If we already had: int *this; int that; and then we do the following: this = malloc(sizeof(int)); this will allocate a new memory space for the pointer and makes the pointer variable refer to it as shown below: (2) Assign the address of a declared variable. int alpha; this = α / this refers to the same address as alpha */ (3) Assigning a value to a variable to which a pointer points. *this = 4; This type of assignment will assign the value 4 to the integer variable to which the pointer variable refers. situation before assignment: situation after the assignment: (4) Making one pointer variable refer to another pointer variable. that = this; Input and Output Operators in C this? this? this 4 this 4 that
Conversion Character Works With How corresponding argument is read or written c printf or scanf as a character d printf or scanf as a decimal integer e printf as a floating point in scientific notation f printf or scanf as a floating point g printf in the e or f format whichever is shorter s printf or scanf as a string lf scanf as a floating point (double) Lf scanf as a floating point (long double) A Simple C Program Literals /* This program adds the two numbers that it reads and prints the sum / / LAB SECTION NUMBER: ASSIGNMENT NUMBER: YOUR NAME: DATE: / #include <stdio.h> int main (void) { int num1, num2, sum; / variable declarations / printf(“Enter your first integer. \n”); /display user prompt / scanf(“%d”, &num1); / read first integer / printf(“Enter your second integer. \n”); /display user prompt / scanf(“%d”, &num2); / read second integer / sum = num1 + num2; /calculate sum / printf(“The sum is %d.\n”, sum); /print the sum / return(0); /programs ends correctly */ }
Literals are data values that are “hardwired” into the algorithm. The value of a literal is taken literally! Each of the following assignment statements contains a literal value. int num, score; char grade; num = 1; grade = ‘A’; score = 42; Constants A constant allows the designer of the algorithm to give a name (identifier) to a fixed value. This makes the program easier to read and understand. For example, suppose we define the following constants: #define PI 3. #define NUMBER_STUDENTS 60 Then instead of : area = 3.1415926 * radius * radius; we would have: area = PI * radius * radius; Or, instead of average_score = total_of_scores/60; we would have: average_score = total_of_scores/NUMBER_STUDENTS; Another Simple C Program Example Algorithms - 8 /* This program will determine the circumference and area of a circle given its radius / / LAB SECTION NUMBER: ASSIGNMENT NUMBER: YOUR NAME: DATE: / #include <stdio.h> #define PI 3. int main () { / variable declarations / double radius, diameter, circumference, area; / get the data values / printf(“Enter the radius”); scanf(“%d”, &radius); / perform the calculations / diameter = radius * 2; circumference – 2 * PI & radius; area = PI * radius * radius; / display the results */ printf(“\n\nThe circumference is: %d “, circumference); printf(“The area is %d.\n”, area); }
Using a graphical description such as a flowchart the algorithmic decision would be represented as: false true Using a programming language, such as C, the algorithmic decision would be expressed as: if (number_of_hours <= 15) … Relational Operators The general syntax of a conditional expression is:
= 15
strings of characters with these operators. Characters are compared in alphabetical order, so for example, ‘a’ < ‘b’. Practice Problems Here are some examples of Boolean expressions. See if you can figure out if they are true or false. char ch; int x, y; ch = ‘j’; x = 62; y = 3x – 10 % x; (1) x + y > 40 (2) ch == ‘k’ (3) (x-y) != (7 * (3 – x % 7)) Boolean Operators All but the simplest of algorithms will require complex decisions to be made during execution of the algorithm. Frequently, the decision will have multiple parts. For example, if (the class is not full) AND (it does not conflict with a class in the schedule) … Boolean operators allow us to express compound conditional like the one above. There are three Boolean operators. AND (&&) o Binary operator. o Requires that both conditions be true if the expression is to evaluate to true. o ((5 > 4) && (4 > 7)) evaluates to FALSE. OR (||) o Binary operator.
Value of a Value of (!a) True False False True Truth table for NOT (!) operator Acting on Decisions Once the conditional expression has been evaluated (you know whether it is true or not) a control structure is required to dictate what happens next in the algorithm. Most modern programming languages have several different types of control structures. Perhaps the simplest of these is the if-then- else construct: general form: if <boolean_expression> statement 1; else statement 2; statement 3; statement 4; … Execution of an if-then-else statement is as follows:
if (