Basics of C/C++ Programming

From ICO wiki
Jump to navigationJump to search

Introduction

Lecturers

Rait Liiv: rait.liiv@itcollege.ee

Janno Tomingas: janno.tomingas@itcollege.ee


Video recordings of lectures and practices

https://echo360.org.uk/section/d5161b62-4e7a-4b54-9578-10d86353e3d0/home


Code from lectures

http://enos.itcollege.ee/~rliiv/

C

Lectures

Recordings of lectures and practices:

https://echo360.org.uk/section/d5161b62-4e7a-4b54-9578-10d86353e3d0/home

Code:

http://enos.itcollege.ee/~rliiv/

Practices

Practice 1

Console input/output, conditions, loops, pointers, arrays, functions

Practice 2

File input/output, dynamic memory allocation, structs, .h/.c files


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

Additional info:

Always close the file stream after you are done using it.

Make sure to check for error codes.

2. Write a program that dynamically allocates a small array of ints. Fill the array and print its contents. Free the memory after you're done using it.

3. Write a program that reads the contents of a whole text file into a dynamically sized array of characters (and outputs the information into a new file).

Additional info:

Use "fseek", "ftell" and "rewind" to find out how large the buffer should be.

Make sure you have enough room for a null terminator. (You may have to assign it to the end of the array yourself)

Remember to free the allocated memory after calling malloc or calloc.

Make sure to check for error codes.

4. 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:
  • How do you know which element to access?
  • How do you know if an array is full?
  • How should the functions behave when a stack is empty?
  • How should the functions behave when a stack is full?
  • What will happen if any of the functions are called many times or in unexpected order?

5. Write a program that reads an unknown number of integers (each on a separate line) from a file into the stack you created. After reading the whole file, print out all ints that were read (in reverse order).

Practice 3 (first half)

insertion sort, sorting structs

1. Implement insertion sort to sort an array of integers in ascending order.

2. Create a struct to hold integral key-value pairs. Modify the insertion sort algorithm to sort an array of these structs instead.

Assignment

Write a program that reads values from a file and outputs the result to stdout.

Input

Input file's name is "input.txt".

The file contains index-value pairs of integers on a single line. Integers in a pair are separated with a comma (,) and pairs are separated with a semicolon (;).

All indices between 0 and (count_of_pairs - 1) are guaranteed to be present.

The indices are guaranteed to be unique.

The indices are not guaranteed to appear in an ascending order.

Sample input: 1,4;0,2;2,8

Output

The program should output the differences between consecutive values as the index increases.

Output should be printed as a comma separated list of values on a single line to stdout.

For input:

0,1;2,4;1,2

Calculates:

0,1 to 1,2 -> 2 - 1 = 1
1,2 to 2,4 -> 4 - 2 = 2

And outputs:

1,2
Examples
->
0,2 -> 0
1,2;0,1 -> 1
1,4;0,2;2,8 -> 2,4
1,4;0,2;2,-2 -> 2,-6

C++

Lectures

Practices

Practice 3 (second half)

C++ console i/o, std::string, std::vector, references, range based for, auto

1. Write a Hello, World! program in C++

2. Write a program that performs an addition operation based on user input. A sample input string: "2+2".

3. Write a program that asks the user for his name, stores it in an std::string and outputs the name back to the console window.

4. Write a program that asks the user for several strings and stores them in an std::vector. Upon receiving the last string, the program should output all strings in the order they were received.

5. Modify the last program by adding a function to modify a string by reference. Use that function on all input strings.

6. Modify the last program to output the strings using a range based for loop. Bind the values to an auto&.

Practice 4

file i/o, command line parameters, new/delete, classes, constructors/destructors, smart pointers

1. Read strings from a file into a std::vector<std::string>, then print them out to another file.

2. Modify the last program to read two file names from the command line and use these file names for files.

3. Create a class that logs its creation and destruction via std::cout. Use a custom defined constructor and destructor for that. Observe how actions are logged in different scenarios of instance creation and destruction, including smart pointers.

Assignment

Exploits

Lectures

Practices

Assignment

History

Basics_of_C/C++_Programming 2016