The basics of programming in c++ for beginners

Tasks and Solutions: Pointers C ++

Тему Указатели в C мы рассмотрели в двух статьях:

–  Part 1: C++ Pointers

– Part 2: C++ Pointers

Теперь вам придется много практиковаться, чтобы закрепить знания и до конца разобраться. Решим несколько задач для начала.

1.  The task of the elementary, but it requires a good understanding of what pointers. Dan pointer: double **p = 0; Perform the following tasks (solutions can execute within the function main):
* create a design, shown in Figure;
* print number, specified in the box, the screen;
* then delete all dynamic objects.

with the solution of the problem, C ++ pointers

Yet again – We are looking for the shortest solution. Author:  

2. Объявите указатель на массив типа double и предложите пользователю выбрать его размер. Далее напишите четыре functions: первая должна выделить память для массива, second– заполнить ячейки данными, третья – показать данные на экран, четвертая – освободить занимаемую память.  The program should prompt the user to continue working (создавать новые динамические массивы ) or withdraw from the program.

3. Объявите указатель на массив типа int и выделите память память для 12-ти элементов. It is necessary to write a function, которая поменяет значения четных и нечетных ячеек массива. Например есть массив из 4-х элементов:

задачи и решения указатели c++

задачи и решения указатели c++

4.  Declare and populate a two-dimensional array of dynamic random numbers from 10 to 50. Показать его на экран. Для заполнения и показа на экран написать отдельные функции. (подсказка: функции должны принимать три параметра – указатель на динамический массив, количество строк, количество столбцов).  The number of rows and columns the user selects.

40 thoughts on “Tasks and Solutions: Pointers C ++

  1. well come?

    1. Deal with someone else's code is much more complicated and disgusting, you write your (especially, if the editor of this site distorts your code).
      The answer to the question “well come?” always look for compiling and running your code, better on a different set of input data.

  2. Maybe in the third problem instead of adding a variable swap without the addition of a variable?
    Arr[i] += Arr[i + 1];
    Arr[i + 1] = Arr[i] – pArr[i + 1];
    Arr[i] -= Arr[i + 1];
    What's better: the creation of a variable or about such operations?

  3. Function “void swapDataInCells(int* ptrArr, int sizeOfArr)” to replace the elements in the third task.
    In the loop “for (int i = 0; i < sizeOfArr; i )" you need to take away from the unit sizeOfArr:
    for (int i = 0; i < sizeOfArr – 1; i ).

      1. nonsense… for loop (), expression i < sizeOfArr shows to which value varies i (the index). Отняв единицу вы "уменьшаете" number of searched index array elements, but not the array itself

  4. 3.

  5. 4.

  6. Hello! elementary wrote a program to shift elements in the array to the left, the number of e-s – n is given by, displacement d is also defined by step, but the problem, d that the last element is not clear what happens, tell me what is the problem?!
    #include
    #include
    #include

    using namespace std;

    void zaz(int s, int * on);
    int main(){
    int n,d;
    srand(time(0));
    cout <> n;
    int *p = new int[n];
    int *z = new int[n];

    for (int i = 0; i < n; i ){
    p[i] = rand() %100 ;
    cout << p[i] << " ";
    }
    z = p;

    cout <> d;
    for (int i = 0; i < n; i ){
    p[i] = p[i + d];
    if (i == n – d)
    for (int k = 0; i < d; i ){
    p[k+n-d] = z[k];
    }
    }
    cout << p[10];
    //ZAZ(n, p);
    cout << endl;
    system("pause");
    return 0;
    }

    void zaz(int s, int * on)
    {
    for (int i = 0; i < s; i ){
    cout << With[i] << " ";
    }

    }

  7. 3. Declare a pointer to an array of type int and allocate memory for 12 elements. It is necessary to write a function, которая поменяет значения четных и нечетных ячеек массива. Например есть массив из 4-х элементов:

  8. The third task the replacement of even and odd elements. I made by copying an array.

    int changeelements(int* arr,int*arr2, int size) //function replacement in the element
    {
    for (int i = 0; i < size; i )
    {
    if (i % 2 == 0)
    {
    arr2[i] = arr[i + 1];
    }
    else if(i%2!=0)
    {
    arr2[i] = arr[i – 1];
    }
    }

    return *arr2;
    }

    int fillarray(int* arr, int size) //fill an array function
    {
    srand(time(NULL));
    for (int i = 0; i < size; i )
    {
    arr[i] = rand() % 100;
    }
    return *arr;
    }
    void showarray(int* arr, int size) //array output function
    {
    cout << endl;
    for (int i = 0; i < size; i )
    {
    cout << arr[i] << " ";
    }
    }
    int main()
    {
    int size;
    cout << "Enter massive's size" <> size; //enter the size of the array
    cout << "Massive's size = " << size << endl;
    int* arr = new int[size];
    int* arr2 = new int[size]; //We created a second array,to copy elements
    fillarray(arr,size);
    showarray(arr, size);
    changeelements(arr,arr2, size);
    showarray(arr2, size);
    delete[]arr;
    delete[]arr2;
    }

    1. It's a task only for an odd number of elements

      using namespace std;
      int changeelements(int* arr,int*arr2, int size)
      {
      for (int i = 0; i < size; i )
      {
      if (i % 2 == 0 && i + 1 == size) // conditions are met, If an odd number of elements
      {
      arr2[i] = arr[i];
      }
      else if (i % 2 == 0)
      {
      arr2[i] = arr[i + 1];
      }
      else if(i%2!=0)
      {
      arr2[i] = arr[i – 1];
      }

      }

      return *arr2;
      }

      int fillarray(int* arr, int size) //fill an array function
      {
      srand(time(NULL));
      for (int i = 0; i < size; i )
      {
      arr[i] = rand() % 100;
      }
      return *arr;
      }
      void showarray(int* arr, int size)//array output function
      {
      cout << endl;
      for (int i = 0; i < size; i )
      {
      cout << arr[i] << " ";
      }
      }
      int main()
      {
      int size;
      cout << "Enter massive's size" <> size;
      cout << "Massive's size = " << size << endl;
      int* arr = new int[size];
      int* arr2 = new int[size];
      fillarray(arr,size);
      showarray(arr, size);
      changeelements(arr,arr2, size);
      showarray(arr2, size);
      delete[]arr;
      delete[]arr2;
      }

Leave a Reply

Your email address will not be published. Required fields are marked *