moving array values

Coding arrays, one even, odd and then one merged for smallest value to larges. Only on even so far and when I input 4 then 2 it just prints 2. Can someone please point me in the right direction.

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
68
69
70
71
#include <iostream>
using namespace std;

int main() {

	int even[32] = { 0 };
	int odd[32] = { 0 };
	int merge[32] = { 0 };
	int eventop = 0;
	int oddtop = 0;
	int i = 1;
	int last = 0;
	int invalue;
	cout << "Input an integer, enter 0 to terminate: " << flush;
	cin >> invalue;
	while (invalue != 0) {
		//add to even list
		if (invalue == 2 || invalue == 4 || invalue == 6 || invalue == 8)
		{
			//if list empty - eventop is 0
			if (eventop == 0) {
				even[0] = invalue;
				eventop = invalue;
				cout << "Input an integer, enter 0 to terminate: " << flush;
				cin >> invalue;
			}
			else {
				i = 0;
				//not at the end - finding spot to insert
				while (even[i] != 0) {
					if (even[i] > invalue) { break; } //found spot
					i++;
				}		
				if (even[i] == 0) {
					even[i] = invalue;
					cout << "Input an integer, enter 0 to terminate: " << flush;
					cin >> invalue;
				}
				else
				{
					//need move all values forward 1 spot
					//find last index of array
					last = i;
					while (even[last] != 0) {
						last++;
					}
					//moves all the values forward
					while (last != i)
					{
						even[last - 1] = even[last];
						last--;
					}
					even[i] = invalue;
					cout << "Input an integer, enter 0 to terminate: " << flush;
					cin >> invalue;
				}				
			}
		}
		//add to odd list
		else
		{
			;//odd[] = invalue;
		}
	}
	int b = 0;
	while (even[b] != 0) {
		cout << even[b];
		b++;
	}
	return 0;
}
One possibility using C++ standard library algorithms and iterators to sort and merge 2 arrays into a third array:
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
#include <iostream>
#include <algorithm> // std::sort, std::merge
#include <iterator>  // std::size, std::begin, std::end

int main()
{
   // create an array
   int first[] { 5, 10, 15, 20, 25 };

   // sort the 1st array
   std::sort(std::begin(first), std::end(first));

   // create a second array
   int second[] { 50, 40, 30, 20, 10 };

   // sort the 2nd array
   std::sort(std::begin(second), std::end(second));

   // get the total size of both arrays
   const int size { std::size(first) + std::size(second) };

   // create an empty third array with a combined size of the other arrays
   int third[size] { };

   // merge the two arrays into the third
   std::merge(std::begin(first), std::end(first),
              std::begin(second), std::end(second),
              std::begin(third));

   // output the merged array
   std::cout << "The merged array contains:\n";
   for (auto itr { std::begin(third) }; itr != std::end(third); ++itr)
   {
      std::cout << *itr << ' ';
   }
   std::cout << '\n';
}
The merged array contains:
5 10 10 15 20 20 25 30 40 50
Without using the STL, perhaps:

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
#include <iostream>

constexpr size_t MAXARR {32};

void domerge(int inp[MAXARR], int merge[MAXARR])
{
	for (size_t i = 0; i < MAXARR && inp[i]; ++i) {
		size_t m {};

		for (; m < MAXARR && merge[m]; ++m)
			if (inp[i] < merge[m]) {
				for (size_t n = MAXARR - 1; n > m; --n)
					merge[n] = merge[n - 1];

				break;
			}

		merge[m] = inp[i];
	}
}

int main()
{
	int even[MAXARR] {0};
	int odd[MAXARR] {0};
	int merge[MAXARR] {0};
	int eventop {};
	int oddtop {};
	int invalue {};

	std::cout << "Input integers, 0 to exit : ";

	while (eventop < MAXARR && oddtop < MAXARR && std::cin >> invalue && invalue != 0) {
		if (invalue % 2 == 0)
			even[eventop++] = invalue;
		else
			odd[oddtop++] = invalue;
	}

	std::cout << "\nThe even numbers are\n";

	for (size_t i = 0; i < eventop; ++i)
		std::cout << even[i] << ' ';

	std::cout << "\n\nThe odd numbers are\n";

	for (size_t i = 0; i < oddtop; ++i)
		std::cout << odd[i] << ' ';

	domerge(even, merge);
	domerge(odd, merge);

	std::cout << "\n\nThe sorted merged numbers are\n";

	for (size_t i = 0; i < oddtop + eventop; ++i)
		std::cout << merge[i] << ' ';

	std::cout << '\n';
}



Input integers, 0 to exit : 8 6 4 2 1 3 5 7 0

The even numbers are
8 6 4 2

The odd numbers are
1 3 5 7

The sorted merged numbers are
1 2 3 4 5 6 7 8

Last edited on
Hello phazed99,

When I was looking over your code I noticed something.
1
2
3
4
5
6
7
8
9
10
int main()
{
    int even[32] = { 0 };  // <--- You do not need the (=) or the (0)zero.
    int odd[32]{};         // <--- This is all you need.
    int merge[32] = { 0 };
    int eventop = 0;
    int oddtop = 0;
    int i{ 1 };            // <--- Changed.
    int last = 0;
    int invalue;           // <--- ALWAYS initialize your variables. 

The "even" and "odd" arrays can 32 numbers, but if they do how so you expect to put 64 numbers into the "merge" array that can only hold 32 numbers?

This is what I was thinking:
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
using uInt = unsigned int;

int main()
{
    constexpr uInt MAXSIZE{ 32 };

    int even[MAXSIZE] = { 0 };  // <--- You do not need the (=) or the (0)zero.
    int odd[MAXSIZE]{};         // <--- This is all you need.
    int merge[MAXSIZE * 2] = { 0 };
    int eventop = 0;
    int oddtop = 0;
    int i{ 1 };            // <--- Changed.
    int last = 0;
    int invalue;           // <--- ALWAYS initialize your variables.

    //cout << "Input an integer, enter 0 to terminate: " << flush;
    //cin >> invalue;

    while (std::cout << "Input an integer, enter 0 to terminate: " && (std::cin >> invalue) && invalue != 0)
    //while (invalue != 0)
    {
        //add to even list
        if (!(invalue % 2))
        //if (invalue == 2 || invalue == 4 || invalue == 6 || invalue == 8)
        {

The while statement means the you do not have to repeat lines 16 and 17 so many times in the program. Once and twice is OK, but after that you should think about doing something so as not to duplicate code.

Line 23 works much better than what you have. What about even numbers from 10 to 100? That would make a very long if statement. The when you do the same for the odd numbers it would be easier to write a program just to write the if condition.

I am not sure about the rest of the program. When I entered "2 22 4 44 6 66 8 88 0" only the single digit numbers were stored in the array.

That is what I noticed so far.

Andy
Hello phazed99,

Since everyone is giving their suggestions:
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
68
69
70
71
72
73
74
75
76
77
78
79
#include <iostream>
#include <limits>

#include <algorithm>

using namespace std;

using uInt = unsigned int;

int main()
{
    constexpr uInt MAXSIZE{ 32 };

    //int even[MAXSIZE]{};  // <--- These 3 lines for normal run.
    //int odd[MAXSIZE]{};
    //int evenIdx{}, oddIdx{}, mergeIdx{};

    int even[MAXSIZE]{ 2, 22, 44, 4, 6, 66 };  // <--- These 3 lines used for testing. Switch comments when finished.
    int odd[MAXSIZE]{ 11, 1, 33, 3, 55, 5 };
    int evenIdx{ 6 }, oddIdx{ 6 }, mergeIdx{};

    int merge[MAXSIZE * 2]{};
    int inVAlue{};

    // <--- Uncomment for normal run.[/b]
    //while (std::cout << "Input an integer, enter 0 to terminate: " && (std::cin >> inVAlue) && inVAlue != 0)
    //{
    //    //add to even list
    //    if (!(inVAlue % 2))
    //    {
    //        even[evenIdx++] = inVAlue;
    //    }
    //    //add to odd list
    //    else
    //    {
    //        odd[oddIdx++] = inVAlue;//odd[] = invalue;
    //    }
    //}

    std::sort(even, even + evenIdx);
    std::sort(odd, odd + oddIdx);

    int b = 0;

    std::cout << "\n Unmerged arrays:\n ";

    while (even[b] != 0)
    {
        cout << even[b] << ' ';
        merge[mergeIdx++] = even[b++];
    }

    std::cout << "\n ";

    b = 0;

    while (odd[b] != 0)
    {
        cout << odd[b] << ' ';
        merge[mergeIdx++] = odd[b++];
    }

    std::cout << "\n\n Merged arrays:\n ";

    for (uInt idx = 0; idx < mergeIdx; idx++)
    {
        std::cout << merge[idx] << ' ';
    }

    std::cout << '\n';

    // A fair C++ replacement for "system("pause")". Or a way to pause the program.
    // The next line may not be needed. If you have to press enter to see the prompt it is not needed.
    //std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires header file <limits>.
    std::cout << "\n\n Press Enter to continue: ";
    std::cin.get();

    return 0;  // <--- Not required, but makes a good break point.
}

I do not know what output you are expecting because you did not say, but the code produces this:

 Input Numbers:
 2, 22, 44, 4, 6, 66
 11, 1, 33, 3, 55, 5

 Unmerged arrays:
 2 4 6 22 44 66
 1 3 5 11 33 55

 Merged arrays:
 2 4 6 22 44 66 1 3 5 11 33 55


 Press Enter to continue:


If you can not use "std::sort()" you can write your own sort part.

Andy
Thanks everyone for the help.
Topic archived. No new replies allowed.