standard quote function in C

recently i’m seeing a lot of these functions around web

#include <stdio.h>
void quote(char const *argument){
printf("Google %s and you are gonna end up in one of those amazing parts of web which you never knew that they exist !", argument);
}
int main(){
quote("Google");
return 0;
}
view raw quote.c hosted with ❤ by GitHub

Shuffling Tehniques

It’s true that we cannot generate pure random numbers, although the random.org proposes to provide true random, I wonder how they overcome the modulo division defect!

Shuffling is one technique that we use every day when playing music. Which is obviously achieved through random numbers. We have to generate random numbers n times where n is the size of the array. A naive approach to shuffle a list would be like

void swap(int *p,int *q)
{
int temp;
temp=*p;
*p=*q;
*q=temp;
}
void shuffle(int *data,int size)
{
int i,j;
srand(time(NULL));
for (i = 0; i < size-1; ++i)
{
j=rand()%(size-i) + i;
swap(data+i,data+j);
}
}
view raw shuffle.c hosted with ❤ by GitHub

But the true randomness is not always favored, we need the songs to be biased based on our like. So i came up with a solution, somewhat look like this one

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <stddef.h>
#include <math.h>
#include <string.h>
#include <ctype.h>
#include <time.h>
#include <assert.h>
#include <errno.h>
#include <limits.h>
#include <float.h>
#include <setjmp.h>
#include <signal.h>
int matrix[]={0,1,2,3,4,5,6,7,8};
void seedrand()
{
static int i=1;
if (i)
srand(time(NULL));
i=0;
}
void swap(int *p, int *q)
{
int temp;
temp=*p;
*p=*q;
*q=temp;
}
void shuffle(int *data, int low, int high)
{
int j;
seedrand();
if (low>=high)
return ;
j=rand()%(high-low) + low;
swap(data+low, data+j);
shuffle(data, low+1, j);
shuffle(data, j+1, high);
return ;
}
int main(int argc, char const *argv[])
{
return 0;
}
view raw myshuffle.c hosted with ❤ by GitHub

here the input i have taken is not same as the one before(regarding the notion). here the array is ranks of the songs based on listen count. I don’t know whether it’s an efficient but it surely does the job of playing more times the song whose listen count is more and the opposite for songs whose listen count is low. It’s MY ALGORITHM

strcmp() drawback

It’s been a while, the strcmp() function provided by the default library may come useful in many times but if the implementation is tweaked a bit then it will be greatly useful for the dictionary search programs, sorting of strings, last or first occurring word in a lexicographically sorted array of strings. So here, I present my tweak.

Github :

https://github.com/eightnoteight/e_strcmp

/*
Author : eightnoteight (Srinivas Devaki)
Date : Wed May 28
Time : 9:44:31 AM
e-mail : mr.eightnoteight [at] gmail [dot] com
website : https://eightnoteight.wordpress.com
ABSTRACT : The deafult strcmp() merely returns info
about which string is lexographically
greater, but e_strcmp() returns the
difference between the words where the
first char difference between the strings.
Description : If the returned integer is positive then
*p string is lexographically greater than *q.
If the returned integer is negative then
*p string is lexographically lesser than *q.
If the returned integer is 0 *p is identical
to *q
If the returned integer is greater than 27
then the *q string is a substrng of *p
If the returned integer is less than -27 then
the *p string is a substring of *q
*/
int e_strcmp(char *p, char *q)
{
while(*p)
{
if (*q == '\0')
return *p - *q;
if (*p > *q)
return *p - *q;
if (*p < *q)
return *p - *q;
p++;
q++;
}
if (*q)
return *p - *q;
return 0;
}
/*Original Implemantion*/
/*Original Implemantion*/
/*Original Implemantion*/
/*Original Implemantion*/
/*Original Implemantion*/
/*
int strcmp(char *p, char *q)
{
while(*p)
{
if (*q == '\0')
return 1;
if (*p > *q)
return 1;
if (*p < *q)
return -1;
p++;
q++;
}
if(*q)
return -1;
return 0;
}
*/
view raw e_strcmp.h hosted with ❤ by GitHub

A sluggish Implementation of Quick Sort

I just came to know about the quick sort. An easy way of implementing the quick sort is by choosing the lowest or highest element or some positioned element which will continue to exist in the recursively decreasing input array up to the penultimate base case. so here’s my lame implementation of Quick Sort.

/*Test Program*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <assert.h>
#include <ctype.h>
#include "quicksort.h"
#define MAXSIZ 100000
int main(int argc, char const *argv[])
{
long long int a[MAXSIZ];
long long int n,i;
scanf("%lld", &n);
for (i = 0; i < n; ++i)
scanf("%lld", &a[i]);
quicksort(a,0,n-1);
for (i = 0; i < n; ++i)
printf("%lld\n", a[i]);
return 0;
}
view raw Quicksort.c hosted with ❤ by GitHub
/*Header File for the Quick Sort*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <assert.h>
#include <ctype.h>
int quicksort(long long int unsortedarray[], long long int low, long long int high)
{
long long int pivot=unsortedarray[low];
long long int k,temp,i;
if (low<high)
{
k=low+1;
for (i = low+1; i <= high; ++i)
{
if (unsortedarray[i]<pivot)
{
temp=unsortedarray[i];
unsortedarray[i]=unsortedarray[k];
unsortedarray[k++]=temp;
}
}
temp=unsortedarray[low];
unsortedarray[low]=unsortedarray[--k];
unsortedarray[k]=temp;
quicksort(unsortedarray,low,k-1);
quicksort(unsortedarray,k+1,high);
}
return 0;
}
view raw Quicksort.h hosted with ❤ by GitHub

Logarithm Functions in math.h

#include <math.h>
double log (double x)
long logl (long double x)
float logf (float x)
double log2 (double x)
long log2l (long double x)
float log2f (float x)
double log10 (double x)
long log10l (long double x)
float log10f (float x)
double log1p (double x)
long log1pl (long double x)
float log1pf (float x)

Some Description:

  • The log function computes the value of the natural logarithm of argument x.
  • The log2 function computes the value of the logarithm of argument x to base 2.
  • The log10 function computes the value of the logarithm of argument x to base 10.
  • The log1p function computes the value of \log(1+x) accurately even for very small values of x.

codechef april cook-off is a disaster

It was really a disaster, I have just solved a single problem, but that one too I submitted it lately. I have submitted it with in 13 minutes and 45 seconds, by that time 180 members have already made a positive submission. and second question I took was Travelling with chef, which is very time taking yet easy question. I don’t even understand why my code is giving wrong answer,

I wrote the code for calculating the quotients of the polynomial with Cramer’s rule of matrices, and after finding the quotients I went to integrate the velocity polynomial to get the distance at a specified time, and then as the question states that chef has started at time 0, I have assumed the integrating constant to be zero. and after printed the result. But it is giving me a wrong answer, I don’t understand it at all.

believe me you shouldn’t go to the question page. I was literally frightened when I saw code of a solution.

But the good news is that my ratings for short contest have increased by 262 points.

Screenshot from 2014-04-21 00:36:10

 

here’s my code


#include <stdio.h>
#include <math.h>
double determinant(double *q,double *w,double *e,double *r);
int main()
{
double T3[4],T2[4],T1[4],P[4],X[4],ultimatetime,det,A[4]={1};
int i,j,testcases;
scanf("%d",&testcases);
for(j=0;j<testcases;j++)
{
scanf("%lf", &ultimatetime);
for (i = 0; i < 4; i++)
{
scanf("%lf%lf", &T1[i], &X[i]);
T2[i]=pow(T1[i],2);
T3[i]=pow(T1[i],3);
}
det=determinant(T3,T2,T1,A);
P[0]=determinant(T3,T2,T1,X);
P[1]=determinant(T3,T2,X,A);
P[2]=determinant(T3,X,T1,A);
P[3]=determinant(X,T2,T1,A);
for (i = 0; i < 4; ++i)
{
P[i]/=det;
}
printf("%lf\n", ((P[0]*pow(ultimatetime,4))/4.0)+(P[1]*pow(ultimatetime,3)/3.0)+(P[2]*pow(ultimatetime,2)/2.0)+(P[3]*ultimatetime));
}
return 0;
}
double determinant(double *q,double *w,double *e,double *r)
{
return (*q)*(*w+1)*(*e+2)*(*r+3)-(*q)*(*w+1)*(*e+3)*(*r+2)-(*q)*(*e+1)*(*w+2)*(*r+3)+(*q)*(*e+1)*(*w+3)*(*r+2)+(*q)*(*r+1)*(*w+2)*(*e+3)-(*q)*(*r+1)*(*w+3)*(*e+2)-(*q)*(*q+1)*(*e+2)*(*r+3)+(*q)*(*q+1)*(*e+3)*(*r+2)+(*q)*(*e+1)*(*q+2)*(*r+3)-(*q)*(*e+1)*(*q+3)*(*r+2)-(*q)*(*r+1)*(*q+2)*(*e+3)+(*q)*(*r+1)*(*q+3)*(*e+2)+(*e)*(*q+1)*(*w+2)*(*r+3)-(*e)*(*q+1)*(*w+3)*(*r+2)-(*e)*(*w+1)*(*q+2)*(*r+3)+(*e)*(*w+1)*(*q+3)*(*r+2)+(*e)*(*r+1)*(*q+2)*(*w+3)-(*e)*(*r+1)*(*q+3)*(*w+2)-(*q)*(*w+1)*(*r+2)*(*e+3)+(*q)*(*w+1)*(*r+3)*(*e+2)+(*q)*(*r+1)*(*w+2)*(*e+3)-(*q)*(*r+1)*(*w+3)*(*e+2)-(*q)*(*e+1)*(*w+2)*(*r+3)+(*q)*(*e+1)*(*w+3)*(*r+2);
}