Answer the question
In order to leave comments, you need to log in
How to set an array with integer and real numbers?
How to set an array in which you can write both integer and real numbers?
I'm trying to set the type to float or bouble, but I stumble on the line arr2=new int [n].
#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
setlocale (LC_ALL, "RUS");
int n;
int x;
int *arr1=NULL;
int *arr2;
int i;
int j;
int tp;
n=0;
cout<<"Введите массив "<<endl;
while ((cin.peek()!=10)&&(n<11))
{
++n;
arr2=new int [n];
if (arr1!=NULL)
{
for (i=n-2; i>=0; --i)
{
arr2[i]=arr1[i];
}
delete [] arr1;
}
arr1=arr2;
cin>>arr2[n-1];
}
for (i=0; i<n; ++i)
{
for (j=i+1; j<n; ++j)
{
if (arr1[i]>arr1[j])
{
tp=arr1[i];
arr1[i]=arr1[j];
arr1[j]=tp;
}
}
}
cout<<"\nМассив неубывающих чисел: "<<endl;
for (i=0; i<n; ++i)
{
cout<<arr1[i]<<' ';
}
cout<<endl;
getch();
}
Answer the question
In order to leave comments, you need to log in
Not a bouble, but a double.
In the array description write
double *arr2;
and in initialization -
arr2=new double[n];
If you change the type in only one place, there will be an error.
How to set an array in which you can write both integer and real numbers?
I'm trying to set the type to float or bouble,
How to set an array in which you can write both integer and real numbers?
boost::variant <int, double>
Just make sure you understand what you're doing and what you want first.
Oleg Tsilyurik
Quite right, and this type can be the sum of several other types (a sum in the sense of the sum of sets of allowed values).
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question