A
A
Ashley Evanston2015-10-24 16:34:16
Programming
Ashley Evanston, 2015-10-24 16:34:16

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

3 answer(s)
M
Mrrl, 2015-10-24
@Inessevanston

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.

O
Oleg Tsilyurik, 2015-10-24
@Olej

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,

1. An array containing values ​​of int & double types cannot be created! - C++ is a typed language, and an array can only contain elements of one type;
2. when initializing a double array, you can specify int constants, but they will be immediately converted to double type (but not vice versa);
3. if you try later to use such loaded values ​​as int - it will fail, they have already been converted to double.

S
Stanislav Makarov, 2015-10-25
@Nipheris

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 question

Ask a Question

731 491 924 answers to any question