Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write the following programs in C++ 1. Write a program to merge two integer arra

ID: 3758893 • Letter: W

Question

Write the following programs in C++

1. Write a program to merge two integer arrays into a third array with alternate elements from the first and second array respectively. Your program should do the following:
a. Input size of 2 arrays
b. Input the array elements
c. Write a loop structure to copy elements into a third array in an alternate fashion
d. Print the merged array
Note: If one array is extinguished, continue to copy the remaining elements of the other array
Example:
size1 = 3
size2 = 4
array1 = {2,3,5}
array2 = {1,8,3,0}
array3 = {2,1,3,8,5,3,0}
output 2138530

2. Write a program to check if a string is a palindrome. Print is a palindrome if yes and is not a palindrome if no. Your program must do the following:
a. Input a string into a character array
b. Write a loop structure to check if the string is a palindrome ignoring case of alphabet ­ treat uppercase and lowercase as same alphabet
c. Print the appropriate message using if­else
Note:A palindrome is a word that reads the same backwards.

Example1: char a[] = “Kayak”
output
Kayak is a palindrome

Example 2: char a[] = “Hello”
output
Hello is not a palindrome

Explanation / Answer

Merge two integer arrays:

#include <iostream.h>

void merge(int [], int, int [], int, int []);

int main() {
int a[100], b[100], m, n, c, sorted[200];

cout<<"Input number of elements in first array ";;
cin>>m;

cout<<"Enter Array elements : ";;
for (c = 0; c < m; c++) {
cin>>a[i];
}

cout<<"Input number of elements in second array ";
cin>>n;

cout<<"Enter Array elements : ";;
for (c = 0; c < n; c++) {
cin>>b[i];
}

merge(a, m, b, n, sorted);

cout<<"Merged array: ");

for (c = 0; c < m + n; c++) {
cout<<sorted[c];
}

return 0;
}

void merge(int a[], int m, int b[], int n, int sorted[]) {
int i, j, k;

j = k = 0;

for (i = 0; i < m + n;i=i+2) {
for(j=0;j<m;j++)
   sorted[i] = a[j];
}
for (i = 1; i < m + n;i=i+2) {
for(k=0;k<n;k++)
   sorted[i] = a[k];
}
}

To check if a string is a palindrome:

#include<iostream.h>
#include<string.h>
int main(){
char *str,*rev;
int i,j;
cout<<" Enter a string:";
cin>>str;
for(i=strlen(str)-1,j=0;i>=0;i--,j++)
rev[j]=str[i];
rev[j]='';
if(strcmp(rev,str))
cout<<str<<" is not a palindrome";
else
cout<<str<<" is a palindrome";
return 0;
}