Lecture #18: Review

November 12, 1999

Bob Pitha

bpitha@cs.ucsd.edu

http://www-cse.ucsd.edu/classes/fa99/cse5a

lectures

•      Lecture 2: Computers and computer languages

–   Basic terminology

–   Computer numbering

•   binary

•   octal

•   hexadecimal

–   What is a computer program?

–   Computer languages

•      Lecture 3: How is programming like driving?

–   That “driving directions” analogy

–   Example program: “Hello, world!”

•      Lecture 4: The C Language

–   A second example program: printing numbers

•   variables

•   loops

–   Introducing variables and data types

•   identifiers/variable names

•   why have data types?

•   integer types

•      Lecture 5: Data Types

–   The 7 basic data types

–   FtoC example program

•      Lecture 6: More Data Types and Constants

–   signed/unsigned data types

–   constants

•   numerical constants

•   character constants (which are numbers, too!)

•   escape sequences

•      Lecture 7: Declarations

–   variable declarations

–   initialization

•      Lecture 8: Operators

–   operators and expressions

–   expression concepts

•   lvalues!!!

–   conversions in expressions

–   precedence and associativity

–   arithmetic operators

•      Lecture 9: More operators

–   increment/decrement operators

–   assignment operators

–   relational operators

•      Lecture 10: Operators, expressions, statements

–   equality operators

–   logical operators

–   expressions

–   statements

•   expression statements

•   compound statements

•      Lecture 11: Statements 2

–   selection statements: if-else

•      Lecture 12: Statements 3

–   the conditional expression

–   selection statements: switch

–   iteration statements (loops)

•   while

•   for

•      Lecture 13: Statements 4

–   iteration statements

•   do-while

–   jump statements: break and continue

•      Lecture 14: Writing Programs

–   interpreting a program

•   multiplication table example

–   writing a program

•   printing prime numbers

•      Lecture 15: Quiz answers and discussion

•      Lecture 16: Functions 1

•      Lecture 17: Functions 2

textbook sections

•      Chapter 1: A Tutorial Introduction

–   this isn’t necessary, but it does give some decent examples.

•      Chapter 2: Types, Operators, and Expressions

–   we’ve covered basically everything except the bitwise operators (§2.9)

–   don’t worry about subjects we haven’t addressed in class:

•   §2.3: strings and enumeration constants

•   §2.4: const declarations

•      Chapter 3: Control Flow

–   we’ve covered the whole chapter

–   it contains valuable information and examples

•   they use getchar()

–   §3.8 Goto and Labels

•   this isn’t critical material, but it’s good to know

•      Chapter 4: Functions and Program Structure

–   §4.1 – 4.4

–   §4.2 uses arrays in its example

exercises

•      Consider trying the exercises in your text, especially:

–   Chapter 1

•   1-1 – 1-5

–   Chapter 2

•   2-2, 2-10

–   Chapter 3

•   3-1

review questions

•      These questions are just to help you review:

–   What does the line #include <stdio.h> do for us?

–   Why do we use comments?

–   What are the largest and smallest values that can reliably be stored in a:

•   char

•   int

–   What is the difference between the constants 6 and ‘6’?

–   What are the two different kinds of division that the / operator can do? Under what circumstances does it perform each of them?

–   Name at least two different uses for the semicolon in C

–   what would the equivalent code, using a while loop, be for this example:

Text Box: for (i = 0; i < 10; i = i + 1)
    printf (“i is %d\n”);
 


–   What is the numeric value of the expression 3<4?

–   Under what conditions will this code print “water”?

Text Box: if (T < 32)
    printf (“ice\n”);
else if (T < 212)
    printf (“water\n”);
else
    printf (“steam\n”)
 


–   What would this code print?

Text Box: int x = 3;
if (x)
    printf (“yes\n”);
else
    printf (“no\n”);
 


–   What would this code print?(Caution: trick question!)

Text Box: int i;
for (i = 0; i < 3; i = i + 1)
    printf (“a\n”);
    printf (“b\n”);

printf (“c\n”);
 


–   What do these loops print?

Text Box: for (i = 0; i < 10; i += 2)
    printf (“%d\n”);
 

Text Box: for (i = 100; i >= 0; i -= 7)
    printf (“%d\n”);
 

Text Box: for (i = 1; i <= 10; i++)
    printf (“%d\n”);
 

Text Box: for (i = 2; i < 100; i *= 2)
    printf (“%d\n”);
 


–   What is the difference between a function declaration (prototype) and a function definition?

–   What is the difference between the prefix and postfix forms of the ++ operator?

–   (trick question) What would the expression

Text Box: i = i++
 


   do?

–   Write a program to print the numbers from 1 to 10 and their squares, like this:

Text Box: 1.	1
2.	4
3.	9
4.	16
etc.
 


   You should use a function to compute the squares of the numbers. It might have the following declaration:

Text Box: int square (int i);
 


–   Write a program that prints a triangle, like this (using loops, not just a bunch of printf statements!):

Text Box: *
**
***
****
*****
******
*******
********
*********
**********
 


–   Write a function that computes and returns the average of five numbers, which are passed in as arguments. The function should be declared like this:

Text Box: int average (int a, int b, int c, int d, int e);
 


–   Write a program that prints the numbers between 1 and 10, along with an indication of whether each is even or odd (you can use the % operator). Your output should look like this:

Text Box: 1 is odd
2 is even
3 is odd
etc.