Write a program to count the palindromic array elements from the group of 30 array elements where array is allocated memory dynamically. (in C++)
Answers
Explanation:
#include <iostream>
#include <random>
using namespace std;
int main()
{
int* A = new int[30];
srand(10);
for (int i = 0; i < 30; i++)
{
A[i] = rand() % 5;
}
int count = 0;
for (int i = 0; i < 30 / 2; i++)
{
if (A[i] == A[29 - i])
{
cout << count + 1 << " palindromic elements in array: " << A[i] << ". Position: " << i << " and " << 29-i << endl;
count++;
}
}
cout << endl;
cout << "Total count of palindromic array elements is: " << count << endl;
delete[] A;
return 0;
}
#include <iostream>
#include <ctime>
#include <random>
#include <string>
using namespace std;
int main()
{
string str;
int* Array = new int[30];
srand(time(0));
for (int i = 0; i < 30; i++)
Array[i] = rand() % 6;
int count = 0;
cout << "[";
for (int i = 0; i < 30 / 2; i++)
{
if (Array[i] == Array[29 - i])
{
cout << Array[i];
count++;
str += to_string(Array[i]);
continue;
}
cout << '.';
str += '.';
}
for (int i = str.size() - 1; i >= 0; i--)
{
cout << str[i];
}
cout << "]";
cout << endl << endl;
cout << "Count the palindromic array elements is: " << count << endl;
delete[] Array;
return 0;
}