-
what is a program
set of instructions for accomplishing a task
-
what are the two key considerations when writing instructions
- 1. write clear unambiguous instructions
- 2. write in a language that is understood
-
what does 1 represent
true or the power being on
-
what does 0 represent?
false or the power being off
-
why dont programmers program with 1 and 0
time consuming and prone to cause errors
-
comments
- - not required by the compiler
- -essential for readers to understand program
- - cant have a comment within a comment
- - /*comment*/
- -minimum code should have one comment telling the reader what the program is intended to do the date and the author
-
include statements
- tell the compiler witch sets of eextra commands (functions to permit the user to use ) example standard IO
-
semicolons
each statement must end in a semicolon
-
pseudocode
- is a description of a computer program or algorithm
- -uses conventions of computer programming
- -written for humans to read rather than machines
-
compiler directives
- commands given to the compiler
- always stars with #sign
- example #include <stdio.h>
- tells the program to include the contents the program its making -- user telling the compiler to use it
-
the main function
- a function is just a program that returns a value
- every c program must contain one function
- that is the function that is executed when the program is run
-
variables
- -used to store values
- -must be a type and must be written (it float double char)
- -example:
- Int number;
To assign a value to the variable
number = 4;
-
variable declaration
int area; variable declaration
-
storing an initial value
- int area= 1;
- called initializing a variable
type shows how much memory to store for the variable
ultimately all data is stored as numbers
-
identifiers
- the name of the variable
- rules:
- not to short or too long
readers should be able to know what its used for
use camel case
example
camelCase
don’t start with capital or number
no blanks commas or special characters
may contain letters underscores or digits
must start with a letter not a number or underscore
case sensitive
don’t choose similar identifiers that differ by case
-
expressions
- -combinations of constants and variables and function calls
- -have both value and type
- -can have logical conditions that are true or false
- - has some operator (=<>)
-
-
unary operators
only has one argument
-
binary operators
two operands one on each side of the operator
%remainder “modulo”
&& and
|| or
<= less than or equal to
>= greater than or equal to
? conditional operator
-
assignment operator
used to change the value of a variable
-
assignment statements
value of a variable is replaced with the value of an expression
-
algorithms
computable set of steps to achieve a desired result
-
algorithm process
- 1.
- Identify the program requirements
- 2.
- Investigate the problem
- 5.
- Implement the algorithm
-
boolean logic
- Some operator create expressions that return
- only a true or false value
- n Needed when
- computer needs to know whats next
n True (1)
n False (0)
n && and
n || or
- n ! not causes expression
- that is true to be false and vice versa
-
if statements
if (expression) statement;
-
what is a block of statements
statements surrounded by braces
-
switch statement
- a multiway conditional
- statement that generalizes the if-else statement
- - use it when there are multiple if statements
- - 1 switch (expression)
- 2 {
- 3 case label1: statement1;
- 4 case label2: statement2;
- 5 ...
- 6 case labeln: statement;
- 7 default: defaultStatement;
- 8 }
cases are based on the value of the expression
-
pointer
- a variable whose value just happens to be a memory address
- -position in memory called address
- -manipulate the values stored at specific memory addresses
- -strings are pointers
- int *x, y=24;
- x=&y
- x is assigned the location of the variable y in memory
- *-- extracts the address of y NOT THE VALUE
- *x=&y DOES NOT WORK
- use scanf ("%d", &*X);
- or scanf("%d",x);
-
pointers and strings
- when using scanf dont use & to read the string
- char word[20];
- scanf("%s", word);
char *jedi = "Mara Jade";
cant alter the string -- segmentation fault
-
syntax errors
- prevent the program from compiling
- violates grammar rules for the C language
-
logic errors or semantic errors
- prevent the program from running correctly in all situations
- doesnt do what you want it to
- examples
- infinite loop
- misunderstanding operator precedence
-
divide and conquer
- use temporary printf statements to check the output
- helps you tell which part is working and which needs debugging
-
run time errors
- errors occur when you run a program
- two types:
- logic errors
- fatal errors
-
fatal error
- when the executable crashes
- stack overflow
- divide by zero etc
-
-
-
for loops
- number of passes through the loop is known in advance
- 1 for (expr1; expr2; expr3)
- 2 statement;
expr1 initialize some parameter (control variable loop variable or index)
expr2 is the condition that must be true for the loop to continue
expr3 is used to modify the value of the control variable initially assigned by expr1
-
nesting loops
- loop within a loop
- executed form the inside out
- 1 for (i = 1; i <= 4; i++)
- 2 {
- 3 for (j = 1; j <= 5; j+=2)
- 4 {
- 5 k = i + j;
- 6 printf("i=%d, j=%d, k=%d", i, j, k); 7 }
- 8 }
- In the example above, i is the outer loop counter and j is the inner loop counter. The outer loop is executed 4 times, when i =1,2,3, and 4. For each i value the inner loop is executed 3 times. Since j=1,3, and 5 the inner loop is executed 4*3 or 12 times.
-
while loop
- situations in which the number of passes is not known in advanced
- while (expression)
- 2 statement;
- expression represents a condition that must be met
- repeats until expression is not true
- statement must include something that alters the expression to avoid infinite loop
- can have nested loops
- loop control variables should be integers
-
break
can be used to provide an early exit from for while a and do loops
-
continue
the continue statement causes the current iteration of a loop to stop and the next iteration to begin immediately
-
do while
will execute at least once
-
usability looks at six components which are:
- 1. learnability -- user can learn how to accomplish the basic task the first time that the software is used
- 2. efficiency -- quick and minimum effort from the user
- 3. memorability -- user can remember how to use it
- 4. errors - software is usable if user makes errors
- 5. satisfaction -- pleasant and enjoyable to use
- 6. utility -- high utility when it provides the features needed to do the tasks
-
float point arithmetic
is not exact
-
cancellation error
when large number may cancel out the smaller number
-
underflow
very small computational result is represented by a zero
-
overflow
a error which results from an attempt to represent a computional relut that is too large to fit into the storage type
-
automatic conversion
- expression is being evaluated
- ex int converted to double
- going from d to i is okay
- going from i to d loses info
-
-
function
self contained program segment that carries out some specific tast
-
function definition has two components
- header (including parameter declarations
- body of the function )
- general form
- 1 function_header
- 2 {
- 3 functionBody
- 4 }

-
array
data type that can hold multiple vaues
|
|