You are on page 1of 6

# COMMAT1 Mock Exam

I. Identification Write the answer as Main ( ) Integer Dennis Ritchie Header Files 1 Integers 2147483647 ' ' ! described by each of the following. 1) The function that is executed first by default for every C program 2) The data type for the conversion character %d 3) The man behind the invention of new B 4) These are outside references of the c program, particularly c libraries 5) These numbers represent TRUE in the C syntax 6) Only these numbers can be operands of the % operator 7) The maximum possible value of an integer 8) Strings are enclosed in these symbols 9) Characters are enclosed in these symbols 10) The unary operator that negates any expression

II. Expressions Write T if the expression is true, F if it is false, and E if it is Erroneous. Assuming integers a = 10, b = 10, c = 7, d = 11, what is the value of each of the following expressions? T 1. a + b > c || b + a / c - d T 9. a >= b && c + d / c == 8 || a != a F 2. a * b % d > b F 10.(d * b *c * a) % c == d % b * c % a F 3. a != b || d == a + 1 && a - 10 || a b T 4. c * d % a == c || (d + c) > c * 3 T 11. a && b && c && d || a-b && c a F 5. d > a >= b T 12. a <= b + c || c b % d && a - b F 6. b / d || b > d T 13. c / d / a / b == 0 == a - a T 7. a + b % b == a F 14. a * b * c * d != 0 && a b > -1 T 8. b * c / a % d != 0 T 15. (9 * b % d) b + (a * c) % 2 == 0 III. Tracing Execute the following lines of code and answer the following questions. 1. int a, b, c, d; for (a=1, c=0, d=0; a<6; a++) { for (b=0; b<a; b++) { if (b % 2 == 0) { c--; d+=4; } else { c+=3; d-=2; } } printf ("%d %d %d\n", a, c, d); } How many lines are there in the program output? What is in output line 3? What is in output line 4? What is in output line 5? What is in output line 6 2. int x=44, y=2; do { x=x-y; switch (x%5) { case 0: y+=4; case 1: y+=3; break; case 2: y+=2; default: y++; } printf ("%d\n", x); } while (x>y); 5 3 4 5 N/A 2 6 9 12 16 24

How many lines are there in the program output? What is in output line 2? What is in output line 3? What is in output line 4? What is in output line 5

5 37 29 20 4

IV. Programming Write a program that will compute for the greatest common divisor of two positive input integers nNum1 and nNum2. You may only use two other integer variables for your solution. Part of the code is already written for you. #include<stdio.h> main() { int nNum1, nNum2, nOutput, n; printf("Enter number 1: "); scanf("%d", &nNum1); printf("Enter number 2: "); scanf("%d", &nNum2); if(nNum1>nNum2) { for(n=1;n<=nNum2;n++) { if(nNum1%n == 0 && nNum2%n == 0) nOutput = n; } } else if(nNum1<nNum2) { for(n=1;n<=nNum1;n++) { if(nNum1%n==0 && nNum2%n==0) nOutput = n; } } printf("GCF is %d \n", nOutput); getch(); } Write a program that will draw the common snowflake, given the size of one side of the snowflake. The snowflake is composed of an X and a cross, and a smaller square in the middle. An example with side = 11 #include <stdio.h> main() { int nSide, x, y; printf("Enter side length: "); scanf("%d", &nSide); for (y=0; y<nSide; y++) { for (x=0; x<nSide; x++) { if (x==y|| x==nSide/2 || y==nSide/2 || y==nSide-1-x || (x>=nSide/4 && x<=nSide*3/4 && (y==nSide/4 || y==nSide*3/4)) || (y>=nSide/4 && y<=nSide*3/4 && (x==nSide/4 || x==nSide*3/4))) printf("*"); else printf(" "); } printf("\n"); } getch(); }

COMMAT1 Computer program The set or list of instructions that directs the computer on its operations Compiler - A computer program that transforms source code written in a programming language into another computer language Programmer The person who develops a program Programming The act of developing a program Steps in Program Planning and Development 1. problem analysis 2. setting up an algorithm 3. coding 4. encoding 5. running, testing, and debugging 6. documentation Algorithm A list or sequence of steps that will solve the given problem Coding The process of converting this list of instructions to a code or language that the computer can understand and execute Program The list of instructions understood by the computer Encoding The process of entering the program through a computer terminal directly into computer memory Bugs Errors that come out during program execution Debugging The art of correcting these errors Syntax The grammatical rules of the language defining its legal constructs Compilation Translating the program codes into a form that the physical computing machine can understand Ken Thompson Designed and implemented the first high-level language implemented under the UNIX operating system in 1970 in Bell Laboratories. This was B, which was based on BCPL. Dennis Ritchie Designed and implemented a new typed language based on B originally called NB (New B) but later named C at Bell Laboratories in 1972. Tokens Words in a code Literals Numeric 1. No comma. 2. No space between the unary sign (+ or -) and the digits. 3. Must begin and end with a digit. Integers Whole numbers (ex. 0, -1, 4, 16) Floating point Real numbers (ex. 3.14, e, 0.33, .57, -1.2) Non-Numeric Character Enclosed in ' ' (ex. 'a', ':', '9' '+') String A series of characters enclosed in (ex. La Salle, Hello, 09123456789) Identifier Defined by the programmer 1. Must consist of only letters, digits, and underscores 2. Cannot begin with a digit 3. Not allowed to be keywords 4. Case sensitive Variables Can store a value that can be changed Constants Can store a value that cannot be changed Keywords These are special words reserved by the programming language for processing Expressions Arithmetic Operations which yeild numbers Operator Meaning Rule Example + Addition integer + integer = integer integer + real = real real + integer = real real + real = real integer - integer = integer integer - real = real real - integer = real real - real = real 5+2=7 5 + 2.0 = 7.0 5.0 + 2 = 7.0 5.0 + 2.0 = 7.0 5-2=3 5 - 2.0 = 3.0 5.0 - 2 = 3.0 5.0 - 2.0 = 3.0

Subtraction

Multiplication

integer * integer = integer integer * real = real real * integer = real real * real = real integer / integer = integer integer / real = real real / integer = real real / real = real integer % integer = integer

5 * 2 = 10 5 * 2.0 = 10.0 5.0 * 2 = 10.0 5.0 * 2.0 = 10.0 5/2=2 5 / 2.0 = 2.5 5.0 / 2 = 2.5 5.0 / 2.0 = 2.5 5%2=1 -5 % 2 = -1 5 % -2 = 1 -5 % -2 = -1 2%5=2

Division

Remainder

Relational Comparing values; Yeilds either 0 (false) or 1 (true) Relational Operators < <= > >= Logical Less than Less than or equal Greater than Greater than or equal Logical Operator && Meaning and Truth Table true && true = true true && false = false false && true = false false && false = false true || true = true true || false = true false || true = true false || false = false !(true) = false !(false) = true == != Equality Operators Equal Not equal

||

or

not

Operator Precedence (from highest to lowest) () !, unary +, - (positive/negative) *, /, % binary +, - (addition/subtraction) <, <=, >, >= ==, != && Structure of a C Program <Header Files> <Constant Definitions> <Function Definition> <Main Program> <Variable Declaration> <Statements> Comment Enclosed in /* */ (for multiple lines) or preceded by // (for single lines); Not read by the program Header Files Statements that tell the compiler what additional files to process before processing the actual program #include <<header file>> example: #include <stdio.h> Constant Definition Constants are defines for values which will not change throughout the entire program

#define <constant identifier> <literal> example: #define SCHOOL De La Salle University Main Program The main section or the main block of the entire program; The first part of the program to be executed main() { /*variable declaration*/ /*statements*/ } Variable Declaration Tells the C compiler what type of data will be stored in each variable and how that data will be represented in memory <datatype> <variable list>; examples: int nAge; float fTotal; double dAmount; char cOption; Range Of int and long data types is -2147483648 to +2147483647 Specifier Output Data Type %c %d or %i %f %lf* Character Integer Float Double* char int float double

%s String string *%lf is used only for scanf, use %f for printf Assignment Statement Stores a value or a computational result in a variable <variable> = <expression>; examples: x=1; total=num1+num2; final=last; Variables can be given an initial value at the start of the program <datatype> <variable> = <value> example: int nMiles=2; printf() - Used to display to the console printf( <format string> , <print list> ); examples: printf(Hello world); printf(Your grade is %d, nGrade); scanf() - Used to take input scanf( <format string>, &<input list> ); example: scanf(Enter age: , &nAge); f Found in printf and scanf stands for formatted Conditional Statements Statements that check certain conditions before executing certain statements Single Selection Statement The statement will be executed if and only if the expression evaluates to true if (expression) statement; Multiple Selection Statement Allows us to choose among alternatives if (expression) statement1; else statement2; Switch Statement Can only check equality relationships switch (expression) { case label1 : statement1; break; : : : case labeln : statementn; break; default : statement; } Loop Allows an operation to be repeated multiple times

While Statement continues to execute as long as the expression evaluates to true while (expression) statement; Do-while Statement The body of the loop is always executed at least once because the condition is not checked until the end of the loop. do { statement; } while (expression); For Statement It executes a statement (or group of statements) for a specified and definite number of times for (controlvar = initcount; expression; increment) statement; controlvar is an arbitrary integer variable initcount is the starting count of the loop and the starting value of the controlvar expression is the condition for the loop to continue increment is the next value of the controlvar (ex. for (i=1; i<=100; i++) statement;)