Answer the question
In order to leave comments, you need to log in
Even and odd elements in an array in C++?
There is such a problem, I wrote a code in which the elements fall down and go in ascending order. It is necessary that in those that fall there were only odd ones, and in growth - even ones.
Using a random number generator, generate an array of 20 elements. Arrange the elements of an array so that all even-numbered elements are in ascending order and odd-numbered elements in descending order
#include <iostream>
#include <cstdlib>
#include <time.h>
using namespace std;
int main()
{
const int n=21;
int m[n];
srand(time(0));
cout<<"Massiv M:\n";
for(int i=0;i<n;i++)
{
m[i]=rand();
cout<<"m["<<i<<"]= "<<m[i]<<endl;
}
// Рост
int temp;
for(int i=0;i<n;i++)
{
for(int j=1;j<n-i;j++)
{
if(m[j]<m[j-1]) { temp=m[j]; m[j]=m[j-1]; m[j-1]=temp;}
}
}
cout<<"Vporyadkovanyj massib po zrostanny:\n";
for(int i=0;i<n;i++)
{
cout<<m[i]<<" ";
}
cout<<endl;
// Убивание
for(int i=0;i<n;i++)
{
for(int g=1;g<n-i;g++)
{
if(m[g]>m[g-1]) { temp=m[g]; m[g]=m[g-1]; m[g-1]=temp;}
}
}
cout<<"Vporyadkovanyj massib po spad:\n";
for(int i=0;i<n;i++)
{
cout<<m[i]<<" ";
}
return 0;
}
Answer the question
In order to leave comments, you need to log in
Didn't look at the code. Fill it in with the condition that "variable" %2==0" else{It won't be even}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question