FANDOM


This page is to summarise the various methods that made coding simpler and/or faster. I suspect most of the things will be for C only.


Sorting in CEdit

There is a qsort function in in stdlib that can sort using quick sort and hence there is no need to explicitly write functions for sorting. Since it is a part of stdlib, the use can't be restricted as well. The only catch is that, you have to write a simple compare function for this quicksort to work. Following is the use of the qsort in-built function:

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

/change based on what you are sorting. Int, long int, characters, etc.
int comp (const void * elem1, const void * elem2)
{
    int f = *((int*)elem1);
    int s = *((int*)elem2);
    if (f > s) return  1;
    if (f < s) return -1;
    return 0;
  }
  
  int main(){
  
  int array[5] = {34,56,12,2,76};
  int i;
  
  //call to the quick sort function
  qsort(array,5,sizeof(int),comp);
  
  for(i=0;i<5;i++){
  
  printf("%d ",array[i]);
  
  }
  
  return 0;
  
  }

LimitsEdit

Often we need the values for the maximum/minimum possible value that can be assigned to a datatype. As long as the datatype is standard, we can use limits.h library for figuring out the maximum/minimum. As explained in the following (taken from StackOverflow)

Include limits.h in your program, thereafter the following constants are defined:

CHAR_BIT   = number of bits in a char
SCHAR_MIN  = minimum value for a signed char
SCHAR_MAX  = maximum value for a signed char
UCHAR_MAX  = maximum value for an unsigned char
CHAR_MIN   = minimum value for a char
CHAR_MAX   = maximum value for a char
MB_LEN_MAX = maximum multibyte length of a character accross locales
SHRT_MIN   = minimum value for a short
SHRT_MAX   = maximum value for a short
USHRT_MAX  = maximum value for an unsigned short
INT_MIN    = minimum value for an int
INT_MAX    = maximum value for an int
UINT_MAX   = maximum value for an unsigned int
LONG_MIN   = minimum value for a long
LONG_MAX   = maximum value for a long
ULONG_MAX  = maximum value for an unsigned long
LLONG_MIN  = minimum value for a long long
LLONG_MAX  = maximum value for a long long
ULLONG_MAX = maximum value for an unsigned long long

Format Specifiers in CEdit

Not like this is very important but here are a list of format specifiers in c.

%c     The character format specifier.
%d     The integer format specifier (same as %i).
%ld    The long signed format specifier
%llu   The long long signed format specifier
%lu    The long unsigned format specifier
%llu   The long long unsigned format specifier
%f     The floating-point format specifier.
%lf    The double format specifier.
%e     The scientific notation format specifier.
%E     The scientific notation format specifier.
%g     Uses %f or %e, whichever result is shorter.
%G     Uses %f or %E, whichever result is shorter.
%o     The unsigned octal format specifier.
%s     The string format specifier.
%u     The unsigned integer format specifier.
%x     The unsigned hexadecimal format specifier.
%X     The unsigned hexadecimal format specifier.
%p     Displays the corresponding argument that is a pointer.
%n     Records the number of characters written so far.
%%     Outputs a percent sign.

Scanning Strings in CEdit

Strings are insanely irritating to work with in C. The functions made for them work weirdly and lead to irritating bugs. So far I think, getline is the safest functions to use for scanning strings. There are a few things to keep in mind:

1) getline is C++ functions. Hence you can remove a few warning that come with using getline if you add this before the includes:

#define _GNU_SOURCE

2) Suppose you expect a input of the format: Integer N specifying the number of lines, followed by N lines of character strings as so:

2 
SPORT 
LDXTW KXDTL NBSFX BFOII LNBHG ODDWN BWK

remember to scan one extra character(corresponding to the newline) after the integer, or else your string reading functions will go for a toss. Example code snipper:

scanf("%d ",&N);  //The space after the %d is important!
while(N--){
   getline(&line,&size,stdin);
}

3) While reading a line, getline also stores the newline character, if there is one.

Modulo Arithmetic Edit

A good number of problems might ask to output the answer modulo 10^9 + 7. For this the following rules will come handy:

A few distributive properties of modulo are as follows:
1. ( a + b ) % c = ( ( a % c ) + ( b % c ) ) % c
2. ( a * b ) % c = ( ( a % c ) * ( b % c ) ) % c
3. ( a – b ) % c = ( ( a % c ) – ( b % c ) ) % c ( see note )
4. ( a / b ) % c NOT EQUAL TO ( ( a % c ) / ( b % c ) ) % c
So, modulo is distributive over +, * and – but not / .

This link explains division further, read it at leisure: https://codeaccepted.wordpress.com/tag/modulo-1000000007/

On abs Edit

The absolute value function in C works for only for the int datatype. for long and long long use labs and llabs respectively. Include stdlib to use the labs and llabs

In C++ you can include cmath for a good number of datatypes. point to remember is that the function converts input parameters into double and the return type is also double. So if you performing some operation on the value returned by the abs function which is valid only for int (for example, %), you need to explicitly type cast the answer to int.

Converting a number into any base Edit

#include<stdio.h>
char *fun(unsigned int num, int base);

int main()
{
    char *s;
    s=fun(128, 2);
    s=fun(128, 16);
    printf("%s\n",s);
    return 0;
}
char *fun(unsigned int num, int base)
{
    static char buff[33];
    char *ptr = &buff[sizeof(buff)-1];
    *ptr = '\0';
    do
    {
        *--ptr = "0123456789abcdef"[num %base];
        num /=base;
    }while(num!=0);
    return ptr;
}

Operator Precedence Edit

Operator TypeOperatorAssociativity

Primary ExpressionOperators() [] . -> expr++ expr-- left-to-right

Unary Operators* & + - ! ~ ++expr --expr (typecast) sizeof right-to-left

Binary Operators* / % + ->> <<< > <= >=== !=&^|&&|| left-to-right

Ternary Operator ?: right-to-left

Assignment Operators = += -= *= /= %= >>= <<= &= ^= |= right-to-left

Comma , left-to-right

TO DO Edit

1> Binary Indexed Trees