K & R C Programs Exercise 1-24.

K and R C, Solution to Exercise 1-24:
C program for rudimentary syntax errors like unbalanced parentheses, brackets,quotes,and braces. This program is hard if we do it in full generality. 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>

#define MAXLINE 1000 /* max input line size */
char line[MAXLINE]; /*current input line*/

int getline(void); /* taken from the KnR book. */

int main() {
int len = 0;
int t = 0;
int brace = 0, parenthesis = 0, brack = 0;
int s_quote = 1, d_quote = 1;

while ((len = getline()) > 0) {
t = 0;
while (t < len) {
if (line[t] == '{') {
brace++;
}
if (line[t] == '}') {
brace--;
}
if (line[t] == '[') {
brack++;
}
if (line[t] == ']') {
brack--;
}
if (line[t] == '(') {
parenthesis++;
}
if (line[t] == ')') {
parenthesis--;
}
if (line[t] == ''') {
s_quote *= -1;
}
if (line[t] == '"') {
d_quote *= -1;
}
t++;
}
}
if (d_quote != 1)
printf("Mismatching double quote markn");
if (s_quote != 1)
printf("Mismatching single quote markn");
if (parenthesis != 0)
printf("Mismatching parenthesisn");
if (brace != 0)
printf("Mismatching bracesn");
if (brack != 0)
printf("Mismatching bracket markn");
if (brack == 0 && brace == 0 && parenthesis == 0 && s_quote == 1 && d_quote
== 1)
printf("Syntax appears to be correct.n");
return 0;
}

/* getline function */
int getline(void) {
int c, i;
extern char line[];

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

}

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 for Simple ASC order Priority QUEUE Implementation

Data structures using C,Priority QUEUE is a abstract data type in which the objects are inserted with respect to certain priority. In this program, we created the simple ascending order priority queue, here items are inserted in ascending order. 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
***********************************************************/
#define SIZE 5 /* Size of Queue */
int PQ[SIZE],f=0,r=-1; /* Global declarations */

PQinsert(int elem)
{
int i; /* Function for Insert operation */
if( Qfull()) printf("nn Overflow!!!!nn");
else
{
i=r;
++r;
while(PQ[i] >= elem && i >= 0) /* Find location for new elem */
{
PQ[i+1]=PQ[i];
i--;
}
PQ[i+1]=elem;
}
}

int PQdelete()
{ /* Function for Delete operation */
int elem;
if(Qempty()){ printf("nnUnderflow!!!!nn");
return(-1); }
else
{
elem=PQ[f];
f=f+1;
return(elem);
}
}

int Qfull()
{ /* Function to Check Queue Full */
if(r==SIZE-1) return 1;
return 0;
}

int Qempty()
{ /* Function to Check Queue Empty */
if(f > r) return 1;
return 0;
}

display()
{ /* Function to display status of Queue */
int i;
if(Qempty()) printf(" n Empty Queuen");
else
{
printf("Front->");
for(i=f;i<=r;i++)
printf("%d ",PQ[i]);
printf("<-Rear");
}
}

main()
{ /* Main Program */
int opn,elem;
do
{
clrscr();
printf("n ### Priority Queue Operations(ASC order) ### nn");
printf("n Press 1-Insert, 2-Delete,3-Display,4-Exitn");
printf("n Your option ? ");
scanf("%d",&opn);
switch(opn)
{
case 1: printf("nnRead the element to be Inserted ?");
scanf("%d",&elem);
PQinsert(elem); break;
case 2: elem=PQdelete();
if( elem != -1)
printf("nnDeleted Element is %d n",elem);
break;
case 3: printf("nnStatus of Queuenn");
display(); break;
case 4: printf("nn Terminating nn"); break;
default: printf("nnInvalid Option !!! Try Again !! nn");
break;
}
printf("nnnn Press a Key to Continue . . . ");
getch();
}while(opn != 4);
}


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

C Program to implementing two Stacks on Single Array.

Data structures using C, Stack is a data structure in which the objects are arranged in a non linear order. In stack, elements are aded or deleted from only one end, i.e. top of the stack. In this program, we implement the two stacks using the single array. 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
***********************************************************/
#define SIZE 10 /* Size of Stack */
int s[SIZE],top[3]={0,-1,SIZE};
/* Global declarations */
push(int elem,int stno)
{
int pos; /* Function for PUSH operation */
if( Sfull()) printf("nn Overflow!!!!nn");
else
{
if(stno==1) pos= ++top[stno];
else pos=--top[stno];
s[pos]=elem;
}
}


int pop(int stno)
{ /* Function for POP operation */
int elem,pos;
if(Sempty(stno)){ printf("nnUnderflow!!!!nn");
return(-1); }
else
{
pos=top[stno];
elem=s[pos];
if(stno == 1)top[stno]--;
else
top[stno]++;
return(elem);
}
}

int Sfull()
{ /* Function to Check Stack Full */
if(top[1] == top[2]-1) return 1;
return 0;
}

int Sempty(stno)
{
/* Function to Check Stack Empty */
switch(stno)
{
case 1: if(top[1] == -1) return 1; else return 0;
case 2: if(top[2] == SIZE) return 1;else return 0;
}
}

display(int stno)
{ /* Function to display status of Stack */
int i;
if(Sempty(stno)) printf(" n Empty Stackn");
else
{
if(stno == 1)
{
for(i=0;i<=top[stno];i++)
printf("%dn",s[i]);
printf("^Top");
}
else
{
for(i=SIZE-1;i>=top[stno];i--)
printf("%dn",s[i]);
printf("^Top");
}
}
}

main()
{ /* Main Program */
int opn,elem,stno;
do
{
clrscr();
printf("n ### Stack Operations ### nn");
printf("n Stack Number (1,2): ");
scanf("%d",&stno);
printf("n Press 1-Push, 2-Pop,3-Display,4-Exitn");
printf("n Your option ? ");
scanf("%d",&opn);

switch(opn)
{
case 1: printf("nnRead the element to be pushed ?");
scanf("%d",&elem);
push(elem,stno); break;
case 2: elem=pop(stno);
if( elem != -1)
printf("nnPopped Element is %d n",elem);
break;
case 3: printf("nnStatus of Stack %d nn",stno);
display(stno); break;
case 4: printf("nn Terminating nn"); break;
default: printf("nnInvalid Option !!! Try Again !! nn");
break;
}
printf("nnnn Press a Key to Continue . . . ");
getch();
}while(opn != 4);
}


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-22.

K and R C, Solution to Exercise 1-22:
 C program that folds the very long lines of input into two or more shorter 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 MAXSIZE 1000

char line[MAXSIZE];

int get_line(void);


int
main()
{
int i,strlen;
int location, spaceholder;
const int foldlength=70;

while (( strlen = get_line()) > 0 )
{
if( strlen < foldlength )
{
}
else
{

i = 0;
location = 0;
while(i<strlen)
{
if(line[i] == ' ')
spaceholder = i;

if(location==foldlength)
{
line[spaceholder] = 'n';
location = 0;
}
location++;
i++;
}
}
printf ( "%s", line);
}
return 0;
}


int get_line(void)
{
int c, i;
extern char line[];

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

}
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

K & R C Programs Exercise 1-20.

K and R C, Solution to Exercise 1-20:
C Program to replace the tabs by proper number of blanks. 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 TABSIZE 6 /*Tab increment size*/
main()
{
int c, no_blanks,pos;
no_blanks = 0; /*number of blamks required*/
pos = 1; /* position of the character*/
while((c=getchar()) != EOF){
if(c == 't'){
no_blanks = TABSIZE-(pos-1)%TABSIZE;
while(no_blanks>0){

putchar(' ');
++pos;
--no_blanks;
}
else if(c == 'n'){
putchar(c);
pos = 1;
} else {
putchar(c);
++pos;
}
}
}
}

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 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

C Program to implement Radix Sort.

Radix sort is an algorithm. Radix Sort sorts the elements by processing its individual digits. Radix sort processing the digits either by Least Significant Digit(LSD) method or by Most Significant Digit(MSD) method. 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 MAX 100
#define SHOWPASS

void print(int *a, int n) {
int i;
for (i = 0; i < n; i++)
printf("%dt", a[i]);
}

void radix_sort(int *a, int n) {
int i, b[MAX], m = 0, exp = 1;
for (i = 0; i < n; i++) {
if (a[i] > m)
m = a[i];
}

while (m / exp > 0) {
int box[10] = { 0 };
for (i = 0; i < n; i++)
box[a[i] / exp % 10]++;
for (i = 1; i < 10; i++)
box[i] += box[i - 1];
for (i = n - 1; i >= 0; i--)
b[--box[a[i] / exp % 10]] = a[i];
for (i = 0; i < n; i++)
a[i] = b[i];
exp *= 10;

#ifdef SHOWPASS
printf("nnPASS : ");
print(a, n);
#endif
}
}

int main() {
int arr[MAX];
int i, num;

printf("nEnter total elements (num < %d) : ", MAX);
scanf("%d", &num);

printf("nEnter %d Elements : ", num);
for (i = 0; i < num; i++)
scanf("%d", &arr[i]);

printf("nARRAY : ");
print(&arr[0], num);

radix_sort(&arr[0], num);

printf("nnSORTED : ");
print(&arr[0], num);

return 0;
}

Read more Similar C Programs
Array In C

Sorting Techniques

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-17

K and R solution Exercise 1-17,to print all input lines that are longer than 80 characters. 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 MAXINLINE 1000 /* maximum line sizes defined*/
#define LONGLINESIZE 80 /* longer line defined as 80 */
int get_lines(char line[], int maxline);
/*print lines longer than LONGLINESIZE */
main()
{
int len; /*current line length */
char line[MAXINLINE];/* current input line */
while((len=get_lines(line,MAXINLINE))>0)
if(len> LONGLINE)
printf("%s", line);
return 0;

}

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

for(i = 0, j = 0; (c = getchar())!=EOF && c != 'n'; ++i)
{
if(i < line - 1)
{
s[j++] = c;
}
}
if(c == 'n')
{
if(i <= line - 1)
{
s[j++] = c;
}
++i;
}
s[j] = '';
return i;
}
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

K&R C Program Exercise 1-16

K and R solutions to Revise the the main routine of the longest-line program so it will correctly print the length of arbitrarily long input lines, and as much as possible of the text. 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 MAXINLINE 1000 /* maximum input line size */

int getline(char line[], int MAXINLINE);
void copy(char to[], char from[]);

/* print longest input line */
int main(void)
{
int len; /* current line length */
int max; /* maximum length seen so far */
char line[MAXINLINE]; /* current input line */
char longest[MAXINLINE]; /* longest line saved here */

max = 0;

while((len = getline(line, MAXINLINE)) > 0)
{
printf("%d: %s", len, line);

if(len > max)
{
max = len;
copy(longest, line);
}
}
if(max > 0)
{
printf("Longest LINE IN THE PROGRAM is %d characters:n%s", max, longest);
}
printf("n");
return 0;
}

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

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

/* copy: copy 'from' into 'to'; assume 'to' is big enough */
void copy(char to[], char from[])
{
int k;

k = 0;
while((to[k] = from[k]) != '')
{
++k;
}
}


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