顯示具有 C++ 標籤的文章。 顯示所有文章
顯示具有 C++ 標籤的文章。 顯示所有文章

2015年3月4日 星期三

2014年10月3日 星期五

[Google Interview] No. 28 - A Pair with the Maximal Difference

Problem: A pair contains two numbers, and its second number is on the right side of the first one in an array. The difference of a pair is the minus result while subtracting the second number from the first one. Please implement a function which gets the maximal difference of all pairs in an array. For example, the maximal difference in the array {2, 4, 1, 16, 7, 5, 11, 9} is 11, which is the minus result of pair (16, 5).

2014年10月2日 星期四

[Google Interview] No. 13 - First Character Appearing Only Once

Problem: Implement a function to find the first character in a string which only appears once.
For example: It returns ‘b’ when the input is “abaccdeff”.

2014年10月1日 星期三

[Google Interview] No. 03 - Maximum Sum of All Sub-arrays

Question: A sub-array has one number of some continuous numbers. Given an integer array with positive numbers and negative numbers, get the maximum sum of all sub-arrays. Time complexity should be O(n).


2012年10月3日 星期三

[Programming] scanf()

Issue: scanf() by char or string goes wrong result.

#include <stdio.h>

int main(int argc, char **argv)
{
        int valA;
        char valB;

        scanf ("%d", &valA);
        printf ("valA=%d\n", valA);
        scanf ("%c", &valB);
        printf ("valB=%c\n", valB);

        return 0;
}

Result:

3
valA=3
valB=

Expect result:
3
valA=3
4
valB=4

Reason: scanf() doesn't handle new line character, "\n".


Solution1: Discard the last character "\n" by "%*c".


#include <stdio.h>

int main(int argc, char **argv)
{
        int valA;
        char valB;

        scanf ("%d%*c", &valA);
        printf ("valA=%d\n", valA);
        scanf ("%c%*c", &valB);
        printf ("valB=%c\n", valB);

        return 0;
}


Solution2: Add a space character to second scanf().
#include <stdio.h>

int main(int argc, char **argv)
{
        int valA;
        char valB;

        scanf ("%d", &valA);
        printf ("valA=%d\n", valA);
        scanf (" %c", &valB); // It is " %c". There is a space character in front of %c.
        printf ("valB=%c\n", valB);

        return 0;
}


Solution3: Read out the "\n" by getchar();

#include <stdio.h>

int main(int argc, char **argv)
{
        int valA;
        char valB;

        scanf ("%d", &valA);
        getchar();
        printf ("valA=%d\n", valA);

        scanf ("%c", &valB);
        getchar();
        printf ("valB=%c\n", valB);

        return 0;
}

2011年11月15日 星期二

[Unix Programming] Zombie

Zombie: Child process terminates without parent's waiting.
As a result, all you have to do is to wait the child process while terminating.


void waitchild() {
  signal( SIGCHLD, waitchild); // re-enable the signal to fit all systems
  while( wait(NULL) <= 0 )
    /*NOTHING*/;
}


int
main (int argc, char **argv)
{
  signal( SIGCHLD, waitchild);
  if (fork() == 0) {
    // child process
  } else {
    // parent
  }
}

Refer:
http://home.educities.edu.tw/shirock/comp/Anti_zombie_process.htm
http://starryalley.twbbs.org/blog/index.php?/archives/683-linux-zombie-process.html

2011年11月8日 星期二

[C] call by value/reference/address

Call by Value, Call by Address(Pointer), C++ includes Call by Reference
http://csie-tw.blogspot.com/2010/03/call-by-valuecall-by-pointercall-by.html

Call by Value:
int main()
{
  int x=1;
  printf("x=%d\n", x);
  foo(x);
  printf("x=%d\n", x);
}
void foo(int x)
{
  x++;
}

result:
1
1


Call by Address/Pointer:
int main()
{
  int x=1;
  printf("x=%d\n", x);
  foo(&x);
  printf("x=%d\n", x);
}
void foo(int *x)
{
  (*x)++;
}

result:
1
2


Call by Reference:(C++)
int main()
{
  int x=1;
  printf("x=%d\n", x);
  foo(x);
  printf("x=%d\n", x);
}
void foo(int &x)
{
  x++;
}

result:
1
2

[C] pointer to function

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

void func(int);

main(){
      void (*fp)(int);

      fp = func;

      (*fp)(1);
      fp(2);

      exit(EXIT_SUCCESS);
}

void
func(int arg){
      printf("%d\n", arg);
}