Untitled
user_9831585
plain_text
2 years ago
3.1 kB
7
Indexable
#include <iostream> void initialization1(); int ress_sum(int* arr , int size); int main() { initialization1(); return 0; } void initialization1() { int* arr = NULL;//Initialize an array from pointer type to int to null int size = 0;//Initialize an size from type to int to 0 std::cout<<"Enter size for input numbers in arr -> "<<std::endl;//A request from the user to enter the desired size to insert the numbers into the array std::cin>>size;//Reception of the size according to the number entered by the user arr = new int[size];//A request from the operating system to bring us memory for an int type array of the requested size if (!arr)//Checking whether the operating system was able to assign us the space we requested { std::cout<<"Eroor -> Out Memory"<<std::endl;//Notification to the user that there is not enough memory and there is an error return;// } std::cout<<"Enter your numbers for arr -> "<<std::endl;//A request from the user to insert the numbers he wants into the array for (int i = 0; i < size; ++ i)//Run in a loop until the size entered by the user { std::cin>>arr[i];//Pick up the numbers that the user entered into the array } int result = ress_sum(arr,size);//The array and size are sent to the function when the result is a variable of the appropriate type that receives the result if (result == 0)//Checking whether the array is empty, meaning the result is 0 { std::cout<<"No numbers in arr -> " <<result<<std::endl;//Printing to a user that is not in the number array return; } if (result % 2 == 0)//Checking whether the sum of the numbers in an even array is even { std::cout<<"The sum in arr is -> even "<<result<<std::endl;//Printing to the user that the sum of the numbers in an even array and printing the sum } else std::cout<<"The sum in arr is -> odd "<<result<<std::endl;//f the sum is not even, then it is odd, therefore the use of else and printing to the user that the sum is odd and printing the sum delete[] arr;//Freeing the memory from the operating system because we no longer use it } int ress_sum(int* arr , int size) { int sum = 0;//Defining a sum variable of type int that will sum up the sum of the elements in the array when it is initialized to 0 if (size == 0)//Checking whether the size of the array we received is 0, an empty array { return 0; } if (size == 1)//Checking whether the array we received in one size has the number 1 in it { sum = arr[0];//Initializing the amount for the first element in the array } sum = arr[0];//Initializing the amount for the first element in the array return sum += ress_sum(arr + 1, size - 1);//Calling the function while advancing the array to the next member and reducing the size to the member before it because we guarantee to reach the stopping condition 0 using += operators which will calculate the sum of the array into a sum variable and return the sum to the function from which we sent the array and the size }
Editor is loading...