[英]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;
}
它將ith
元素與-1比較,然后將其存儲在pos數組中的kth
索引處,並且
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();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.