Thursday, July 17, 2008

input real numbers and find the mean, variance and standard deviation

/* Write a C program to input real numbers and find the *
* mean, variance and standard deviation */

#include stdio.h
#include conio.h
#include math.h
#define MAXSIZE 10

void main()
{
float x[MAXSIZE];
int i, n;
float avrg, var, SD, sum=0, sum1=0;

clrscr();

printf("Enter the value of N\n");
scanf("%d", &n);

printf("Enter %d real numbers\n",n);
for(i=0; i

This program is straight forward calculation of mean, variance and standard deviation from its formulae directly. It uses mathematical functions for simplicity. Hence reduce the execution length.


/* Write a C program to sort N numbers in ascending order *
* using Bubble sort and print both the given and the sorted *
* array with suitable headings */

#include stdio.h
#include conio.h
#define MAXSIZE 10

void main()
{
int array[MAXSIZE];
int i, j, N, temp;

clrscr();

printf("Enter the value of N\n");
scanf("%d",&N);

printf("Enter the elements one by one\n");
for(i=0; i< j="0;"> array[j+1])
{
temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
}
}
printf("Sorted array is...\n");
for(i=0; i

Bubble sort is a simple sorting algorithm. It works by repeatedly stepping through the list to be sorted, comparing two items at a time and swapping them if they are in the wrong order. The pass through the list is repeated until no swaps are needed, which indicates that the list is sorted. The algorithm gets its name from the way smaller elements "bubble" to the top of the list. Because it only uses comparisons to operate on elements, it is a comparison sort.


The logic contains inportant concepts to note. Lets see at the nested for loop where all the execution is done. The outer for(i Based) keeps track of the number of times the loop is looked after arranging successive elements. From the above, by the algorithm of Bubble sort, the program has go through the loop N number of times to sort N numbers. This is not an advisible algorithm for large data set as the program exexution time becomes extremly high.


The inner For (j based) keeps track of the element that is swapped now. It keeps increasing untill the Limit of N-i-1 is reached. The inside of this For loop is a simple swap program. So our necessary job is done


Tuesday, July 15, 2008

linear search

/* Write a C program to input N numbers (integers or reals) *
* and store them in an array. Conduct a linear search for a *
* given key number and report success or failure in the form *
* of a suitable message */

#include stdio.h
#include conio.h

void main()
{
int array[10];
int i, N, keynum, found=0;

clrscr();

printf("Enter the value of N\n");
scanf("%d",&N);

printf("Enter the elements one by one\n");
for(i=0; i {
scanf("%d",&array[i]);
}
printf("Input array is\n");
for(i=0; i {
printf("%d\n",array[i]);
}
printf("Enter the element to be searched\n");
scanf("%d", &keynum);

/* Linear search begins */
for ( i=0; i < N ; i++)
{
if( keynum == array[i] )
{
found = 1;
break;
}
}
if ( found == 1)
printf("SUCCESSFUL SEARCH\n");
else
printf("Search is FAILED\n");

} /* End of main */
/*------------------------------------
Output
RUN 1
Enter the value of N
5
Enter the elements one by one
23
12
56
43
89
Input array is
23
12
56
43
89
Enter the element to be searched
56
SUCCESSFUL SEARCH

RUN 2
Enter the value of N
3
Enter the elements one by one
456
213
879
Input array is
456
213
879
Entee the element to be searched
1000
Search is FAILED
--------------------------------------*/


The following program is also based on array concept. We input the data into an array and then print it using a for loop. Then we take the input of necessary number to search. Now we rotate a loop to compare out KEYNUM with each element of an array. If there is a match then the variable FOUND is assiged an identifier. We use a break statemennt so that as soon as the number is assigned we jump out of loop and print the value. We use this as we didnt use an else statement. So if we are not using a BREAK statement, then the program runs into an infinite loop. The output is printed using an IF statement using the identifier we assing above.


program to read N integers (zero, +ve and -ve) into an Array

/* Write a C program to read N integers (zero, +ve and -ve) *
* into an array A and to *
* a) Find the sum of negative numbers *
* b) Find the sum of positive numbers and *
* c) Find the average of all input numbers *
* Output the various results computed with proper headings */

#include stdio.h
#include conio.h
#define MAXSIZE 10

void main()
{
int array[MAXSIZE];
int i, N, negsum=0, posum=0;
float total=0.0, averg;

clrscr();

printf ("Enter the value of N\n");
scanf("%d", &N);

printf("Enter %d numbers (-ve, +ve and zero)\n", N);
for(i=0; i< N ; i++)
{
scanf("%d",&array[i]);
fflush(stdin);
}

printf("Input array elements\n");
for(i=0; i< N ; i++)
{
printf("%+3d\n",array[i]);
}

/* Summing begins */
for(i=0; i< N ; i++)
{

if(array[i] < 0)
{
negsum = negsum + array[i];
}
else if(array[i] > 0)
{
posum = posum + array[i];
}
else if( array[i] == 0)
{
;
}
total = total + array[i] ;
}

averg = total / N;
printf("\nSum of all negative numbers = %d\n",negsum);
printf("Sum of all positive numbers = %d\n", posum);
printf("\nAverage of all input numbers = %.2f\n", averg);

} /*End of main()*/
/*-------------------------------------
Output
Enter the value of N
5
Enter 5 numbers (-ve, +ve and zero)
5
-3
0
-7
6
Input array elements
+5
-3
+0
-7
+6

Sum of all negative numbers = -10
Sum of all positive numbers = 11

Average of all input numbers = 0.20
--------------------------------------*/


This program is a intro for Array elements. It is a good example for array inputs and execution. The beauty of C language lies in Arrays and pointers. So i stress the readers to concentrate more on programs from here as all are in the order of increasing complexity


The first thing to note is the usage of Symbolic constant MAXSIZE to specify the size of array. If you want to know more about array size and different ways of its specification, Use the search box.


In this program we are initializing all the values to a preset value. By doing so we are manually flusing all the variables. There is a chance that these variables might be assigned some value at some time of execution of other programs. Those will be cleared.


Now we are using a for loop to input the array elements. Here the point to note is the usage of fflush() function, which clears all the input buffers after taking the input. It is advisory that we keep clearing both input and output buffers periodically to aviod any variable over lap.


Then we are reading each element of array and adding using a for loop. We use an else.. if to seperate positive and negative numbers and then add.

find the number of integers divisible by 5 in a given range

/* Write a C program to find the number of integers divisible by 5 *
* between the given range N1 and N2, where N1 < N2 and are integers.*
* Also find the sum of all these integer numbers that divisible by 5*
* and output the computed results */

#include stdio.h
#include conio.h

void main()
{
int i, N1, N2, count = 0, sum = 0;

clrscr();

printf ("Enter the value of N1 and N2\n");
scanf ("%d %d", &N1, &N2);

/*Count the number and compute their sum*/
printf ("Integers divisible by 5 are\n");

for (i = N1; i < N2; i++)
{
if (i%5 == 0)
{
printf("%3d,", i);
count++;
sum = sum + i;
}
}

printf ("\nNumber of integers divisible by 5 between %d and %d = %d\n",
N1,N2,count);
printf ("Sum of all integers that are divisible by 5 = %d\n", sum);

} /* End of main()*/
/*-----------------------------
Output
Enter the value of N1 and N2
2
27
Integers divisible by 5 are
5, 10, 15, 20, 25,
Number of integers divisible by 5 between 2 and 27 = 5
Sum of all integers that are divisible by 5 = 75
------------------------------------------------------*/



<

This program diplayes all the numbers divisible 5 between a given range. So we set a variable to given first number of range(N1 here) and increment it till end of range(N2 here) and diving every numder coming inside it by 5 with the help of FOR loop. When there is a remainder '0' we will print that number.

To generate and print prime numbers in a given range

/* Write a C program to generate and print prime numbers in a given *
* range. Also print the number of prime numbers */

#include stdio.h
#include conio.h
#include stdlib.h
#include math.h

void main()
{
int M, N, i, j, flag, temp, count = 0;

clrscr();

printf("Enter the value of M and N\n");
scanf("%d %d", &M,&N);

if(N < 2)
{
printf("There are no primes upto %d\n", N);
exit(0);
}
printf("Prime numbers are\n");
temp = M;

if ( M % 2 == 0)
{
M++;
}
for (i=M; i<=N; i=i+2)
{
flag = 0;

for (j=2; j<=i/2; j++)
{
if( (i%j) == 0)
{
flag = 1;
break;
}
}
if(flag == 0)
{
printf("%d\n",i);
count++;
}
}
printf("Number of primes between %d and %d = %d\n",temp,N,count);
}
/*---------------------------------
Output
Enter the value of M and N
15 45
Prime numbers are
17
19
23
29
31
37
41
43
Number of primes between 15 and 45 = 8
-------------------------------------------*/

This program is an extention of the previous one. We add another extra loop to increment the number isself untill it reaches the limit set of the priniting of prime numbers. This uses the netsed FOR loop concept for execution.

check whether a given number is prime or not

/* Write a C program to check whether a given number is prime or not *
* and output the given number with suitable message */

#include stdio.h
#include stdlib.h
#include conio.h

void main()
{
int num, j, flag;

clrscr();

printf("Enter a number\n");
scanf("%d", &num);

if ( num <= 1)
{
printf("%d is not a prime numbers\n", num);
exit(1);
}

flag = 0;

for ( j=2; j<= num/2; j++)
{
if( ( num % j ) == 0)
{
flag = 1;
break;
}
}

if(flag == 0)
printf("%d is a prime number\n",num);
else
printf("%d is not a prime number\n", num);
}
/*------------------------
Output
RUN 1
Enter a number
34
34 is not a prime number

RUN 2
Enter a number
29
29 is a prime number
-----------------------------*/


This program checks whether the given number is prime or not. We know that a prime number is not divisible by any natural number other than 1. So if we think a little bit it is the same that if any number till the half of given number is not diving it, It is meant to be a prime. Because if the one factor is greater than half of the given number, then the other factor is less 2, which is not a natural number. Hence it can be termed as prime.


Hence we initialize a variable to 2 and increment it till it reaches half of the given number. If at any time we get a remainder zero, we declare it as not prime, else its accepted as a prime number