Monthly Archives: August 2010
Function that returns the average of all the array elements except for the final element
/* (Version 1) The array version of the function */ float average(int m[]){ int total=0,i; float avg; for(i=0;m[i];i++) total+=m[i]; avg=(float)(total)/i; return avg; } /* (Version 2) The pointer version of the function */ float average(int *pm){ int total=0,*pt=pm; … Continue reading
Re-arranging strings using a two dimensional array in C
The program takes a list of strings and re-arranging them into either alphabetical or reverse alphabetical order. #include <stdio.h> #include <string.h> void reorder(int n,char x[10][6]) { char temp[10][6]; int i,item; for(item=0;item<n-1;++item) for(i=item+1;i<n;++i) if(strcmp(&x[item][0],&x[i][0])>0) { strcpy(&temp[0][0],&x[item][0]); strcpy(&x[item][0],&x[i][0]); strcpy(&x[i][0],&temp[0][0]); … Continue reading
strcpy and strncpy in C
#include <stdio.h> #include <string.h> int main(){ char s1[80],s2[10]; int n; printf("Enter two strings : "); scanf("%s%s",s1,s2); strcpy(s1,s2); printf("nString created by strcpy is %s.",s1); printf("nLength of the new string is %d.",strlen(s1)); strncpy(s1,s2,5); s1[5]=”; printf("nString created by strncpy … Continue reading
Example program using strcat and strncat in C
#include <stdio.h> #include <string.h> int main() { char s1[80],s2[10]; int n; printf("Enter two strings : "); scanf("%s%s",s1,s2); strcat(s1,s2); printf("nString built by strcat is %s.",s1); printf("nLength of the string built by strcat is %d.",strlen(s1)); strncat(s1,s2,5); printf("nString built … Continue reading
Using strncmp in C language
The function strncmp has three arguments. The first two arguments are two strings. The third is an integer that specifies the number of characters to compare. #include <stdio.h> #include <string.h> int main() { char s1[80],s2[80]; int outcome; printf("Enter … Continue reading
Illustrate the use of the strcmp function in C
The strcmp function takes two string arguments. Let us call these arguments s1 and s2, respectively. An integer is returned that is less than, equal to, or greater than zero, depending on whether s1 is lexicographically less than, equal to, … Continue reading
Use strlen to find out the length of a string in C
#include <stdio.h> #include <string.h> int main() { char text [80]; int length; printf("Enter a string : "); scanf("%s",text); length=strlen(text); printf("nThe length of the string is : %d.n",length); return 0; }
Example program which uses sscanf and sprintf functions in C
#include <stdio.h> int main() { char s2[130]; int i, j; printf("Enter two numbers : n"); gets(s2); sscanf(s2,"%d%d",&i,&j); printf("nThe values are : %d and %d.n",i,j); return 0; }
Separate each word from the input in C
#include <stdio.h> int main() { char line[80]; int i; puts("Enter a line:"); gets(line); puts("Line is : "); puts(line); for(i=0;line[i]!=”;i++) if(line[i]!=’ ‘) putchar(line[i]); else printf("n"); return 0; }
Illustrating gets and puts functions in C
#include <stdio.h> int main() { char s1[80]; int outcome; printf("Enter a string : "); gets(s1); printf("The string is %sn",s1); printf("Using puts(): ",s1); outcome=puts(s1); printf("nThe value returned by puts( ) is %d.n",outcome); return 0; }
Program to reverse an input string and print the ASCII sum of the characters comprising the string
#include <stdio.h> int main() { const int MaxLine=100; // Sets the limit of characters in string char Line[MaxLine]; int i,Sum=0,ch; /*Read in the string*/ printf("nEnter a set of characters : n"); for(i=0;((ch=getchar())!=’n');++i) Line[i]=(char)ch; Line[i]=”; /*Print the reverse … Continue reading
Program to print the elements of a 2-D array in Row major and Column major orders
#include <stdio.h> int main() { int Temp[3][2]; int i, j; /*Read in the values for the array Temp*/ printf("nEnter the elements of array Temp(3×2) row-wise…n"); for(i=0;i<3;i++) for(j=0;j<2;j++) scanf("%d",&Temp[i][j]); /*Print the array Temp*/ printf("nThe array Temp as … Continue reading
Program to multiply two matrices in C
#include <stdio.h> int main() { const int M=3; // M=no of rows of A and no of rows of C const int N=2; // N=no of cols of A and no of rows of B const int P=2; // … Continue reading
Program to add two matrices using functions in C
#include <stdio.h> int main() { const int M=3,N=3; // M holds no of rows, N holds no of cols int c[M][N],d[M][N],e[M][N]; int i, j; /*Read in the values for the c and d matrices*/ printf("nEnter the elements of … Continue reading
Program to add two matrices in C
#include <stdio.h> int main() { const int M=3,N=3; // M holds no of rows, N holds no of cols int c[M][N],d[M][N],e[M][N]; int i, j; /*Read in the values for the c and d matrices*/ printf("nEnter the elements of … Continue reading
Illustrate the use of 2-D arrays in C
A program that fills a two-dimensional array, prints out values, and sums up the elements of the array. #include <stdio.h> int main() { const int M=3,N=4; // M holds no of rows, N holds no of cols int a[M][N]; … Continue reading
Illustrate the use of 1-D arrays in C
Fill an array, print out values and sum the elements of the array. #include <stdio.h> int main() { int a[6]; // Space for a[0],a[1], … , a[5] is allocated int i,Sum=0; /*Fill the array*/ for(i=0;i<=5;i++) a[i]=i*i; /*Print the … Continue reading
A recursive solution to the towers of Hanoi problem in C
#include <stdio.h> void Hanoi(int, char, char, char) int main() { char src,aux,dest; int n; src=’A'; aux=’B'; dest=’C'; printf("Enter the number of discs in the game : "); scanf("%d",&n); if(n == 0) printf("nNothing has to be moved.n"); … Continue reading
Program to compute a specified Fibonacci number using recursion – modified
#include <stdio.h> long ncalls=0; long fib(int); int main() { int n; printf("Enter the value : "); scanf("%d",&n); printf("nFib(%d) = %ld.",n,fib(n)); printf("nNo of calls to the function : %dn",ncalls); return 0; } long fib(int n) { ncalls++; … Continue reading
Program to compute a specified Fibonacci number using recursion in C
#include <stdio.h> int fib(int); int main() { int n; printf("Enter the value : "); scanf("%d",&n); printf("nFib(%d) = %dn",n,fib(n)); return 0; } int fib(int n) { if(n > 2) return (fib(n-1) + fib(n-2)); else return 1; }