Hello all, like always : D

I'm working on a structure program that has an array (m) that asks the user to enter its values, and then it transfers the even numbers to an array (even) and the odd to another (odd). Both (evan) and (odd) arays are in a structure (x) ; plus, I have made another structure (y) that is connected to (x) and has it same attributes.

My problem is, that after I run the program and add the numbers, I don't get the values in the (odd) and (even) arrays ; but instead I get their memory addresses I guess!

The code is:

``````#include<iostream.h>
#include <stdio.h>
#include <conio.h>

struct x
{
int even[20];
int odd[20];
int countEven;
int countOdd;
};

main()
{
int m[20],i,sum=0;

struct x y;

y.countEven = 0;
y.countOdd = 0;

for (i=0;i<20;i++)
{
printf("\n Enter No.[%d] = ",i);
scanf("%d",&m[i]);
sum+=m[i];

if (m[i] % 2 == 0)
y.even [y.countEven++]=m[i];
else
y.odd [y.countOdd++]=m[i];
}
cout<<"Even numbers are:"<<y.even<<endl;
cout<<"Odd numbers are:"<<y.odd<<endl;

return ( 0 ) ;
}``````

Plus in the (cout) area at the end of the program, I wanna add more cool things that displays higher/lower numbers in both arrays, and also the total count/sum of both arrays, so the program will display these things after adding the values by the user:

- Even numbers are: ------------
- Odd numbers are: -------------
- Higher even number: ---------
- Lower even number: ----------
- Higher odd number: -----------
- Lower odd number: -----------
- The sum of even numbers: -------
- The sum of odd numbers: --------

3
Contributors
16
Replies
17
Views
8 Years
Discussion Span
Last Post by endframe
Featured Replies
• 1

In the non-virtual world, how would you perform these actions? Get out a pen and paper and start writing a set of steps to describe how you would perform them. Once you have finished that, you would have algorithms to use as a guide for creating the code. Think of …

...
Both (evan) and (odd) arays are in a structure (x) ; plus, I have made another structure (y) that is connected to (x) and has it same attributes.
...

I think you need to understand what you have created first. What you call 'y' is an object/instance of the custom/user type 'struct x', it is not a separate type definition.

You are getting a memory address because you are displaying the pointer contents rather than the element values. When you access the elements of an array, you need to specify the element you want by using square brackets and an index. For example, to display the value of the fifth element of an array called "sampleArray" you would state `sampleArray[B][4][/B]` not just "sampleArray".

Edited by Fbody: n/a

I think you need to understand what you have created first. What you call 'y' is an object/instance of the custom/user type 'struct x', it is not a separate type definition.

You are getting a memory address because you are displaying the pointer contents rather than the element values. When you access the elements of an array, you need to specify the element you want by using square brackets and an index. For example, to display the value of the fifth element of an array called "sampleArray" you would state `sampleArray[B][4][/B]` not just "sampleArray".

Ok what then I have to add to make it display the whole contents of the array? I tried the following:

``````cout<<"Even numbers are:"<<y.even[i]<<endl;
cout<<"Odd numbers are:"<<y.odd[i]<<endl;``````

But it didn't work.

That's because you don't have it in a loop. Take a closer look at your visual formatting, then add it to your for loop.

Edited by Fbody: n/a

That's because you don't have it in a loop. Take a closer look at your visual formatting, then add it to your for loop.

``````for (i=0;i<20;i++)
{
cout<<"Even numbers are:"<<y.even[i]<<endl;
cout<<"Odd numbers are:"<<y.odd[i]<<endl;
}``````

But when I ran it, it had a problem with showing the values. What's my mistake?

You'll have to elaborate on your problem. The only thing I see is that you don't monitor your array length so you'll probably start outputting garbage values toward the end of the run.

Also, you'll wind up getting:

Even numbers are:####
Odd numbers are:###
Even numbers are:####
Odd numbers are:###
Even numbers are:####
Odd numbers are:###
Even numbers are:####
Odd numbers are:###
Even numbers are:####
Odd numbers are:###
Even numbers are:####
Odd numbers are:###
Even numbers are:####
Odd numbers are:###
Even numbers are:####
Odd numbers are:###

because it outputs the message each time by. You're better off displaying the even prompt then the values then moving on to the odd values as completely separate operations.

For example:

``````cout<<"Even numbers are:";
for (int i = 0; i < numberOfEvens; i++) {
cout << " " << y.even[i];
}
cout<<endl<<"Odd numbers are:";
for (int i = 0; i < numberOfOdds; i++) {
cout << " " << y.odd[i];
}
cout << endl;``````

Edited by Fbody: n/a

You'll have to elaborate on your problem. The only thing I see is that you don't monitor your array length so you'll probably start outputting garbage values toward the end of the run.

Also, you'll wind up getting:

because it outputs the message each time by. You're better off displaying the even prompt then the values then moving on to the odd values as completely separate operations.

For example:

``````cout<<"Even numbers are:";
for (int i = 0; i < numberOfEvens; i++) {
cout << " " << y.even[i];
}
cout<<endl<<"Odd numbers are:";
for (int i = 0; i < numberOfOdds; i++) {
cout << " " << y.odd[i];
}
cout << endl;``````

Got it, and fixed that problem, now what appears to me is that there's a random numbers that shows up in the results.

Here's an example when I entered the numbers from 1 to 20:

Even numbers are:2 4 6 8 10 12 14 16 18 20 0 5599 7335 7343 13459 6351 4118 4064
0 5599
Odd numbers are:1 3 5 7 9 11 13 15 17 19 9771 900 7559 9796 4118 4064 9796 8653
6287 26681

They aren't random numbers, they are old un-initialized information that you are forgetting to take into account in your array traversals.

Because you are splitting 20 numbers up into 2 arrays based on even or odd, each one winds up with less than 20 elements. As a result, you are showing junk/garbage elements. You need to cut off your display after you have shown the valid elements, I've mentioned this 3 times now. I also gave you an example of how.

Perhaps this updated version of my previous example will make it more obvious:

``````//display the even numbers:
cout<<"Even numbers are:";
if (y.numberOfEvens == 0)
cout << " empty set" << endl;
for (int i = 0; i < y.numberOfEvens; i++) {
cout << " " << y.even[i];
}

//display the odd numbers:
cout<<endl<<"Odd numbers are:";
if (y.numberOfOdds == 0)
cout << " empty set" << endl;
for (int i = 0; i < y.numberOfOdds; i++) {
cout << " " << y.odd[i];
}
cout << endl;``````

Edited by Fbody: n/a

They aren't random numbers, they are old un-initialized information that you are forgetting to take into account in your array traversals.

Because you are splitting 20 numbers up into 2 arrays based on even or odd, each one winds up with less than 20 elements. As a result, you are showing junk/garbage elements. You need to cut off your display after you have shown the valid elements, I've mentioned this 3 times now. I also gave you an example of how.

Perhaps this updated version of my previous example will make it more obvious:

``````cout<<"Even numbers are:";
if (y.numberOfEvens == 0)
cout << " empty set" << endl;
else
for (int i = 0; i < y.numberOfEvens; i++) {
cout << " " << y.even[i];
}
cout<<endl<<"Odd numbers are:";
if (y.numberOfOdds == 0)
cout << " empty set" << endl;
else
for (int i = 0; i < y.numberOfOdds; i++) {
cout << " " << y.odd[i];
}
cout << endl;``````

My bad, sorry. I'm a slow learner.

I put it like this:

``````cout<<"Even numbers are:";
for (i=0;i<y.even[i];i++)
{
cout<<y.even[i]<<" ";
}
cout << endl;
cout<<"Odd numbers are:";
for (i=0;i<y.odd[i];i++)
{
cout<<y.odd[i]<<" ";
}
cout << endl;``````

And those numbers are gone from the even array, but not from the odd array!

Didn't see your last edit on your post, gonna try and fix it like how u edited it.

My bad, sorry. I'm a slow learner.

I put it like this:

``````cout<<"Even numbers are:";
for (i=0;i<y.even[i];i++)
{
cout<<y.even[i]<<" ";
}
cout << endl;
cout<<"Odd numbers are:";
for (i=0;i<y.odd[i];i++)
{
cout<<y.odd[i]<<" ";
}
cout << endl;``````

And those numbers are gone from the even array, but not from the odd array!

No worries, just make sure you pay attention. You're not cutting off your traversals correctly.

Besides the arrays, what other variables are part of your "struct x" that you could use? I called them "numOfEvens" and "numOfOdds".

>>Didn't see your last edit on your post, gonna try and fix it like how u edited it.
Looks like we had some overlap. I didn't see this post.

Edited by Fbody: n/a

I don't see where you even tried Fbody's suggestion

``````cout<<"Even numbers are:";
for (int i = 0; i < numberOfEvens; i++) {
cout << " " << y.even[i];
}``````

All you are doing is testing i with a value in the array. That's never going to work.

How many values were put in the array? Keep track. Then use that value in the loop.

Sorry guys for the delay, finaly got it working and understood what was my mistake thanks to Fbody ; I used y.countEven and y.countOdd just like how he was telling me. So this part is done, what I wanna add now is to give the program this results after the user enters the numbers in the array:

- Even numbers are: ------------
- Odd numbers are: -------------
- Higher even number: ---------
- Lower even number: ----------
- Higher odd number: -----------
- Lower odd number: -----------
- The sum of even numbers: -------
- The sum of odd numbers: --------

of course the first two is done successfully. Any start ideas on the rest of them?

Edited by endframe: n/a

In the non-virtual world, how would you perform these actions? Get out a pen and paper and start writing a set of steps to describe how you would perform them.

Once you have finished that, you would have algorithms to use as a guide for creating the code. Think of the computer as a complete imbecile and start describing the steps to the computer in language that it can understand.

What you have already written will serve as a framework to which you add additional statements to perform the required actions.

Edited by Fbody: n/a

In the non-virtual world, how would you perform these actions? Get out a pen and paper and start writing a set of steps to describe how you would perform them.

Once you have finished that, you would have algorithms to use as a guide for creating the code. Think of the computer as a complete imbecile and start describing the steps to the computer in language that it can understand.

What you have already written will serve as a framework to which you add additional statements to perform the required actions.

I'm already thinking of an IF statement to compare between the numbers in an array and a loop to continue this comparing, but isn't there a ready function to use to get the higher and the lower numbers in an array?

Nope you need to do anything like this manually. Besides, it's something every programmer should know how to do.

Edited by Fbody: n/a

Nope you need to do anything like this manually.

Oh, I'll work on it then and post back if I got to any trouble.

Thanks for all your great help.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.