Monday, December 27, 2010

sorting array in descending order

/****************************
* Umang B Bhatt *
* bhatt.umang7@gmail.com *
*****************************/

/**
* program for scannign n numbers and sorting them in descending order
*/

#include<stdio.h>
void main()
{


int
n = 0 , i =0 , j = 0;

int
no[100];
int
temp; // for swapping the number
printf("How many numbers you want to scan ? ");

scanf("%d",&n);
if
(n < 0 || n>100)
{


printf("Invalid number. It must be brtween 1 to 100");
}

else

{

// logic for scanning the numbers
for(i = 0 ; i < n ;i++)
{


printf("Enter element %d: ", i);
scanf("%d",&no[i]);
}



// the logic for sorting the numbers
for(i = 0 ; i < n ;i++)
{

for
(j = 0 ; j < n ; j++ )
{


if
(no[i] > no[j])
{

// if the number at index i is greater than the number at index j then swap the numbers
temp = no[i];

no[i] = no[j];
no[j] = temp;
}
}
}


for
(i = 0 ; i < n ;i++)
{

printf("\nnumber at index %d is %d ", i , no[i]);
}
}


printf("\n");
}

scan n numbers and display them (using array)

/****************************
* Umang B Bhatt *
* bhatt.umang7@gmail.com *
*****************************/

/**
* program for scannign n numbers and displaying them
*/

#include<stdio.h>
void main()
{


int
n = 0 , i =0 ;

int
no[100];
printf("How many numbers you want to scan ? ");
scanf("%d",&n);

if
(n < 0 || n>100)
{

printf("Invalid number. It must be brtween 1 to 100");
}


else

{

// logic for scanning the numbers
for(i = 0 ; i < n ;i++)
{


printf("Enter element %d: ", i);
scanf("%d",&no[i]);
}



// the logic for displaying the numbers
for(i = 0 ; i < n ;i++)
{


printf("\nnumber at index %d is %d ", i , no[i]);
}
}

printf("\n");
}

pattern

A program that will generate above pattern:








#include<stdio.h>
// this program works well when the value of rows is less than 20
void main()
{


int
n = 5;
int
i = 0 , j = 0 , k = 0;

int
temp = 0 ;
printf("Enter no of rows: ");

scanf("%d",&n);
temp = n;
for
(i = 0; i<n ;i++)
{


// logic for space
for(k = 0 ; k < temp ; k++ )
{


printf(" ");
}

temp--;
// for printing the actuak number
for(j = 0; j <= i ; j++)
{


printf("%3d ", fun(i,j,n));
}

printf("\n");
}
}


int
fun(int a , int b, int n )
{


int
sum = 0 ;
int
i = 0 , j = 0;

for
(i = n-1 ; i >= 0 ; i--)
{


for
(j = 0 ; j <= i ; j++)
{


sum++;
if
( i==a && b==j )
{


return
sum;
}
}
}

return
sum;
}

pattern

Question: Write a program that generates output shown in left image.


The code is:











#include<stdio.h>
// this program works well when the value of rows is less than 20
void main()
{


int
n = 5;
int
i = 0 , j = 0;

printf("Enter no of rows: ");
scanf("%d",&n);
for
(i = 0; i<n ;i++)
{


for
(j = i; j >=0;j--)
{


printf("%3d ", fun(i,j));
}

printf("\n");
}
}


int
fun(int a , int b )
{

int
sum = 0 ;

int
i = 0 , j = 0;

for
(i = 0 ; i <= a ; i++)
{


for
(j = 0 ; j <= i ; j++)
{


sum++;
if
( i==a && b==j )
{


return
sum;
}
}
}

return
sum;
}

Wednesday, November 3, 2010

1234 -> 4123 -> 3412number pattern

/*

Write a program that generated output something like this (here 1234 is input).
Here, keep all the numbers as integers only. Do not convert them to string or character:
1234
2341
3412
4123

note: here 0 will not be inputted at ay place
*/


import java.util.*;

public class
a
{


static
int length(int n)
{


int
len = 0;
while
(n > 0)
{


len++;
n /= 10;
}

return
len;
}


static
int generate(int n)
{

int
temp = (int) Math.pow(10, length(n) - 1);

int
temp2 = n / (int) Math.pow(10, length(n) - 1);

temp = n % temp;
temp = temp * 10;

temp = temp + temp2;
return
temp;
}


public static
void main(String a[])
{

int
n;

Scanner s = new Scanner(System.in);
n = s.nextInt();

int
temp = n;
/* n-1 if you do not want to include the original digit and
n if you want to include original digit
*/

for
(int i = 0; i < length(n) - 1; i++)
{


temp = generate(temp);
System.out.println(temp);
}

}
}

Tuesday, August 31, 2010

SSTF : in os subject

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


void
main()
{

clrscr();
int
n;
int
arr[100];

int
curr_pos = 0;
printf("Enter no of numbers; ");
scanf("%d", &n);

for
(int i = 0; i < n; i + +)
{


printf("Enter no ", i + 1, ": ");
scanf("%d", &arr[i]);
}


printf("\nEnter current position: ");
scanf("%d", &curr_pos);
int
temp_n = n;

int
total = 0;
for
(int j = 0; j < temp_n; j++)
{


int
diff = 0;
int
k = 0;

int
temp_k = 0;
// if(n>1){
for (k = 0; k < n; k++)
{


int
temp = abs(curr_pos - arr[k]);

if
(k == 0)
{

diff = abs(curr_pos - arr[0]);
}


if
(temp <= diff)
{

temp_k = k;

diff = temp;
// printf("\ntemp = %d",temp);
}
}

k = temp_k;

printf("\n%6d - %6d = %6d", curr_pos, arr[temp_k], abs(arr[temp_k] - curr_pos));

total = total + abs(arr[temp_k] - curr_pos);

curr_pos = arr[temp_k];
for
(int l = temp_k; l < n; l++)
{


arr[l] = arr[l + 1];
}

n--;
}


printf("\n\n\Total is : %d", total);

getch();
}

FCFS : in OS subject

#include
#include
#include
void main()
{
clrscr();
int n;
int arr[100];
int curr_pos=0;
printf("Enter no of numbers; ");
scanf("%d",&n);
//int i;
//i=0;
for(int i=0;i<n;i++)
{
printf("Enter no " , i+1 , ": " );
scanf("%d",&arr[i]);
}
/* for(int j=0;j<n;j++)
{
printf("\n%d\n",arr[j]);
}*/
printf("\nEnter current position: ");
scanf("%d",&curr_pos);
int temp=curr_pos;
int total = 0;
for(int k=0;k<n;k++)
{
printf("%6d to %6d = %6d ms\n", temp, arr[k], abs(temp-arr[k]));
total = total + abs(temp-arr[k]);
temp = arr[k];
}
printf("\nTotal is : %d " , total);
getch();
}