Showing posts with label 1-D Array Programs. Show all posts
Showing posts with label 1-D Array Programs. Show all posts

Friday, 10 January 2014

C Program to Read Array Elements

Program :
#include<stdio.h>
#include<conio.h>
void main()
{
 int i,a[50],sum,n;
 printf("n Enter no of elements :");
 scanf("%d",&n);

 /* Reading values into Array */

 printf("n Enter the values :");
 for(i=0;i < n;i++)
 scanf("%d",&a[i]);

 /* computation of total */

 sum=0;
 for(i=0;i < n;i++)
 sum=sum+a[i];

 /* printing of all elements of array */

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

 /* printing of total */

 printf("n sum=%d",sum);

getch();
}

C Program to Print Array Elements

#include<stdio.h>
#include<conio.h>
void main()
{
 int i,a[50],sum,n;
 printf("n Enter no of elements :");
 scanf("%d",&n);

 /* Reading values into Array */

 printf("n Enter the values :");
 for(i=0;i < n;i++)
 scanf("%d",&a[i]);

 /* computation of total */

 sum=0;
 for(i=0;i < n;i++)
 sum=sum+a[i];

 /* printing of all elements of array */

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

 /* printing of total */

 printf("n sum=%d",sum);

getch();
}

C Program to Delete an element from the specified location from Array

Program :
//  n - no.of elements in array
//  i - for traversing the array
//  j - location of the element to be deleted
//  a - Array 

#include<stdio.h>
#include<conio.h>
void main()
{
 int a[30],n,i,j;

 printf("n Enter no of elements :");
 scanf("%d",&n);

 /* read n elements in an array  */

 printf("n Enter %d elements :",n);
 for(i=0;i 〈 n;i++)
 scanf("%d",&a[i]);

 /* read the location of the element to be deleted */
 printf("n location of the element to be deleted :");
 scanf("%d",&j);

 /* loop for the deletion  */
 while(j 〈 n)
 {
  a[j-1]=a[j];
  j++;
 }
 n--;    /* no of elements reduced by 1 */

 /* loop for printing  */
 for(i=0;i 〈 n;i++)
 printf("n %d",a[i]);
 getch();
}

C Program to Insert an element in an Array

#include<stdio.h>

int main()
{
 int arr[30],element,num,i,location;
 printf("n Enter no of elements :");
 scanf("%d",&num);

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

 printf("n Enter the element to be inserted :");
 scanf("%d",&element);
 printf("n Enter the location");
 scanf("%d",&location);

 /* create space at the specified location */
 for(i = num ;i >= location ; i--)
		arr[i] = arr[i-1];

 num++;
 arr[location-1] = element;

 /* Print out the Result of Insertion */
 for(i = 0 ;i < num ;i++)
        printf("n %d",arr[i]);

return(0);
}

Output of the Program :

Enter no of elements : 5
 1 2 3 4 5
 Enter the element to be inserted : 6
 Enter the location : 2
 1 6 2 3 4 5

C Program to Copy all elements of an array into Another array

//Title : Copy Array Elements from one array to another
//a - Source Array ( elements will be copied from this array)
//b - Destination array ( elements copied into this array)
//---------------------------------------------------------------------
#include<stdio.h>
#include<conio.h>
void main()
{
int a[30],b[30],i,n;

// Element of an array 'a' will be copied into array 'b'

printf("n Enter no of elements :");
scanf("%d",&n);

/* Accepting values into Array a*/
printf("n Enter the values :");
for(i = 0 ; i < n ; i++)
scanf("%d",&a[i]);

/* Copying data from array 'a' to array 'b */

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

/* printing of all elements of array */

printf("the copied array is :");
for(i=0;i < n;i++)
printf("nb[%d]=%d",i,b[i]);
getch();
}

C Program to Search an element in Array

#include<stdio.h>
#include<conio.h>
void main()
{
 int a[30],x,n,i;
/*
 a - for storing of data
 x - element to be searched
 n - no of elements in the array
 i - scanning of the array
*/
 printf("nEnter no of elements :");
scanf("%d",&n);
/* Reading values into Array */

printf("nEnter the values :");
for(i=0;i < n;i++)
scanf("%d",&a[i]);

/* read the element to be searched */

printf("nEnter the elements to be searched");
scanf("%d",&x);

/* search the element */

i=0; /* search starts from the zeroth location */
while(i < n && x!=a[i])
i++;

/* search until the element is not found i.e. x!=a[i]
search until the element could still be found i.e. i 〈 n */

if(i < n) /* Element is found */
printf("found at the location =%d",i+1);
else
printf("n not found");

getch();
}

C Program to Merge Two arrays in C Programming

//-------------------------------------------------------
//  Title : Merging of the Two arrays
//  a  - First Array
//  b  - Second Array
//  c  - Resultant Array after merging
//  i  - Subscript variable for array a
//  j  - Subscript variable for array b
//  k  - Subscript variable for array c
//  n1 - Total number of elements in array1
//  n2 - Total number of elements in array2
//-------------------------------------------------------

#include<stdio.h>
#include<conio.h>
void main()
{
 int a[30],b[30],c[30],i,j,k,n1,n2;

 printf("n Enter no of elements in 1'st array :");
 scanf("%d",&n1);

 for(i=0;i 〈 n1;i++)
     scanf("%d",&a[i]);

 printf("n Enter no of elements in 2'nd array :");
 scanf("%d",&n2);

 for(i=0;i 〈 n2;i++)
  scanf("%d",&b[i]);

 i=0;j=0;k=0;  /* merging starts */

 while(i 〈 n1 && j 〈 n2)
 {
  if(a[i]= b[j])
   {
   c[k]=a[i];
   i++;k++;
   }
  else
   {
   c[k]=b[j];
   k++;j++;
   }
 }

 /* Some elements in array 'a' are still remaining
    where as the array 'b' is exhausted */

 while(i 〈 n1)
  {
  c[k]=a[i];
  i++;k++;
  }

 /* some elements in array b are still remaining
    whereas the array 'a' is exhausted */

 while(j 〈 n2)
  {
  c[k]=b[j];
  k++;j++;
  }

 /* Displaying elements of array 'c' */

        printf("nMerged array is :");

        for(i=0;i 〈 n1+n2;i++)
           printf("n %d",c[i]);
getch();

}

C Program to Reversing an Array Elements in C Programming

#include<stdio.h>
#include<conio.h>

void main()
{
 int a[30],i,j,n,temp;

 printf("n Enter no of elements :");
 scanf("%d",&n);

 /* read n elements in an array  */
 for(i=0 ; i < n ; i++)
  scanf("%d",&a[i]);

 j = i-1;   // j will Point to last Element
 i = 0;     // i will be pointing to first element 

 while(i < j)
 {
 temp = a[i];
 a[i] = a[j];
 a[j] = temp;
 i++;             // increment i and decrement j 
 j--;
 }
  /* Print out the Result of Insertion */

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

   getch();
}

C Program to Find Largest Element in Array in C Programming

Program : Find Largest Element in Array in C Programming
// ------------------------------------------
// Title : Find out Largest Number From Array
// Largsest - It Stores the Largest number
// ------------------------------------------
#include<stdio.h>
#include<conio.h>
void main()
  {
  int a[30],i,n,largest;

  printf("n Enter no of elements :");
  scanf("%d",&n);

  /* read n elements in an array  */
  for(i=0 ; i < n ; i++)
     scanf("%d",&a[i]);

  largest = a[0];

  for(i = 0;i<n;i++)
  {
  if(a[i] > largest )
      largest = a[i];
  }

  /* Print out the Result */
     printf("nLargest Element : %d",largest);
     getch();
}

C Program to Find Smallest Element in Array in C Programming

Program : Find Smallest Element in Array in C Programming
// ------------------------------------------------------
// Title : Find out smallest Number From Array
// smallest - It Stores the smallest number
// ------------------------------------------------------

#include<stdio.h>
#include<conio.h>
void main()
{
 int a[30],i,n,smallest;

 printf("n Enter no of elements :");
 scanf("%d",&n);

 /* read n elements in an array  */
 for(i=0 ; i < n ; i++)
   scanf("%d",&a[i]);

  smallest = a[0];

  for(i = 0 ;i < n ; i++)
  {
  if ( a[i] < smallest )
   smallest = a[i];
  }

  /* Print out the Result */

 printf("nSmallest Element : %d",smallest);

   getch();
}

C Program to Calculate Addition of All Elements in Array

Program : Addition of All Elements of the Array
#include<stdio.h>
#include<conio.h>

void main()
{
 int i,a[50],sum,n;
 printf("nEnter no of elements :");
 scanf("%d",&n);

 /* Reading values into Array */

 printf("nEnter the values :");
 for(i=0;i < n;i++)
 scanf("%d",&a[i]);

 /* computation of total */

 sum=0;
 for(i=0;i < n;i++)
 sum=sum+a[i];

 /* printing of all elements of array */

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

 /* printing of total */

 printf("nSum=%d",sum);
}

C Program to Delete duplicate elements from an array

Program : To delete duplicate elements in an array
#include<stdio.h>
#include<conio.h>
main()
{
int a[20],i,j,k,n;
clrscr();

printf("nEnter array size : ");
scanf("%d",&n);

printf("nAccept Numbers : ",n);
for(i=0;i<n;i++)
 scanf("%d",&a[i]);

clrscr();

printf("nOriginal array is : ");
for(i=0;i<n;i++)
 printf(" %d",a[i]);

printf("nUpdated array is  : ");
for(i=0;i<n;i++)
{
   for(j=i+1;j<n;)
   {
      if(a[j]==a[i])
      {
         for(k=j;k<n;k++)
             a[k]=a[k+1];
          n--;
      }
      else
         j++;
   }
}

for(i=0;i<n;i++)
    printf("%d ",a[i]);
getch();
}

Output :
Enter array size : 5
Accept Numbers : 1 2 2 3 4
Original array is : 1 2 2 3 4
Updated array is  : 1 2 3 4

C Program to Read integers into an array and Reversing them using Pointers

Write a C program using pointers to read in an array of integers and print its elements in reverse order.

#include<stdio.h>
#include<conio.h>
#define MAX 30

void main()
{
int size,i,arr[MAX];
int *ptr;
clrscr();

ptr=&arr[0];

printf("Enter the size of array : ");
scanf("%d",&size);

printf("nEnter %d integers into array:n",size);
   for(i=0;i<size;i++)
   {
   scanf("%d",ptr);
   ptr++;
   }

ptr=&arr[size-1];

printf("nElements of array in reverse order are:n");

 for(i=size-1;i>=0;i--)
   {
   printf("nElement%d is %d :",i,*ptr);
   ptr--;
   }

getch();
}
Output :
Enter the size of array : 5
Enter 5 integers into array : 11 22 33 44 55
Elements of array in reverse order are :
Element 4 is : 55
Element 4 is : 44
Element 4 is : 33
Element 4 is : 22
Element 4 is : 11

Program to read integers into an array and reversing them using pointers Explanation :

  1. We have declared one pointer variable and one array.
int size,i,arr[MAX];
int *ptr;
  1. Address of first element of array is stored inside pointer variable.
ptr=&arr[0];
  1. Accept Size of an Array.
printf("Enter the size of array : ");
scanf("%d",&size);
  1. Now we have accepted element one by one using for loop and scanf statement .
printf("nEnter %d integers into array:n",size);
   for(i=0;i<size;i++)
   {
   scanf("%d",ptr);
   ptr++;
   }
  1. Increment pointer variable so that it will then point to next element of array.
  2. After accepting all elements store address of last element inside pointer variable.
ptr=&arr[size-1];
  1. Again using reverse for loop and printf statement print an array.
for(i=size-1;i>=0;i--)
   {
   printf("nElement%d is %d :",i,*ptr);
   ptr--;
   }

C Program to Implement Stack Operations Using Array

C Program to implement Stack Operations Using Stack

Program for implementing a stack using arrays.It involves
various operations such as push,pop,stack empty,stack full and
display.
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define size 5
/* stack structure*/
struct stack  {
          int s[size];
          int top;
           }st;
//-------------------------------------
int stfull()
{
 if(st.top>=size-1)
    return 1;
 else
    return 0;
}
//-------------------------------------
void push(int item)
{
 st.top++;
 st.s[st.top] =item;
}
//-------------------------------------
int stempty()
{
 if(st.top==-1)
    return 1;
 else
    return 0;
}
//-------------------------------------
int pop()
 {
 int item;
 item=st.s[st.top];
 st.top--;
 return(item);
 }
//-------------------------------------
void display()
{
 int i;
 if(stempty())
    printf("n Stack Is Empty!");
 else
  {
  for(i=st.top;i>=0;i--)
     printf("n%d",st.s[i]);
  }
}
//-------------------------------------
void main(void)
{
int item,choice;
char ans;
st.top=-1;
clrscr();
printf("ntt Implementation Of Stack");
do
{
 printf("n Main Menu");
 printf("n1.Pushn2.Popn3.Displayn4.exit");
 printf("n Enter Your Choice");
 scanf("%d",&choice);
 switch(choice)
 {
  case 1:
        printf("n Enter The item to be pushed");
        scanf("%d",&item);
        if(stfull())
            printf("n Stack is Full!");
        else
            push(item);
        break;
  case 2:
        if(stempty())
            printf("n Empty stack!Underflow !!");
        else
          {
          item=pop();
          printf("n The popped element is %d",item);
          }
        break;
  case 3:
        display();
        break;
  case 4:
        exit(0);
 }
 printf("n Do You want To Continue?");
 ans=getche();
}while(ans =='Y'||ans =='y');
getch();
}

Explanation of the C Porgram : Stack Using Array

Step 1 : Declare One Stack Structure

#define size 5

struct stack
{
     int s[size];
     int top;
}st;
  1. We have created ‘stack’ structure.
  2. We have array of elements having size ‘size’
  3. To keep track of Topmost element we have declared top as structure member.

Step 2 : Push/Pop Operation

While pushing remember one thing in mind that we are incrementing the top and then adding element. and while removing or poping the element, we are firstly removing the element and then decrementing the top.
void push(int item) {
    st.top++;
    st.s[st.top] =item;
}