-
January 11th, 2014, 06:46 PM
#1
How to write an array into 3 diff. arrays? odd, even, negative
question : Place the even lucky numbers in an array called evenList, the odd lucky numbers in an array called oddList, and the negative lucky numbers in an array called negList.
//So in main main i passed the array as parameter and the size;
void lucknumberlist(int favnum[], int size)
{
int even = 0, odd = 0, neg = 0;
int evenArray[even];
int oddArray[odd];
int negArray[neg];
if(favnum[even] % 2 == 0)
{
evenArray[even] = favnum[even];
even++ ;
}
else if(favnum[odd] % 2 == 1)
{
oddArray[odd] = favnum[odd];
odd++;
}
else
{
negArray[neg] = favnum[neg];
neg++;
}
//tried printing to check if worked.. but no luck!!
for (int i = 0; i < size; i++)
{
cout << oddArray[i] << " ";
}
}
-
January 11th, 2014, 07:06 PM
#2
Re: How to write an array into 3 diff. arrays? odd, even, negative
Code:
int even = 0, odd = 0, neg = 0;
int evenArray[even];
In standard c++, this is not a valid way to define an array as even is a variable and not a const. Even if this way of defining an array was legal, you would have an array of 0 elements as even is 0.
You would be better off using vectors to store the even, odd and negative numbers.
Also when posting code, please use code tags. Go Advanced, select the code and click '#'.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|