13422 - Cheat Sheet (I2P1-final)   

Description

CHEAT SHEET

BASIC I/O

printf() and  scanf() format

printf("%d", n);

scanf("%d", &n);

 

FORMAT  ARGUMENT TYPE

%d, %i  int           decimal

%u      unsigned int

%x      unsigned int  hexadecimal

%#x     unsigned int  hexadecimal with prefix 0x

%f      double  

%Lf     long double

%e, %E  double      scientific notation

%c      int         to print a character

%s      char *      string (character array ended with '\0')

%p      void *      print memory address

%g, %G  double      %f or %e depending on the length
 

FORMAT  ARGUMENT TYPE

%d      int *       &n, store the input integer in n

%ld     long *

%lld    long long *

%u      unsigned int *

%f      float *     read float

%lf     double *    read double

%Lf     long double *   read long double

%c      char *      read 3 characters %3c

%s      char *      read a string until whitespace

%n      int *       with %s, to get string length

                   char a[100]; int len; 
                  scanf("%s%n", a, &len);
                  len will have the string length

 

OPERATION
!   &&    ||    ==     !=    +    -    *    /    %
>   <     >=    <=

 

To create a 5-by-5 two-dimensional array, we need to write

int a[5][5];

It will be indexed as follows:

 a[0][0] 

 a[0][1]

 a[0][2]

 a[0][3]

 a[0][4]

 a[1][0]

 a[1][1]

 a[1][2]

 a[1][3]

 a[1][4]

 a[2][0]

 a[2][1]

 a[2][2]

 a[2][3]

 a[2][4]

 a[3][0]

 a[3][1]

 a[3][2]

 a[3][3]

 a[3][4]

 a[4][0]

 a[4][1]

 a[4][2]

 a[4][3]

 a[4][4]

 


How to read the following data?
1 2 3 4 5 e
#include <stdio.h>

int main(void)
{
    int x;
    while (scanf("%d", &x) == 1) {   
     printf("x=%d\n", x);

    }
    return 0;
}

 

How to read the following data?

2

L 5 2
D 5 3

#include <stdio.h>

int main(void)

{

   char ch;
   int i, n, row, col;

   scanf("%d", &n);

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

      while(getchar()!='\n');

      scanf("%c%d%d", &ch, &row, &col);

   }

   return 0;

}

 

Using for loops to print a two-dimensional array

   for(i = 0; i < row; i++) {
      for (j = 0; j < col; j++) {
         printf("%5d", A[i][j]);
      }
      printf("\n");
   } 

 

Using bubble sort to rearrange an array ap

for (i = 0; i < n-1; i++)
{
    for (j = 0; j < n-1-i; j++)
    {
        if (ap[j] > ap[j + 1])
        {
            temp = ap[j];
            ap[j] = ap[j + 1];
            ap[j + 1] = temp;
        }
    }
}

 

qsort 

you have to include <stdlib.h>

usage :

void qsort (void *array, size_t count, size_t size, comparison_fn_t compare);

qsort an int array

int compare_int (const void *a, const void *b)

{

    const int *va = (const int *) a;

    const int *vb = (const int *) b;

    return *va-*vb;

}

 

qsort a double array

int compare_double (const void *a, const void *b)

{

    const double *da = (const double *) a;

    const double *db = (const double *) b;

    return (*da > *db) - (*da < *db);

}

 

How to avoid common errors and how to debug for OJ

1. Put the arrays in the 'global' area. Set their size bigger than required. Avoid using variable-length arrays (e.g. int arr[n];). Keep the array size fix (e.g., int arr[1000];).

2. After writing the code for reading input data, you may print out the data to check if your code reads them correctly. Do not proceed to write subsequent code before you confirm that.

3. If your program crashes, usually it is caused by memory-related errors. Check the ranges of for-loops to see if your code attempts to read or write the elements out of the arrays’ boundary.

*(a+i) is equivalent to a[i]
(a+i) is equivalent to &a[i]

 

FREQUENTLY USED FUNCTIONS

#include <string.h>

char str[10];

scanf("%s", str);
fgets (str, 10, stdin);

gets(str);

 

to get the string length : strlen(str)

to compare two strings : strcmp(str1, str2) ==0 if equal

to compare the first n chars of two strings : strncmp(str1, str2, n) ==0 if equal

to copy str2 to str1 : strcpy(str1, str2)

to copy the first n chars of str2 to str1 : strncpy(str1,str2, n), remember to add '\0' to str1

to get the first token in str1 splitted by delim : token = strtok(str1, delim), and use loop to get next token

example:

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

int main () {
   char str[80] = "This is - www.tutorialspoint.com - website";
   const char s[2] = "-";
   char *token;
   
   /* get the first token */
   token = strtok(str, s);
   
   /* walk through other tokens */
   while( token != NULL ) {
      printf( " %s\n", token );
    
      token = strtok(NULL, s);
   }
   
   return(0);
}

to check if str2 is str1's substring: strstr(str1, str2), return pointer to the first character of the substring in str1.
example:

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


int main () {
   const char haystack[20] = "TutorialsPoint";
   const char needle[10] = "Point";
   char *ret;

   ret = strstr(haystack, needle);

   printf("The substring is: %s\n", ret);
   
   return(0);
}

 

#include <ctype.h>

isspace(ch), islower(ch), isupper(ch), isdigit(ch)

isalpha(ch), toupper(ch), tolower(ch)

 

Input

Output

Sample Input  Download

Sample Output  Download

Tags




Discuss