Thursday, September 27, 2012

C program to delete the specified integer from the Array.

#include <stdio.h>

void main()
{
int vectx[10];
int i, n, pos, element, found = 0;

printf("Enter how many elements\n");
scanf("%d", &n);

printf("Enter the elements\n");
for(i=0; i<n; i++)
{
scanf("%d", &vectx[i]);
}

printf("Input array elements are\n");
for(i=0; i<n; i++)
{
printf("%d\n", vectx[i]);
}

printf("Enter the element to be deleted\n");
scanf("%d",&element);

for(i=0; i<n; i++)
{
if ( vectx[i] == element)
{
found = 1;
pos = i;
break;
}
}

if (found == 1)
{
for(i=pos; i< n-1; i++)
{
vectx[i] = vectx[i+1];
}

printf("The resultant vector is \n");
for(i=0; i<n-1; i++)
{
printf("%d\n",vectx[i]);
}
}
else
printf("Element %d is not found in the vector\n", element);

}

RECURSION determine if two arrays are identic


#include <stdio.h>
int kontrollo(int a[], int b[], int n)
{
if(n==1)
{
    if(a[0]=b[0])
    return 1;
    else return 0;
}
else    if(a[n-1]==b[n-1])
    return (kontrollo(a, b, n-1));
else
    return 0;

}
int main()
{
    int a[10]={2, 25, 75};
    int  b[10]={2, 25, 75};
    int u=3;
    int x;
    x=(kontrollo(a, b, u));
    printf("%d", x);



    return 0;

}

Sunday, September 23, 2012

C PROGRAMM TO FIND MAXIMUM SUM OF COLUMN


#include <stdio.h>
int main()
{
    int v[20][20], i, j, m, n, max;
    int a=1, b[20];
    printf("GIVE NUMBER OF  ROWS AND COLUMNS");
    scanf("%d %d",&m, &n);
    printf("elementet e matr");
    for(i=0;i<m;i++)
    for(j=0;j<n;j++)
    scanf("%d", &v[i][j]);
    for(j=0;j<n;j++)
    {
        for(i=0;i<m;i++)
        a=a*v[i][j];
        b[j]=a;
    }
    max=b[0];
    for(j=1;j<n;j++)
    {
        if(b[j]>max)
        max=b[j];
    }
    printf("MAX IS %d" ,max);

}

Recursion how to Find the Shortest distance of the flight


/* Hill-climbing */
#include <stdio.h>
#include <string.h>
#define MAX 100
/* structure of the flight database */
struct FL {
char from[20];
char to[20];
int distance;
char skip; /* used for backtracking */
};
struct FL flight[MAX]; /* array of db structures */
int f_pos = 0; /* number of entries in flight db */
int find_pos = 0; /* index for searching flight db */
int tos = 0; /* top of stack */
struct stack {
char from[20];
char to[20];
int dist;
} ;
struct stack bt_stack[MAX]; /* backtrack stack */
void setup(void), route(char *to);
void assert_flight(char *from, char *to, int dist);
void push(char *from, char *to, int dist);
void pop(char *from, char *to, int *dist);
void isflight(char *from, char *to);
int find(char *from, char *anywhere);
int match(char *from, char *to);

int main(void)
{
char from[20], to[20];
setup();
printf("From? ");
gets(from);
printf("To? ");
gets(to);
isflight(from,to);
route(to);
return 0;
}
/* Initialize the flight database. */
void setup(void)
{
assert_flight("New York", "Chicago", 1000);
assert_flight("Chicago", "Denver", 1000);
assert_flight("New York", "Toronto", 800);
assert_flight("New York", "Denver", 1900);
assert_flight("Toronto", "Calgary", 1500);
assert_flight("Toronto", "Los Angeles", 1800);
assert_flight("Toronto", "Chicago", 500);
assert_flight("Denver", "Urbana", 1000);
assert_flight("Denver", "Houston", 1500);
assert_flight("Houston", "Los Angeles", 1500);
assert_flight("Denver", "Los Angeles", 1000);
}
/* Put facts into the database. */
void assert_flight(char *from, char *to, int dist)
{
if(f_pos < MAX) {
strcpy(flight[f_pos].from, from);
strcpy(flight[f_pos].to, to);
flight[f_pos].distance = dist;
flight[f_pos].skip = 0;
f_pos++;
}
else printf("Flight database full.\n");
}
/* Show the route and the total distance. */
void route(char *to)
{
int dist, t;
dist = 0;
t = 0;
while(t < tos) {
printf("%s to ", bt_stack[t].from);
dist += bt_stack[t].dist;
t++;
}
printf("%s\n", to);
printf("Distance is %d.\n", dist);
}
/* If flight between from and to, then return
the distance of flight; otherwise, return 0. */
int match(char *from, char *to)
{
register int t;
for(t=f_pos-1; t > -1; t--)
if(!strcmp(flight[t].from, from) &&
!strcmp(flight[t].to, to)) return flight[t].distance;
return 0; /* not found */
}
/* Given from, find the farthest away "anywhere". */
int find(char *from, char *anywhere)
{
int pos, dist;
pos=dist = 0;
find_pos = 0;
while(find_pos < f_pos) {
if(!strcmp(flight[find_pos].from, from) &&
!flight[find_pos].skip) {
if(flight[find_pos].distance>dist) {
pos = find_pos;
dist = flight[find_pos].distance;
}
}
find_pos++;
}
if(pos) {
strcpy(anywhere, flight[pos].to);
flight[pos].skip = 1;
return flight[pos].distance;
}
return 0;
}
/* Determine if there is a route between from and to. */
void isflight(char *from, char *to)
{
int d, dist;
char anywhere[20];
if(d=match(from, to)) {
/* is goal */
push(from, to, d);
return;
}
/* find any connection */
if(dist=find(from, anywhere)) {
push(from, to, dist);
isflight(anywhere, to);
}
else if(tos > 0) {
pop(from, to, &dist);
isflight(from, to);
}
}
/* Stack Routines */
void push(char *from, char *to, int dist)
{
if(tos < MAX) {
strcpy(bt_stack[tos].from, from);
strcpy(bt_stack[tos].to, to);
bt_stack[tos].dist = dist;
tos++;
}
else printf("Stack full.\n");
}
void pop(char *from, char *to, int *dist)
{
if(tos > 0) {
tos--;
strcpy(from, bt_stack[tos].from);
strcpy(to, bt_stack[tos].to);
*dist = bt_stack[tos].dist;
}
else printf("Stack underflow.\n");
}

Saturday, September 22, 2012

Swapping numbers using call by reference



//Swapping numbers using call by reference

#include <stdio.h>

void swap(int*, int*);

int main()
{
   int x, y;

   printf("Enter the value of x and y\n");
   scanf("%d%d",&x,&y);

   printf("Before Swapping\nx = %d\ny = %d\n", x, y);

   swap(&x, &y);

   printf("After Swapping\nx = %d\ny = %d\n", x, y);

   return 0;
}

void swap(int *a, int *b)
{
   int temp;

   temp = *b;
   *b = *a;
   *a = temp;
}

C Program To Sort An Array Using Bubble Sort


#include<stdio.h>
int main() {
 int a[50], n, i, j, temp = 0;

 printf("Enter how many numbers you want:\n");
 scanf("%d", &n);
 printf("Enter the %d elements:\n", n);
 for (i = 0; i < n; i++) {
  scanf("%d", &a[i]);
 }
 printf("\n\t\tThe given array is:\n");
 for (i = 0; i < n; i++) {
  printf("\n\t\t%d", a[i]);
 }
 for (i = 0; i < n; i++) {
  for (j = i + 1; j < n; j++) {
   if (a[i] > a[j]) {
    temp = a[i];
    a[i] = a[j];
    a[j] = temp;
   }
  }
 }

 printf("\n\n\n\t\tThe sorted array using Buble sort is:\n");
 for (i = 0; i < n; i++) {
  printf("\n\t\t%d", a[i]);
 }
 return 0;
}

Friday, September 21, 2012

c program to get ip address of a computer


#include<stdlib.h>

main()
{
   system("C:\\Windows\\System32\\ipconfig");
   system("pause");

   return 0;
}