CS Unplugged

“CS Unplugged is a collection of free learning activities that teach Computer Science through engaging games and puzzles that use cards, string, crayons and lots of running around.

The activities introduce students to Computational Thinking through concepts such as binary numbers, algorithms and data compression, separated from the distractions and technical details of having to use computers. Importantly, no programming is required to engage with these ideas!

CS Unplugged is suitable for people of all ages, from elementary school to seniors, and from many countries and backgrounds. Unplugged has been used around the world for over twenty years, in classrooms, science centers, homes, and even for holiday events in a park!”

Source: CS Unplugged Website

Download the book from here

Advertisements

C++ Quiz no.4

Fix the following code so the B is destroyed properly:

struct A {};
struct B: A {};

int main()
{
   A * p = new B;
   delete p;
}

————————————

Answer to C++ Quiz no.3

#include <iostream>
class example
{
 public:
   example& operator+(const example& obj1);
};

example &example::operator+(const example& obj2)
{
 return *this; //Every object in C++ has access to its own address through an important pointer called this pointer.
}

int main(void)
{
  example a, b;
  example* c;
  c = &(a + b);
  //std::cout<< &c <<" "<<"\n";
}

C++ Quiz no.3

Write minimal implementation of A and B such that the following expression to be legal:

A x;
B y;
y = & ( x + 1);

————————————

Answer to C++ Quiz no.2

#include <iostream>
using std::cout;

struct Row
{
 int a[3];
 Row(int x) { a[0] = x; a[1] = ++x; a[2] = ++x; }
 int * begin() { return a; } // a == &a[0]
 int * end() { return a + 3; } // a + 3 == &a[3]
};

struct Matrix
{
 Row r[3] = { 1, 4, 7 };
 Row * begin() { return r; }
 Row * end() { return r + 3; }
};

int main()
{
 Matrix m;
 for ( auto r : m ) //in pre-C++11 == for ( Row * r = m.begin(); r!= m.end(); ++r) // inside the body we need to use *r which is type Row
 {
  for ( auto e : r )
    cout << ' ' << e;
  cout << '\n';
 }
}

C++ Quiz no.2

Implement a Matrix class for which

Matrix m;
for( auto r : m )
{
   for( auto e : r )
      cout << ' ' << e;
   cout << '\n';
}

prints

1 2 3
4 5 6
7 8 9

————————————

Answer to C++ Quiz no.1

There is an invalid conversion from [int** -> const int**] although [int* -> const int*] is a legal conversion. Trying to increase constness with [int** -> const int**], we introduce a gap between pointer q and object c (i.e. a pointer p between our pointer q and the object c). In order to cover this hole, the following two efforts introduce errors for different reasons (included as comments):

int main ()
{
  const int c = 1;
  const int * p;
  const int ** q = &p; 
  *q = &c; 
  *p = 10; // error: assignment of read-only location (*p) i.e. no way to deceive const restrictions (without explicit conversions).
}
int main ()
{
  const int c = 1;
  int * p;
  const int * const* q = &p;
  *q = &c; // error: assignment of read-only location (*q) i.e. (*q) is a const variable and cannot be modified.
  *p = 10;
}

Shell Script for Building a Complete GCC Cross-Compiler

This article illustrates how to install on a Debian-based distribution the complete toolchain to cross compile in a shell-script automated way. A short intro In several cases it happens that the platform you’re developing on and the computer you’re developing for don’t match. For instance, you may want to develop an application for ARM or PowerPC on your x86 computer. In order to do that, it is necessary to build a cross-compiler that will enable you to build applications for other targets on your machine i.e. a compiler that knows how to write machine code for your target platform.  The required process to install the compiler is provided in the following bash script: a script to download packages for, configure, build and install a GCC cross-compiler from scratch.

#!/bin/sh

#-------------------------------------------------------------------------------------------
# Author: Charalambos Konstantinou 
# W: http://harryskon.com/
# Github: https://github.com/harryskon/cross_compilers
# 
# This script will download packages for, configure, build and install a GCC cross-compiler.
# 
# Published also @ http://w3-tutorials.com/item/29-shell-script-for-building-a-complete-gcc-cross-compiler
#-------------------------------------------------------------------------------------------

# See http://gcc.gnu.org/install/specific.html for more examples on host and target specifics
# For our case: HOST="x86_64-pc-linux-gnu"

# Choose your target: https://gcc.gnu.org/install/specific.html
TARGET="powerpc-eabisim"
# Tried parallel build with e.g. MAKEOPTS="-j 2" but didn't work thus leave it empty
MAKEOPTS=""

abort() {
 echo "**************************************************************"
 echo error: $@
 echo "**************************************************************"
 exit 1
}
success() {
 echo "***************************************************************"
 echo success: $@
 echo "***************************************************************"
 echo "\a"
 sleep 3s
}

# GCC should not be build in the source directory: why? http://gcc.gnu.org/ml/gcc-bugs/2007-06/msg00234.html
prepare_clean_build() {
 rm -rf $BUILDDIR
 mkdir $BUILDDIR
 cd $BUILDDIR
}

CROSSDIR=$HOME/ppc
BUILDDIR=$CROSSDIR/build
SOURCEDIR=$CROSSDIR/sources
DOWNLOADS=$CROSSDIR/downloads
INSTDIR=$CROSSDIR/cross-gcc

export PATH="$INSTDIR/bin:$PATH"

test -d $CROSSDIR || mkdir $CROSSDIR
test -d $DOWNLOADS || mkdir $DOWNLOADS
test -d $SOURCEDIR || mkdir $SOURCEDIR
test -d $INSTDIR && rm -rf $INSTDIR; mkdir $INSTDIR

# 1. Install binutils (containing the assembler and the linker)
 BINUTILS=binutils-2.25
 cd $DOWNLOADS
 test -f $BINUTILS.tar.gz || wget http://ftp.gnu.org/gnu/binutils/$BINUTILS.tar.gz
 cd $SOURCEDIR
 test -d $BINUTILS || tar -xzf $DOWNLOADS/$BINUTILS.tar.gz
 prepare_clean_build
 $SOURCEDIR/$BINUTILS/configure --prefix=$INSTDIR --target=$TARGET --disable-nls --disable-werror
 make $MAKEOPTS all install || abort "building of $BINUTILS failed"
 success "$BINUTILS successfully installed to $INSTDIR"

# 2. Install GMP (GNU multiple precision arithmetic library)
 GMP=gmp-4.3.2
 cd $DOWNLOADS
 test -f $GMP.tar.bz2 || wget ftp://gcc.gnu.org/pub/gcc/infrastructure/$GMP.tar.bz2
 cd $SOURCEDIR
 test -d $GMP || tar -xjf $DOWNLOADS/$GMP.tar.bz2
 prepare_clean_build
 $SOURCEDIR/$GMP/configure --disable-shared --enable-static --prefix=$INSTDIR
 make $MAKEOPTS all install || abort "building of $GMP failed"
 success "$GMP successfully installed to $INSTDIR"

# 3. Install MPFR (library for multiple-precision floating-point computations)
 MPFR=mpfr-2.4.2
 cd $DOWNLOADS
 test -f $MPFR.tar.bz2 || wget ftp://gcc.gnu.org/pub/gcc/infrastructure/$MPFR.tar.bz2
 cd $SOURCEDIR
 test -d $MPFR || tar -xjf $DOWNLOADS/$MPFR.tar.bz2
 prepare_clean_build
 $SOURCEDIR/$MPFR/configure --disable-shared --enable-static --prefix=$INSTDIR --with-gmp=$INSTDIR
 make $MAKEOPTS all install || abort "building of $MPFR failed"
 success "$MPFR successfully installed to $INSTDIR"

# 4. Install MPC (library for the arithmetic of complex numbers with arbitrarily high precision)
 MPC=mpc-0.8.1
 cd $DOWNLOADS
 test -f $MPC.tar.gz || wget ftp://gcc.gnu.org/pub/gcc/infrastructure/$MPC.tar.gz
 cd $SOURCEDIR
 test -d $MPC || tar -xzf $DOWNLOADS/$MPC.tar.gz
 prepare_clean_build
 $SOURCEDIR/$MPC/configure --disable-shared --enable-static --prefix=$INSTDIR --with-gmp=$INSTDIR --with-mpfr=$INSTDIR
 make $MAKEOPTS all install || abort "building of $MPC failed"
 success "$MPC successfully installed to $INSTDIR"

# 5. Install GCC (version 4 and above need GMP, MPFR and MPC development libraries to be installed)
 GCC=gcc-4.8.4
 cd $DOWNLOADS
 test -f $GCC.tar.bz2 || wget http://mirrors.kernel.org/gnu/gcc/$GCC/$GCC.tar.bz2
 cd $SOURCEDIR
 test -d $GCC || tar -xjf $DOWNLOADS/$GCC.tar.bz2
 prepare_clean_build
 $SOURCEDIR/$GCC/configure --prefix=$INSTDIR --target=$TARGET --disable-nls --disable-libssp --enable-languages="c" --without-headers --with-newlib --with-gmp=$INSTDIR --with-mpfr=$INSTDIR --with-mpc=$INSTDIR
 make $MAKEOPTS all install || abort "building of $GCC failed"
 success "$GCC successfully installed to $INSTDIR"

# 6. Install newlib (library implementation intended for use on embedded systems)
 NEWLIB=newlib-2.2.0
 cd $DOWNLOADS
 test -f $NEWLIB.tar.gz || wget ftp://sources.redhat.com/pub/newlib/$NEWLIB.tar.gz
 cd $SOURCEDIR
 test -d $NEWLIB || tar -xzf $DOWNLOADS/$NEWLIB.tar.gz
 prepare_clean_build
 $SOURCEDIR/$NEWLIB/configure --prefix=$INSTDIR --target=$TARGET --disable-nls
 make $MAKEOPTS all install || abort "building of $NEWLIB failed"
 success "$NEWLIB successfully installed to $INSTDIR"


echo "Use cross-compiler by typing:"
echo "$INSTDIR/bin/$TARGET-gcc sourcefile.c"
echo "For more options type: $INSTDIR/bin/$TARGET-gcc --help"
echo "For example: include the -msim option to choose the specific (ppc) machine type"

C++ Quiz no.1

The next posts will include C++ related stuff due to the current Mazonka-based mentoring. Here is a simple quiz to start with:

Is there something wrong with the following code? If yes, can you explain why?

{
 const int c = 1;
 int * p;
 const int ** q = &p; // q points to p
 *q = &c; // now p points to c
 *p = 10; // modify c !
}

Answer will be provided to the next post.