Basics of C/C++ Programming: Difference between revisions

From ICO wiki
Jump to navigationJump to search
Jtominga (talk | contribs)
Jtominga (talk | contribs)
Line 4: Line 4:
=== Lecturers ===
=== Lecturers ===
Rait Liiv: rait.liiv@itcollege.ee
Rait Liiv: rait.liiv@itcollege.ee
Janno Tomingas: janno.tomingas@itcollege.ee
Janno Tomingas: janno.tomingas@itcollege.ee



Revision as of 01:41, 20 September 2016

Introduction

Lecturers

Rait Liiv: rait.liiv@itcollege.ee

Janno Tomingas: janno.tomingas@itcollege.ee

C

Lectures

Practices

Practice 1

Console input/output


1. Write a program, that outputs "Hello, World!"

2. Write a program, that asks the user for a number and then writes the number back to the user

3. Write a simple calculator, that supports addition, subtraction, multiplication and division. The program should ask the user for two numbers and the operation to perform and output the result to the console.

Practice 2

Functions, pointers, out variables, function return values as success/error codes


1. Write a function that adds two integers, outputs the result via an out variable and returns 0 to indicate a successful operation.

Hint:


2. Write a function that swaps two integers and returns 0 to indicate a successful operation.

Hint:


3. Write a function that reverses a string and returns 0 to indicate a successful operation.

Hint:


4. Write a function that sorts an array of ints and returns 0 to indicate a successful operation.

Hint:


Practice 3

File input/output, dynamic memory allocation


1. Write a program that outputs the contents of a text file into the console.

Hint:


2. Write a program that reads the contents of a text file into a dynamically sized array and outputs the information into a new file.

Hint:


Practice 4

Dynamic memory allocation continued, structs, .h/.c files


1. Create an implementation of a stack data structure (Stack (abstract data type)) that uses a dynamically allocated array for storage.

  • Required functions:
    • init - Initializes stack's variables and allocates required dynamic memory with an initial(default) size,
    • destroy - Frees all allocated memory and optionally resets other stored information for a stack,
    • push - Adds an element to the top of the stack,
    • peek - Gets the top element of the stack,
    • pop - Gets and removes the top element of the stack;


  • Describe the stack struct and functions in a separate .h file,
  • Implement the functions in a corresponding .c file,
  • Test your stack with some operations. Remember to check for edge cases;
Hints: