Statical / dynamic arrays, IO
Arrays: how to store multiple values?
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;
}
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);
}
Which definition of a list element is correct? Why?
// A
struct item_t
{
item_t next;
};
// B
struct item_t
{
item_t* next;
};
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;
}
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;
}
// creating statical array
int integers[40];
// assigning values
integers[0] = 10;
// reading value
int last = integers[39];
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;
}
Check C++ array access mechanism:
#include <iostream>
using std::cout;
using std::endl;
int main()
{
int x = 10;
int y = 20;
}
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;
}
There is a 3D point structure (x,y,z). Write a program which:
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 |
Create own database for one entity.
Application should ask user, if he wants execute one of the action: