Yearly Archives: 2016


Google Hash Code 2017 Limassol Cyprus – Call for participation

We’ll be hosting a hub at the Cyprus University of Technology for the Online Qualification Round of Hash Code, a team-based programming competition created by Google for university students and industry professionals. The Online Qualification Round takes place on the 23rd of February at 19:30 EET and registered teams from Cyprus are invited to participate from our hub, which will take place at the Labs of the University. Top scoring teams from the Online Qualification Round will then be invited to Google’s Paris office to compete in the Final Round of the competition in April.

If you’re interested in joining our hub, find a team (two to four people) and register at g.co/hashcode. Make sure to select Cyprus University of Technology from the list of hubs in the Judge System.

For more information about this and other hubs in Cyprus (including the twin event in Nicosia) visit https://goo.gl/XSfUPv

Hash Code 2017 Limassol Cyprus – Facebook Event

Thanks!

Address:

Cyprus University of Technology
Room: ΚΧΕ 1 - Computer Lab
Polyxeni Loizia and Eleni Autonomou Building (Old Cadastre)
Athinon Street
Limassol

Τεχνολογικό Πανεπιστήμιο Κύπρου
Δωμάτιο: ΚΧΕ 1 -  Εργαστήριο Ηλεκτρονικών Υπολογιστών
Κτήριο Πολυξένη Λοϊζία και Ελένη Αυτονόμου (Παλιό Κτηματολόγιο)
Οδός Αθηνών
Λεμεσός

Date and Time:

23 February 2017
From: 19:30 EET
To: 23:30 EET

Free Amenities Offered

High speed Internet access
Wi-Fi access to the Internet for your mobile devices (personal computers and smart phones)
Lab computers will be available for use by the participants
Food in the form of snacks and beverages will be available outside the labs


C/C++: Full example using a linked list with custom struct

The following code is an example that presents some basic functionality on simply linked lists. Specifically, it presents how to add an element to an empty list and then how to add more elements either to the start (prepend) or to the end (append) of the list.

[download id=”2444″]

We assume that our structure holds an integer and a dynamically created string, which we free after pop.

[download id=”2444″]

 

Source file (list_helpers.c)

#include <malloc.h>
#include "list_helpers.h"

void append(node_t **head, element_t *element) {

  struct node_t *new = malloc(sizeof(node_t));
  new->element = element;
  new->next = NULL;

  if (*head == NULL) {
    *head = new;
    return;
  }

  struct node_t *current = *head;

  while (current->next != NULL) {
    current = current->next;
  }

  current->next = new;
  return;
}

void prepend(node_t **head, element_t *element) {
  struct node_t *new = malloc(sizeof(node_t));

  new->element = element;
  new->next = *head;
  *head = new;
}

element_t *pop(node_t **head) {

  node_t *next = NULL;

  if (*head == NULL) {
    return NULL;
  }

  next = (*head)->next;
  element_t *element = (*head)->element;
  free(*head);
  *head = next;

  return element;
}

void clear(node_t **head) {
  element_t *current = pop(head);
  while (current != NULL) {
    free(current->username);
    free(current);
    current = pop(head);
  }
}

Header file (list_helpers.h)

#ifndef GM_S_LITTLE_HELPERS_LIST_HELPERS_H
#define GM_S_LITTLE_HELPERS_LIST_HELPERS_H

#ifdef __cplusplus
extern "C" {
#endif

typedef struct element_t element_t;
struct element_t {
  //We add random members to the element struct for the sake of the example
  char *username;
  unsigned int server;
};

typedef struct node_t node_t;
struct node_t {
  element_t *element;
  node_t *next;
};

void append(node_t **head, element_t *element);

void prepend(node_t **head, element_t *element);

element_t *pop(node_t **head);

void clear(node_t **head);

#ifdef __cplusplus
}
#endif

#endif //GM_S_LITTLE_HELPERS_LIST_HELPERS_H

Usage example (main.cpp)

#include <iostream>
#include "list_helpers.h"

element_t *create_user(const unsigned int server, const char *username) {

  element_t *user = (element_t *) malloc(sizeof(element_t));
  user->server = server;

  //For the sake of the example we used snprintf.
  //Upon successful return, snprintf returns the number of characters printed (excluding the null byte used to end output to strings).
  //For that reason we add one at the end of the length.
  const int length = snprintf(NULL, 0, "%s", username) + 1;
  user->username = (char *) malloc((sizeof(char) * length));
  snprintf(user->username, (size_t) length, "%s", username);
  return user;
}

int main(int argc, char *argv[]) {

  node_t *head = NULL;

  //Add the first element to the linked list
  append(&head, create_user(10, "xeirwn"));

  //Add the second element to the end of the linked list
  append(&head, create_user(12, "test"));

  //Add the third element to the end of the linked list
  append(&head, create_user(14, "banana"));

  //Add the fourth element to the beginning of the linked list
  prepend(&head, create_user(8, "apple"));

  //Popping each one to process it and then free it
  //Clearing the list
  element_t *current = pop(&head);
  while (current != NULL) {

    printf("%s\t%u\n", current->username, current->server);
    free(current->username);
    free(current);
    current = pop(&head);
  }

  //Safely clear the list. In this specific scenario it will have 0 side effects as the list was cleared above
  clear(&head);
  return 0;
}

[download id=”2444″]


C/C++: Full example of using C code in a C++ project

The following set of code present a fully functioning example of using a simple C library as part of a CPP based project to print on screen.

[download id=”2434″]

The trick relies on encapsulating the C header definitions in the extern "C" declaration. extern "C" will make all function and variable names in C++ have C linkage. What this means at the compiler level is that the compiler will not modify the names so that the C code can link to them and use them using a C compatible header file containing just the declarations of your functions and variables.

[download id=”2434″]

main.c

#include "cpp_library.h"
#include "c_library.h"

extern "C" void c_hello_world();

int main() {

    cpp_hello_world();
    c_hello_world();
    return 0;
}

cpp_library.h

#ifndef CPP_BASE_CPP_LIBRARY_H
#define CPP_BASE_CPP_LIBRARY_H

void cpp_hello_world();

#endif //CPP_BASE_CPP_LIBRARY_H

cpp_library.cpp

#include <iostream>
#include "cpp_library.h"

void cpp_hello_world() {

    std::cout << "Hello, World!" << std::endl;
}

c_library.h

#ifndef CPP_BASE_C_LIBRARY_H
#define CPP_BASE_C_LIBRARY_H

#ifdef __cplusplus
extern "C" {
#endif

void c_hello_world();

#ifdef __cplusplus
}
#endif

#endif //CPP_BASE_C_LIBRARY_H

c_library.c

#include <stdio.h>
#include "c_library.h"

void c_hello_world() {

    printf("Hello, World!\n");
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.6)
project(CPP_Base)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")

set(SOURCE_FILES main.cpp cpp_library.cpp cpp_library.h c_library.c c_library.h)
add_executable(CPP_Base ${SOURCE_FILES})

[download id=”2434″]


C/C++: Full example of using C++ code in a C project

The following set of code present a fully functioning example of using a simple CPP library as part of a C based project to print on screen.

[download id=”2428″]

The trick relies on encapsulating the CPP header definitions in the extern "C" declaration. extern "C" will make all function and variable names in C++ have C linkage. What this means at the compiler level is that the compiler will not modify the names so that the C code can link to them and use them using a C compatible header file containing just the declarations of your functions and variables.

[download id=”2428″]

 

main.c

#include "cpp_library.h"
#include "c_library.h"

int main() {

    cpp_hello_world();
    c_hello_world();
    return 0;
}

cpp_library.h

#ifndef C_BASE_CPP_LIBRARY_H
#define C_BASE_CPP_LIBRARY_H

#ifdef __cplusplus
extern "C" {
#endif

void cpp_hello_world();

#ifdef __cplusplus
}
#endif

#endif //C_BASE_CPP_LIBRARY_H

cpp_library.cpp

#include <iostream>
#include "cpp_library.h"

void cpp_hello_world() {

    std::cout << "Hello, World!" << std::endl;
}

c_library.h

#ifndef C_BASE_C_LIBRARY_H
#define C_BASE_C_LIBRARY_H

void c_hello_world();

#endif //C_BASE_C_LIBRARY_H

c_library.c

#include <stdio.h>
#include "c_library.h"

void c_hello_world() {

    printf("Hello, World!\n");
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.6)
project(C_Base)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")

set(SOURCE_FILES main.c cpp_library.cpp cpp_library.h c_library.c c_library.h)
add_executable(C_Base ${SOURCE_FILES})

[download id=”2428″]