Answer the question
In order to leave comments, you need to log in
How to add two more elements to four array elements?
in the commented line....
//добавить два элемента к массиву и посчитать кол-во элементов
#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
int rez;
int arr[] = {1,8,3,2};
//arr[] += {7,8}; ????
rez = sizeof(arr) / 4; //кол-во элементов в массиве
cout << rez << endl;
for (int i=0; i<=rez; i++)
cout << arr[i] << ' ';
getch();
return 0;
}
Answer the question
In order to leave comments, you need to log in
Use std::vector.
#include <iostream>
#include <conio.h>
#include <vector>
using namespace std;
int main()
{
int rez;
int arr_src[] = {1,8,3,2};
std::vector<int> arr(arr_src, arr_src + sizeof(arr_src)/sizeof(arr_src[0]));
arr.push_back(7);
arr.push_back(8);
rez = arr.size(); //кол-во элементов в массиве
cout << rez << endl;
for (int i = 0; i < rez; i++)
cout << arr[i] << ' ';
getch();
return 0;
}
You need either an array with a margin (for example, for 6 places), or a dynamic one (for example, std::vector).
C++ is a rather low-level language, and the programmer himself can see where memory is dynamically allocated and where it is allocated in advance.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question