Do not use vectors or linked lists Accepts array size from the keyboard. Size mu
ID: 3815315 • Letter: D
Question
Do not use vectors or linked lists Accepts array size from the keyboard. Size must be a positive integer that is 4. Use the size from step 1 in order to create an integer array. Populate the created array with random integer values between 25 and 500 inclusive Display the generated array. Recursive Table of Squares Write a function that uses recursion in order to display squares of integers in ascending order, starting from 1 to last number in the array. Recursive Power Function Write a function that uses recursion to raise a number to a power. The function should take two arguments, the number to be raised to the power is the first number in array and the power is 2. Recursive sumofSquares function Write a function named sumofSquares that return the sums of squares of the numbers from 0 to a Array of SIZE 2 Do not use global variable
Explanation / Answer
#include <iostream>
#include <stdlib.h>
#include <math.h>
using namespace std;
#define MAX_SIZE 10000
unsigned int create_array(unsigned int*** arr_add) {
unsigned int size = 0;
unsigned int* arr = NULL;
cout<< "Enter array size";
cin >> size;
while (size <= 4 || size > MAX_SIZE) {
cout <<"wrong input(size should be gerater than 4 and hsould be less than 10000)";
cin>> size;
}
//allocate space
arr = new unsigned int[size];
if (NULL == arr) {
cout<<" memory allocation error";
exit(1);
}
//fill array
srand(time(NULL));
for(int i=0;i<size;i++) {
arr[i] = rand() % 476 + 25;
}
*arr_add = &arr;
return size;
}
void display(unsigned int* arr, unsigned int size) {
cout <<" Contents of array are..";
for(int i=0;i<size;i++)
cout<<" "<<arr[i];
}
void print_squares(unsigned int* arr,unsigned int last,unsigned int index) {
if (index > last)
return;
else {
cout<<" "<<arr[index]<<" : "<<arr[index] * arr[index];
print_squares(arr,last,index+1);
}
}
void print_power(unsigned int* start,unsigned int* end) {
if (start == end) {
cout << " " << *end <<" : "<<pow(2,*end);
}
else {
cout << " " << *start <<" : "<<pow(2,*start);
print_power(start+1,end);
}
}
long unsigned int sum_of_sq(unsigned int* arr,unsigned int last,unsigned int index) {
long unsigned int sum = 0;
if (index <= last) {
sum = sum + (arr[index] * arr[index]) + sum_of_sq(arr,last,index+1);
} else
return sum;
}
int main()
{
unsigned int size = 0;
unsigned int** arr_add = NULL;
unsigned int* arr = NULL;
size = create_array(&arr_add);
arr = *arr_add;
display(arr,size);
cout<<" SQUARES OF NUMBERS IN ARRAY"<<endl;
print_squares(arr,size-1,0);
cout<<" ELEMENTS OF ARRAY RAISED TO POWER 2"<<endl;
print_power(arr,arr+size-1);
cout<<" SUM OF SQUARES OF NUMBERS OF ARRAY is"<<endl;
cout<<sum_of_sq(arr,size-2,0)<<endl;
return 0;
}
Please let me know in case of any doubts.
Thanks
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.