How to convert array of char to int?

Given the task of function templates: the permutation of elements in the array as follows: first, the negative elements in descending order, then non-negative in increasing order.
There are 2 arguments, char and double
How to convert char array to int? And where can be error?
#include <iostream>
#include <stdio.h>
#include <stdlib.h>

template <typename T>
void swap(T& first, T& second)
{
 T temp(first);
 first = second;
 second = temp;
}

template <typename T1>
void out_array(const T1* array, size_t arraySize)
{
 for (size_t i = 0; i < arraySize; ++i)
 std::cout << array[i] << " ";
 std::cout << std::endl;
}

template <class T1>
void sort_array(T1* array, size_t arraySize)
{
 T1 temp;
 for (size_t i = 0; i < arraySize - 1; i++)
{
 for (size_t j = 0; j < arraySize - i - 1; j++)
{
 if (array[j] < 0)
 if (array[j] < array[j + 1])
{
 temp = array[j];
 array[j] = array[j + 1];
 array[j + 1] = temp;
}
 if (array[j] > 0)
 if (array[j] > array[j + 1])
{
 temp = array[j];
 array[j] = array[j + 1];
 array[j + 1] = temp;
}
}
}
}

int main()
{
 setlocale(0, "");
 const size_t n = 10;
 double array1[n] = { 2.1,1.1,4.2,5.2,3.1,-3.1,-1.2,-2.3,-4.4,-5.6 };
 char array2[n] = { '2','1','4','5','3','-3','-1','-2','-4','-5' };
 int array3[n] = atoi(array2);
 std::cout << "Unsorted array:" << std::endl;
 out_array(array1, n);
 out_array(array3, n);
 sort_array(array1, n);
 sort_array(array2, n);
 std::cout << "Sorted array:" << std::endl;
 out_array(array1, n);
 out_array(array3, n);
}

Error:
1>lab5.cpp
1>C:\Prog\Visual Studio 2019\lab5\lab5.cpp(52,41): warning C4838: conversion from ' int ' to 'char' requires a narrowing conversion
1>C:\Prog\Visual Studio 2019\lab5\lab5.cpp(52,66): warning C4305: initializing: truncation from ' int ' to 'char'
1>C:\Prog\Visual Studio 2019\lab5\lab5.cpp(52,41): warning C4309: initializing: truncation of constant value
1>C:\Prog\Visual Studio 2019\lab5\lab5.cpp(52,46): warning C4838: conversion from ' int ' to 'char' requires a narrowing conversion
1>C:\Prog\Visual Studio 2019\lab5\lab5.cpp(52,46): warning C4309: initializing: truncation of constant value
1>C:\Prog\Visual Studio 2019\lab5\lab5.cpp(52,51): warning C4838: conversion from ' int ' to 'char' requires a narrowing conversion
1>C:\Prog\Visual Studio 2019\lab5\lab5.cpp(52,51): warning C4309: initializing: truncation of constant value
1>C:\Prog\Visual Studio 2019\lab5\lab5.cpp(52,56): warning C4838: conversion from ' int ' to 'char' requires a narrowing conversion
1>C:\Prog\Visual Studio 2019\lab5\lab5.cpp(52,56): warning C4309: initializing: truncation of constant value
1>C:\Prog\Visual Studio 2019\lab5\lab5.cpp(of 52.61): warning C4838: conversion from ' int ' to 'char' requires a narrowing conversion
1>C:\Prog\Visual Studio 2019\lab5\lab5.cpp(of 52.61): warning C4309: initializing: truncation of constant value
1>C:\Prog\Visual Studio 2019\lab5\lab5.cpp(53,35): error C2440: initializing: cannot convert "int" to "int [10]"
1>C:\Prog\Visual Studio 2019\lab5\lab5.cpp(53,16): message : there are No conversions to array types, although there are conversions to references or pointers to arrays
1>Build project "lab5.vcxproj" completed with error.
March 25th 20 at 13:19
1 answer
March 25th 20 at 13:21
Solution
Errors in this code quite a lot.

Let's start with void swap(T& first, T& second).
It simply is not needed because std::swap.

template <typename T1>
void out_array(const T1* array, size_t arraySize)

Parameter arraySize is also not needed. You work with raw arrays of fixed lengths and can give your function template understanding the length of the array. This is done so.
template < typename TElementType, size_t LENGTH >
void out_array( const TElementType array[ LENGTH ] )


Next.
char array2[n] = { '2','1','4','5','3','-3','-1','-2','-4','-5' };

This place just two errors. The first is to initialize a symbolic literals. '2' will not be equivalent to 2. Since then, the sorting will sort by the code character table, and not on the value of a number. This is a mistake, because this is clearly not the behavior that you expect.
The second error is '-3' , etc. Type such a design will be int, not char. The compiler you this is trying to say.

int array3[n] = atoi(array2);
This design is difficult to understand. You can say one thing - a fixed-length array to initialize will fail.
If the design you wanted to describe the initialization of an array, you can try to do this:
int array3[n];
std::transform(
 std::begin( array2 ), std::end( array2 ), std::begin( array3 ),
 []( const char item ) -> int { return item; } 
);

But it is also a slippery solution. In your case, everything will be fine, in General it is a bad decision. Study the documentation on the function std::transform.
Thanks, fixed it - jackson.La commented on March 25th 20 at 13:24

Find more questions by tags C++