Category Archives: CSE 130


Statics vars allow a var to retain value for multiple declarations, does not reinitialize each time.

Static & extern are auto initialized to 0

Structures (Structure Type)

possible to aggregate components into a single named variables

IE: Bank Account – Account #, balance, interest, name, address

Structure is user defined. You can declare an array of structures

struct account {
long number;
float balance;
float interestRate;
} accnt2, myAccounts[3];
struct account myAccount;

Members can be accessed using structure member operator “.”

myAccount.number = 1248609385;
myAccount.balance = 1004.005;
myAccount.interestRate = 0.18237672;

Can declare a structure and then declare the variables

struct fruit {
char name[15];
int calories;
typedef struct fruit fruits;
struct fruit lunch = {“plum”,150}; // order is important
fruits dinner = {.calories=150,.name=”peach”}; // order not important

you can copy structs of the same type to each other dinner = lunch

Leave a Comment

Filed under CSE 130

Enumeration Types & Storage Class

Enumeration Types

  • Int Float and double have a large range.
  • Chars are stored using their Interger ASCII values 0 to 255. This only takes up one int byte.
  • Data type specifies a range of values
  • A variable of a specified type can have any value form this range or set.
  • What if values are from a set of non numeric values? IE Days in the week
  • Enumeration means to describe or list or name the values, one by one.
  • Used to name a finite set

enum color {red,blue,green,yellow}

color is the tag name
“enum color” is the type
You can give variables this type, and assign them values from the set you described

enum color c1,c2;
c1 = green

That just declared two variables of type enum color. Say you get bored of typing on enum color all the time. You can define the type using typedef.

typedef int newIntType

That just created a new type of interger variable type called newIntType. Enum stores things as intergers. For example, in the color example, red corresponds to 0, blue to 1 and so on…

enum day{sun,mon,tues,wednes,thurs,fri,sat};
typedef enum day days;
days today = fri;
switch (today) {
case fri:
//And so on….

Enums are good for switch cases!

Storage class
Variables have two attributes, type and storage class. Storage class in how and where memory is allocated. There are four types in C. auto(matic), extern, register, static.

Auto – Used for local variables, kept on the stack
Extern – global variables, present until the program stops running, not kept in Stack.
Register – defaults to auto if necessary, kept on the processor chip. Very special and used to improve execution speed, treated as advice only.

Leave a Comment

Filed under CSE 130

More on Recursive Functions

Ackermans Function – Interesting Recursive function example

Towers of Hanoi Problem

n-1 to tower B using C as temp storage.

n to tower C

n-1 to tower C using A as temp storage.

Leave a Comment

Filed under CSE 130


int rabbit(int n)
if (n==1) return 1;
else if (n==2) return 2;
else return rabbit(n-1)+rabbit(n-2)

All recursive solutions can be stated in iterative terms (loops)

Leave a Comment

Filed under CSE 130

Newtons Method for Square roots

(pages 131 – 132 in text book)

Set allowed Error variable

if guess2 –x < error { Square root found }
guess = (x/guess + guess) /2
else, guess again…

Function must be written in line by line order, unless you write a header for the function above main.

Use Function prototyping to avoid having to write your code in bottom up order.  Function prototyping is placing the header for the function above main.

The .h files represent header files! only the functions actually included in your code are taken and inserted into your compiled code.

You can restrict access to functions by prototyping a function inside of another function. Don’t actually write the function body into another function though.

Leave a Comment

Filed under CSE 130

Automatic Local Variables

Automatic Local Variables

  • Variables defined inside a function
  • Created each time a function is called
  • Initialized and get a value each time
  • Accessible inside function they are declared in only.
  • Not available outside the function

The Stack Frame keeps an activation record For Example

Main variables
Printf variables
Another Function Variables

And so on.

Language translation is done by the compiler
Language implementation is done by the stack

Variable Scope
Score refers to the area of a program for which a  variable is defined

Global Variable

  • Declared above main
  • Constants tend to be global
  • No need to pass global var’s

Leave a Comment

Filed under CSE 130

More on Functions

A function definition consists of a header and body. Header specifies return type, name and arguments list.

return_type name(int j, int i) {
return return_type;

You can declare local variables inside a function. If you do not want to return a value, return type is void.

There are 4 types of functions:

  1. No Input, No return value – skipline();
  2. No Input, Returns a value – srand();
  3. Input, No return value – rand();
  4. Input, Return value – sqrt();

void skipline() {

You can scanf strings

scanf(“%s”, nameArray);

Notice, the name of my array does not need to have &nameArray. That is because the name of an array without an index is actually its address.

When writing functions, for every argument you must declare the type ie:


myFunction(int I, int J) {}


myFunction(int I,J) {}

These argument declarations are called formal parameters. They reserve space in the memory.

Leave a Comment

Filed under CSE 130


A function is a small block of code that can be called from another point in the program.

 Functions create structure, and structure creates understanding

Main() function delegates.

Calling Function sends work out to called function

Logical delinking – Which functions your code does not have to follow one step at a time. It can jump around.

Loops and Functions

  •  Functions have explicit call-return mechanism. Loops do not.

Arguments are pieces of data that are passed into functions.

Call by Value: Changes to argument are not sent back to calling function.

A Function gives back return values (usually) To return a value: return value;

The returned value must match the return type in the function header

float sqrt(x) {
       return value_float;

 If not returning the header needs nothing

void function() {

 A function may have several return statements, but it will only return one at a time.

Leave a Comment

Filed under CSE 130

Random Number Generator and Transposing Arrays

time(0) returns times since unix started.

srand() seed the random number generator

rand() get a random number

t = print tab.

Transpose a square array n x n.

The diagonal stays the same, check in i==j. If it does, you have a diagonal number.

Only swap the upper triangular half. do not touch the bottom.

outer loop i = 0 to i = n-1

Inner Loop j = i+1 to j=n-1

 for (i=0; i<n;i++) {
      for (j=i+1);j<n;j++) {
            Swap variable here….

Leave a Comment

Filed under CSE 130


To Declare an array
type name[#];
int fish[3];

To Access an Array
int i=2;

The name of an array refers to the spot in memory to begin storing. The index tells how much to offset.

WATCH for Array index out of bounds. C doesn’t warn you, it just writes over something else.

float sample[5]= {[2]=50.2,[1]=110.0}
Will make all other indices 0.

int name[5] = {1,2,4,5,6};

int name[20] = {5,6,7};
the first three values are 5, 6 and 7 everything else is 0

Sparse Arrays, are very few non zero values.

You can create arrays of characters

char myString[]={“Hello!”}; // This is a 7 character string The memory uses the 7th spot to store which is a numeric character for null.

Anything inside of “ “ is a string

char letters[5] {‘A’,’B’,’C’,’D’,’E’};

Leave a Comment

Filed under CSE 130