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

Descending date order.

https://echo360.org.uk/media/c002b483-d428-4249-842a-9e92fdebfce8/public

https://echo360.org.uk/media/7efb0957-f384-4eb1-85ad-dd713fe4d65d/public

https://echo360.org.uk/media/bba448ab-8a38-405c-b2a2-5353e227265f/public

https://echo360.org.uk/media/df9ae848-a582-4ba2-999a-8df18d3132c6/public

https://echo360.org.uk/media/02aa7ba3-4bcc-4ace-b676-fd04b58eeff8/public

https://echo360.org.uk/media/8c1485c5-61f3-4062-851d-916906503fd3/public

https://echo360.org.uk/media/2e7e5c6a-10af-4b8c-8303-2e6e4650a206/public

https://echo360.org.uk/media/3917eeef-a127-4338-85e5-207d692504b7/public

https://echo360.org.uk/media/54f0ef26-6ef2-4bc7-9637-c3cde6a27aaa/public


https://echo360.org.uk/media/e099b072-a8ab-4c4b-9c3e-532715cc2d7e/public

https://echo360.org.uk/media/6f39d546-5572-4cda-ad97-e61b968506ff/public

https://echo360.org.uk/media/c02ce02d-9e01-45dd-9f54-3be66a1dc2b2/public

https://echo360.org.uk/media/2cb53a4e-f2b7-44b7-9bd6-93d90b26043b/public

https://echo360.org.uk/media/d366f877-b4ae-4fcc-a90c-0d433772252a/public

https://echo360.org.uk/media/35cd1108-141b-4a1a-8fb2-63e5f3a5b766/public

https://echo360.org.uk/media/befc74e4-5873-4c34-a65f-84ebd00e3370/public

https://echo360.org.uk/media/c3a24815-2b5c-4e3c-8649-bebde59784a3/public

Code from lectures

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

C

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:

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:

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:

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++

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