Showing posts with label array. Show all posts
Showing posts with label array. Show all posts

Thursday, September 27, 2012

C program to delete the specified integer from the Array.

#include <stdio.h>

void main()
{
int vectx[10];
int i, n, pos, element, found = 0;

printf("Enter how many elements\n");
scanf("%d", &n);

printf("Enter the elements\n");
for(i=0; i<n; i++)
{
scanf("%d", &vectx[i]);
}

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

printf("Enter the element to be deleted\n");
scanf("%d",&element);

for(i=0; i<n; i++)
{
if ( vectx[i] == element)
{
found = 1;
pos = i;
break;
}
}

if (found == 1)
{
for(i=pos; i< n-1; i++)
{
vectx[i] = vectx[i+1];
}

printf("The resultant vector is \n");
for(i=0; i<n-1; i++)
{
printf("%d\n",vectx[i]);
}
}
else
printf("Element %d is not found in the vector\n", element);

}

RECURSION determine if two arrays are identic


#include <stdio.h>
int kontrollo(int a[], int b[], int n)
{
if(n==1)
{
    if(a[0]=b[0])
    return 1;
    else return 0;
}
else    if(a[n-1]==b[n-1])
    return (kontrollo(a, b, n-1));
else
    return 0;

}
int main()
{
    int a[10]={2, 25, 75};
    int  b[10]={2, 25, 75};
    int u=3;
    int x;
    x=(kontrollo(a, b, u));
    printf("%d", x);



    return 0;

}

Saturday, September 22, 2012

C Program To Sort An Array Using Bubble Sort


#include<stdio.h>
int main() {
 int a[50], n, i, j, temp = 0;

 printf("Enter how many numbers you want:\n");
 scanf("%d", &n);
 printf("Enter the %d elements:\n", n);
 for (i = 0; i < n; i++) {
  scanf("%d", &a[i]);
 }
 printf("\n\t\tThe given array is:\n");
 for (i = 0; i < n; i++) {
  printf("\n\t\t%d", a[i]);
 }
 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("\n\n\n\t\tThe sorted array using Buble sort is:\n");
 for (i = 0; i < n; i++) {
  printf("\n\t\t%d", a[i]);
 }
 return 0;
}