#include<stdio.h>
#include<stdlib.h>
int *a,n;
void bubblesort();
void swap_w(int,int);
main()
{
int i,j;
printf("Enter the number of elements ");
scanf("%d",&n);
a=(int *)calloc(n,sizeof(int));
printf("Enter the elements\n");
for(i=0;i<n;i++)
scanf("%d",a+i);
bubblesort(0,n-1);
printf("The sorted sequence is\n");
for(i=0;i<n;i++)
printf("%d ",a[i]);
printf("\n");
return;
}
void bubblesort()
{
int i,j;
for(i=0;i<n-1;i++)
for(j=n-1;j>=i+1;j--)
if(a[j]<a[j-1])
swap_w(j,j-1);
}
void swap_w(int i,int j)
{
int temp;
temp=a[i];
a[i]=a[j];
a[j]=temp;
return;
}
Wednesday, 12 March 2014
Thursday, 9 January 2014
Written by Kousik :
C provides some fundamental DATA types. They are int, float, char, double. They are very useful and
important in C language. But they have a limiting fact. The fact is that a
variable of these types can store only one value at a time. With variables of
those types we cannot handle a large volume of data of same type at a time. So
we need a powerful data type with which we can solve this problem. C supports
such a data type called ARRAYS.
Definition: An array is a well defined collection of elements of same data
type. In others words One can say that an array is grouping of some elements
that are of same data type. Some applications of arrays in C are given below:
§ To store age of some boys.
§ To store marks of subjects.
§ To store daily rainfall data.
And so on.
Declaration
of ARRAYS: ARRAY in C can be declared by following format
int
array[50]; , float temperature[7];
See carefully that an array is declared by
defining the data type first then the name and then the size of array.
Now a question should be come to reader’s
mind and that is how can we take input of many elements at a time by using
array? Answer of this question is very easy. This problem can be solved by
using for loop for taking input by using following syntax.
For
(i=0; i<n; i++)
Scanf
(“%d”, &arr[i]);
Where n is the size of the array and arr is
the name of array.
Now here a example is given to understand
ARRAYS and the way of using an ARRAY in C.
#include<stdio.h>
int main()
{
int a[25],i,n,sum=0,mean;
printf("Enter the number of array elements\n");
scanf("%d",&n);
printf("Enter the value of array elements\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<n;i++)
{
Printf(“%d ”, a[i]);
}
return 0;
}
There are two kinds of array those can be
used in C language. One is ONE
DIMENSIONAL and the other one is TWO
DIMENSIONAL.
In this section we will discuss about only
one dimensional array.
INITIALIZATION
OF ONE DIMENSIONAL ARRAY: After declaring an array
its elements can be initialized. Otherwise elements of array are of ‘garbage’.
The following syntax can be used to initialize an array.
Data
type variablename[size]={values};
Example
: int student[3]={1,2,3};
Above example is called compile time
initialization. Another type of initialization can be used for initialize an
array. That is Run time initialization. For example take the following segment.
for(j=0;j<50;j++)
{
If(j<20)
A[i]=0.0;
Else
A[i]=1.0;
}
We
can also use a scanf function to initialize an array as we discuss earlier with
the answer of the question how to take input value of elements of an array.
The following example illustrates a sorting
program in ascending order as an important application of arrays in C science.
/* a program to sort an array */
# include<stdio.h>
int main()
{
int a[25],i,j,n,temp,item;
char c;
printf("Enter the number of array elements\n");
scanf("%d",&n);
printf("Enter the value of array elements\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
/* sorting of array */
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
printf("After sorting \n");
for(i=0;i<n;i++)
{
printf("%d ",a[i]);
}
}
Sunday, 5 January 2014
Here in this post we represent two basic C programming examples. Those example are only for the beginners.
v Check whether a number is palindrome or not through C program.
v Check whether a number is prime or not through C program.
Now the answers are given below:
Answer of no 1:
A number is said to be palindrome if the reverse of that number is same as the original number. Coding is given below…
/* C program to check whether a number is palindrome or not */#include<stdio.h>#include<stdlib.h>int main(){int n,t,rev=0;printf("Give the value of of the number\n");scanf("%d",&n);t=n;while(t!=0){rev=rev*10;rev=rev+t%10;t=t/10;}if(rev!=n)printf("the entered number is not a palindrome\n");elseprintf("the entered number is palindrome number\n\n");}
Answer of no 2:
A number is said to be prime if that number is only divisible by 1 and the number itself. Coding is given below…
/* C program to check whether a number is prime or not*/
#include<stdio.h>
int main()
{
int a,c=0,i,num;
printf("Give the value of num\n");
scanf("%d",&num);
for(i=1;i<=num;i++)
{
a=num%i;
if(a==0)
{
c=c+1;
}
}
if(c==2)
printf("num is prime number");
else
printf("num is not a prime number");
}
Subscribe to:
Posts (Atom)