AD1

Saturday, 17 June 2017

C program to left rotate an array

Write a program to left rotate an array by n position
Example
Input
Input 10 elements in array: 1 2 3 4 5 6 7 8 9 10
Input number of times to rotate: 3
Output
Array after left rotation: 4 5 6 7 8 9 10 1 2 3
Solution
#include <stdio.h>
#include <stdlib.h>
#define size 5
void rotate_array_left(int *array,int times)
{
int temp=0;
for(int x=0;x<times;x++)
{
temp=array[0];
for(int i=0;i<=size-2;i++)
{
array[i]=array[i+1];
}
array[size-1]=temp;
}
}
int main(int argc, char** argv) {
int data[size]={1,2,3,4,5};
rotate_array_left(data,2);
for(int j=0;j<=size-1;j++)
{
printf("%d\t",data[j]);
}
return (EXIT_SUCCESS);
}
view raw rotatearray.c hosted with ❤ by GitHub

Count Set bits in an array of integers

assume you have an array of 5 integers and you want to count the numbers of set bits in all array elements , write a function to do that

Solution
#include <stdio.h>
#include <stdlib.h>
#define size 5
int count_set_bits(int *array)
{
int count=0;
for(int i=0;i<size;i++)
{
for(int j=0;j<32;j++)
{
if(array[i]&(1<<j))
{
count++;
}
}
}
return count;
}
int main(int argc, char** argv) {
int data[size]={1,2,3,4,5};
printf("Number of set bits in this array = %d bits \n",count_set_bits(data));
return (EXIT_SUCCESS);
}
view raw setbits.c hosted with ❤ by GitHub


Circular Bit Shift

Write two functions RL(data,n) which performs circular left shift on variable data by n bits , RR(data,n) which performs circular right shift on variable data by n bits
note: data is 1 byte variable

refer to this video to know more about circular bit shifts




Solution
#include <stdio.h>
#include <stdlib.h>
char RL(unsigned char byte,unsigned int n)
{
unsigned char temp=0;
for(int x=0;x<n;x++)
{
temp=byte&(1<<7);//save last bit
byte=byte<<1;//shift n left by 1 bit
byte|=temp;//save last bit in first position from right
}
return byte;
}
char RR(unsigned char byte,unsigned int n)
{
unsigned char temp=0;
for(int x=0;x<n;x++)
{
temp=byte&(1<<0);//save first bit
byte=byte>>1;//shift n left by 1 bit
byte|=(temp<<7);//save first bit in last position from right
}
return byte;
}
int main(int argc, char** argv) {
printf("%d\n%d\n",RL(16,2),RR(16,2));
return (EXIT_SUCCESS);
}
view raw circularshift.c hosted with ❤ by GitHub

Friday, 16 June 2017

Beautiful Matrix

Problem 
codeforces.com/problemset/problem/263/A

Solution
#include <iostream>
#include <cstdlib>
#include <math.h>
using namespace std;
int main(int argc, char** argv) {
int input;
// read matrix
for(int r=0;r<=4;r++)
{
for(int c=0;c<=4;c++)
{
cin>>input;
if(input==1)
{
cout<<abs(r-2)+abs(c-2)<<endl;
break;
}
}
if(input==1){break;}
}
return 0;
}
view raw beautiful.cpp hosted with ❤ by GitHub


Teams Problem

Problem 
http://codeforces.com/contest/231/problem/A

Solution
#include <iostream>
#include <cstdlib>
using namespace std;
int main(int argc, char** argv) {
unsigned int n;// number of problems
unsigned int o1,o2,o3;// team memebrs
unsigned int pass=0;
unsigned int fail=0;
cin>>n;
for(int i=0;i<n;i++)
{
cin>>o1>>o2>>o3;
if(o1+o2+o3>=2){pass++;}
else if(o1+o2+3<2){fail++;}
}
cout<<pass;
return 0;
}
view raw teams.cpp hosted with ❤ by GitHub




Saturday, 3 June 2017

What is dangling pointer

The following code snippet illustrate the idea of dangling pointers


#include <iostream>
#include <cstdlib>
using namespace std;
int main(int argc, char** argv) {
int *p=(int*)malloc(sizeof(int));
*p=1;
/*
* now p is an integer pointer pointes to space(4 bytes)allocated
* by malloc function , also the allocated space is initilized by 1
*/
cout<<*p<<endl;//result will be 1
//now we will free or deallocate reserved 4 bytes
free(p);
/*
* now p is a dangling pointer , pointes to deallocated space
* so data at this pointer is garbage
*/
cout<<*p;// output will be garbage
return 0;
}
view raw dangling.cpp hosted with ❤ by GitHub