Reputation: 43
I have a task to sort an integer array into an even number array and odd number array. Then i have to show what digits has been placed in which. However, in my code not all places of the array are occupied, so in the end i receive random numbers when I want to show what the arrays odds and evens contain. Instead of random numbers I would like to have literally nothing added in their place.
I did the following:
int main()
{
int evens[10], whole[10], odds[10], i;
printf("Enter 10 integer(/whole) numbers\n");
for (i = 0; i < 10; i++)
{
scanf("%d", &whole[i]);
if (whole[i] % 2 == 0)
{
evens[i] = whole[i];
else odds[i] = whole[i];
}
}
printf("Your even numbers are the following:\n");
for (i = 0; i < 10; i++)
{
printf("%d\n", evens[i]);
}
printf("Your odd numbers are the following:\n");
for (i = 0; i < 10; i++)
{
printf("%d\n", odds[i]);
}
return 0;
}
and then I get this output for having entered digits from 1 to 10:
Your even numbers are the following:
-1832565259
2
1985901418
4
4200864
6
4200958
8
74
10
Your odd numbers are the following:
1
4200864
3
6356652
5
1986281400
7
1985964450
9
1985901427
So how do i get an odds/even array without these random digits like 1985964450 in between? Is there a command to add literally nothing instead?
Upvotes: 2
Views: 264
Reputation: 96
int main()
{
int evens[10], temp, odds[10], i;
int oddIndex = 0, evenIndex = 0;
printf("Enter 10 integer(/whole) numbers\n");
for (i=0; i<10; i++)
{
scanf("%d", &temp);
if(temp%2)
odd[oddIndex++]=temp;
else
even[evenIdex++]=temp;
}
printf("Your even numbers are the following:\n");
for (i=0; i<10; i++)
printf("%d\n", evens[i]);
printf("Your odd numbers are the following:\n");
for (i=0; i<10; i++)
printf("%d\n", odds[i]);
return 0;
}
Upvotes: 1
Reputation: 1820
It is better to have two variables that represents indexes, one you use to add odd numbers, other to add even numbers. Then you will have two arrays without redundant data :)
int evensIndex = 0;
int oddsIndex = 0;
for (i=0; i<10; i++)
{
scanf("%d", &whole[i]);
if (whole[i] %2 == 0)
{
evens[evensIndex] = whole[i];
evensIndex++;
}
else
{
odds[oddsIndex] = whole[i];
oddsIndex++;
}
}
Upvotes: 2
Reputation: 3538
You should have a counter of odds and a counter of evens.
int oddcount = 0;
int evencount = 0;
When you decide that a number is even, you use this counter to know where in the array it should go. For example:
if (whole[i] % 2 == 0) {
evens[evencount] = whole[i];
evencount++;
}
Notice that evencount
not only gives you the number of even numbers, but since arrays indices begin at zero, it also tells you what is the position of the next even number.
Then you modify your for
loops at the end to use the actual number of even and odd numbers that were typed. You can even check for zero and print a specific message such as No even numbers supplied
.
Also, unless you have been specifically asked to keep the input numbers in an array, you don't need whole
. You can do just like so:
int input;
for (i=0; i<10; i++)
{
scanf("%d", &input);
if (input %2 == 0)
/* ... */
else
/* ... */
}
As a final remark, you should indent your code. Indentation is simply augmenting the number of spaces before the code (like I did inside the if
s above). It is very important to indent your code because it makes the structure of your code clear. For a more comprehensive discussion on this, read here: Importance of code indentation.
Upvotes: 3