Good day. There is a code below that needs to be redone using pointers. Those. I understand what is written here, but it is not in my power to alter it under signposts. I tried to do it on my own, but it turned out completely wrong. Please help.

#include <iostream> #include <stdlib.h> using namespace std; int main() { system("color F0"); int first = 10, second = 10, summ, temp, i, array1[10]; for (i = 0; i < 10; i++) array1[i] = rand() % 10 - 5; for (i = 0; i < 10; i++) cout << endl << array1[i]; if (array1[0] > 0) summ = array1[0]; else summ = -array1[0]; for (i = 1; i < 10; i++) { if (array1[i] >= 0) temp = array1[i]; else temp = -array1[i]; if (summ < temp) summ = temp; } cout << endl; cout << "Max element: " << summ; i = -1; do { i++; if (array1[i] > 0) first = i; } while ((array1[i] <= 0) && (i < 10)); do { i++; if (array1[i] > 0) second = i; } while ((array1[i] <= 0) && (i < 10)); if ((second == 10) || (first == 10)) cout << "-----" << endl; else { summ = 0; for (i = first; i <= second; i++) summ = summ + array1[i]; cout << endl; cout << "Sum: " << summ; } } 
  • std :: vector cannot be used? - KoVadim
  • replace all 10s with a constant to start with - vp_arth
  • Sorry, incorrectly wrote the task. Already fixed. It is necessary to alter this code using pointers. Already head spin comes from these arrays. - Boredix

1 answer 1

In your case, essentially all you need to do is replace

 int first=10, second=10,summ,temp,i, array1[10]; 

on

 int first=10, second=10,summ,temp,i, *array1 = new int[10]; 

and add before the last brace

 delete [] array1; 

As such, a dynamic array — i.e. array that changes its size - you do not need. It simply uses an array in dynamic memory ...

  • "you do not need" - but the question was about what was needed. Probably need a size to request from the user? And then change in some way? о.О - vp_arth
  • Sorry, incorrectly wrote the task. Already fixed. It is necessary to alter this code using pointers. Already head spin comes from these arrays. - Boredix
  • But with pointers - the most it. Unless, if you really want, you can still replace array1[i] with *(array1+i) - Harry
  • Thanks, all worked well. I did not think it would be so easy. :) - Boredix