K & R C Programs Exercise 4-8.

K and R C, Solution to Exercise 4-8:
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-7, Suppose there will never be more than one character of pushback. Modify getch and ungetch accordingly. 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>
char buf = 0;

//getch: get a (possible pushed character back) character
int getch(void)
{
 int c;
 if(buf != 0)
  c = buf;
 else
  c getchar();
 buf = =0;
 return c;
}

/*ungetch: push string back onto the input*/
void ungetch(int c)
{
 if (buf != 0)
  printf("ungetch: too many characters!n");
 else
  buf = c;
}
int main(void)
{
 int c;

 while ((c = getch()) != EOF) {
  if (c == '/') {
   putchar(c);
   if ((c = getch()) == '*') { 
    ungetch('!');
   }         
  } 
  putchar(c);               
 }
 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 3-6.

K and R C, Solution to Exercise 3-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 change version of itoa that accepts three arguments instead of two(K and R C Exercise 3-4). The third argument is a minimum field width; the converted number must be paddle with blanks on the left if necessary to make it wide enough. 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 <stdlib.h>
#include <stdio.h>
#include <limits.h>
#define abs(x) ((x) < 0 ? -(x) : (x))
void itoa(int n, char s[], int w);
void reverse(char s[]);

int main(void) {
char buffer[20];

printf("INT_MIN: %dn", INT_MIN);
itoa(INT_MIN, buffer);
printf("Buffer : %sn", buffer);

return 0;
}
//itoa: convert to n characters in s, w characters wide.
void itoa(int n, char s[], int w)
{
void int i, sign;
void everse(char s[]);
sign = n;
i = 0;
do{
s[i++] = abs(n%10) + '0';
printf("%d %% %d + '0' = %dn", n, 10, s[i-1]);
}while((n/=10)!=0);
if(sign < 0)
s[i++] = '-';
while(i < w)
s[i++] = ' ';
s[i] = '';
reverse(s);
}
void reverse(char s[]) {
int c, i, j;
for ( i = 0, j = strlen(s)-1; i < j; i++, j--) {
c = s[i];
s[i] = s[j];
s[j] = 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

C Program to reverse a Linked List.

Data structures using C,
C Program to reverse a Linked List. Linked list is a data structure in which the objects are arranged in a linear order. Linked List contains group of nodes, in which each node contains two fields, one is data field and another one is the reference field which contains the address of next node. In this program, We reverse the linked list, but without sorting the linked list.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 <stdlib.h>

#define MAX 100

struct leftnode {
int num;
struct leftnode *next;
};


void linklist_add(struct leftnode **n, int val);

void linklist_reverse(struct leftnode **n);

void linklist_display(struct leftnode *n);

int main(void) {
struct leftnode *new = NULL;
int i = 0;

for(i = 0; i <= MAX; i++)
linklist_add(&new, i);

printf("Linked list before reversal:n");
linklist_display(new);
linklist_reverse(&new);
printf("Linked list after reversal:n");
linklist_display(new);

return 0;
}


void linklist_add(struct leftnode **n, int val) {
struct leftnode *temp = NULL;
temp = malloc(sizeof(struct leftnode));
temp->num = val;
temp->next = *n;
*n = temp;
}


void linklist_reverse(struct leftnode **n) {
struct leftnode *a = NULL;
struct leftnode *b = NULL;
struct leftnode *c = NULL;
a = *n, b = NULL;

while(a != NULL) {
c = b, b = a, a = a->next;
b->next = c;
}

*n = b;
}

void linklist_display(struct leftnode *n) {
while(n != NULL)
printf(" %d", n->num), n = n->next;

printf("n");
}

Read more Similar C Programs
Data Structures

Learn C Programming

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 1-23.

K and R C, Solution to Exercise 1-23: C program that removes the all comments from a C Program. K and R C Program. Exercises provides the solution to all the exercises in the C Programming Language, second addition, by Brian W.Keringhan and Dennis M.Ritchie(Prentice Hall,1988). You can learn and solve K&R C Programs Exercise. 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"
void rcomment(int c);
void in_comment(void);
void echo_quote(int c);
main();
{
int c ,d;
while((c= getchar()) !=EOF)
rcomment(c);
return 0;
}
void rcomment(int c) {
int d;
if(c == '/')
if((d= getchar))=='*')
in_comment();
else if(d== '/') {
putchar(c);
rcomment(d);
} else {
putchar(c);
putchar(d);
}
else if(c=='' || c=='"')
echo_quote(c);

}
void in_comment() {
int c, d;
c = getchar();
d = getchar();
while (c!= '*' || d!!='/') {
c = d;
d = getchar();
}
}
void echo_quote(int c) {
int d;
putchar(c);
while ((d = getchar()) != c) {
putchar(d);
if (d == '\')
putchar(getchar());

}
putchar(d);
}
Read more Similar C Programs
C Basic

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 generate Graph using grphics.h

C Program to Generate the graph sheet using the grphics.h library. To use graphics.h, we have to install the drivers in to the the system by using the initgraph() function. Here  we derive the graph of input sizes verses time taken for input sizes. x axis represents inputs(0,10000,20000,—-), y axis rep time(0,0.05,0.1,0.15—). 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"
#include "graphics.h"
void main() {
int gd = DETECT, gm;
int y = 0, x = 10, m[20], k[20], n, a[20], i;
float b[20];
initgraph(&gd, &gm, "c:\tc\bgi");
printf("nntGenerating the Graphsnn");
printf("nEnter the no. of inputst");
scanf("%d", &n);
printf("nEnter the input sizes and corresponding time takenn");
for (i = 0; i < n; i++) {
printf("nEnter input sizet");
scanf("%d", &a[i]);
printf("nEnter time takent");
scanf("%f", &b[i]);
}
cleardevice();
//represents y axis
line(10, 0, 10, 400);
//represents x axis
line(10, 400, 600, 400);
while (y <= 400) {
line(0, y, 10, y);
y = y + 20;
}
while (x <= 600) {
line(x, 400, x, 410);
x = x + 20;
}
outtextxy(20, 440, "1unit=20 pixels , origin is (10,400)");
outtextxy(
20,
450,
"x axis represents inputs(0,10000,20000,----), yaxis rep time(0,0.05,0.1,0.15---)");
setcolor(5);
for (i = 0; i < n; i++) {
k[i] = (a[i] * 0.002);
m[i] = (400 - (b[i] * 400));
putpixel(k[i], m[i], 11);
}
for (i = 0; i < n - 1; i++)
line(k[i], m[i], k[i + 1], m[i + 1]);
getch();
}
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

C Program to implement Linear regression algorithm.

Linear Regression  is the predicting the value of one scalar variable(y) using the explanatory another variable(x). Linear regression  is represented by the equation Y = a + bX, where X is the explanatory variable and Y is the scalar variable. The slope of the line is b, and a is the intercept. For linear list square modeling, Linear regression is very helpful. 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"
#include "math.h"
#include "string.h"

float mean(float *a, int n);
void deviation(float *a, float mean, int n, float *d, float *S);

void main() {
float a[20], b[20], dx[20], dy[20];
float sy = 0, sx = 0, mean_x = 0, mean_y = 0, sum_xy = 0;
float corr_coff = 0, reg_coff_xy = 0, reg_coff_yx = 0;
char type_coff[7];
int n = 0, i = 0;

clrscr();

printf("Enter the value of n: ");
scanf("%d", &n);
printf("Enter the values of x and y:n");
for (i = 0; i < n; i++)
scanf("%f%f", &a[i], &b[i]);
mean_x = mean(a, n);
mean_y = mean(b, n);
deviation(a, mean_x, n, dx, &sx);
deviation(b, mean_y, n, dy, &sy);

for (i = 0; i < n; i++)
sum_xy = sum_xy + dx[i] * dy[i];
corr_coff = sum_xy / (n * sx * sy);
printf("Enter the type of regression coefficient as 'x on y' or 'y on x': ");
fflush(stdin);
gets(type_coff);

if (strcmp(type_coff, "x on y") == 1) {
reg_coff_xy = corr_coff * (sx / sy);
printf("nThe value of linear regression coefficient is %f",
reg_coff_xy);
} else if (strcmp(type_coff, "y on x") == 1) {
reg_coff_yx = corr_coff * (sy / sx);
printf("nThe value of linear regression coefficient is %f",
reg_coff_yx);
} else
printf("nEnter the correct type of regression coefficient.");
getch();
}

float mean(float *a, int n) {
float sum = 0, i = 0;
for (i = 0; i < n; i++)
sum = sum + a[i];
sum = sum / n;
return (sum);
}

void deviation(float *a, float mean, int n, float *d, float *s) {
float sum = 0, t = 0;
int i = 0;
for (i = 0; i < n; i++) {
d[i] = a[i] - mean;
t = d[i] * d[i];
sum = sum + t;
}
sum = sum / n;
*s = sqrt(sum);
}

Read more Similar C Programs
Learn C Programming

Simple C Programs

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 Exercise 1-18

K and R C, Solution to Exercise 1-18:
Remove trailing blanks and tabs from each line of input, and to delete entirely blank lines. K and R C Programs Exercises provides the solution to all the exercises in the C Programming Language, second addition, by Brian W.Keringhan and Dennis M.Ritchie(Prentice Hall,1988). You can learn and solve K&R C Programs Exercise. 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 MAXLINE 1000

int get_lines(char line[], int maxline);
int char remove(char str[]);

main()
{

char line[MAXINLINE];/* current input line */

while((get_lines(line,MAXINLINE))>0)
if(remove(line)> 0)
printf("%s", line);
return 0;

}
/*
remove trailing blanks and tabs from character string str
*/
int remove(char str[])
{
int i=0;
while(str[i] !='n')
++i;

--i;

while(i>=0 && (str[i]==' ' || str[i]=='t'))
--i;
if(i>=0)
{
++i;
s[i]='n';
++i;
s[i]='';

}
return i;
}
/* getline: read a line into str, return length */
int get_lines(char str[], int line)
{
int c, i, j;

for(i = 0, j = 0; (c = getchar())!=EOF && c != 'n'; ++i)
{
if(i < line - 1)
{
str[j++] = c;
}
}
if(c == 'n')
{
if(i <= line - 1)
{
str[j++] = c;
}
++i;
}
str[j] = '';
return i;
}

Read more Similar C Programs
C Basic

K and R C Programs Exercise

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 Program Exercise 1-12

K and R C Programs Exercises provides the solution to all the exercises in the C Programming Language, second addition, by Brian W.Keringhan and Dennis M.Ritchie(Prentice Hall,1988). You can learn and solve K&R C Programs Exercise. C Program that prints its input one word per line.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>
int main(void)
{
int ch;
int flag;

flag = 0;
while((ch = getchar()) != EOF)
{
if(ch == ' ' || ch == 't' || ch == 'n')
{
if(flag == 0)
{
flag = 1;
putchar('n');
}

}
else
{
flag = 0;
putchar(ch);
}
}
return 0;
}
Read more Similar C Programs
C Basic

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!

(c) www.c-program-example.com

K&R C Program Exercise 1-09

K and R C Programs Exercises provides the solution to all the exercises in the C Programming Language, second addition, by Brian W.Keringhan and Dennis M.Ritchie(Prentice Hall,1988). You can learn and solve K&R C Programs Exercise. C Program to replacing each string of one or more blanks by a single blank.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>

int main(void)
{
int ch;
while ((ch = getchar()) != EOF) {
if (ch == ' ') {
putchar(ch);
while((ch = getchar()) == ' ' && ch != EOF)
;
}
if (ch == EOF)
break;

putchar(ch);
}
return 0;
}
Read more Similar C Programs
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!

(c) www.c-program-example.com

K&R C Program Exercise 1-08

K and R C Programs Exercises provides the solution to all the exercises in the C Programming Language, second addition, by Brian W.Keringhan and Dennis M.Ritchie(Prentice Hall,1988). You can learn and solve K&R C Programs Exercise. C Program to count blanks, tabs, and newlines. 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>

int main(void)
{
int bl, tb, nl;
int c;
int done = 0;
int lastc = 0;

bl = 0;
tb = 0;
nl = 0;

while(done == 0)
{
c = getchar();

if(c == ' ')
++bl;

if(c == 't')
++tb;

if(c == 'n')
++nl;

if(c == EOF)
{
if(lastc != 'n')
{
++nl;
}
done = 1;
}
lastc = c;
}

printf("Blanks: %dnTabs: %dnLines: %dn", bl, tb, nl);
return 0;
}
Read more Similar C Programs
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!

(c) www.c-program-example.com