Monday, September 17, 2012

Code #54:C Program To Delete an Element From an Array

| |
0 comments

#include <stdio.h>

main()
{
   int array[100], position, c, n;

   printf("Enter number of elements in array\n");
   scanf("%d", &n);

   printf("Enter %d elements\n", n);

   for ( c = 0 ; c < n ; c++ )
      scanf("%d", &array[c]);

   printf("Enter the location where you wish to delete element\n");
   scanf("%d", &position);

   if ( position >= n+1 )
      printf("Deletion not possible.\n");
   else
   {
      for ( c = position - 1 ; c < n - 1 ; c++ )
         array[c] = array[c+1];

      printf("Resultant array is\n");

      for( c = 0 ; c < n - 1 ; c++ )
         printf("%d\n", array[c]);
   }

   return 0;
}
Read More

Code # 53:C Program To Insert An Element Into Array

| |
0 comments

#include <stdio.h>

int main()
{
   int array[100], position, c, n, value;

   printf("Enter number of elements in array\n");
   scanf("%d", &n);

   printf("Enter %d elements\n", n);

   for (c = 0; c < n; c++)
      scanf("%d", &array[c]);

   printf("Enter the location where you wish to insert an element\n");
   scanf("%d", &position);

   printf("Enter the value to insert\n");
   scanf("%d", &value);

   for (c = n - 1; c >= position - 1; c--)
      array[c+1] = array[c];

   array[position-1] = value;

   printf("Resultant array is\n");

   for (c = 0; c <= n; c++)
      printf("%d\n", array[c]);

   return 0;
}
Read More

Code# 52:C Program To Reverse Array Using Pointers

| |
0 comments

#include<stdio.h>
#include<stdlib.h>

void reverse_array(int*, int);

main()
{
   int n, c, *pointer;

   scanf("%d",&n);

   pointer = (int*)malloc(sizeof(int)*n);

   if( pointer == NULL )
      exit(EXIT_FAILURE);

   for ( c = 0 ; c < n ; c++ )
      scanf("%d",(pointer+c));

   reverse_array(pointer, n);

   printf("Original array on reversal is\n");

   for ( c = 0 ; c < n ; c++ )
      printf("%d\n",*(pointer+c));

   free(pointer);

   return 0;
}

void reverse_array(int *pointer, int n)
{
   int *s, c, d;

   s = (int*)malloc(sizeof(int)*n);

   if( s == NULL )
      exit(EXIT_FAILURE);

   for ( c = n - 1, d = 0 ; c >= 0 ; c--, d++ )
      *(s+d) = *(pointer+c);

   for ( c = 0 ; c < n ; c++ )
      *(pointer+c) = *(s+c);

   free(s);
}
Read More

Code# 51:C Program To Reverse Array By Swapping

| |
0 comments

#include <stdio.h>
 
int main() {
  int array[100], n, c, t, end;
 
  scanf("%d", &n);
  end = n - 1;
 
  for (c = 0; c < n; c++) {
    scanf("%d", &array[c]);
  }
 
  for (c = 0; c < n/2; c++) {
    t          = array[c];
    array[c]   = array[end];
    array[end] = t;
 
    end--;
  }
 
  printf("Reversed array elements are:\n");
 
  for (c = 0; c < n; c++) {
    printf("%d\n", array[c]);
  }
 
  return 0;
}
Read More

Code# 50:C Program To Reverse Array

| |
0 comments

c program to reverse an array :- This program reverses the array elements. For example if a is an array of integers with three elements
such that
a[0] = 1
a[1] = 2
a[2] = 3
then on reversing the array will be
a[0] = 3
a[1] = 2
a[0] = 1

#include <stdio.h>

int main()
{
   int n, c, d, a[100], b[100];

   printf("Enter the number of elements in array\n");
   scanf("%d", &n);

   printf("Enter the array elements\n");

   for (c = 0; c < n ; c++)
      scanf("%d", &a[c]);

   /*
    * Copying elements into array b starting from end of array a
    */

   for (c = n - 1, d = 0; c >= 0; c--, d++)
      b[d] = a[c];

   /*
    * Copying reversed array into original.
    * Here we are modifying original array, this is optional.
    */

   for (c = 0; c < n; c++)
      a[c] = b[c];

   printf("Reverse array is\n");

   for (c = 0; c < n; c++)
      printf("%d\n", a[c]);

   return 0;
}
Output:
Enter the number of elements in array
4
Enter the array elements
34
56
7
5
Reverse array is
5
7
56
34

Read More
Powered by Blogger.