我必须编程然后创建数组 1)找到所有正元的总和 2)按模块将最大和中间元素之间的所有元素相乘 3)从最大elem到min。排序数组。
这一切都应该在几个函数中并使用模板进行。 我写了这个程序,但每次都会显示这个错误。
#include <iostream>
#include <math.h>
using namespace std;
//void create(T *arr, int n);
//int positiv(int *arr, int n);
//int proizv(int *arr, int n);
//void sort(int *arr, int n);
//void print(int *arr, int n);
template <typename T>
int main(int argc, char *argv[])
{
int n;
cin >> n;
T *arr1 = new T[n];
create(arr1, n);
T s1 = positiv(arr1, n);
T p1 = proizv(arr1, n);
sort(arr1, n);
T *arr2 = new T[n];
create(arr2, n);
T s2 = positiv(arr2, n);
T p2 = proizv(arr2, n);
sort(arr2, n);
T *arr3 = new T[n];
create(arr3, n);
T s3 = positiv(arr3, n);
T p3 = proizv(arr3, n);
sort(arr3, n);
cout << s1 << " ";
if (p1 == -100)
cout << "No elements between max and min." << endl;
else
cout << p1 << endl;
print(arr1, n);
cout << s2 << " ";
if (p2 == -100)
cout << "No elements between max and min." << endl;
else
cout << p2 << endl;
print(arr2, n);
cout << s3 << " ";
if (p3 == -100)
cout << "No elements between max and min." << endl;
else
cout << p3 << endl;
print(arr3, n);
system("pause");
return 0;
}
template <typename T>
void create(T *arr, int n)
{
for (int i = 0; i < n; i++)
{
cin >> arr[i];
}
}
template <typename T>
int positiv(T *arr, int n)
{
int i, summ;
for (i = summ = 0; i < n; i++)
{
if (arr[i] > 0) summ += arr[i];
}
return summ;
}
template <typename T>
int proizv(T *arr, int n)
{
int i, maxelem, minelem;
maxelem = minelem = 0;
for (maxelem = minelem = i = 0; i < n; i++)
{
if (fabs(arr[i]) > fabs(arr[maxelem])) maxelem = i;
if (fabs(arr[i]) < fabs(arr[minelem])) minelem = i;
}
int temp;
if (abs(maxelem - minelem) > 1)
{
int stelem = (maxelem < minelem ? maxelem : minelem);
int endelem = (maxelem < minelem ? minelem : maxelem);
for (i = stelem + 1, temp = 1; i < endelem; i++)
temp *= arr[i];
}
else
temp = -100;
return temp;
}
template <typename T>
void sort(T *arr, int n)
{
int temp;
for (int i = 0; i < n - 1; i++)
for (int j = i + 1; j <= n - 1; j++)
if (arr[i] < arr[j]) {
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
template <typename T>
void print(T *arr, int n)
{
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
delete[] arr;
}