练习:将数组值拆分为正数组和负数组
exercise: split the array values into positive and negative arrays
我很难思考如何解决这个问题我需要将负值和正值分开,但当我启动程序时,结果是错误的。我对编程很陌生,尤其是在数组中,所以任何帮助都会很感激。
以下是锻炼问题:a) 定义一个最大值为20个整数值的数组,并用您自己选择的数字填充数组作为初始化符,然后编写、编译和运行一个c++程序,该程序读取数组中的数字,并将所有零和正数放在一个名为正的数组中,最后将所有负数放在名为负的数组中,让您的程序同时显示正和负阵列中的值
这是代码:
#include <iostream>
using namespace std;
int main()
{
const int MAXVAL = 5; // created 5 values for num, pos, neg i set to 5 because for testing purposes
int num[MAXVAL];
int pos[MAXVAL];
int neg[MAXVAL];
int i, k, c, c2, j;
c = 0;
c2 = 0;
cout << "enter the numbers: " << endl;
for(i = 0; i < MAXVAL; i++) // inputs the users
{
cin >> num[i];
}
for(i = 0, k = 0; i < MAXVAL; i++){ // this function finds the positivevalue
if(num[k] > -1){
pos[k] = num[k];
k++;
c = c + 1;
}
else{
pos[k] = pos[k];
k++;
}
}
for(i = 0, j = 0; i < MAXVAL; i++){ // this function finds the negative value
if(num[j] < 0){
neg[j] = num[j];
j++;
c2 = c2 + 1;
}
else{
neg[j] = neg[j];
j++;
}
}
cout << "the positive numbers is: " << endl;////// displays the result
for(i = 0; i < c; i++){
cout << " " << pos[i];
}
cout << "nthe negative numbers is: " << endl;
for(i = 0; i < c2; i++){
cout << " " << neg[i];
}
return 0;
}
结果是这样的:
输入一个数字:1,-5,4,-55,5
正数是:1 8 4
负数为:4286352-5
首先,在头<algorithm>
中已经声明了标准函数std::partition_copy
,它能够完成这项工作。以下是的示例
#include <iostream>
#include <algorithm>
#include <iterator>
int main()
{
const size_t MAX_VAL = 5;
int num[MAX_VAL] = { 1, -5 , 4, -55, 5 };
int pos[MAX_VAL];
int neg[MAX_VAL];
for ( int x : num ) std::cout << x << ' ';
std::cout << std::endl;
auto p = std::partition_copy( std::begin( num ), std::end( num ),
std::begin( pos ), std::begin( neg ),
[]( int x ) { return !( x < 0 ); } );
for ( int *first = pos; first != p.first; ++first ) std::cout << *first << ' ';
std::cout << std::endl;
for ( int *first = neg; first != p.second; ++first ) std::cout << *first << ' ';
std::cout << std::endl;
return 0;
}
程序输出为
1 -5 4 -55 5
1 4 5
-5 -55
如果你想在不使用标准算法的情况下自己编写程序,那么这种方法可以看起来像
#include <iostream>
int main()
{
const size_t MAX_VAL = 5;
int num[MAX_VAL] = { 1, -5 , 4, -55, 5 };
int pos[MAX_VAL];
int neg[MAX_VAL];
for ( int x : num ) std::cout << x << ' ';
std::cout << std::endl;
size_t n1 = 0, n2 = 0;
for ( size_t i = 0; i < MAX_VAL; i++ )
{
if ( !( num[i] < 0 ) )
{
pos[n1++] = num[i];
}
else
{
neg[n2++] = num[i];
}
}
for ( size_t i = 0; i < n1; i++ ) std::cout << pos[i] << ' ';
std::cout << std::endl;
for ( size_t i = 0; i < n2; i++ ) std::cout << neg[i] << ' ';
std::cout << std::endl;
return 0;
}
程序输出与上述相同。
当然,您可以更改程序,让用户自己输入原始数组的值。
您正在用自己初始化未初始化的元素。这是你的问题,用0
:这样的重要值初始化它们
pos[k] = 0;
当循环查找正和负时,应该使用循环索引进行比较,然后存储在第二个索引中,如下所示:
if(num[i] > -1){
pos[k] = num[i];
k++;
c = c + 1;
}
它将CCD_ 4元素与-1进行比较,然后将其存储在pos数组和中的CCD_
if(num[i] < 0){
neg[j] = num[i];
j++;
c2 = c2 + 1;
}
您还需要删除else子句,否则您可能会在最大索引c
和c2
之后写入,也因为pos[k] = pos[k]
是错误的。
如果这是c++,请不要使用c样式数组
使用vector
的解决方案可能如下所示:
#include <iostream>
#include <vector>
using namespace std;
int main()
{
int n;
vector<int> num, pos, neg;
cout << "enter count :";
cin >> n;
for(int i = 0; i < n; ++i)
{
cout << "enter number :" ;
int val;
cin >> val;
num.push_back(val);
}
for( auto val : num )
{
if( val >= 0 )
pos.push_back(val);
else
neg.push_back(val);
}
cout << "the positive numbers are: " << endl;
for( auto val : pos )
cout << " " << val;
cout << "nthe negative numbers are: " << endl;
for( auto val : neg )
cout << " " << val;
}
关于vector
如何在中工作的几点注意事项
// this creates a empty vector of int's, named v;
vector<int> v;
// this adds one element (in this case 3) last to the vector
v.push_back(3);
// this gets the current number of elements
int n = v.size();
// this retrieves the first element of the vector
int a = v[0]; // zero based index
// this is a range based for loop, that prints every element
for( auto elem : v )
cout << elem << " " ;
// that could also be done with a regular for loop
for( int i=0; i<v.size(); ++i )
cout << v[i] << " ";
// this makes the vector empty
v.clear();
相关文章:
- Mongodb c++驱动程序:如何查询元素的数组
- 将数组的地址分配给变量并删除
- 从C++本机插件更新Vector3数组
- lambda参数转换为constexpr技巧,然后获取带链接的数组
- 将数组作为参数传递给函数安全吗?作为第三方职能部门,可以探索他们想要的之外的其他元素
- 数组索引的值没有增加
- 将对象数组的引用传递给函数
- 为char数组调整zlib-zpipe
- 2D数组来自文本输入,中间有空格
- std::向量与传递值的动态数组
- 在c++中用vector填充一个简单的动态数组
- 使用strcpy将char数组的元素复制到另一个数组
- 使用指针从C++中的数组中获取最大值
- 练习循环和使用循环 1 次以及如何获取数组的最大值
- 尺寸数组 [矩阵] 练习的问题
- 练习:将数组值拆分为正数组和负数组
- 练习:使用数组计算方差
- 很好的资源,练习c++数组和指针
- 如何实现这个练习(动态数组)
- 关于我正在做的练习中的二维数组