The specified array contains zero elements that divide the array into subarrays. Calculate the sum of the elements for each of the subarrays and display the result to the user in the format: {subarray elements 1}, sum1 = number; {elements of subarray 2}, sum = number ... 1) How to rebuild what elements of the array would be entered by the user? 2) It is desirable to make it so that the pointer * would be used.

#include <iostream> using namespace std; int main() { const int n(17); int arr[] = {1,4,3,0,8,9,0,1,3,2,0,7,7,7,7,0,1},counter(0); for(int i = 0;i<n;++i){ cout << counter++ << " : "; int sum(0); while(arr[i] && i<n){ cout << arr[i] << ' '; sum += arr[i++]; } cout << "\n" << sum << "\n\n"; } cout << endl; return 0; } 

Closed due to the fact that off-topic participants yrHeTaTeJlb , nick_n_a , αλεχολυτ , AK , Let's say Pie 29 Oct '18 at 22:08 .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:

  • " Learning tasks are allowed as questions only on the condition that you tried to solve them yourself before asking a question . Please edit the question and indicate what caused you difficulties in solving the problem. For example, give the code you wrote, trying to solve the problem "- yrHeTaTeJlb, nick_n_a, αλεχολυτ, Let's say Pie
If the question can be reformulated according to the rules set out in the certificate , edit it .

  • one
    Rule of thumb: If you have no question mark in the question, most likely it is not a question, but a task. Tasks here usually do not - yrHeTaTeJlb

2 answers 2

  #include <iostream> using namespace std; int main(void) { int size = 0; int sum = 0; int *array = NULL; cin >> size; array = new int [size]; for(int i = 0; i < size; i++) cin >> *(array + i); for(int i = 0; i < size; i++) { if(*(array + i) != 0) { cout << *(array + i) << "\t"; sum += *(array + i); }else { cout << "Number:" << sum; sum = 0; } } return 0; } 

It seems so, maybe some kind of error will be given for writing on the phone))

    Should zero elements be placed in exactly this order, or does their location depend on the user? In order to use an array through a pointer, you need to use the dereference operator "*", that is: int arr[5]; for(int i = 0; i < 5; i++); cin >> *(arr + i); int arr[5]; for(int i = 0; i < 5; i++); cin >> *(arr + i); since the name of our array points to the address of the first element of the same array, then using the dereference operation * (arr) we will get the value of the array at this address. And considering that when declaring an array of a certain size, we select a sequence of cells to store values ​​of the specified type, we can sequentially move from the address of the first cell to the following using the expression: "arr + n", where arr is a pointer to the first address, and n is the number of cells to which we want to move.