(Some code solutions from exam 3) |
|||
Line 5: | Line 5: | ||
− | + | int main (int argc, char *argv[]) | |
+ | { | ||
+ | FILE *infhd, *outfhd: | ||
+ | if(argc <3){ | ||
+ | printf("error" | ||
+ | return -1;} | ||
+ | |||
+ | infhd = fopen(argv[1]. "r'); | ||
+ | outfhd = fopen(argv[2], "wb"); | ||
+ | |||
+ | if(infhd ==NULL || outfhd == NULL){ | ||
+ | return -1; | ||
+ | } | ||
+ | |||
+ | int count [NUM_LETTER]; | ||
+ | int iter; | ||
+ | for(iter = 0; inter <NUM_LETTER; iter++){ | ||
+ | count[iter] = 0; | ||
+ | } | ||
+ | |||
+ | int ch; | ||
+ | |||
+ | while(!feof(infhd){ | ||
+ | ch = fgetc(infhd); | ||
+ | |||
+ | ch = tolower(ch); //seperate header file | ||
+ | OR | ||
+ | if((ch >= 'a') && (ch <="z")){ | ||
+ | count[ch-'a']++; | ||
+ | } | ||
+ | else if((ch >= "A') && (ch <= "Z')){ | ||
+ | count[ch -"A']++; | ||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | fwrite(count, sizeof(int), NUM_LETTER, outfhd); | ||
+ | |||
+ | fclose(outfhd); | ||
+ | fclose(infhd); | ||
+ | } | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | Question 2 | ||
+ | |||
+ | //when not dynamically allocated, you have to make a copy (name) | ||
+ | |||
+ | Student *Student_create(char *n, double g) | ||
+ | { | ||
+ | Student *s = malloc(sizeof(Student)); | ||
+ | s->name = strdup(n); | ||
+ | or | ||
+ | s->name = malloc(sizeof(n) +1 * sizeof(char)); | ||
+ | strcpy(s->name, n); | ||
+ | |||
+ | s->gpa = g; | ||
+ | return s; | ||
+ | } | ||
+ | |||
+ | Student *Student_copy(Student *s) | ||
+ | { | ||
+ | return Student_create(s->name, s->gpa); | ||
+ | } | ||
+ | |||
+ | void Student_destroy(Student *s) | ||
+ | { | ||
+ | free(s->name); | ||
+ | //NOT OK free(s->gpa); | ||
+ | free(s); | ||
+ | } | ||
+ | |||
+ | void Student_assign (Student **dest, Student *src) | ||
+ | { | ||
+ | if((*dest) ==src) return; | ||
+ | Student_destroy(*dest); //destroy destination pointing to | ||
+ | *dest = Student_copy(src); | ||
+ | } | ||
+ | |||
+ | // Student *s1 is a pointer | ||
+ | //pointer to pointer you have to use & | ||
+ | |||
+ | Student_assign(&S3, S1); | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | Question 3 | ||
+ | |||
+ | |||
+ | Node *List_search(Node *head, int v) | ||
+ | { | ||
+ | while(head != NULL) { | ||
+ | if(head ->value == v) return head; | ||
+ | head = head ->next; | ||
+ | } | ||
+ | return NULL; //never found | ||
+ | } | ||
+ | |||
+ | OR | ||
+ | |||
+ | if(head == NULL) return NULL; | ||
+ | if(head->value == v) return head; | ||
+ | return List_search(head->next, v); | ||
+ | |||
+ | |||
+ | Node *List_reverse(Node *head) | ||
+ | { | ||
+ | Node *new_head = NULL; | ||
+ | |||
+ | while(head != NULL){ | ||
+ | Node *tmp = head; | ||
+ | head = head->next; | ||
+ | |||
+ | //next pointer of temp will be old head | ||
+ | tmp->next = new_head; | ||
+ | new_head = tmp; | ||
+ | } | ||
+ | return new_head; | ||
+ | } | ||
+ | |||
+ | Node *List_delete(Node *head, int v) | ||
+ | { | ||
+ | //three cases | ||
+ | |||
+ | //empty list | ||
+ | if(head == NULL) return NULL; | ||
+ | |||
+ | //value is in head | ||
+ | if(head->value == v){ | ||
+ | Node *new_head = head->next; | ||
+ | Node_destroy(head); | ||
+ | return new_head; | ||
+ | } | ||
+ | |||
+ | //Iteration case | ||
+ | while {curr-> next != NULL){ | ||
+ | |||
+ | //Success case | ||
+ | if(curr->next ->value == v){ | ||
+ | Node *tmp = curr->next; | ||
+ | Node_destroy(tmp); | ||
+ | } | ||
+ | curr = curr->next; | ||
+ | } | ||
+ | |||
+ | |||
+ | //If we reached this point, there is no node of that value | ||
+ | return head; | ||
+ | } | ||
Latest revision as of 10:15, 27 April 2011
Exam3 review
int main (int argc, char *argv[]) { FILE *infhd, *outfhd: if(argc <3){
printf("error" return -1;}
infhd = fopen(argv[1]. "r'); outfhd = fopen(argv[2], "wb");
if(infhd ==NULL || outfhd == NULL){
return -1;
}
int count [NUM_LETTER]; int iter; for(iter = 0; inter <NUM_LETTER; iter++){ count[iter] = 0; }
int ch;
while(!feof(infhd){ ch = fgetc(infhd);
ch = tolower(ch); //seperate header file OR if((ch >= 'a') && (ch <="z")){ count[ch-'a']++; } else if((ch >= "A') && (ch <= "Z')){ count[ch -"A']++; }
}
fwrite(count, sizeof(int), NUM_LETTER, outfhd);
fclose(outfhd); fclose(infhd); }
Question 2
//when not dynamically allocated, you have to make a copy (name)
Student *Student_create(char *n, double g) { Student *s = malloc(sizeof(Student)); s->name = strdup(n); or s->name = malloc(sizeof(n) +1 * sizeof(char)); strcpy(s->name, n);
s->gpa = g; return s; }
Student *Student_copy(Student *s) { return Student_create(s->name, s->gpa); }
void Student_destroy(Student *s) { free(s->name); //NOT OK free(s->gpa); free(s); }
void Student_assign (Student **dest, Student *src) { if((*dest) ==src) return; Student_destroy(*dest); //destroy destination pointing to *dest = Student_copy(src); }
// Student *s1 is a pointer //pointer to pointer you have to use &
Student_assign(&S3, S1);
Question 3
Node *List_search(Node *head, int v)
{
while(head != NULL) {
if(head ->value == v) return head;
head = head ->next;
}
return NULL; //never found
}
OR
if(head == NULL) return NULL; if(head->value == v) return head; return List_search(head->next, v);
Node *List_reverse(Node *head)
{
Node *new_head = NULL;
while(head != NULL){ Node *tmp = head; head = head->next;
//next pointer of temp will be old head tmp->next = new_head; new_head = tmp; } return new_head; }
Node *List_delete(Node *head, int v) { //three cases
//empty list if(head == NULL) return NULL;
//value is in head if(head->value == v){ Node *new_head = head->next; Node_destroy(head); return new_head; }
//Iteration case while {curr-> next != NULL){
//Success case if(curr->next ->value == v){ Node *tmp = curr->next; Node_destroy(tmp); } curr = curr->next; }
//If we reached this point, there is no node of that value
return head;
}