> but I met some errors and don't have a clue respectively how to finish the code.
Did you have a clue how to start the code?
Or did you just pick up the brush, start painting madly, and now you find yourself in a corner.
Some initial thoughts.
1. You have a mass of meaningless single letter identifiers - wtf do they all mean?
I bet you don't even know unless you read the code in detail.
Before you went all pointer mad (why are you using them in a C++ program anyway - use a std::vector if you want variable length), you should have started with this.
1 2 3 4 5 6 7
|
int main() {
int original[5] = { 1, 2, 3, 4, 5 };
int evens[5];
int odds[5];
// copy (as a function)
// print (as a function)
}
|
Something nice and simple that gets you moving in the right direction.
When you have something basic working, then you can incrementally improve it towards your desired goal.
- adding randomness.
- change ONE array to be a pointer with allocation and deletion.
- change the rest of the arrays to be pointers with allocation and deletion.
It's absolutely vital you get to grips with the idea of breaking problems down into smaller steps, and breaking programs down into smaller functions.
2. Your indentation could be better.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
|
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <iomanip>
using namespace std;
int main()
{
srand(time(0));
int *a = new int;
int *b = new int;
int *c = new int;
int *n = new int;
int *k = new int;
int *m = new int;
*n = rand() % 10 + 2;
*n = *k;
*n = *m;
int *v = new int[*n];
int *p = new int[*k];
int *im = new int[*m];
for (int i = 0; i < *n; i++) {
*a = rand() % 10 + 2;
*b = rand() % 10 + 2;
*c = rand() % 10 + 2;
*(v + i) = *a + *b - *c;
cout << i << " a=" << *a << " b=" << *b << " c=" << *c;
cout << " a+b-c=" << *a + *b - *c << endl;
if (*(v + i) % 2 == 0)
*(v + i) = *p;
else
*(v + i) = *im;
}
delete a;
delete b;
delete c;
for (int i = 0; i < *n; i++) {
cout << setw(3) << *(v + i);
}
delete[]v;
for (int i = 0; i < *n; i++) {
cout << setw(3) << *(p + k);
}
delete k;
delete[]p;
for (int i = 0; i < *n; i++) {
cout << setw(3) << *(im + m);
}
delete m;
delete n;
delete[]im;
return 0;
}
|