【发布时间】:2013-11-16 12:22:26
【问题描述】:
任务: 给定一个数组 C(15)。从数组中删除最大负元素。写它的索引。显示初始和更改的数组。
注意:此代码写入数组中最小的负元素,即'-8'。
需要帮助:我需要改变一些东西,它会写出最大的负元素,即数组中的“-5”。
#include <iostream>
using namespace std;
int main()
{
int c[15]= { 1, 2, 3, 4, 5, 6, -5, 8, 9 , 10, -7, 12, -8, 14, 15};
int nmaxelement = c[0];
int nmaxelementplace;
int i;
cout<<"The array is: \n";
for (i=0; i<15; i++)
{
cout<<c[i]<<" ";
}
for (int i=0; i<15; i++)
if (c[i]<nmaxelement)
{
nmaxelement = c[i];
nmaxelementplace = i;
}
cout<<"\nNegative max element is "<<nmaxelement<<endl;
cout<<"Its place: "<<nmaxelementplace<<endl;
int k=nmaxelementplace;
int n=15;
cout<<"Array with the deleted element: "<<endl;
for (int i=k; i<n; i++)
c[i]=c[i+1];
n=n-1;
for (int i=0; i<n; i++)
cout<<c[i]<<" ";
return 0;
}
【问题讨论】:
-
它已经完成了它应该做的事情。它显示 -8 是数组中的最小元素。你想在新创建的数组中找到最小的数字吗?
-
@user2699298 再次阅读问题。代码没有做它应该做的事情。