Home


Lesson 2

Statical / dynamic arrays, IO

Arrays: how to store multiple values?

Task 0

Change question marks into proper c++ statements.


#include <iostream>

using std::cout;
using std::endl;

int main()
{
    int x = 10;

    cout << "address of x: " << ? << endl;
    cout << "show value of x using its address: " << ? << endl;
}
Task 1

Find new language elements. Can we write "*addr.city"? What about "->"?


#include <iostream>
#include <string>

struct house
{
    std::string city;
    std::string street;
    int number;
};

void buyingOnline(house *addr)
{
    std::cout << (*addr).city   << std::endl
              << (*addr).street << std::endl
              << (*addr).number << std::endl;
}

int main()
{
    house home;
    home.city = "Warsaw";
    home.street = "Koszykowa";
    home.number = 86;

    buyingOnline(&home);
}
Task 2

Which definition of a list element is correct? Why?


// A
struct item_t
{
    item_t next;
};
                    

// B
struct item_t
{
    item_t* next;
};
                    
Task 3

Write a method, which will return a number from range [1,100]. Use the code below and % operator.

Check, how programs changes on srand method invoking.


#include <cstdlib>
#include <iostream>
#include <ctime>

using std::srand;
using std::time;
using std::rand;

int main()
{
    // setting global seed
    srand(time(nullptr));
    // randomizing in range: [0,RAND_MAX]
    std::cout << "[0, " << RAND_MAX << "]: " << rand() << std::endl;
}
            
Task 4

Analyse code written below. Describe all new elements.


#include <iostream>
#include <cstdlib>
using std::cout;
using std::endl;
using std::cin;

int main()
{
    int size;
    cout << "Type a size for the array: ";
    cin >> size;

    int *arr = new int[size];

    for (int i = 0; i < size; ++i)
    {
        cin >> arr[i];
    }

    delete[] arr;
}
    

Tasks for you

Example: using static arrays

// creating statical array
int integers[40];
// assigning values
integers[0] = 10;
// reading value
int last = integers[39];
                
Task 5

Create an integer array, which size will be determined by a preprocessor constant.

Assign value of each element as value of cell's index.


#include <iostream>
#define constant_size 10

int main()
{
    std::cout << constant_size << std::endl;
}
            
Task 6

Check C++ array access mechanism:

  1. read a value outside bounds of the array
  2. read uninitialized value of array element
Task 7
  1. Create a new variable called "px". It should refer to a variable "x".
  2. Using "px" increase value of "x" by 3.
  3. Make "px" refering to "y".
  4. Increase value of "px".
  5. Try to increase the value, which "px" refers.
  6. Describe mechanisms of pointners operating.

#include <iostream>

using std::cout;
using std::endl;

int main()
{
    int x = 10;
    int y = 20;
}
Task 8
  1. Create two methods. One should take "int" as an argument, the second "int*".
  2. Each method should increase values of each given argument.
  3. Call methods using one variable in main's body.
  4. Describe the difference between two methods.
Task 9
  1. Display the value of *arr. What it relates to?
  2. Modify the code from task 4 to avoid using "[ ]" braces to access the array.
Task 10

Analyse the code written below. One of the statements is invalid. One is missing. Comment the wrong line.


    #include <iostream>
    #include <string>
    #include <cstdlib>
    
    using std::cout;
    using std::endl;
    using std::cin;
    
    struct house
    {
        std::string city;
        std::string street;
        int number;
    };
    
    int main()
    {
        house *h = new house();
        
        *h.city = "Warsaw";
        (*h).street = "Koszykowa";
        h->number = 86;
    }
        
Task 11

There is a 3D point structure (x,y,z). Write a program which:

  1. Try to create a statical array of 3D points with size given by user.
  2. Fulfill the array with random values.
  3. Save results to a file with extension csv.

Try decomposite your program with writing own methods.


#include <iostream>
#include <fstream>
#include <cstdlib>

int main()
{
    std::fstream file;
    file.open("results.csv", std::ios::out);
    if (!file.good()) {
        std::cout << "Error" << std::endl;
        return 1;
    }
    file << "lorem ipsum" << std::endl;
    file.close();
    std::cout << "Success" << std::endl;
}
            
Mode Meaning
in Opening a file for reading
out Opening a file for writing
binary File won't be load as text.
app New data will be append at the end of a file.
trunc Truncating content of file at the beginning
Task 12

Create own database for one entity.

  1. Application should ask user, if he wants execute one of the action:

    • Adding a record
    • Deleting a record
    • Override a record
    • Read a record (by id)
  2. Database should be stored in RAM.
  3. After each modification of data, database should store all changes in a file.