Home c++ Working with C++ arrays

Working with C++ arrays

Author

Date

Category

Tell me please how to form an array of positive from the already existing array, exactly how to work with the dimension of the new array, if the rule of the dimension should be fixed, but if the initial array is changed, the number of positive numbers may vary.


Answer 1, Authority 100%

  1. You can use a static array in which we work with the first elements

    # include & lt; iostream & gt;
    #Include & lt; String & GT;
    #Include & lt; sstream & gt;
    #Include & lt; vector & gt;
    #Define Max 1000.
    Using Namespace STD;
    INT MAIN () {
    INT ARRAY1 [MAX];
    INT ARRAY2 [MAX];
    int size1 = 30;
    INT SIZE2 = 0;
    // Form the first array and display on the screen
    For (int i = 0; i & lt; size1; i ++) {
      Array1 [i] = -50 + Rand ()% 100;
      COUT & LT; & LT; Array1 [i] & lt; & lt; "";
    }
    COUT & LT; & LT; Endl;
    // Form the second array
    For (int i = 0; i & lt; size1; i ++) {
      If (Array1 [i] & gt; 0) {
        Array2 [Size2] = Array1 [i];
        Size2 ++;
      }
    }
    // Display the second array
    For (int i = 0; i & lt; size2; i ++) {
      COUT & LT; & LT; Array2 [i] & lt; & lt; "";
    }
    COUT & LT; & LT; Endl;
    Return 0;
    }
    
  2. or use container vector

    vector & lt; int & gt; MAS1;
    Vector & lt; int & gt; MAS2;
    For (int i = 0; i & lt; size1; i ++) {
      Mas1.Push_back (-50 + Rand ()% 100);
      COUT & LT; & LT; MAS1 [i] & lt; & lt; "";
    }
    COUT & LT; & LT; Endl;
    For (int i = 0; i & lt; mas1.size (); i ++) {
      If (MAS1 [I] & GT; 0) {
        mas2.push_back (MAS1 [I]);
      }
    }
    

Answer 2

To begin with, I will still figure out what is the dynamic memory area and how data is stored there. You probably used to use a simple array with a fixed number of objects that store objects. But there are “dynamic arrays” that are stored in a heap. If you know what pointers are, then it will not be difficult for you to figure it out.

Here is an example of a dynamic array:

int * array; // pointer to the data type int
array = new int [5] {1,5,3,6,2}; // Selecting memory for an array to 5 objects {1,5,3,6,2}
// Now the pointer contains the address of this array and you can work with it, as with an ordinary array.
STD :: COUT & LT; & LT; array [1] & lt; & lt; STD :: ENDL; // Output: 5
array [2] = 99;
STD :: COUT & LT; & LT; Array [2] & lt; & lt; STD :: ENDL; // Output: 99
Delete [] Array; // But when working with an array, you need to clean my memory
// After cleaning the memory, Array pointer stores the address, but on an empty memory area
// and we can continue to work
array = new int [2] {1,5}; // Create an array to 2 objects
// What if we want to add another object? - Please
int * temp = new int [3];
For (int i & lt; 2)
{
  TEMP [i] = Array [i];
}
TEMP [2] = 888;
Delete [] Array; // Repeat Memory again
Array = Temp; // We say that our pointer will know about our new array.
// and then you can work with an array just as before
STD :: COUT & LT; & LT; Array [2] & lt; & lt; STD :: ENDL; // Output: 888

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions