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

Saturday, February 14, 2015

C program to find largest and second largest no from a 2D array

C++ program to find largest and second largest no from a 2D array

#include<iostream.h>
#include<conio.h>

void main()
{
clrscr();
int a[5][5],big1,big2,n,m,i,j;
cout<<"Enter no of rows and columns(max 5):";
cin>>m>>n;
cout<<"Enter the array:
";

for(i=0;i<m;i++)
for(j=0;j<n;++j)
cin>>a[i][j];

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

big2=a[0][0];
for(i=0;i<m;++i)
for(j=0;j<n;++j)
{
if(a[i][j]>big2&&a[i][j]<big1)
big2=a[i][j];
}

cout<<"

Largest number:"<<big1;

cout<<"
Second largest number:"<<big2;

getch();
}

Read more »

C Program for Implementation of Circular Queue Using Array

#include<stdio.h>
#define MAX 10

typedef struct Q
{
int R,F;
int data[MAX];
}Q;

void initialise(Q *P);
int empty(Q *P);
int full(Q *P);
void enqueue(Q *P,int x);
int dequeue(Q *P);
void print(Q *P);

void main()
{
Q q;
int op,x;
initialise(&q);

do
  {
printf("

1)Insert
2)Delete
3)Print
4)Quit");

printf("
Enter Your Choice:");

scanf("%d",&op);
switch(op)
  {
case 1: printf("
Enter a value:");

scanf("%d",&x);
if(!full(&q))
enqueue(&q,x);
else
printf("
Queue is full !!!!");

break;
case 2: if(!empty(&q))
  {
x=dequeue(&q);
printf("Deleted Data=%d",x);
  }
else
printf("
Queue is empty !!!!");

break;
case 3: print(&q);break;
  }
      }while(op!=4);
}

void initialise(Q *P)
{
P->R=-1;
P->F=-1;
}

int empty(Q *P)
{
if(P->R==-1)
return(1);
return(0);
}

int full(Q *P)
{
if((P->R+1)%MAX==P->F)
return(1);
return(0);
}

void enqueue(Q *P,int x)
{
if(P->R==-1)
{
P->R=P->F=0;
P->data[P->R]=x;
}
else
{
P->R=(P->R+1)%MAX;
P->data[P->R]=x;
}
}

int dequeue(Q *P)
{
int x;
x=P->data[P->F];
if(P->R==P->F)
{
P->R=-1;
P->F=-1;
}
else
P->F=(P->F+1)%MAX;
return(x);
}

void print(Q *P)
{
int i;
if(!empty(P))
{
printf("
");

for(i=P->F;i!=P->R;i=(i+1)%MAX)
printf("%d ",P->data[i]);
printf("%d ",P->data[i]);
}
}

C Program for Implementation of Circular Queue Using Array
Read more »

Tuesday, February 10, 2015

C program to sort an Array by using Bubble sort

C program to sort an Array by using Bubble sort

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

void main()
{
int a[50],n,i,j,temp;
clrscr();
printf("Enter the size of array: ");
scanf("%d",&n);
printf("Enter the array elements: ");

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

for(i=1;i<n;++i)
for(j=0;j<(n-i);++j)
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}

printf("Array after sorting: ");
for(i=0;i<n;++i)
printf("%d ",a[i]);
getch();
}
Read more »

Wednesday, February 4, 2015

How to find the Greatest common divisor of an integer array in java

In this post Im going to illustrate very simple algorithm to find the Greatest Common Divisor (GCD)  of set of integers (an array) using java.

  • In this algorithm we first find the smallest integer in the int array. 
  • Then we get the modulus for each element in the numbers of the array by dividing smallest integer and add all those modulus together. 
  • After processing each element in the array we check if the total of modulus is equals to 0.  If it is equal to 0 then the GCD is  current smallest value if that total is not equals 0 we deduct 1 from the previous smallest value and do the same computation. 
  • The algorithm will continue with  the same operations until total of modulus becomes 0 or until smallest integer is 2.
  • If there is any GCD, algorithm will return it or otherwise it will return -1.

See the following implementation of the algorithm.


public class GCD {

public static int findGcd(int... numbers) {

//Find the smallest integer in the number list

int smallest = numbers[0];

for (int i = 1; i < numbers.length; i++) {
if (numbers[i] < smallest) {
smallest = numbers[i];
}
}

//Find the GCD
while (smallest > 1) {

int counter = 0;
int modTot = 0;

while (counter < numbers.length) {

modTot += numbers[counter] % smallest;
counter++;

}

if (modTot == 0) {
//Return the gcd if any
return smallest;
}

//System.out.print(" "+ smallest);
smallest--;

}
//return -1 if there is no gcd
return -1;
}

public static void main(String[] x) {
System.out.println("The GCD of 15 18 42 108 : "+GCD.findGcd(new int[]{15, 18, 42,108}));
}
}

Here is the output for 15, 18, 42 and 108


Here is the output for 3,7 and 12



Read more »