Free Web Hosting Provider - Web Hosting - E-commerce - High Speed Internet - Free Web Page
Search the Web

CSE142AD: Final Review

 

Here’s an outline of what you should know:

 

 

Concentrate more on the second half of the quarter. However, in programming, you always need to know the basic (i.e. declaring variables, loops, etc). Although there would be question on file I/O anr recursion on the final, you should concentrate more on pointers, arrays , and struct.

 

 

 

 

Pointers

 

Pointers are used in programs to access memory and manipulate address.

In this class, you often use them as function parameters if you want to change the actual value or you want to return multiple value.

 

Declaring and assigning an integer

int num;

num = 10;

When declaring an integer, the computer goes into its free memory and allocate a place to store the value of num, which in this case is 10.

 

Declare a pointer to an integer

int *nump;

When I first declare a pointer to an integer, it is not pointing to anything. I need to assign an address to be pointed at. Hence the following statement:

nump = #

Now nump points to the address of num.

When you do a printf and want to see what value num is, you need to deference it.

 

Remember that nump contains the address not the value.

printf(" %d ", nump) will print out the address.

 

To get the actual value, I need to deference it. To deference a pointer, I need a *

printf(" %d ", *nump) will print out the value that the address has (i.e. 10)

 

Questions:

  1. What happens if I insert this line after nump = &num?
  2. num = 20;

  3. What happens if I insert this line after nump = &num?
  4. *nump = 40;

  5. Write a function that take a number, and decrement it by one

 

è & refers to the address

è * references a pointer

 

Remember the swap function. When we do a swap, we need to actually change the input parameters. Therefore we use pointers. If we don’t, the swap will be disregarded by the function that called swap. Calling the function involves passing the address (i.e. swap (&a, &b)).

void swap( int *a , int *b)

{

int temp;

temp = *a;

*a = *b;

*b= temp;

}

Recursion

Recursion is when a function calls itself

 

There are two rules that MUST be obeyed in recursion.

  1. There has to be a base case
  2. When calling the function again, we have to be able to reach the base case

 

Let examine the factorial function

 

int factorial (int n) {

 

if (n <= 1) /* BASE CASE */

return 1;

else

return(n * factorial(n-1));

/* the preceding is the recursive call. Notice that it will reach the base case

because the parameter is n -1 */

}

 

Let assume that n = 4 initially,

1st time: 4 * factorial(3)

2nd time: 4 * 3 * factorial(2)

3rd time: 4 * 3 * 2 * factorial(1)

4th time: 4 * 3 * 2 * 1

 

Questions

void display (int number)

{

if (number == 0)

return;

else {

printf("%d\n", number);

display(number –1);

return;

}

}

 

void display (int number)

{

if (number ==0)

return;

else {

display(number –1);

printf("%d\n", number);

return;

}

}

 

What will the output be?

 

 

Arrays

An array is a set of consecutive memory locations used to store data.

 

We use array if we want to store numerous similar items.

For example, we want to store the age of 10 people:

int age[10];

 

REMEMBER, when dealing with array, USE LOOPS . I have said it a thousand time, but people still forget it.

Loop will allow you to have access to each element in the array. Once you have access to them, you can basically do anything that you like.

 

Assuming that I have a list of 10 people’s ages, and I like to find the average age.

 

int average (int age[])

{

int sum = 0;

int average;

int j;

 

/* calculate sum */

for (j=0; j < 10; j++)

sum=sum+age[j];

 

average = sum / 10;

return average;

}

 

A two-dimensional array is similar to an one-dimensional array. The only difference is that in order to access all the elements in the array, you need two nested loop.

à The hardest thing about array is knowing how to access its elements. Once you mastered that, you should have no problem with array.

 

Arrays and Pointers

When passing an array to a function, you are NOT passing every elements in the array. That would be too much (just think of having to pass an array of size 100000). You’re passing the address of the array (pointer). That’s why when you change the elements of an array, you actually change the array itself.

 

Questions

int list[10];

fun1(list);

fun2(list[2]);

 

Are we passing the address of the array in both functions?

String

 

String is a list of characters. You can regard a string as an array of characters. This is actually how C treats a string.

 

Declaring and assigning a string

char name[] = "Jean-Paul";

char name[20] = "Jean-Paul";

 

I cannot do the following:

char name[20];

name = "Jean-Paul";

 

In order to assign a string after declaration, I need to do the following:

strcpy(name, "Jean-Paul");

 

Let say that I want the user to input his/her name,

 

int name[40];

printf ("enter your name: ");

scanf ("%s", name);

 

As you can notice, I didn’t put a & in name for the scanf.

How do I determine that the name of the user is 40 characters long? That’s just an arbitrary number. The user’s name doesn’t have to be exactly 40 characters. I just pick a number that I know will be the max that a name can be. I could as well have picked 100, but I would then waste a lot of memory.

 

The following is a complex function that calculates the occurrence of a character in a string. If you understand this function, you should understand string, array, and the use of ASCII code.

void count_occurence(char name[])

{

int count[26]; /* count the occurrence of a character */

int i = 0; /* used for loop */

 

for(i=0; i<26; i++) /* initialize counter */

count[i] = 0;

 

for(i=0; i<strlen(name); i++) /* count */

count[name[i]-'a']++;

 

for(i=0; i<26; i++) /* print out alphabet */

printf("%c: %d\n", i+'a', count[i]);

}

Note: when dealing with string, it is always a good idea to #include <string.h>

 

Struct

 

Structure is a collection of values of possibly different types.

For example, a phone book.

 

typedef struct {

char name[40];

int phone_number;

} phone_entry;

 

Be careful about declaring a phone number to be an int. The reason is if you put a -, it will substract the number. 123-4567 à -4444. Therefore make sure that you omit the ‘-‘. Else you can declare it as a string -- char phone_number[20]

 

Declaring a structure is like declare an int, double

 

phone_entry my_phone_book[20];

 

To access any item in my phone book, I need to do the following.

  1. Specify with element it is (array)
  2. Specify with item of the structure I’m looking at.

 

For example, my_phone_book[1].name is accessing the name of the 2nd entry in my phone book.

 

 

Questions

  1. How do I sort the phone book in alphabetic order according to the name?

Do this during your free time, and send me the code. I will look at it and make comment.

 

 

File I/O

 

File I/O is used to store information on the disk, so it can be used at later time. For example, the phone book that you created above would be better if the information that you enter is stored on a disk that can be retrieve at later date, than to have to type in the information all over again each time you use the program.

 

When doing file I/O, do the following:

#include <file.h>

 

Here are some of the functions you can use:

 

Declaring a file pointer: FILE *filep;

Opening a file to read: filep = fopen("filename", "r");

Opening a file to write: filep = fopen("filename", "w");

Opening a file to read & write: filep = fopen("filename","r+");

Closing a file: close(filep);

 

Reading from a file: fscanf(filep,"%…", &var, ..);

Writing to a file: fprintf(filep,"%….", var, …);

 

Example:

Writing your grades in this class to a file name grades.out". You have 6 grades so far.

 

void write_grade(void)

{

FILE *filep;

int grade,i;

 

filep= fopen("grades.out", "w");

 

for(i = 0; i < 6; i++) {

printf("enter grade: "); /* prompt user to enter* / scanf("%d", &grade); /* his/her grades */

fprintf(filep, "%d ", grade); /*writing grades to disk */

}

fclose(filep); /* close file */

}

 

 

void read_grades(void)

{

FILE *filep;

int grades[6];

 

filep = fopen("grades.out", "r"); /* open file for reading */

 

for(i = 0; i< 6; i++)

fscanf(filep, "%d", &grades[i]);/* read file and store

value in array */

 

fclose(filep); /* close file */

 

/* print out grades */

printf("my grades so far are: \n");

for(i = 0; i< 6; i++)

printf("%d\n", grades[i]);

 

}

 

 

 

 

 

 

GOOD LUCK ON THE FINAL