(6 intermediate revisions by 6 users not shown)
Line 1: Line 1:
 +
[[Category:ECE264]] [[Category:Programming]] [[Category:C]] [[Category:lecture notes]]
 +
 +
=Lecture 23, [[ECE264]], Spring 2012, Prof. Lu=
 +
----
 
Lecture 23 _4/5_Kailu Song
 
Lecture 23 _4/5_Kailu Song
  
Line 87: Line 91:
 
strstr,
 
strstr,
 
strcat
 
strcat
 +
 +
 +
===================
 +
 +
Kevin Tan(0023987592), section#2 notes 04/05
 +
 +
random access
 +
 +
arr[5]; arr[9];
 +
 +
FILE *fp= NULL; fp = fopen(filename,"rb");
 +
 +
int a; double b; fread(&a,sizeof(int),1,fp); fread(&b,sizeof(double),1,fp); . . fscanf(fp,"%d%d",&a,&b);
 +
 +
-------------------------
 +
 +
Wudi Zhou, Professor Lu's section, April 9th
 +
 +
Exam 2 covers: File I/O, Structure, Dynamic Structure,recursive
 +
 +
FILE * fptr;
 +
fptr = fopen(filename,"rb");
 +
int a;
 +
double b;
 +
fread(&a, sizeof(int),1,fptr);
 +
fread(&b,sizeof(double),1,fptr);
 +
 +
fptr = fopen(filename,"b");
 +
ch = fgetc(fptr);
 +
char oneline[256];
 +
gets(oneline,256,fptr);
 +
 +
fscanf(fptr,"%d", &a);/*return 1 if successful*/
 +
fscanf(fptr,"%d",&a, &b); /*return 2 if both successful*/
 +
 +
Person P;
 +
fread(&p, sizeof(person),1,fptr);
 +
int arr[10];
 +
fread(arr,sizeof(int),10,fptr);
 +
fwrite(&p,sizeof(person),1,fptr);
 +
 +
-------------------------------------
 +
Hanye Xu April 10
 +
 +
Exam 2 covers file input and file output
 +
fread and fsanf are not the same
 +
 +
 +
fptr = fopen(filename,"rb");
 +
 +
int a;
 +
 +
double b;
 +
 +
fread(&a,sizeof(int),1,fptr);
 +
 +
fread(&b,sizeof(double),1,fptr);
 +
 +
Person P;
 +
 +
fread(&P,sizeof(Person),1,fptr);
 +
 +
int arr[10];
 +
 +
fread(arr,sizeof(int),10,fptr);
 +
 +
int ch;
 +
 +
ch = fgetc(fptr);
 +
 +
char = oneline[256];
 +
 +
fets(oneline,256,fptr);
 +
 +
(stop at '\n', or reach the limit'\o');
 +
 +
int a,b;
 +
 +
fscanf(fptr,"%d %d", &a,&b);
 +
 +
strlen not include '\o';
 +
 +
strcpy,strstr,strcat;
 +
 +
 +
 +
 +
 +
 +
 +
--------------------------------------------------------------------
 +
Lecture 0406 Huayi Guo  taught by Niklas Nlmqvist
 +
/*
 +
lecture0406
 +
 +
Anouncement:
 +
Final exam
 +
Sat , May 5 8am-10am
 +
the location may not hold. And the exam will most likely be electronics.
 +
*/
 +
 +
void Putcell(int x, int y, char c, int dim, char* block)
 +
{
 +
  block[(y*dim)+x] =c;
 +
}
 +
 +
 +
int visitCell(int x,int y, int dim, char* block)
 +
{
 +
  //drop the breadcrumb!
 +
  putCell(x, y, "*", dim, data);
 +
 
 +
  //check neighbors;
 +
  int count = 1;
 +
  if(getcell(x-1, y, dim, data) =="1"){
 +
    count += visitCell(x -1; y, dim, data):
 +
  }
 +
    if(getcell(x+1, y, dim, data) =="1"){
 +
    count += visitCell(x +1; y, dim, data):
 +
  }
 +
 
 +
  if(getcell(x, y-1, dim, data) =="1"){
 +
    count += visitCell(x ; y-1, dim, data):
 +
  }
 +
    if(getcell(x, y+1, dim, data) =="1"){
 +
    count += visitCell(x ; y+1, dim, data):
 +
  }
 +
 
 +
 +
}
 +
 +
int isconnected(int dim, char *block)
 +
{
 +
  char *data = strdup(block);
 +
  int x =0;
 +
  int y=0;
 +
  while(getCell(x, y dim, block) != '1'){
 +
    x++;
 +
if(x>= dim){
 +
  y++;
 +
  x=0;  
 +
}
 +
  } 
 +
  //REcursively visit each cell
 +
  int numConnected = visitCell(x, y, dim, data);
 +
  free(data);
 +
  return numConnected = dim;
 +
}
 +
 +
 +
 +
int getcell(int x, int y, int dim, char *block)
 +
{
 +
  if(x<0 || x>=dim ||y<0 ||y>dim) return 0;
 +
  return block[()]
 +
}
 +
 +
 +
 +
int countones(int dim, char * block)
 +
{
 +
  int i;
 +
  int counter = 0;
 +
  for(i =0; i < dim* dim; i++)
 +
  {
 +
    if( block[i] == '1'){
 +
    counter ++;
 +
}
 +
  }
 +
  return counter;
 +
}
 +
 +
 +
int main()
 +
{
 +
  //check the command
 +
  if(argc != 2)
 +
  {
 +
 
 +
  }
 +
 
 +
  //open the file
 +
 
 +
 
 +
  //initalization
 +
  const int BUFFE_LENGTH =101;
 +
  char buffer[];
 +
 
 +
  int valid = 0;
 +
  int invalid = 0;
 +
 
 +
  while (fgets(buffer, BUFFER_LENGTH, buffer) != NULL){
 +
    //consume any terminating newline
 +
if(buffer[strelen(buffer)-1] =='\n')
 +
{
 +
  buffer[strlen(buffer) -1] ='\0';
 +
}
 +
 +
//Figure out the dimeconsion of the block
 +
int dim = sqrt(strlen(buffer));
 +
 +
// Rule 1: check that the dimension is valid
 +
if(dim * dim != strlen(buffer)){//if not n^2 then this one is invalid   
 +
        invalid++;
 +
continue; // stops the loop and goes to next iteration
 +
//break, while stop the entire loop
 +
    }
 +
 +
//Rule2 L check number of ones
 +
if(countones(dim, buffer) != dim)
 +
{
 +
  invalid ++;
 +
  continue;
 +
}
 +
     
 +
    //Rule3 : important! and hard!
 +
    if(!isConnected(dim, buffer)){
 +
  invalid ++;
 +
  continue ; 
 +
 +
}
 +
 +
    //Rule4: for a zero, there should be no more than 1 neighbor that is 1??
 +
 +
}
 +
 +
 +
//DFS
 +
//Depth-first search
 +
//BFS
 +
//breadth first search
 +
===============================
 +
Rohan Khante
 +
================================
 +
exam review
 +
IMP- FREAD AND FSCANF are NOT the SAME
 +
 +
fptr = fopen(filename,"rb");
 +
 +
int a;
 +
 +
double b;
 +
 +
fread(&a,sizeof(int),1,fptr);
 +
 +
fread(&b,sizeof(double),1,fptr);
 +
 +
Person P;
 +
 +
fread(&P,sizeof(Person),1,fptr);
 +
 +
int arr[10];
 +
 +
fread(arr,sizeof(int),10,fptr);
 +
 +
int ch;
 +
 +
ch = fgetc(fptr);
 +
 +
char = oneline[256];
 +
 +
fets(oneline,256,fptr);
 +
 +
(stop at '\n', or reach the limit'\o');
 +
 +
int a,b;
 +
 +
fscanf(fptr,"%d %d", &a,&b);
 +
 +
strcpy,strstr,strcat;
 +
Strlen strstr and strcat were discussed
 +
NOTE- strlen DOES NOT include the termination character '\0'.
 +
so strlen performed on string have abc will return THREE
 +
 +
strstr- return a pointer to the first occurence of str2 in str1 or a null pointer if not found
 +
 +
 +
strcat = concatenation function for strings.
 +
strcat("ece","264") will return ece264 string
 +
 +
NOTE2= all these functions are in string.h and this should be included in the header.
 +
#include <string.h>
 +
----
 +
[[2012_Spring_ECE_264_Lu|Back to ECE264, Spring 2012, Prof. Lu]]

Latest revision as of 04:27, 11 July 2012


Lecture 23, ECE264, Spring 2012, Prof. Lu


Lecture 23 _4/5_Kailu Song

Review for exam 2,students could ask question 1.read file FILE *fptr;

fptr = fopen(filename,"rb");

int a;

double b;

fread(&a,sizeof(int),1,fptr);

fread(&b,sizeof(double),1,fptr);

Person P;

fread(&P,sizeof(Person),1,fptr);

int arr[10];

fread(arr,sizeof(int),10,fptr);

int ch;

ch = fgetc(fptr);

char = oneline[256];

fets(oneline,256,fptr);

(stop at '\n', or reach the limit'\o');

int a,b;

fscanf(fptr,"%d %d", &a,&b);

strlen not include '\o';

strcpy,strstr,strcat;

2.linklist with n nodes sorted,insert one more value

3, three outcomes:1.file;2.structure;3.dynamic structure(linked list);


========================================

Shiyu Wang Lec23 April 8th

Review for Exam2

randon access

arr[5]

arr[29]


FILE* fptr; fptr = fopen(filename,"rb");

                   (binary)

int a;

double b;

fread(&a,sizeof(int),1 fptr);

fread(&b,sizeof(double),1,fptr);

int ch;

ch=fgetc(fptr);

char oneline[256];

fgets(oneline,256,fptr);


(it will stop at'\n' or reach the limit'\0')

How to use strcpy, strstr, strcat


=======

Kevin Tan(0023987592), section#2 notes 04/05

random access

arr[5]; arr[9];

FILE *fp= NULL; fp = fopen(filename,"rb");

int a; double b; fread(&a,sizeof(int),1,fp); fread(&b,sizeof(double),1,fp); . . fscanf(fp,"%d%d",&a,&b);


Wudi Zhou, Professor Lu's section, April 9th

Exam 2 covers: File I/O, Structure, Dynamic Structure,recursive

FILE * fptr; fptr = fopen(filename,"rb"); int a; double b; fread(&a, sizeof(int),1,fptr); fread(&b,sizeof(double),1,fptr);

fptr = fopen(filename,"b"); ch = fgetc(fptr); char oneline[256]; gets(oneline,256,fptr);

fscanf(fptr,"%d", &a);/*return 1 if successful*/ fscanf(fptr,"%d",&a, &b); /*return 2 if both successful*/

Person P; fread(&p, sizeof(person),1,fptr); int arr[10]; fread(arr,sizeof(int),10,fptr); fwrite(&p,sizeof(person),1,fptr);


Hanye Xu April 10

Exam 2 covers file input and file output fread and fsanf are not the same


fptr = fopen(filename,"rb");

int a;

double b;

fread(&a,sizeof(int),1,fptr);

fread(&b,sizeof(double),1,fptr);

Person P;

fread(&P,sizeof(Person),1,fptr);

int arr[10];

fread(arr,sizeof(int),10,fptr);

int ch;

ch = fgetc(fptr);

char = oneline[256];

fets(oneline,256,fptr);

(stop at '\n', or reach the limit'\o');

int a,b;

fscanf(fptr,"%d %d", &a,&b);

strlen not include '\o';

strcpy,strstr,strcat;





Lecture 0406 Huayi Guo taught by Niklas Nlmqvist /*

lecture0406

Anouncement:
Final exam
Sat , May 5 8am-10am
the location may not hold. And the exam will most likely be electronics.
  • /

void Putcell(int x, int y, char c, int dim, char* block) {

 block[(y*dim)+x] =c;

}


int visitCell(int x,int y, int dim, char* block) {

  //drop the breadcrumb!
  putCell(x, y, "*", dim, data);
  
  //check neighbors;
  int count = 1;
  if(getcell(x-1, y, dim, data) =="1"){
    count += visitCell(x -1; y, dim, data):
  }
   if(getcell(x+1, y, dim, data) =="1"){
    count += visitCell(x +1; y, dim, data):
  }
  
  if(getcell(x, y-1, dim, data) =="1"){
    count += visitCell(x ; y-1, dim, data):
  }
   if(getcell(x, y+1, dim, data) =="1"){
    count += visitCell(x ; y+1, dim, data):
  }
  

}

int isconnected(int dim, char *block) {

 char *data = strdup(block);
 int x =0;
 int y=0;
 while(getCell(x, y dim, block) != '1'){
    x++;

if(x>= dim){ y++; x=0; }

  }  
  //REcursively visit each cell
  int numConnected = visitCell(x, y, dim, data);
  free(data);
  return numConnected = dim;

}


int getcell(int x, int y, int dim, char *block) {

  if(x<0 || x>=dim ||y<0 ||y>dim) return 0;
  return block[()]

}


int countones(int dim, char * block) {

  int i;
  int counter = 0;
  for(i =0; i < dim* dim; i++)
  {
    if( block[i] == '1'){

counter ++; }

  }
  return counter;

}


int main() {

 //check the command 
 if(argc != 2) 
 {
 
 }
 
 //open the file
 
 
 //initalization
 const int BUFFE_LENGTH =101;
 char buffer[];
 
 int valid = 0;
 int invalid = 0;
 
 while (fgets(buffer, BUFFER_LENGTH, buffer) != NULL){
    //consume any terminating newline

if(buffer[strelen(buffer)-1] =='\n') { buffer[strlen(buffer) -1] ='\0'; }

//Figure out the dimeconsion of the block int dim = sqrt(strlen(buffer));

// Rule 1: check that the dimension is valid if(dim * dim != strlen(buffer)){//if not n^2 then this one is invalid

       invalid++;

continue; // stops the loop and goes to next iteration //break, while stop the entire loop

    }

//Rule2 L check number of ones if(countones(dim, buffer) != dim) { invalid ++; continue; }

    //Rule3 : important! and hard!
    if(!isConnected(dim, buffer)){

invalid ++; continue ;

}

    //Rule4: for a zero, there should be no more than 1 neighbor that is 1??

}


//DFS //Depth-first search //BFS //breadth first search

===================

Rohan Khante

====================

exam review IMP- FREAD AND FSCANF are NOT the SAME

fptr = fopen(filename,"rb");

int a;

double b;

fread(&a,sizeof(int),1,fptr);

fread(&b,sizeof(double),1,fptr);

Person P;

fread(&P,sizeof(Person),1,fptr);

int arr[10];

fread(arr,sizeof(int),10,fptr);

int ch;

ch = fgetc(fptr);

char = oneline[256];

fets(oneline,256,fptr);

(stop at '\n', or reach the limit'\o');

int a,b;

fscanf(fptr,"%d %d", &a,&b);

strcpy,strstr,strcat; Strlen strstr and strcat were discussed NOTE- strlen DOES NOT include the termination character '\0'. so strlen performed on string have abc will return THREE

strstr- return a pointer to the first occurence of str2 in str1 or a null pointer if not found


strcat = concatenation function for strings. strcat("ece","264") will return ece264 string

NOTE2= all these functions are in string.h and this should be included in the header.

  1. include <string.h>

Back to ECE264, Spring 2012, Prof. Lu

Alumni Liaison

Correspondence Chess Grandmaster and Purdue Alumni

Prof. Dan Fleetwood