(5 intermediate revisions by one other user not shown) | |||
Line 1: | Line 1: | ||
− | Lecture 1: | + | [[Category:ECE264]] [[Category:Programming]] [[Category:C]] [[Category:lecture notes]] |
+ | =[[ECE264]] Lecture notes, Spring 2011, Prof. Lu= | ||
+ | Lecture 1: | ||
− | [[Image:ECE264Lecture1-1.png|500x700px]] | + | [[Image:ECE264Lecture1-1.png|500x700px]] |
− | [[Image:Yuanhua1-1.png|500x700px]] | + | [[Image:Yuanhua1-1.png|500x700px]] |
− | Lecture 2: | + | Lecture 2: |
+ | |||
+ | [[Image:Yuanhua 2-1.png|500x700px]] | ||
+ | |||
+ | [[Image:Yuanhua 2-2.png|500x700px]] | ||
+ | |||
+ | = '''Lecture (1-21-2011):'''<br> = | ||
+ | |||
+ | <br>Gcc –wall progl.c –o pl <br> Argument shell | ||
+ | |||
+ | >ls<br>>-l<br>>cd | ||
+ | |||
+ | int main(int argc, char* argv[])<br>}<br> int abc = 5;<br> int xyz = 10;<br> xyz = f1(abc);<br>if(abc==c)<br>{<br> …<br>}<br> Return 0;<br>} | ||
+ | |||
+ | int f1(int abc, char xyz, double stu)<br>{<br> int counter;<br> abc = 10;<br> f2(abc);<br> return -1;<br>} | ||
+ | |||
+ | <br>void f2(int xyz)<br>{<br> int abc = -1;<br> <br>} | ||
+ | |||
+ | Stack vs. Heap!<br>order of first in last out<br>counter /////<br>double 0.5<br>xyz char ‘a’<br>abc (main) 5<br> L1<br>xyz (int) 10<br>abc (from f1) 5 | ||
+ | |||
+ | <br>frame <br>abc -1<br>xyz 10<br>L2 | ||
+ | |||
+ | Regarding maze traversal:<br>know your location<br>track your steps go back to last intersection<br>map your file<br>priority list <br>detect exit (confirm there is one)<br>read maze | ||
+ | |||
+ | Height is unlimited<br>Max will not exceed 80 for width<br>(0,0) (0,1)<br>* * * * *<br>* *<br>* E<br>* * *<br>* * *<br>* * * * * | ||
+ | |||
+ | It can start at a intersection | ||
+ | |||
+ | (3,1) (2,3) ASCII<br>(2,1) (3,3) American<br> standard<br> code<br> for info interchange? | ||
+ | |||
+ | FILE * find = fopen(argv[1], “r”);<br>if(fhd == 0);<br>{<br> /*error*/<br>} | ||
+ | |||
+ | while(!feof(fhd))<br>{<br> int ch = fgetc(fhol);<br> ch is ‘*’;<br> ‘\n’<br>}<br>fclose(fhd)<br><br> | ||
+ | |||
+ | <br> | ||
+ | |||
+ | = '''Lecture (1-26-2011):'''<br> = | ||
+ | |||
+ | #include <stdio.h> | ||
+ | |||
+ | File *fhd;<br>int numRow=0;<br>int numCol=0;<br>int col = 0;<br>int ch; | ||
+ | |||
+ | <br> | ||
+ | |||
+ | if (arg<2)<br>{<br> /*error*/<br>} | ||
+ | |||
+ | fhd = fopen(argv[1]."r");<br>if(fhd==0)<br>{<br> /*error*/<br>}<br> while(!feof(fhd))<br> {<br> ch = fgetc(fhd);<br> if(ch=='\n')<br> {<br> numRow++;<br> }<br> if(num Col<col)<br> {<br> numCol = col;<br> col = 0;<br> }<br> col++;<br> }<br>printf("width=%d,height=%d\n",numCol,numRow); | ||
+ | |||
+ | gcc prog.c -o prog | ||
+ | |||
+ | We need to know the enviornment and the direction we are going to take:<br>Need to have a way to remember where we are located: | ||
+ | |||
+ | Pointers:<br>int x;<br>x=5 | ||
+ | |||
+ | Value ---> 800<br>address 650y | ||
+ | |||
+ | int *y;<br>/* y is a pointer to integer*/<br>y = 'x'; <-- warning message<br>take x's addr | ||
+ | |||
+ | *y = LHS left hand side take y's value as an adr, modify the value at that addr. | ||
+ | |||
+ | = *y Right hand side (RHS) ---> Means assign <br>Take y's value as an addr, read from that addr. | ||
+ | |||
+ | *y = 50;<br>y = &z;<br>*y = 35; ---> z = 35 | ||
+ | |||
+ | Goo mainstreet ---> address<br>family Johnson ----> value | ||
+ | |||
+ | int a[10];<br> ^-- 3<br>if(ch=='A')<br> ^-- 65 ascII | ||
+ | |||
+ | memory<br>one - dimension array<br>int *a;<br>a = malloc(numElem *sizeof(int));<br>^---a's value is the start addr of a piece of memory <br>a[0] = ____<br>x = a [0];<br>.<br>.<br>.<br>free(a); <--- lose 50 points RELEASE THE MEMORY (Memory Leak if not there) | ||
+ | |||
+ | Stack Heap<br>a 1000 | ||
+ | |||
+ | C does not specify sizeof(int)<br>Portable: Run on different machine | ||
+ | |||
+ | malloc(#elem*sizeof(one elem));<br>^--- modify the value at that addr | ||
+ | |||
+ | if(a==0)<br>{<br> /* malloc fails */<br> //if malloc fails a is 0<br>} | ||
+ | |||
+ | you already know the size of the maze. | ||
+ | |||
+ | int array[numRow]; The program does not know when you compile the program!<br> | ||
+ | |||
+ | = '''Lecture (2-9-2011):'''<br> = | ||
+ | |||
+ | #ifndef MAZE_H<br>#define MAZE_H<br>#include <stdio.h> | ||
+ | |||
+ | typedef struct<br>{<br> int startRow,startCol;<br> int exitRow, exitCol;<br> int numRow,numCol;<br> <br> int** cells;<br>}Maze; | ||
+ | |||
+ | Maze Maze_construct(char* frame);<br>void Maze_traverse(Maze m);<br>void Maze_destruct(Maze m); | ||
+ | |||
+ | #endif <br>main.c | ||
+ | |||
+ | <br>maze.c | ||
+ | |||
+ | <br>/*maze.c*/<br>#include "maze.h"<br>#include <stdio.h><br>Maze Maze_contruct(char* fname)<br>{<br> FIEL* fMaze;<br> fMaze = fopen(fname);<br> if(fMaze ==0)<br> {<br> //error<br> }<br> while(!feof(fMaze))<br> {<br> //stuff<br> }<br>} | ||
+ | |||
+ | <br>#include <stdio.h> main.c<br>#include "maze.h"<br>int main()<br>{<br> Maze m;<br> if(argc<2)<br> { <br> //error<br> }<br> m = Mazeconstruct(argv[1]);<br> Maze_traverse(m)<br> Maze_destruct(m)<br> return 0;<br>} | ||
+ | |||
+ | gcc -c maze.c need malloc<br>gcc maze.o main.o -o <br>implicitly link c library | ||
+ | |||
+ | m.cells = malloc (sizeof(char*) * numRow);<br>for | ||
+ | |||
+ | typedef struct<br>{<br> int x;<br> int y;<br> int z;<br>}Vector; | ||
+ | |||
+ | Vector Vector_construct(int a int b int c);<br>Vector Vector_add(vector v1 vector v2); | ||
+ | |||
+ | Vector Vector_construct(int a, int b, int c)<br>Pbecor v;; | ||
+ | |||
+ | gcc -c main.c <br>gcc -c maze.c<br>gcc main.o maze.o -o ipa1<br>./ipa1 maze1<br>current directory<br>make file for each directory<br>makeclean | ||
+ | |||
+ | int numLine;<br>Question:<br>What does it mean when you return with struct, copy attribute by attribute<br> | ||
+ | |||
+ | = Lecture (2-11-2011):<br> = | ||
+ | |||
+ | ECE 264 seat = D9 | ||
+ | |||
+ | Practice <br>find the number of ways to partition 10 into the sum of positive and incraesing integers | ||
+ | |||
+ | 5 = 1 + 4 | 1 + 1 + 3 wrong<br> = 2 + 3 | 2 + 1 + 2 wrong<br> = 5 | 3 + 2 does not count | ||
+ | |||
+ | tail recursion for quiz | ||
+ | |||
+ | f(int **array)<br>{ | ||
+ | |||
+ | } | ||
+ | |||
+ | int ** cells;<br>cells = malloc()<br>f(cells); | ||
+ | |||
+ | Maze_traverse(Maze m)<br>{<br> if(m.cells[m.currRow][m.currCol]=='E')<br> {<br> done!<br> }<br> try east<br> if(m.cells[m.currRow][m.currCol+1]==' ')<br> {<br> m.currCol++;<br> Maze_traverse(m);<br> }<br> if(m.cells[m.currRow+1][m.currCol] == ' ')<br> {<br> m.currRow++;<br> Maze_traverse(m);<br> }<br>}<br>if all are not satisfied you backtrack.<br>tail recursion?<br> | ||
+ | |||
+ | = Lecture (2-18-2011):<br> = | ||
+ | |||
+ | #include "maze.h" | ||
+ | |||
+ | Maze fileReadin(char* fileName)<br>{<br> //read it in as a 2 dimensional array<br> int ch;<br> int numVert = 0;<br> int col = 0;<br> int lcv = 0;<br> int mRow = 0;<br> int mCol = 0;<br> int numLines = 0;<br> <br> <br> Maze m; //declare it locally too<br> <br> //read in file<br> FILE * fMaze; <br> fMaze = fopen(fileName, "r");<br> <br> if(fMaze == 0)<br> {<br> printf("There is an Error!");<br> fclose (fMaze);<br> }<br> while(!feof(fMaze))<br> {<br> ch = fgetc(fMaze);<br> col++;<br> <br> if (ch == '\n')<br> {<br> ++numLines;<br> <br> if(numVert < col)<br> {<br> numVert = col;<br> }<br> col = 0;<br> }<br> }<br> <br> //printf("numLines = %4d numVert = %4d\n", numLines, numVert);<br> m.numRow = numLines;<br> m.numCol = numVert;<br> //-------------------------------------------------DUPLICATE MAtrix<br> <br> m.cells = malloc(m.numRow * sizeof(int *));<br> <br> for(lcv = 0; lcv < m.numRow; lcv++)<br> {<br> m.cells[lcv] = malloc(m.numCol * sizeof(int));<br> }<br> <br> //needed<br> rewind(fMaze);<br> for (mRow = 0; mRow < m.numRow; mRow++)<br> {<br> for (mCol = 0; mCol < m.numCol; mCol++)<br> {<br> <br> m.cells[mRow][mCol] = fgetc(fMaze);<br> if (m.cells[mRow][mCol] == 'E')<br> { <br> m.exitCol = mCol;<br> m.exitRow = mRow;<br> //printf("exitCol = %4d exitRow %4d\n",m.exitCol,m.exitRow);<br> }<br> | ||
+ | |||
+ | if (m.cells[mRow][mCol] == 's')<br> {<br> m.startCol = mCol;<br> m.startRow = mRow;<br> }<br> <br> }<br> }<br> //close the file<br> fclose(fMaze);<br> return m;<br>}<br> | ||
+ | |||
+ | void Maze_traverse(Maze m,int row,int col, int dir, int* progress, int* found)<br>{<br> printf("Move to %d, %d", row, col);<br> m.currentRow = row;<br> m.currentCol = col;<br> <br> <br> if(m.cells[row][col]=='E')<br> {<br> *found = TRUE;<br> return;<br> }<br> //termination condition<br> if((dir!=WEST)&&(canMove(m,row,col,EAST,*found))<br> {<br> *progress = FORWARD;<br> Maze_traverse(m,row,col+1,EAST,progress,found);<br> revisit(*progress, *found, row, col);<br> }<br> if((dir!=NORTH)&&(canMove(m,row,col,SOUTH,*found)<br> {<br> *progress = FORWARD;<br> Maze_traverse(m,row+1,col,SOUTH,progress,found);<br> revisit(*progress, *found, row, col);<br> }<br> if((dir!=EAST)&&(canMove(m,row,col,WEST,*found)<br> {<br> *progress = FORWARD;<br> Maze_traverse(m,row,col-1,WEST,progress,found);<br> revisit(*progress, *found, row, col);<br> }<br> if((dir!=SOUTH)&&(canMove(m,row,col,NORTH,*found)<br> {<br> *progress = FORWARD;<br> Maze_traverse(m,row-1,col,NORTH,progress,found);<br> revisit(*progress, *found, row, col);<br> }<br> *progress = BACKWARD;<br>} | ||
+ | |||
+ | void Maze_destruct(Maze dTemp)<br>{<br> <br> int lcv = 0;<br> // destruct with struct<br> for(lcv = 0; lcv < dTemp.numRow; lcv++)<br> {<br> free(dTemp.cells[lcv]); // must break down the array in the struct before destroying whole struct.<br> }<br> free(dTemp.cells);<br>}<br>// #endif<br> | ||
+ | |||
+ | = Lecture (2-23-2011):<br> = | ||
+ | |||
+ | Review Exam answers<br>Last problem:<br>a = 24 b = 4 c = 4 | ||
+ | |||
+ | Next project:<br>Rectangular packing. <br>No rewind<br>No fseek<br>No fopen fclose then another fopen. | ||
+ | |||
+ | typedef struct listnode<br>{<br> int value;<br> struct listnode *next;<br>}Node; | ||
+ | |||
+ | Node n;<br>Node *p;<br>p = Node_construct(5); | ||
+ | |||
+ | Node* Node_construct(int v)<br>{<br> Node* a;<br> a = malloc(sizeof(Node));<br> a-> value = v;<br> a-> next = 0;<br> return a;<br>} | ||
+ | |||
+ | <br>printf("%d\n", p->value);<br> | ||
+ | ---- | ||
+ | [[2011_Spring_ECE_264_Lu|Back to ECE264, Spring 2011, Prof. Lu]] |
Latest revision as of 05:29, 11 July 2012
Contents
ECE264 Lecture notes, Spring 2011, Prof. Lu
Lecture 1:
Lecture 2:
Lecture (1-21-2011):
Gcc –wall progl.c –o pl
Argument shell
>ls
>-l
>cd
int main(int argc, char* argv[])
}
int abc = 5;
int xyz = 10;
xyz = f1(abc);
if(abc==c)
{
…
}
Return 0;
}
int f1(int abc, char xyz, double stu)
{
int counter;
abc = 10;
f2(abc);
return -1;
}
void f2(int xyz)
{
int abc = -1;
}
Stack vs. Heap!
order of first in last out
counter /////
double 0.5
xyz char ‘a’
abc (main) 5
L1
xyz (int) 10
abc (from f1) 5
frame
abc -1
xyz 10
L2
Regarding maze traversal:
know your location
track your steps go back to last intersection
map your file
priority list
detect exit (confirm there is one)
read maze
Height is unlimited
Max will not exceed 80 for width
(0,0) (0,1)
* * * * *
* *
* E
* * *
* * *
* * * * *
It can start at a intersection
(3,1) (2,3) ASCII
(2,1) (3,3) American
standard
code
for info interchange?
FILE * find = fopen(argv[1], “r”);
if(fhd == 0);
{
/*error*/
}
while(!feof(fhd))
{
int ch = fgetc(fhol);
ch is ‘*’;
‘\n’
}
fclose(fhd)
Lecture (1-26-2011):
- include <stdio.h>
File *fhd;
int numRow=0;
int numCol=0;
int col = 0;
int ch;
if (arg<2)
{
/*error*/
}
fhd = fopen(argv[1]."r");
if(fhd==0)
{
/*error*/
}
while(!feof(fhd))
{
ch = fgetc(fhd);
if(ch=='\n')
{
numRow++;
}
if(num Col<col)
{
numCol = col;
col = 0;
}
col++;
}
printf("width=%d,height=%d\n",numCol,numRow);
gcc prog.c -o prog
We need to know the enviornment and the direction we are going to take:
Need to have a way to remember where we are located:
Pointers:
int x;
x=5
Value ---> 800
address 650y
int *y;
/* y is a pointer to integer*/
y = 'x'; <-- warning message
take x's addr
- y = LHS left hand side take y's value as an adr, modify the value at that addr.
= *y Right hand side (RHS) ---> Means assign
Take y's value as an addr, read from that addr.
- y = 50;
y = &z;
*y = 35; ---> z = 35
Goo mainstreet ---> address
family Johnson ----> value
int a[10];
^-- 3
if(ch=='A')
^-- 65 ascII
memory
one - dimension array
int *a;
a = malloc(numElem *sizeof(int));
^---a's value is the start addr of a piece of memory
a[0] = ____
x = a [0];
.
.
.
free(a); <--- lose 50 points RELEASE THE MEMORY (Memory Leak if not there)
Stack Heap
a 1000
C does not specify sizeof(int)
Portable: Run on different machine
malloc(#elem*sizeof(one elem));
^--- modify the value at that addr
if(a==0)
{
/* malloc fails */
//if malloc fails a is 0
}
you already know the size of the maze.
int array[numRow]; The program does not know when you compile the program!
Lecture (2-9-2011):
- ifndef MAZE_H
#define MAZE_H
#include <stdio.h>
typedef struct
{
int startRow,startCol;
int exitRow, exitCol;
int numRow,numCol;
int** cells;
}Maze;
Maze Maze_construct(char* frame);
void Maze_traverse(Maze m);
void Maze_destruct(Maze m);
- endif
main.c
maze.c
/*maze.c*/
#include "maze.h"
#include <stdio.h>
Maze Maze_contruct(char* fname)
{
FIEL* fMaze;
fMaze = fopen(fname);
if(fMaze ==0)
{
//error
}
while(!feof(fMaze))
{
//stuff
}
}
#include <stdio.h> main.c
#include "maze.h"
int main()
{
Maze m;
if(argc<2)
{
//error
}
m = Mazeconstruct(argv[1]);
Maze_traverse(m)
Maze_destruct(m)
return 0;
}
gcc -c maze.c need malloc
gcc maze.o main.o -o
implicitly link c library
m.cells = malloc (sizeof(char*) * numRow);
for
typedef struct
{
int x;
int y;
int z;
}Vector;
Vector Vector_construct(int a int b int c);
Vector Vector_add(vector v1 vector v2);
Vector Vector_construct(int a, int b, int c)
Pbecor v;;
gcc -c main.c
gcc -c maze.c
gcc main.o maze.o -o ipa1
./ipa1 maze1
current directory
make file for each directory
makeclean
int numLine;
Question:
What does it mean when you return with struct, copy attribute by attribute
Lecture (2-11-2011):
ECE 264 seat = D9
Practice
find the number of ways to partition 10 into the sum of positive and incraesing integers
5 = 1 + 4 | 1 + 1 + 3 wrong
= 2 + 3 | 2 + 1 + 2 wrong
= 5 | 3 + 2 does not count
tail recursion for quiz
f(int **array)
{
}
int ** cells;
cells = malloc()
f(cells);
Maze_traverse(Maze m)
{
if(m.cells[m.currRow][m.currCol]=='E')
{
done!
}
try east
if(m.cells[m.currRow][m.currCol+1]==' ')
{
m.currCol++;
Maze_traverse(m);
}
if(m.cells[m.currRow+1][m.currCol] == ' ')
{
m.currRow++;
Maze_traverse(m);
}
}
if all are not satisfied you backtrack.
tail recursion?
Lecture (2-18-2011):
- include "maze.h"
Maze fileReadin(char* fileName)
{
//read it in as a 2 dimensional array
int ch;
int numVert = 0;
int col = 0;
int lcv = 0;
int mRow = 0;
int mCol = 0;
int numLines = 0;
Maze m; //declare it locally too
//read in file
FILE * fMaze;
fMaze = fopen(fileName, "r");
if(fMaze == 0)
{
printf("There is an Error!");
fclose (fMaze);
}
while(!feof(fMaze))
{
ch = fgetc(fMaze);
col++;
if (ch == '\n')
{
++numLines;
if(numVert < col)
{
numVert = col;
}
col = 0;
}
}
//printf("numLines = %4d numVert = %4d\n", numLines, numVert);
m.numRow = numLines;
m.numCol = numVert;
//-------------------------------------------------DUPLICATE MAtrix
m.cells = malloc(m.numRow * sizeof(int *));
for(lcv = 0; lcv < m.numRow; lcv++)
{
m.cells[lcv] = malloc(m.numCol * sizeof(int));
}
//needed
rewind(fMaze);
for (mRow = 0; mRow < m.numRow; mRow++)
{
for (mCol = 0; mCol < m.numCol; mCol++)
{
m.cells[mRow][mCol] = fgetc(fMaze);
if (m.cells[mRow][mCol] == 'E')
{
m.exitCol = mCol;
m.exitRow = mRow;
//printf("exitCol = %4d exitRow %4d\n",m.exitCol,m.exitRow);
}
if (m.cells[mRow][mCol] == 's')
{
m.startCol = mCol;
m.startRow = mRow;
}
}
}
//close the file
fclose(fMaze);
return m;
}
void Maze_traverse(Maze m,int row,int col, int dir, int* progress, int* found)
{
printf("Move to %d, %d", row, col);
m.currentRow = row;
m.currentCol = col;
if(m.cells[row][col]=='E')
{
*found = TRUE;
return;
}
//termination condition
if((dir!=WEST)&&(canMove(m,row,col,EAST,*found))
{
*progress = FORWARD;
Maze_traverse(m,row,col+1,EAST,progress,found);
revisit(*progress, *found, row, col);
}
if((dir!=NORTH)&&(canMove(m,row,col,SOUTH,*found)
{
*progress = FORWARD;
Maze_traverse(m,row+1,col,SOUTH,progress,found);
revisit(*progress, *found, row, col);
}
if((dir!=EAST)&&(canMove(m,row,col,WEST,*found)
{
*progress = FORWARD;
Maze_traverse(m,row,col-1,WEST,progress,found);
revisit(*progress, *found, row, col);
}
if((dir!=SOUTH)&&(canMove(m,row,col,NORTH,*found)
{
*progress = FORWARD;
Maze_traverse(m,row-1,col,NORTH,progress,found);
revisit(*progress, *found, row, col);
}
*progress = BACKWARD;
}
void Maze_destruct(Maze dTemp)
{
int lcv = 0;
// destruct with struct
for(lcv = 0; lcv < dTemp.numRow; lcv++)
{
free(dTemp.cells[lcv]); // must break down the array in the struct before destroying whole struct.
}
free(dTemp.cells);
}
// #endif
Lecture (2-23-2011):
Review Exam answers
Last problem:
a = 24 b = 4 c = 4
Next project:
Rectangular packing.
No rewind
No fseek
No fopen fclose then another fopen.
typedef struct listnode
{
int value;
struct listnode *next;
}Node;
Node n;
Node *p;
p = Node_construct(5);
Node* Node_construct(int v)
{
Node* a;
a = malloc(sizeof(Node));
a-> value = v;
a-> next = 0;
return a;
}
printf("%d\n", p->value);