Basics of C/C++ Programming: Difference between revisions
Line 141: | Line 141: | ||
====== Output ====== | ====== Output ====== | ||
The program should output the | 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. | Output should be printed as a comma separated list of values on a single line to stdout. |
Revision as of 01:20, 4 October 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
Hint:
Hint:
Hint:
Hint:
Practice 3
File input/output, dynamic memory allocation
Hint:
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;
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 1
Console input/output, strings, references, arrays, new/delete
Example:
- Ask for the number of strings the user will enter,
- Store all input strings in in an appropriately sized array,
- Use a function to count the total number of characters entered;
Example: