Getting wrong output when outputting Odd array
Dec 2, 2016 at 1:02pm UTC
Hi,
I wrote this code to first get an array from input. Then move odd numbers from this array to the odd array and Even numbers to the even array.
All is working fine except when I cout the Odd array, It somehow changes first item to 10 on its first iteration.
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
#include <iostream>
using namespace std;
int main()
{
int sizeEv = 0;
int sizeOd = 0;
int arr[10], odd[sizeEv], even[sizeEv], temp;
int i = 0;
int j = 0;
int k = 0;
cout << "You are required to enter any 10 positive integers. After typing the number, please press Enter\n" ;
do
{
cin >> temp;
if (temp > -1)
{
arr[i] = temp;
i++;
} else {
cout << "!please enter a valid positive integer\n" ;
}
} while (i < 10);
for (i=0; i<10; i++) {
temp = arr[i];
if (temp%2 == 1) {
sizeOd++;
odd[j] = temp;
j++;
} else {
sizeEv++;
even[k] = temp;
k++;
}
} // END FOR
for (i=0; i<sizeOd; i++) {
cout << odd[i] << ", " ;
}
}
The output that I am getting is: 10,3,5,7,9
Instead I should be getting 1,3,5,7,9
Where am I exactly wrong?
Last edited on Dec 2, 2016 at 1:10pm UTC
Dec 2, 2016 at 1:30pm UTC
line 8: how big do you think odd[] and even[] are? Hint they're zero length.
line 28,33: You can't dynamically change the size of the arrays that way. In C++, the size of fixed arrays must be known at compile time.
Dec 2, 2016 at 2:08pm UTC
Awesome! Thanks a bunch! I gave same size to odd[] and even[] as the size in main array but then I had trailing 0s and random numbers in the end of array.
but then I did:
1 2 3 4 5
i = 0;
do {
odd[i] = odd[i]
i++
} while (odd[i] != 0);
and now it works as intended
Dec 2, 2016 at 3:38pm UTC
but then I had trailing 0s and random numbers in the end of array.
You could initialise each array to all zeros like this:
int arr[10] = {};
But it may be better to keep track of the number of values actually stored in each array, rather than looking for some particular value.
Here, I got a bit carried away with the cout at line 39 an 42, but other than that it should be easy to understand:
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
#include <iostream>
using namespace std;
int main()
{
const int SIZE = 10;
int arr[SIZE];
int odd[SIZE];
int even[SIZE];
int sizeEv = 0;
int sizeOd = 0;
int sizeAr = 0;
cout << "You are required to enter any " << SIZE << " positive integers.\n"
"After typing the number, please press Enter\n" ;
while (sizeAr < SIZE)
{
cin >> arr[sizeAr];
if (arr[sizeAr] >= 0)
sizeAr++;
else
cout << "!please enter a valid positive integer\n" ;
}
for (int i=0; i<sizeAr; i++)
{
int temp = arr[i];
if (temp%2)
odd[sizeOd++] = temp;
else
even[sizeEv++] = temp;
}
for (int i=0; i<sizeOd; i++)
cout << odd[i] << ((i== sizeOd-1) ? "\n" : ", " );
for (int i=0; i<sizeEv; i++)
cout << even[i] << ((i== sizeEv-1) ? "\n" : ", " );
}
Dec 2, 2016 at 6:43pm UTC
Thanks Chervil. This actually cleared a few questions I had about initiating an array and the solution is far better by keeping count of each of the array rather than having a condition to catch a certain value.
Topic archived. No new replies allowed.