Posts Tagged array
Variable length argument implemented using array.
This Example demonstrates how we can pass variable argument using array, but this approach is old. Now a days we use Varargs feature included in J2SE 5.
class VariableArr{
public void Test(int[] arr)
{ System.out.println("No. of args "+arr.length+"contents ");
for(int i : arr)
{ System.out.print( i+" ");
}
System.out.println();
}
public static void main(String a[])
{ VariableArr va= nw VariableArr();
int a[]= {20};
int b[]={2,4,6};
int c[]= {};
va.test(a);
va.test(b);
va.test(c);
}
}
Write a program to find total number of +ve, -ve, odd and even numbes in an array.
Posted by admin in C Examples on November 5th, 2009
#include<stdio.h>
numbers(int *arr, int n)
{ int i,a=0,b=0,c=0,d=0;
while(n>0)
{ if(*arr < 0) a++;
if(*arr > 0) b++;
if((*arr % 2)==0) c++;
if((*arr % 2)!=0) d++;
n--;
arr++;
}
printf("\nNumber of +ve numbers:: %d\n",b);
printf("\nNumber of -ve numbers:: %d\n",a);
printf("\nNumber of even numbers:: %d\n",c);
printf("\nNumber of odd numbers:: %d\n",d);
}
main()
{
int n, a[100],i=0;
printf("\nEnter the dimension of array::");
scanf("%d",&n);
printf("\nEnter the elments of array:: ");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
numbers(a,n);
}
Write a program that search a number in an array and also display number of times it appears.
Posted by admin in C Examples on November 5th, 2009
#include<stdio.h>
search(int * arr, int n, int m)
{
int l=0;
while(n>0)
{ if(m== *arr)
l++;
n--;
arr++;
}
return l;
}
main()
{ int n, m,i=0,a[100],l;
printf("\nEnter the dimension of array:: ");
scanf("%d",&n);
printf("\nEnter the elments of array:: ");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("\nEnte the number you want to search:: ");
scanf("%d",&m);
l=search(a,n,m);
printf("\nArray:: ");
for(i=0;i<n;i++)
printf("%d ",a[i]);
if(l!=0)
printf("\nThe number is found and %d times it appears in array\n", l);
else printf("\n number not found!!!\n");
}
Program to delete an element of array.
import java.io.*;
class test
{
public static void main(String[] asd)throws IOException
{
BufferedReader obj=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter the number of values you wamma enter.");
String num=obj.readLine();
int n=Integer.parseInt(num);
int arr[]= new int[n];
System.out.println("Enter the values of the array not more then "+n+".");
for(int i=0;i<n;i++)
{
num=obj.readLine();
arr[i]=Integer.parseInt(num);
}
System.out.print("Enter the position which you wanna delete.");
num=obj.readLine();
int x=Integer.parseInt(num);
System.out.println("ARRAY-->");
for(int i=0;i<n;i++)
System.out.print(arr[i]+" ");
for(int i=x;i<n;)
{
arr[i-1]=arr[i++];
}
System.out.println("Modified Array is::");
for(int i=0;i<n-1;i++)
System.out.print(arr[i]+" ");
}
}
Program to read and write an array.
import java.io.*;
class test
{ public static void main(String[] asd)throws IOException
{
BufferedReader obj= new BufferedReader(new InputStreamReader(System.in));
String num;
System.out.print("Enter the number of value you want to enter in ARRAY:: ");
num=obj.readLine();
int n=Integer.parseInt(num);
int arr[]=new int[n];
System.out.println("Enter the values of array..not more then "+n);
for(int i=0;i<n;i++)
{ num=obj.readLine();
//int x=Integer.parseInt(num);
arr[i]=Integer.parseInt(num);
}
System.out.print("ARRAY--> ");
for(int i=0;i<n;i++)
System.out.print(arr[i]+" ");
}
}
Programs that implement Queue (its operations) using Arrays.
Posted by admin in C Examples on August 7th, 2009
#include<stdio.h>
#include<alloc.h>
#include<conio.h>
#define size 10
#define true 1
#define false 0
struct q_arr
{
int f,r;
int num;
int a[size];
};
void init(struct q_arr* queue);
int e_que(struct q_arr* queue);
int f_que(struct q_arr* queue);
int add_ele(struct q_arr* queue,int);
int rem_ele(struct q_arr* queue);
void display_ele(struct q_arr* queue);
/*main function*/
void main()
{
int ele,k;
int ch;
struct q_arr *queue = (struct q_arr*)malloc(sizeof(struct q_arr));
init(queue);
while(1)
{
clrscr();
printf("\n\n****IMPLEMENTATION OF QUEUE USING ARRAYS****\n");
printf("============================================");
printf("\n\t\tMENU\n");
printf("============================================");
printf("\n\t[1] To insert an element");
printf("\n\t[2] To remove an element");
printf("\n\t[3] To display all the elements");
printf("\n\t[4] Exit");
printf("\n\n\t Enter your choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1:
{
printf("\nElement to be inserted:");
scanf("%d",&ele);
add_ele(queue,ele);
break;
}
case 2:
{
if(!e_que(queue))
{
k=rem_ele(queue);
printf("\n%d element is removed\n",k);
getch();
}
else
{
printf("\tQueue is Empty. No element can be removed.");
getch();
}
break;
}
case 3:
{
display_ele(queue);
getch();
break;
}
case 4:
exit(0);
default:
printf("\tInvalid Choice.");
getch();
break;
}
}
}
/*end main*/
void init(struct q_arr* queue)
{
queue->f = 0;
queue->r = -1;
queue->num = 0;
}
/* Function to check is the queue is empty*/
int e_que(struct q_arr* queue)
{
if(queue->num==0)
return true;
return false;
}
/* Function to check if the queue is full*/
int f_que(struct q_arr* queue)
{
if(queue->num == size)
return true;
return false;
}
/* Function to add an element to the queue*/
int add_ele(struct q_arr* queue,int j)
{
if(f_que(queue))
return false;
if(queue->r == size - 1)
queue->r = -1;
queue->a[++queue->r] = j;
queue->num++;
return true;
}
/* Function to remove an element of the queue*/
int rem_ele(struct q_arr* queue)
{
int j;
if(e_que(queue))
return -9999;
j = queue->a[queue->f++];
if(queue->f == size)
queue->f = 0;
queue->num--;
return j;
}
/* Function to display the queue*/
void display_ele(struct q_arr* queue)
{
int j;
if(e_que(queue))
{
printf("Queue is Empty. No records to display.");
return;
}
printf("\nElements present in the Queue are: ");
for(j=queue->f;j<=queue->r;j++)
printf("%d\t",queue->a[j]);
printf("\n");
}
Recursive function to compute the maximum element of an array.
Posted by admin in C Examples on June 26th, 2009
#include
int max(int a[], int n)
{
if(n==1) return a[0];
else return a[n-1]>max(a,n-1)?a[n-1]:max(a,n-1);
}
int main()
{
int i, a[50], n;
printf("Enter the number of elements in the array: ");
scanf("%d",&n);
printf("Enter the elements of the array: ");
for(i=0;i
printf("Maximum element: %d\n",max(a,n));
return 0;
}
Recursvie function that computes the sum of the elemnts of an array.
Posted by admin in C Examples on June 26th, 2009
#include
Recursvie function that computes the sum of the elemnts of an array.
Posted by admin in C Examples on June 25th, 2009
#include <stdio.h>
int sum(int a[], int n)
{
if(n==1) return a[0];
return a[n-1]+sum(a,n-1);
}
int main()
{
int a[50], n, i;
printf("Enter the number of elements in the array: ");
scanf("%d",&n);
printf("Enter the elements of the array: ");
for(i=0;i
printf("Sum of the elements: %d\n",sum(a,n));
return 0;
}


Recent Comments