Monday, November 19, 2018

Wipro HackerRank Training Questions & Solutions


Pattern - Star Pyramid
C program to print following pyramid pattern of stars n=5
        *
      * * *
    * * * * *
  * * * * * * *
* * * * * * * * *
Input Format
Input contains n
Constraints
1 <= n <= 20
Output Format
Print the pattern
Sample Input 0
5
Sample Output 0
            *
         * * *
      * * * * *
   * * * * * * *
* * * * * * * * *
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {

  int row, c, n, s;

  scanf("%d", &n);

  s = n;

  for (row = 1; row <= n; row++)  // Loop to print rows
  {
    for (c = 1; c < s; c++)  // Loop to print spaces in a row
      printf("  ");

    s--;

    for (c = 1; c <= 2*row - 1; c++) // Loop to print stars in a row
      printf("* ");

    printf("\n");
  }

 
    return 0;
}


Compete Cell
There is a colony of 8 cells arranged in a straight line where each day every cell competes with its adjacent cells(neighbour). Each day, for each cell, if its neighbours are both active or both inactive, the cell becomes inactive the next day, otherwise it becomes active the next day.
Assumptions: The two cells on the ends have single adjacent cell, so the other adjacent cell can be assumed to be always inactive. Even after updating the cell state. consider its previous state for updating the state of other cells. Update the cell information of all cells simultaneously. Write a function cellCompete which takes takes one 8 element array of integers cells representing the current state of 8 cells and one integer days representing the number of days to simulate. An integer value of 1 represents an active cell and value of 0 represents an inactive cell.
Input Format
Input will have 8 array values and the no of days
Constraints
array size is 8 integers
Output Format
print the array
Sample Input 0
1 0 0 0 0 1 0 0
1
Sample Output 0
0 1 0 0 1 0 1 0 



#include <stdio.h>
void cellCompete(int[],int);
int main() {
    int arr[10],i,n;
    for(i=0;i<8;i++)
        scanf("%d ",&arr[i]);
    scanf("%d",&n);
    cellCompete(arr,n);
    for(i=0;i<8;i++)
        printf("%d ",arr[i]);
    return 0;
}
void cellCompete(int arr[],int n){
    int arr1[10],i;
    for(;n>=1;n--)
    {
       arr[-1]=0;
       arr[8]=0;
        for(i=0;i<=7;i++)
            arr1[i]=arr[i-1]^arr[i+1];
        /*copying arr1 to arr */
        for(i=0;i<=7;i++)
            arr[i]=arr1[i];
    }
}


















Count the occurrence of substring
Find the occurrence of a sub string in a parent string
Input Format
Input contains the string and the sub string
Constraints
1<=substring_length <= string_length<=1000
Output Format
print the count
Sample Input 0
hgjghjhab
ab
Sample Output 0
1

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

int main() {
    int i,j,k,ctr=0;
    int l1,l2;
    char str[1000],fstr[1000];
    fgets(str,1000,stdin);
   
    fgets(fstr,1000,stdin);
    l1=strlen(str);
    l2=strlen(fstr);
   
    for(i=0;str[i]!='\0';i++)
    {
        for(j=0,k=i;fstr[j]!='\0' && str[k]!='\0' ;j++,k++)
        {
            if(str[k]!=fstr[j])break;
           
        }
        if(j==l2){
            ctr++;
     i=k-1;
        }
       
    }

printf("%d",ctr);
    return 0;
}



Eliminate Repeated Elements
Given two positive integer arrays ary1 and arr2 of lengths len1 and len2 respectively. write a program to count the number of elements which are not common In the arrays.
The input to the function distinctElementCount of two arrays arr1 and arr2 and their lengths len1 and len2 respectively.
The function return the number of elements which are not common in both arrays.
Example.
arr1 = {1, 2,3, 4, 5, 6, 7, 8, 9, 10}, lent = 10
arr2 = {11, 12, 13, 4, 5, 6, 7, 18, 19, 20}, len2 = 10
The distinct elements are 1, 2, 3, 8, 9, 10, 11, 12, 13, 18, 19 and 20 so the function should return 12.
Input Format
Input contains the length of the arrays and the values
Constraints
1 ≤ n ≤105
1 ≤ values ≤ 109
Output Format
Print the count
Sample Input 0
5 6
34 89 12 45 93
12 93 45 23 78 35
Sample Output 0
5

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int distinctElementCount(int arr1[],int arr2[],int len1,int len2)
{
    int ctr=0;
    int i,j;
   
    for(i=0;i<len1;i++)
    {
        for(j=0;j<len2;j++)
        {
            if(arr1[i]!=arr2[j])
            continue;
            else
                break;
        }
        if(j==len2)
            ctr++;
    }
   
    for(i=0;i<len2;i++)
    {
        for(j=0;j<len1;j++)
        {
            if(arr2[i]!=arr1[j])
            continue;
            else
                break;
        }
        if(j==len1)
            ctr++;
    }
   
    return ctr;
}
int main() {
int a1[105],a2[105];
    int i,n1,n2;
    int ctr;
    scanf("%d%d",&n1,&n2);
    if(n1>=1 && n1<=105)
    if(n2>=1 && n2<=105)
    {
    for(i=0;i<n1;i++) scanf("%d",&a1[i]);
    for(i=0;i<n2;i++)scanf("%d",&a2[i]);     ctr=distinctElementCount(a1,a2,n1,n2);
    printf("%d",ctr);
    }
   
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */   
    return 0;
}




GCD Of N Numbers
Write a 'C' program to find the GCD of N numbers
Input Format
Input will have the no of elements and the values
Constraints
1 ≤ noe ≤ 100000
1 ≤ values ≤ 100000000007
Output Format
print the GCD
Sample Input 0
5 2 4 8 16 32
Sample Output 0
2

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int gcd(int a, int b)
{
    if (a == 0)
        return b;
    return gcd(b % a, a);
}
 
// Function to find gcd of array of
// numbers
int findGCD(int arr[], int n)
{ int i;
    int result = arr[0];
    for (int i = 1; i < n; i++)
        result = gcd(arr[i], result);
 
    return result;
}
 

int main() {

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

    int res=findGCD( a,n);

    printf("%d",res);
    return 0;
}

Saturday, October 6, 2018

Basics about pointers

# include <stdio.h>
# include <conio.h>
main(){
int no=100;   /* decl of int variable */
int*ptr;      /* decl of int* */
ptr=&no;      /* init int var address to poi */

clrscr();
printf("\n%d",*ptr);
printf("\n%x    %x     %x",&ptr,ptr,&no);

*ptr=200;
printf("\n%d",*ptr);

/* char poi   */

char ch='A';
char*cptr;
cptr=&ch;
printf("\n%c",*cptr);
/* array vs poi   */

int n[]={10,20,30,40,50};
ptr=&n[2];    /*   or ptr=n;       initializing 2nd index to poi*/

*(ptr+2)=100;

printf("\n%d        %d",*(ptr+2),n[2]);

/* address decrementing and incrementing  by 2b */
*ptr--;

printf("\n%d",*ptr);
*ptr++;
printf("\n%d",*ptr);

/*  void pointers */
void*vptr;
vptr=&n[0];
printf("\n%d",*(int*)vptr);

vptr=&ch;
printf("\n%c",*(char*)vptr);      /* type casting to char /*


char*str="Ongole";
vptr=str;
printf("\n%s",(char*)vptr);
printf("\n%c",*(char*)vptr);

/* dynamically allocating 10b memory to pointer */

ptr=(int*)malloc(sizeof(int)*5);


/* deleting memory */
free(ptr);

}



Wednesday, September 19, 2018

TEST PROGRAM for 3rd ECE-2

/*read    3x3 matrix elements and print diagonal sum
Ex:-
        1    2    3
        4    5    6
         7    8    9
d1=1+5+9  = 15
d2=3+5+7 =  15
*/
import   java.io.*;
import   java.util.*;
class Matrix
{
    private int mat[][];
    int c,r;
    public void  populate(int  r , int c)
    {
    this.c=c;
    this.r=r;
    mat=new int[r][c];
    Scanner  in=new Scanner(System.in);
    System.out.println("Enter "+r+" X " +c+" Matrix elements?");
    for(int i=0;i<r;i++)
    {
        for(int j=0;j<c;j++)
        {
        mat[i][j]=in.nextInt();
        }
    }
    }
    public void disMatrix()
    {
    System.out.println(" Matrix elements are..");
    for(int i=0;i<r;i++)
    {
        for(int j=0;j<c;j++)
        {
        System.out.print(mat[i][j]+"   ");
        }
    System.out.print("\n");
    }
    }
}
class DiagTest
{
    public static void main(String a[])   
    {
        Matrix  m=new Matrix();
        m.populate(3,3);
        m.disMatrix();
    }
}

/*  add diagonal1()  and diagonal2() methods to display the diagonal
sum */








Sunday, September 2, 2018

Friday, August 3, 2018

PROJECT TITLES FOR 3rd CSE 1 & 2

PROJECT TITLES

follow https://www.codewithc.com for projects

->Select  Title   ( c/c++/java )
->Get Permission for ur Project Tile by guide and get registered.
->submit abstract  [14-08-2018]. and start the project under ur guide.



Wednesday, August 1, 2018

apply() related functions in R

The apply() family pertains to the R base package and is
populated with functions to manipulate slices of data
from matrices, arrays, lists and dataframes in a repetitive way.
These functions allow crossing the data in a number of ways and
avoid explicit use of loop constructs. They act on an input list,
matrix or array and apply a named function with one or several optional
arguments.

family is made up of the apply(), lapply() , sapply(), vapply(), mapply(),
rapply(), and tapply() functions.

> x<-matrix(1:9,nrow=3)
> x
     [,1] [,2] [,3]
[1,]    1    4    7
[2,]    2    5    8
[3,]    3    6    9
>
> apply(x,1,sum)
[1] 12 15 18
> apply(x,2,sum)
[1]  6 15 24

The lapply() Function
-----------------------
You want to apply a given function to every element of a list
and obtain a list as result.
> a<-matrix(1:9,nrow=3)
> b<-matrix(10:18,nrow=3)
> c<-matrix(19:27,nrow=3)
> MyList<-list(a,b,c)
> MyList
[[1]]
     [,1] [,2] [,3]
[1,]    1    4    7
[2,]    2    5    8
[3,]    3    6    9

[[2]]
     [,1] [,2] [,3]
[1,]   10   13   16
[2,]   11   14   17
[3,]   12   15   18

[[3]]
     [,1] [,2] [,3]
[1,]   19   22   25
[2,]   20   23   26
[3,]   21   24   27

> lapply(MyList,"[",,1)
[[1]]
[1] 1 2 3

[[2]]
[1] 10 11 12

[[3]]
[1] 19 20 21

> lapply(MyList,"[",,2)
[[1]]
[1] 4 5 6

[[2]]
[1] 13 14 15

[[3]]
[1] 22 23 24

> lapply(MyList,"[",1,)
[[1]]
[1] 1 4 7

[[2]]
[1] 10 13 16

[[3]]
[1] 19 22 25

The sapply() Function
--------------------------
The sapply() function works like lapply(), but it tries to simplify
the output to the most elementary data structure that is possible.
And indeed, sapply() is a ‘wrapper’ function for lapply().

> sapply(MyList,"[",1,)
     [,1] [,2] [,3]
[1,]    1   10   19
[2,]    4   13   22
[3,]    7   16   25

The mapply() Function
------------------------------
The mapply() function stands for ‘multivariate’ apply. Its purpose is
to be able to vectorize arguments to a function that is not usually
accepting vectors as arguments.

In short, mapply() applies a Function to Multiple List or multiple Vector Arguments.



The Sweep() Function
----------------------
The sweep() function is probably the closest to the apply() family.
You use it when you want to replicate different actions on the MARGIN elements

GRAND TEST FOR 3rd CSE-1 & II Dt: 02-08-2018 Time :9.0 A.M.

Design the following UI (Use Form /JForm /Applet) and add Listeners or Adapter classes
Time : 80Min