The statements at lines 21 and 23 don't do anything. You need to assign the value in a[i] to one or the other of the two target arrays.
Also, don't test i for odd or even, test the value at a[i].
Lastly, you can't use i as the index into the odd or even arrays, you need to keep separate index values for those which you increment when a value gets stored to the arrays.
<grammar Nazi hat on> "intergers" is correctly spelled "integers". </grammar Nazi hat on>
vmanes
Postaholic
2,015 posts since Aug 2007
Reputation Points: 1,283
Solved Threads: 242
Skill Endorsements: 6
<grammar Nazi hat on> "intergers" is correctly spelled "integers". </grammar Nazi hat on>
<über pedantic mode>
You need to wear your spelling Nazi hat because using the correct word in the correct context with incorrect spelling is not a grammatical error, it's a spelling error.
</über pedantic mode>
:D
deceptikon
Challenge Accepted
3,435 posts since Jan 2012
Reputation Points: 822
Solved Threads: 473
Skill Endorsements: 56
This is my new code but when I cout it tells me the array is uninitialized.
Yes, that's correct. It's harder to see because your indentation is atrociously inconsistent, but the effect is like this:
{
int odd[15];
}
cout<< odd[3] <<endl;
odd is declared within the braces, and thus its visibility is limited to code within the braces. Therefire, the cout statement doesn't recognize that odd exists. You probably want to declare both odd and even outside of the loop so that they're not reset after each iteration.
deceptikon
Challenge Accepted
3,435 posts since Jan 2012
Reputation Points: 822
Solved Threads: 473
Skill Endorsements: 56
One step forward, two steps back. You're still redefining the arrays inside the loop. Now it's just new variables hiding the ones defined outside the loop. You also always print even[45], which to me is nonsensical.
Compare and contrast:
#include <fstream>
#include <iostream>
using namespace std;
int main()
{
ifstream in("input.txt");
if (in) {
int even[50], odd[50];
int m = 0, n = 0;
int value;
// Read up to 50 numbers from the file
while (in>> value && (m < 50 && n < 50)) {
if (value % 2 == 0 && m < 50)
even[m++] = value;
else if (n < 50)
odd[n++] = value;
}
// Display the results
cout<<"Even\tOdd\n";
for (int i = 0; i < m && i < n; ++i) {
if (i < m)
cout<< even[i];
cout.put('\t');
if (i < n)
cout<< odd[i];
cout.put('\n');
}
}
}
deceptikon
Challenge Accepted
3,435 posts since Jan 2012
Reputation Points: 822
Solved Threads: 473
Skill Endorsements: 56
Yes, what deceptikon gave in in lines 18 & 20 fill th odd/even arrays. When done filling those arrays, m and n give you the count of items in each of them. With that, you can examine the arrays to find the various values you need.
deceptickon, my Grammar Nazi hat is an all emcompassing model, good for all that is written poorly on the interwebz.
vmanes
Postaholic
2,015 posts since Aug 2007
Reputation Points: 1,283
Solved Threads: 242
Skill Endorsements: 6
good for all that is written poorly on the interwebz.
That's a guaranteed ulcer. ;)
deceptikon
Challenge Accepted
3,435 posts since Jan 2012
Reputation Points: 822
Solved Threads: 473
Skill Endorsements: 56
m is the count of how many items in even[]. Don't change m after the array has been filled.
line 3: max should not be set to 0, what if all the values read in are negative? Set max to be the first element of the array.
line 4: uses m as the limit.
line 5: compare max to the element of the array at index i
line 6: if array element is larger than max, set max to that element's value. Don't mess with i inside the loop!
vmanes
Postaholic
2,015 posts since Aug 2007
Reputation Points: 1,283
Solved Threads: 242
Skill Endorsements: 6
Check the direction of your comparison in line 5
vmanes
Postaholic
2,015 posts since Aug 2007
Reputation Points: 1,283
Solved Threads: 242
Skill Endorsements: 6
That looks correct for finding max of that array. You will use similar methods to fin minimums, and do a bit different work in the loops to find averages and such.
vmanes
Postaholic
2,015 posts since Aug 2007
Reputation Points: 1,283
Solved Threads: 242
Skill Endorsements: 6