Last Lecture Reminder Array Sorts Exam Questions Examples Open Questions ◦ Search Engines ◦ Operating Systems ◦ Multiple Files in Projects Additional Lesson – before MOED ALEF
Linked Lists Files
2345 Head Next Next = NULL
typedef struct link{ int data; struct link *next; }link;
void main(){ link *head=NULL, *temp=NULL; int d; printf("Enter the list values:\n"); scanf ("%d",&d); while (d!=-1){ temp=(link *)malloc(sizeof(link)); if (!temp) break; temp->next=head; temp->data=d; head=temp; scanf ("%d",&d); } A list is presented by its head
while(temp){ printf("%d ",temp->data); temp=temp->next; } while (head){ temp=head->next; free(head); head=temp; }
void free_list(link *head) { if (head== NULL) /* Finished freeing. Empty list */ return; free_list(head->next); /* Recursively free what’s ahead */ free(head); }
C Program STDIN STDOUT STDERR FILE1 FILE2
stdin stdout stderr
#include void main(){ int n; printf("hello world"); fprintf(stdout,"hello world"); fprintf(stderr,"hello world"); fscanf(stdin,"%d", &n); scanf("%d", &n); }
#include void main(){ FILE *f1; f1 = fopen(“test1.txt","w"); if(!f1){ printf("Error opening file!\n"); return; } fprintf(f1,"hello world"); fclose(f1); }
#include void main(){ char ch; FILE *f1; f1 = fopen(“test2.txt","r"); if(!f1){ printf("Error opening file!\n"); return; } fscanf(f1,"%c",&ch); fclose(f1); }
Bubble Sort Selection Sort Insertion Sort Merge Sort Quick Sort
#include void main() { int i, j, min, temp, nums[10]; for (i=0; i<SIZE; i++){ /*input*/ printf ("enter number\n"); scanf ("%d",&nums[i]); } for (i=0; i<SIZE; i++){ /*main loop*/ for (j=0; j<SIZE-i-1; j++) /*finding smaller*/ if (nums[j]>nums[j+1]) { temp = nums[j]; /*swapping*/ nums[j] = nums[j+1]; nums[j+1] = temp; } for (i=0; i<SIZE; i++) /*printing*/ printf ("%d\n",nums[i]); }
Unsorted array: Effective array of size 6: Effective array of size 5: Effective array of size 4: Effective array of size 3: Effective array of size 2: Effective array of size 1: Sorted array: 12
#include void main() { int i, j, min, temp, nums[10]; for (i=0; i<10; i++){ /*input*/ printf ("enter number\n"); scanf ("%d",&nums[i]); } for (i=0; i<9; i++){ /*main loop*/ min = i; for (j=i+1; j<10; j++) /*finding minimum index*/ if (nums[j]<nums[min]) min = j; temp = nums[i]; /*swaping*/ nums[i] = nums[min]; nums[min] = temp; } for (i=0; i<10; i++) /*printing*/ printf ("%d\n",nums[i]); }
375912
#include void main() { int i, j, temp, nums[10]; for (i=0; i<10; i++){ /*input*/ printf ("enter number\n"); scanf ("%d",&nums[i]); } for (i=0; i<10; i++){ /*main loop*/ /*finding location of nums[j]*/ for (j=i; j>0 && nums[j]<nums[j-1]; j--){ temp = nums[j]; /*swaping*/ nums[j] = nums[j-1]; nums[j-1] = temp; } for (i=0; i<10; i++) /*printing*/ printf ("%d\n",nums[i]); }
void mergeSort(int ar[],int lo,int hi){ if (lo<hi) { int m=(lo+hi)/2; mergeSort(ar,lo, m); mergeSort(ar, m+1, hi); merge(ar, lo, m, hi); }
#define SIZE 100 void merge(int a[],int lo,int m, int hi){ int i, j, k, b[SIZE]; for (i=lo; i<=hi; i++) b[i]=a[i]; i=lo; j=m+1; k=lo; while (i<=m && j<=hi) if (b[i]<=b[j]) a[k++]=b[i++]; else a[k++]=b[j++]; while (i<=m) a[k++]=b[i++]; }
What’s printed on the screen when the following programs are run? ◦ printing.c ◦ change_val.c What does this function do? ◦ secret.c
Implement a function such that – ◦ Input: string s that contains (among other things) parentheses ◦ Output: 1 if the parentheses’ nesting is legal, 0 otherwise ◦ For example – (()())() is legal, while ())() and (()(()) are not. Write a program that accepts a string from the user and checks the parentheses.
Write a structure of a point in 2D space and a distance function between two points Write a program that gets an array of points from the user and calculates the largest distance between a pair of points in the array The size of the array is given by the user before the points are read in Solution – max_distance.c
Implement a linked list where each item simply contains an integer Input a number n from the user, and split the original list into two lists such that the first contains all the elements smaller than n, and the other contains all the others Display both linked lists on the screen
Define two structures – a point and a circle Implement a function is_in_circle that accepts a point and a circle, and returns 1 if the point is in the circle, 0 otherwise Write a program that accepts input from user in the following order: ◦ n = the number of circles ◦ A point And outputs the number of circles containing that point n is user input!`
Search Engines Operating Systems Multiple Files in Projects