Please covert this C program to C++ Thanks #include <stdio.h> int main() { int N
ID: 3861529 • Letter: P
Question
Please covert this C program to C++
Thanks
#include <stdio.h>
int main() {
int NUM = 0;
int* base;
int* odd;
int* even;
int i= 0;
int j= 0;
int k=0;
printf("Enter the size of an array: ");
scanf("%d", &NUM);
base = (int*)malloc(NUM * sizeof(int));
odd = (int*)malloc(NUM * sizeof(int));
even = (int*)malloc(NUM * sizeof(int));
printf("Enter the elements (integers) of the array: ");
for (i = 0; i < NUM; ++i) {
scanf("%d", &(base[i]));
}
for (i = 0; i < NUM; i++)
{
if (base[i] % 2 == 0){
even[j] = base[i];
j++;
}
else{
odd[k] = base[i];
k++;
}
}
printf("The elements of the odd array are: ");
for (i = 0; i < k; i++){
printf("%d ", odd[i]);
}
printf("The elements of the even array are: ");
for (i = 0; i < j; i++){
printf("%d ", even[i]);
}
return 0;
}
Explanation / Answer
Answer ->
-------------------------------------------------------------------------------------------------------------------------------------------------------
#include <iostream>
using namespace std;
int main() {
int NUM = 0;
int* base;
int* odd;
int* even;
int i= 0;
int j= 0;
int k=0;
cout<<"Enter the size of an array: ";
cin>>NUM;
base = new int [NUM];
odd = new int [NUM];
even = new int [NUM];
cout<<"Enter the elements (integers) of the array: ";
for (i = 0; i < NUM; ++i) {
cin>>base[i];
}
for (i = 0; i < NUM; i++)
{
if (base[i] % 2 == 0){
even[j] = base[i];
j++;
}
else{
odd[k] = base[i];
k++;
}
}
cout<<"The elements of the odd array are: ";
for (i = 0; i < k; i++){
cout<<odd[i]<<" ";
}
cout<<"The elements of the even array are: ";
for (i = 0; i < j; i++){
cout<<even[i]<<" ";
}
return 0;
}
----------------------------------------------------------------------------------------------------------------------------------------------
OUTPUT -
Enter the size of an array: 6
Enter the elements (integers) of the array:
14
15
18
21
22
23
The elements of the odd array are:
15
21
23
The elements of the even array are:
14
18
22
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.