Posts

C Quiz : Part-I

1. What is the correct value to return to the operating system upon the successful completion of a program?   A. -1 B. 1   C. 0 D. Programs do not return a value.   2. What is the only function all C programs must contain? A. start() B. system()   C. main() D. program()   3. What punctuation is used to signal the beginning and end of code blocks?   A. { } B. -> and <- C. BEGIN and END D. ( and )   4. What punctuation ends most lines of C code? A. .  B. ; C. : D. '   5. Which of the following is a correct comment? A. */ Comments */ B. ** Comment **   C. /* Comment */ D. { Comment }   6. Which of the following is not a correct variable type? A. float   B. real C. int D. double   7. Which of the following is the correct operator to compare two variables? A. := B. = C. equal   D. == 8. Which of the following is true? A. 1 B. 66 C. .1 D. -1 E. All of the above   9. Which of the following is the boolean

String functions in C

1. C Program to Find the Frequency of Characters in a String 2 . C Program to Reverse a String by Passing it to Function Source Code 1: #include <stdio.h> int main (){ char c [ 1000 ], ch ; int i , count = 0 ; printf ( "Enter a string: " ); gets ( c ); printf ( "Enter a characeter to find frequency: " ); scanf ( "%c" ,& ch ); for ( i = 0 ; c [ i ]!= '\0' ;++ i ) { if ( ch == c [ i ]) ++ count ; } printf ( "Frequency of %c = %d" , ch , count ); return 0 ; } Output Enter a string: md. ahsan arif Enter a frequency to find frequency: a Frequency of a = 3   2:   #include <stdio.h> #include <string.h> void Reverse ( char str []); int main (){ char str [ 100 ]; printf ( "Enter a string to reverse: " ); gets ( str ); Reverse ( str ); printf ( "Reversed string: " ); puts ( s

Sorting and Searching(Data Structures)

Click for: Sorting and Searching Using C

Array : Dynamic

/* A dynamically-allocated 1-D array */ #include <stdio.h> int main (void) { double* array; /* declare a pointer only */ int i, size; /* ask user for size of array */ printf ("How large do you want your array? "); scanf ("%d", &size); /* allocate the array in the heap */ array = (double *) calloc (size, sizeof(double)); /* printing the array for verification surprise! a dynamic array is automatically initialized with zeros! */ for (i = 0; i < size; ++i) printf ("%6.2lf", array[i]); /* freeing the memory allocation */ free (array); return (0); }

Array : Search

/* THE BASIC SEARCH ALGORITHM FOR A 1-D ARRAY */ #include <stdio.h> int search ( int arraytosearch[], int valuetosearch, int size ) { int i, found; /* initialize found at -1, if value not found, stays at -1 */ found = -1; /* search until found or until end of array */ i = 0; while (found<0 && i<size) { if (arraytosearch[i] == valuetosearch) found = i; /* I have found it! */ else i = i + 1; } return (found); } int main (void) { int x[] = {12,67,56,60,88,34,123}; int value = 60; int pos, i; pos = search (x, value, 7); if (pos >= 0) printf ("%d was found at position %d.\n", value, pos); else printf ("%d was not found in the array.\n", value); return (0); } Legend: preprocessor directives | variable declarations | main program | helper functions | user-defined structures | comments 60 was found at position 3.

Array : Adding Two Arrays

/* Problem: Add the corresponding values from two arrays of the same size. */ #include <stdio.h> /* the function that adds the two arrays a1 and a2. it "returns" a3 back */ void addarrays ( int a1[], int a2[], int a3[], int n ) { int i; /* do the adding of every corresponding cells */ for (i=0; i<n; ++i) a3[i] = a1[i] + a2[i]; } int main (void) { int x[] = {1,2,3,4}, i; int y[] = {10,20,30,40}; int z[4]; /* call the function */ addarrays (x, y, z, 4); /* print a report */ for (i=0; i<4; ++i) printf ("%3d", x[i]); printf ("\n + \n"); for (i=0; i<4; ++i) printf ("%3d", y[i]); printf ("\n-------------\n"); for (i=0; i<4; ++i) printf ("%3d", z[i]); return (0); }

Array : Filling Partially

/* Problem: This program partially fills an array from a file until the end of file (EOF). We get the actual number of data read */ #include <stdio.h> int array_from_file ( double a[], int size ) { int i; FILE* in; in = fopen ("data_array.dat", "r"); i=0; /* the first cell */ /* filling the array cell by cell */ /* until it is full or until the EOF */ while (i < 100 && fscanf (in, "%lf", &a[i]) != EOF) { i=i+1; } fclose (in); /* the actual number of values in the array */ return (i); } int main (void) { double array[100]; int actual_size, i; actual_size = array_from_file (array, 100); for (i=0; i < actual_size; ++i) printf ("%3.1lf ", array[i]); printf ("\nThe array contains %d values ", actual_size);