Wednesday, June 1, 2011

memset (cstring (string.h))

memset is such a library function which is rarely used in practical. Suppose, in an array, for some certain values you have to make that true. And for any other values that will be false. In this case you have to change all the elements of the array to false first, and then you can make that true for those specific values. memset is the easiest way to do this. An example of it is given below:

bool row [10000 + 5];
bool col [10000 + 5];
memset (row, false, 10005);
memset (col, false, 10005);

void * memset ( void * ptr, int value, size_t num );

Fill block of memory Sets the first num bytes of the block of memory pointed by ptr to the specified value (interpreted as an unsigned char).

Pointer to the block of memory to fill.
Value to be set. The value is passed as an int, but the function fills the block of memory using the unsigned char conversion of this value.
Number of bytes to be set to the value.

Return Valueptr is returned.


/* memset example */
#include <stdio.h>
#include <string.h>

int main ()
  char str[] = "almost every programmer should know memset!";
  memset (str,'-',6);
  puts (str);
  return 0;

------ every programmer should know memset!

UVA Problem#11760



1. Take 2 Boolean array (e.g bool row[10000 + 5]; bool col[10000 + 5]; )
2. Let all the elements of the above two array be false. The easiest way to do this is by using memset(...,...,)
3. Then start taking the inputs as "r" and "c"and turn the row[r] and col[c] into true from false.
4. Then check the 4 places of Arif where he can move. 
5. Print your result.
Pseudo code is given below: