Sunday, February 25, 2018

C program : To output the length of the longest run in the string.

In a string, a "run" is a substring with consisting of consecutive
occurrences of the same character. For example, the string
"mississippi" contains the following runs - "ss", "ss" and "pp".

In this question, given a string, you have to output the length of the
longest run in the string.

Sample Input
abbaaacccc

Sample Output
4

 #include<stdio.h>
#include<string.h>

void main()
{
  int max = 1;
   int current = 1;
   int i;
     char s[100];
  scanf("%s",s);
 
 

   for (i = 1; i < strlen(s); i++) {
      if (s[i - 1] == s[i]) {    /* the run continues */
          current++;
          max = current > max ? current : max;
      } else {    /* the run was broken */
          current = 1;
      }
   }
   printf("%d",max);
    
    }

Thanks
Happy Computing !

C program : To delete all but the last N elements from the linked list, and print the resulting list.



You are given a sequence of integers terminated with a -1. The -1 is
not part of the input sequence.

Next, you are given a positive number N.

You have to create a linked list with the input sequence of integers
as entries.

Now, you have to delete all but the last N elements from the linked
list, and print the resulting list. (i.e. The resulting list will
consist of only the last N elements from the list.)


If N is longer than the length of the linked list, you must print -1.

While printing, the entries of the list must be separated by a single
space.

You can use the following structure.

 struct node{
  int data;
struct node *next;
 };

Sample Input
3 4 5 6 -1
1

Sample Output
6
// C program to find n'th node in linked list
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

/* Link list node */
struct Node
{
    int data;
    struct Node* next;
};

/* Given a reference (pointer to pointer) to the head
    of a list and an int, push a new node on the front
    of the list. */
void push(struct Node** head_ref, int new_data)
{
    /* allocate node */
    struct Node* new_node =
            (struct Node*) malloc(sizeof(struct Node));

    /* put in the data  */
    new_node->data  = new_data;

    /* link the old list off the new node */
    new_node->next = (*head_ref);

    /* move the head to point to the new node */
    (*head_ref)    = new_node;
}

/* Takes head pointer of the linked list and index
    as arguments and return data at index*/
int GetNth(struct Node* head, int index)
{
    struct Node* current = head;
    int count = 0; /* the index of the node we're currently
                  looking at */
    while (current != NULL)
    {
       if (count == index)
          return(current->data);
       count++;
       current = current->next;
    }

    /* if we get to this line, the caller was asking
       for a non-existent element so we assert fail */
    assert(0);
}

/* Drier program to test above function*/
int main()
{
    /* Start with the empty list */
    struct Node* head = NULL;

    /* Use push() to construct below list
     1->12->1->4->1  */

      int n=0,N=0,j=0;
      while(n!=-1)
{

      scanf("%d",&n);
    push(&head, n);
    j+=1;

}
   scanf("%d",&N);
    /* Check the count function */
  if(N<j)
{
    for(int i=0;i<N;i++)
    { 
      printf("%d ", GetNth(head, N-i));
     
    }
}
  else{printf("-1");}
}

C program : (Graphs) To output the list of vertices which are connected n by an edge

 You will be given a vertex number n. You have to output
the list of vertices which are connected n by an edge, in the order in
which the edges were input.

Input

You are given the following.

1. The first line contains an integer, E, between 1 and 1000

2. This is followed by E lines, where each containing a pair of
numbers i and j where i and j are both non-negative integers <=
34,000. No edge will be listed more than once.

3. The last line contains a non-negative integer n <= 34,000. n is
assured to be a vertex listed in one of the E lines in part (2).

Output

You have to output the list of nodes to which n has an edge, in the
order in which the edges were input, one line for each vertex.


#include<stdio.h>
#include<stdlib.h>

struct edges
{
     int vertex1;
    int vertex2;
};
void main()
{
  int E,n;
  struct edges e[1000];
  scanf("%d",&E);
  for(int i=0;i<E;i++)
  {
       scanf("%d %d",&e[i].vertex1, &e[i].vertex2);
  }
  scanf("%d",&n);
  for(int i=0;i<E;i++)
  {
       if(e[i].vertex1==n)
    { printf("%d\n",e[i].vertex2);}
    if(e[i].vertex2==n)
    { printf("%d\n",e[i].vertex1);}
  }
 
 }

Thanks
Happy Computing !

C program : Create a database using Structure

Create a database of students using structures, where in each entry of the database will have the following fields:
  1. a name, which is a string with at most 128 characters
  2. their marks in physics which is an int between 0 and 100
  3. their marks in chemistry which is an int number between 0 and 100
  4. their marks in mathematics which is an int number between 0 and 100
You have to output a list of students in the following order.
  1. if a student 'A' has lower marks in physics than a student 'B', then A's data is listed before B.
  2. If A and B have the same physics marks and A has lower chemistry marks than B, then A is listed before B.
  3. If A and B have the same marks in physics and chemistry, and A has lower marks in mathematics than B, then A is listed before B.
  4. If all marks are equal and A's name precedes B's name in the dictionary order, then A is listed before B.
Input Format :First line contains the number of students n, where 1<=n<=100.
In following n lines each line contains(space separated)  a name and their respective marks in physics, chemistry, maths, where 0<=marks<=100.

Output Format :Sorted database of n lines.


 #include<stdio.h>
#include<string.h>
struct stu
{
  char name[128];
  int p,c,m;
};
void main()
{
  int i=0,n=0,j=0;
  scanf("%d",&n);
  struct stu db[n],temp;
  if(n==1)
  {
    scanf("%s%d%d%d",db[0].name,&db[0].p,&db[0].c,&db[0].m);
    printf("%s\t%d\t%d\t%d",db[0].name,db[0].p,db[0].c,db[0].m);
  }
  else
{

  for(i=0;i<n;i++)
  {scanf("%s%d%d%d",db[i].name,&db[i].p,&db[i].c,&db[i].m);}
  for(i=0;i<n;++i)
  {
    for(j=i+1;j<n;++j)
    {

       if(db[i].p>db[j].p)
          {
                  temp=db[i];
                  db[i]=db[j];
                  db[j]=temp;
           }

          else if(db[i].p==db[j].p && db[i].c>db[j].c)
            {
                temp=db[i];
                db[i]=db[j];
                db[j]=temp;
              }

          else if(db[i].p==db[j].p && db[i].c==db[j].c && db[i].m>db[j].m)
        {
            temp=db[i];
            db[i]=db[j];
            db[j]=temp;
        }


      else  if(db[i].p==db[j].p && db[i].c==db[j].c && db[i].m==db[j].m)
        {

                  if(strcmp(db[i].name,db[j].name)>0)
                    {
                      temp=db[i];
                      db[i]=db[j];
                      db[j]=temp;
                    }

          }


    }
  }
  for(i=0;i<n-1;i++)
  {printf("%s\t%d\t%d\t%d\n",db[i].name,db[i].p,db[i].c,db[i].m);}
    printf("%s\t%d\t%d\t%d",db[n-1].name,db[n-1].p,db[n-1].c,db[n-1].m);
}
}

C Recursive Program which outputs the line with the characters reversed.

Example:
INPUT
This is easy

OUTPUT
ysae si sihT

 #include<stdio.h>
void reverse()
{
int c = getchar();
  if (c != EOF) {
    reverse();
    putchar(c);
  }
}

int main()
{
 
  reverse();
  return 0;
}

Thanks
Happy Computing !

C program : The smallest quadrant index


Consider a matrix M of integers. Divide M into 4 sub-matrices. These sub-matrices are called as Quadrants. Report the Quadrant number which has the smallest minimum-element. If two or more quadrants have same smallest minimum, report the smallest quadrant index. The matrix M is divided into four quadrants by halving the rows and columns. If row/column is an odd number, divide them in such a way that the first half of the row/column should be one smaller than the second half.
The four quadrants are numbered from 1 to 4 in the structure shown below: Q1 | Q2
---+---
 Q3 | Q4
INPUT FORMAT: M is a matrix of integers. You would be given two numbers m and n specifying the number of rows and columns. This would be followed by m lines of n integers each specifying the data of the m*n matrix M.
n and m will be greater than 1 and less than 12. OUTPUT FORMAT: Print in a separate line, the quadrant number with the smallest minimum-element. #include<stdio.h>
void main()
{
int a[12][12],m,n,i,j,p,q,min,row=0,col=0;
scanf("%d%d",&m,&n);

for(p=0;p<m;p++)
{

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

}
}
min = a[0][0];

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

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

if(min>a[i][j])
{
min=a[i][j];
row=i;
col=j;
}
}
}
if((row<m/2)&&(col<n/2))
printf("1");
else if((row<m/2)&&(col>=n/2))
printf("2");
else if((row>=m/2)&&(col<n/2))
printf("3");
else if((row>=m/2)&&(col>=n/2))
printf("4");
}
 


Thanks


Happy Computing ! 

C program : The Collatz function

The Collatz function is defined for a positive integer n as follows.
f(n) = 3n+1 if n is odd
        n/2     if n is even

We consider the repeated application of the Collatz function starting with a given integer n, as follows:
f(n), f(f(n)), f(f(f(n))), …

It is conjectured that no matter which positive integer n you start from, this sequence eventually will have 1 in it. It has been verified to hold for numbers up to 5 × 260

You will be given a positive number <= 32,000. You have to output how many times f has to be applied repeatedly in order to first reach 1.

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

  int n,y=0,count=0;
  scanf("%d",&n);
  if(n==1)
  {printf("0");
  }
  else
  {
  while(y!=1)
  {
      if(n%2==0)
      {
        y=n/2;
        n=y;
      }
      else
      {
         y=3*n+1;
        n=y;
      }
    count++;
  }
  printf("%d",count);
  }
  return 0;
  }


Thanks
Happy Computing ! 

what is crystal field theory and why do we need it?

  Calculating CFSE