C Program to generate sparse matrix.

C Program to generate sparse matrix.
A sparse matrix is a matrix that allows special techniques to take advantage of the large number of zero elements.Sparse matrix is very useful in engineering field, when solving the partial differentiation equations. Read more about how to generate sparse matrix.
Read more about C Programming Language .

/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/

#include<stdio.h>
#include<conio.h>
void main()
{
int A[10][10],B[10][3],m,n,s=0,i,j;
clrscr();
printf("nEnter the order m x n of the sparse matrixn");
scanf("%d%d",&m,&n);
printf("nEnter the elements in the sparse matrix(mostly zeroes)n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("n%d row and %d column: ",i,j);
scanf("%d",&A[i][j]);
}
}
printf("The given matrix is:n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%d ",A[i][j]);
}
printf("n");
}
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
if(A[i][j]!=0)
{
B[s][0]=A[i][j];
B[s][1]=i;
B[s][2]=j;
s++;
}
}
}
printf("nThe sparse matrix is given by");
printf("n");
for(i=0;i<s;i++)
{
for(j=0;j<3;j++)
{
printf("%d ",B[i][j]);
}
printf("n");
}
getch();
}


Read more c programs 
C Basic
C Strings
Matrix in c

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

To browse more C Programs visit this link
(c) www.c-program-example.com

C Program to demonstrate sprintf statement.

C Program to demonstrate the ‘sprintf‘ statement. This example is a bit lame as the same effect can be seen with a ‘printf’. But, it does show a string being built and passed into a function. Read more about C Programming Language .

/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/

#include <stdio.h>

main()
{
int i=1; /* Define an integer variable. */
char message[80]; /* Text string */

/* format text and put into 'message' this a great
* improvement over using 'strcpy' and 'strcat' to
* build a text string.
*/
sprintf (message, "i is %i", i);
/* I may be stating the obvious but a '' is
* put on the end of the string. */

puts(message); /* Display message */

}

Read more c programs 
C Basic
C Strings
K and R C Programs Exercise

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

To browse more C Programs visit this link
(c) www.c-program-example.com

K & R C Programs Exercise 5-6.

K and R C, Solution to Exercise 5-6:
K and R C Programs Exercises provides the solution to all the exercises in the C Programming Language (2nd Edition). You can learn and solve K&R C Programs Exercise.
C Program to Rewrite appropriate programs from earlier chapters and exercises with pointers instead of array indexing. Good possibilities include getline (Chapters 1 and 4), atoi , itoa , and their variants (Chapters 2, 3, and 4), reverse (Chapter 3), and strindex and getop (Chapter 4). Read more about C Programming Language .

/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/




#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>

/* getline: get line into s, return length */
int getline(char *s, int lim)
{
char *p;
int c;

p = s;
while (--lim > 0 && (c = getchar()) != EOF && c != 'n')
*p++ = c;
if (c == 'n')
*p++ = c;
*p = '';
return (int)(p - s);
}


int atoi(char *s)
{
int n, sign;

while (isspace(*s))
s++;
sign = (*s == '+' || *s == '-') ? ((*s++ == '+') ? 1 : -1) : 1;
for (n = 0; isdigit(*s); s++)
n = (n * 10) + (*s - '0');
return sign * n;
}

/*The itoa() function converts an integer value into an
ASCII string of digits.*/

char *utoa(unsigned value, char *digits, int base)
{
char *s, *p;

s = "0123456789abcdefghijklmnopqrstuvwxyz";
if (base == 0)
base = 10;
if (digits == NULL || base < 2 || base > 36)
return NULL;
if (value < (unsigned) base) {
digits[0] = s[value];
digits[1] = '';
} else {
for (p = utoa(value / ((unsigned)base), digits, base);
*p;
p++);
utoa( value % ((unsigned)base), p, base);
}
return digits;
}

char *itoa(int value, char *digits, int base)
{
char *d;
unsigned u;

d = digits;
if (base == 0)
base = 10;
if (digits == NULL || base < 2 || base > 36)
return NULL;
if (value < 0) {
*d++ = '-';
u = -((unsigned)value);
} else
u = value;
utoa(u, d, base);
return digits;
}


static void swap(char *a, char *b, size_t n)
{
while (n--) {
*a ^= *b;
*b ^= *a;
*a ^= *b;
a++;
b++;
}
}

void my_memrev(char *s, size_t n)
{
switch (n) {
case 0:
case 1:
break;
case 2:
case 3:
swap(s, s + n - 1, 1);
break;
default:
my_memrev(s, n / 2);
my_memrev(s + ((n + 1) / 2), n / 2);
swap(s, s + ((n + 1) / 2), n / 2);
break;
}
}

void reverse(char *s)
{
char *p;

for (p = s; *p; p++)
;
my_memrev(s, (size_t)(p - s));
}



static char *strchr(char *s, int c)
{
char ch = c;

for ( ; *s != ch; ++s)
if (*s == '')
return NULL;
return s;
}

int strindex(char *s, char *t)
{
char *u, *v, *w;

if (*t == '')
return 0;
for (u = s; (u = strchr(u, *t)) != NULL; ++u) {
for (v = u, w = t; ; )
if (*++w == '')
return (int)(u - s);
else if (*++v != *w)
break;
}
return -1;
}


#define NUMBER '0'

int getop(char *s)
{
int c;

while ((*s = c = getch()) == ' ' || c == 't')
;
*(s + 1) = '';
if (!isdigit(c) && c != '.')
return c; /* not a number */
if (isdigit(c)) /* collect integer part */
while (isdigit(*++s = c = getch()))
;
if (c == '.') /* collect fraction part */
while (isdigit(*++s = c = getch()))
;
*++s = '';
if (c != EOF)
ungetch(c);
return NUMBER;
}




Read more c programs 
C Basic
C Strings
K and R C Programs Exercise

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

To browse more C Programs visit this link
(c) www.c-program-example.com

K & R C Programs Exercise 5-5.

K and R C, Solution to Exercise 5-5:
K and R C Programs Exercises provides the solution to all the exercises in the C Programming Language (2nd Edition). You can learn and solve K&R C Programs Exercise.
C Program to write the versions of the library functions strncpy, strncat, and strncmp, which operate on at most the first n characters of their argument strings.
For example, strncpy(s, t, n) copies at most n characters of t to s. Read more about C Programming Language .

/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/

/*strncpy: copy n characters from t to s*/
void strncpy(char *s, char *t, int n)
{
while(*t && n--> 0)
*s++ = *t++;
while( n--> 0)
*s++ = '';
}

/*strncat: concatenate n characters of t to end of s*/
void strncat(char *s, char *t, int n)
{
void strncpy(char *s, char *t, int n);
int strlen(char *);
strncpy(s+strlen(s), t, n);
}

/*strncmp: comapre at most n characters of t with s */
int strncmp(char *s, char *t, int n)
{
for( ; *s == *t; s++, t++)
if(*s == '' || --n <= 0)
{
return 0;
}
return *s - *t;
}
Read more c programs 
C Basic
C Strings
K and R C Programs Exercise

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

To browse more C Programs visit this link
(c) www.c-program-example.com

K & R C Programs Exercise 5-4.

K and R C, Solution to Exercise 5-4:
K and R C Programs Exercises provides the solution to all the exercises in the C Programming Language (2nd Edition). You can learn and solve K&R C Programs Exercise.
C Program, that returns 1 if the string t occurs at the end of the string s, and zero otherwise. Read more about C Programming Language .

/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/


#include <stdio.h>

//finds the string length, standard "strlen" function
int str_len(char *s)
{
int n;

for(n = 0; *s != ''; s++)
{
n++;
}
return n;
}

int str_cmp(char *s, char *t)
{
for(;*s == *t; s++, t++)
if(*s == '')
return 0;
return *s - *t;
}


int str_end(char *s, char *t)
{
int Result = 0;
int s_length = 0;
int t_length = 0;

/* get the lengths of the strings */
s_length = str_len(s);
t_length = str_len(t);

/* check if the lengths mean that the string t could fit at the string s */
if(t_length <= s_length)
{
/* advance the s pointer to where the string t would have to start in string s */
s += s_length - t_length;

/* and make the compare using strcmp */
if(0 == str_cmp(s, t))
{
Result = 1;
}
}

return Result;
}
int main(void)
{
char Str1[8192] ;
char Str2[8192] ;
char Str3[8192] ;
printf("n Enter the first string: n");
scanf("%s",Str1);
printf("n Enter the second string: n");
scanf("%s",Str2);
printf("n Enter the third string: n");
scanf("%s",Str3);
printf("String one is (%s)n", Str1);
printf("String two is (%s)n", Str2);
printf("String two is (%s)n", Str3);

if(str_end(Str1, Str2))
{
printf("The string (%s) has (%s) at the end.n", Str1, Str2);
}
else
{
printf("The string (%s) doesn't have (%s) at the end.n", Str1, Str2);
}
if(str_end(Str1, Str3))
{
printf("The string (%s) has (%s) at the end.n", Str1, Str3);
}
else
{
printf("The string (%s) doesn't have (%s) at the end.n", Str1, Str3);
}



return 0;
}


C BasicC StringsK and R C Programs ExerciseYou can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

To browse more C Programs visit this link
(c) www.c-program-example.com

K & R C Programs Exercise 5-3.

K and R C, Solution to Exercise 5-3:
K and R C Programs Exercises provides the solution to all the exercises in the C Programming Language (2nd Edition). You can learn and solve K&R C Programs Exercise.
C Program to concatenate the two strings using the pointers
This program shows, how the standard library function “strcat” works!. Read more about C Programming Language .

/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/


#include <stdio.h>
/* str_cat: concatenate t to the end of s: pointer version */
void str_cat(char *s, char *t)
{
/* run through the destination string until we point at the terminating '' */
while('' != *s)
{

++s;
}

/* now copy until we run out of string to copy */
while('' != (*s = *t))
{
++s;
++t;
}

}
int main(void)
{
char Str1[8192] ;
char Str2[8192] ;
printf("n Enter the first string: n");
scanf("%s",Str1);
printf("n Enter the second string: n");
scanf("%s",Str2);
printf("String one is (%s)n", Str1);
printf("String two is (%s)n", Str2);

str_cat(Str1, Str2);
printf("The combined string is (%s)n", Str1);

return 0;
}


C BasicC StringsK and R C Programs ExerciseYou can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

To browse more C Programs visit this link
(c) www.c-program-example.com

K & R C Programs Exercise 4-14.

K and R C, Solution to Exercise 4-14:
K and R C Programs Exercises provides the solution to all the exercises in the C Programming Language (2nd Edition). You can learn and solve K&R C Programs Exercise.
Write C Program to swap two arguments using macros.
C Program to swap(t, x, y) that interchanges two arguments of type t using the block structure.

/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/

#include <stdio.h>

#define swap(t, x, y)
do {
t safe ## x ## y;
safe ## x ## y = x;
x = y;
y = safe ## x ## y;
} while (0)

int main(void) {
int inum1, inum2;
double dnum1, dnum2;
char *ch1, *ch2;
printf("nEnter two Intgers:n");
scanf("%d%d",&inum1,&inum2);
printf("nIntegers before swap:n inum1= %dn inum2= %dn", inum1, inum2);
swap(int, inum1, inum2);
printf("nIntegers after swap:n inum1= %dn inum2= %dn", inum1, inum2);

printf("nEnter two Doubles:n");
scanf("%f%f",&dnum1,&dnum2);
printf("nDoubles before swap:n dnum1= %gn dnum2= %gn", dnum1, dnum2);
swap(double, dnum1, dnum2);
printf("nDoubles after swap:n dnum1= %gn dnum2= %gn", dnum1, dnum2);

printf("nEnter two Strings:n");
scanf("%s%s",ch1,ch2);
printf("n Strings before swap:n ch1= %sn ch2 = %sn", ch1, ch2);
swap(char *, ch1, ch2);
printf("nStrings after swap:n ch1= %sn ch2= %sn", ch1, ch2);

return 0;
}
Read more Similar C Programs
C Basic
C Strings
K and R C Programs Exercise

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

To browse more C Programs visit this link
(c) www.c-program-example.com

K & R C Programs Exercise 4-12.

K and R C, Solution to Exercise 4-12:
K and R C Programs Exercises provides the solution to all the exercises in the C Programming Language (2nd Edition). You can learn and solve K&R C Programs Exercise.
Write a C program to convert an integer into a string by calling a recursive routine.Recursive routine is the programming technique that a routine invoking itself again and again. Read more about C Programming Language .

/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/

#include<math.h>
void itoa(int n, char s[]);
int main(void) {
char buffer[20];

//for testing!
printf("INT_MIN: %dn", INT_MIN);
itoa(INT_MIN, buffer);
printf("Buffer : %sn", buffer);

return 0;
}
/* itoa: convert n to characters in s; recursive */
void itoa(int n, char s[])
{
static int i;
if(n / 10)
itoa(n /10, s);
else{
i = 0;
if(n < 0)
s[i++] = '-';
}
s[i++] = abs(n) %10 + '0';
s[i] = '';
}

Read more Similar C Programs
C Basic
C Strings
K and R C Programs Exercise

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

To browse more C Programs visit this link
(c) www.c-program-example.com

C Program to find sum and product using macros.

Write C Program to find sum and product using macros.
Macro is a piece of text that is expanded by the preprocessor part of the compiler. This is used in to expand text before compiling. Macro definitions (#define), and conditional inclusion (#if). In many C implementations, it is a separate program invoked by the compiler as the first part of translation.Read more about C Programming Language .

/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/

#include<stdio.h>
#define SUM(A, B) (A + B)
#define PROD(A, B) (A * B)
int main()
{
int num1, num2, sum, product;
printf("nEnter the Two numbersn");
scanf("%d%d",&num1,&num2);
sum=SUM(num1,num2);
product=PROD(num1,num2);
printf("nnSum of two numbers using Macros is:%dn",sum);
printf("nnProduct of two numbers using macros is:%dn",product);
return 0;
}
Read more Similar C Programs
Learn C Programming

Number System

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

To browse more C Programs visit this link
(c) www.c-program-example.com

K & R C Programs Exercise 4-9.

K and R C, Solution to Exercise 4-9:
K and R C Programs Exercises provides the solution to all the exercises in the C Programming Language (2nd Edition). You can learn and solve K&R C Programs Exercise.
C Program to modify the K & R C Programs Exercise 4-8, getch and ungetch do not handlea pushed back EOF correctly. Decide what their properties ought to be if an EOF is pushed back, then implement the design
Read more about C Programming Language .

/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/

#include<stdio.h>
#define BUFSIZE 100
int buf[BUFSIZE];
int bufp = 0;

//getch: get a (possibly pushed back) character
int getch(void)
{
return (bufp > 0) ? buf[--bufp] : getchar();
}
//ungetch: push a character back onto the input
void ungetch(int c)
{
if(bufp >= BUFSIZE)
printf("ungetch: too many characters!");
else
buf[bufp++] = c;
}
Read more Similar C Programs
C Basic
C Strings
K and R C Programs Exercise

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

To browse more C Programs visit this link
(c) www.c-program-example.com