Upcoming SlideShare
×

array, function, pointer, pattern matching

389 views
225 views

Published on

As a part of the course material of CSC 391- Data structure and algorithm. Review of previous concept of array, pointer and functions.

Published in: Education, Technology
0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Views
Total views
389
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
12
0
Likes
0
Embeds 0
No embeds

No notes for slide

array, function, pointer, pattern matching

1. 1. Practice codes for CSC- 391 ARRAY< FUNCTION< POINTERS 1. Passing pointer to array #include <stdio.h> #include <time.h> void getSeconds(unsigned long *par); int main () { unsigned long sec; getSeconds( &sec ); /* print the actual value */ printf("Number of seconds: %ldn", sec ); return 0; } void getSeconds(unsigned long *par) { /* get the current number of seconds */ *par = time( NULL ); return; } 2. passing array to a function #include <stdio.h> #include <stdio.h> double getAverage(int *arr, int size); int main () { int balance[5] = {1000, 2, 3, 17, 50}; double avg; /* pass pointer to the array as an argument */ avg = getAverage( balance, 5 ) ; /* output the returned value */ printf("Average value is: %fn", avg );
2. 2. return 0; } double getAverage(int *arr, int size) { int i, sum = 0; double avg; for (i = 0; i < size; ++i) { sum += arr[i]; } avg = (double)sum / size; return avg; } // double getAverage(int arr[], int size) { int i; double avg; double sum; for (i = 0; i < size; ++i) { sum += arr[i]; } avg = sum / size; return avg; } Some methods in string.h: S.N. Function & Purpose 1 strcpy(s1, s2); Copies string s2 into string s1. 2 strcat(s1, s2); Concatenates string s2 onto the end of string s1. 3 strlen(s1); Returns the length of string s1. 4 strcmp(s1, s2); Returns 0 if s1 and s2 are the same; less than 0 if s1<s2; greater than 0 if s1>s2. 5 strchr(s1, ch); Returns a pointer to the first occurrence of character ch in string s1. 6 strstr(s1, s2); Returns a pointer to the first occurrence of string s2 in string s1. 3. Using string methods: #include <stdio.h> #include <string.h> int main () { char str1[12] = "Hello"; char str2[12] = "World";
3. 3. char str3[12]; int len ; /* copy str1 into str3 */ strcpy(str3, str1); printf("strcpy( str3, str1) : %sn", str3 ); /* concatenates str1 and str2 */ strcat( str1, str2); printf("strcat( str1, str2): %sn", str1 ); /* total lenghth of str1 after concatenation */ len = strlen(str1); printf("strlen(str1) : %dn", len ); return 0; } 4. Returning pointer from a function #include <stdio.h> #include <stdlib.h> #include <time.h> /* function to generate and retrun random numbers. */ int * getRandom( ) { static int r[10]; int i; for ( i = 0; i < 10; ++i) { r[i] = rand(); printf("%dn", r[i] ); } return r; } /* main function to call above defined function */ int main () { int *p; int i; p = getRandom(); for ( i = 0; i < 10; i++ ) {
4. 4. printf("*(p + [%d]) : %dn", i, *(p + i) ); } return 0; } Pattern Matching #include<stdio.h> #include<conio.h> #include<string.h> int main() { char T[100]="Pattern Matching Algorithm"; char P[20]="Pattern"; int r,s,i, k=0, max, index; s=strlen(T); r=strlen(P); max= s-r; while(k<=max) { for(i=0; i<r; i++) { if(P[i]!=T[k+i]) break; } if(i==R) { index=k; break; } else k=k+1; } if(k>max) index=-1; printf("T = %s", T); printf("nP = %s", P); if(index!=-1) printf("nn Index of P in T is %d", index); else printf("n P does not occur in T"); getch(); return 0; }