Computer Science, asked by harsh12012001, 1 month ago

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

Answered by ravipandey9
1

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;

}

Answered by Anonymous
0

 \star\bold\red{answer} \star

#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;

}

Similar questions