Click here to Skip to main content
15,897,371 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need to write a program, Whose output should be like, every first element of an array is 1 and every second element is 0. I need to check in the array if the element is odd then print out 1 and if the element is odd then print out 0. I need to check this condition in if condition.Just like Finite state machine. I am failing in doing so. Any help would be appreciated.

What I have tried:

using namespace std;

int main(){
	
	cout<<"Enter the size of array"<<"\n";
	
	int n,i;
	cin>>n;
	int array[n];
	for( i=0; i<n;i++){
	if(array[i] % 2!=0 && array[i] % 2==0){
		
	}
	cout<<"1,0";
			
}

}
Posted
Updated 27-May-18 6:46am
v2
Comments
[no name] 27-May-18 10:45am    
a.) if(array[i] % 2!=0 && array[i] % 2==0) in words:
if value is odd and value is even

b.) Quote: "I need to check in the array if the element is odd then print out 1 and if the element is odd then print out 0"
Richard MacCutchan 27-May-18 11:09am    
Your main problem is that you never enter any values into the array.
[no name] 27-May-18 11:16am    
A very good Point which I missed :-O
Nabeel Munir 27-May-18 11:37am    
Hello, Could you please be a little more specific?

After trying the code below:
for(i= 0; i < n; i++)
{
if(array[i] % 2 !=0 || array[i] % 2 ==0){
cout<<"1,0,";
}
}
when i execute the code, For example user enters size of array = 4, The output it gives is, 1,0,1,0,1,0,1,0... Which is still not correct. what i need is, If user enters Value 4, The output should be: 1,0,1,0. every odd should be 1, every even should be 0.
[no name] 27-May-18 11:41am    
"every odd should be 1, every even should be 0."
Array Index or Array Content?

What is this now if(array[i] % 2 !=0 || array[i] % 2 ==0)?
It will be always true....

Either a number is odd or even. To check for even (value % 2) == 0 is enough. And if the result of this check is false you know the number is odd....

See also my comments. Assuming you Need to print "1" for odd numbers and "0" for even number you don't even Need an if.

This because
1 % 2 results in 1
2 % 2 results in 0
3 % 2 results in 1
4 % 2 results in 0
... and so on. Consult e.g. wiki for detailed Information about the Modulo Operator.


Version I Check Array values
Now I'm not familar with cout but something like this should do the job:
C++
for(i= 0; i < n; i++)
{
   cout << array[i] % 2;
}


[Edit0]
Version II Check index
C++
for(i= 0; i < n; i++)
{
   cout << i % 2;
}


[Edit1]
Version III Check index'
C++
for(i= 0; i < n; i++)
{
   cout << (i + 1) % 2;
}


Note: Keep in mind the comment of Richard MacCutchan - Professional Profile[^] : "Your main problem is that you never enter any values into the array."
 
Share this answer
 
v5
Comments
Nabeel Munir 27-May-18 11:46am    
@0x01AA,Could you please be a little more specific? I need code to go through every index in the array and check if the index is odd it should print 1 and if even it should print 0. if I go for cout << array[i] % 2; that you have suggested, and execute the program and enter for example array size: 4, the output it gives is: 0010.
And what do you mean by,Your main problem is that you never enter any values into the array."
[no name] 27-May-18 11:50am    
See Version II in updated answer. For this case to Array itself is useless which Looks odd (in other words 1%2) to me :-)
Nabeel Munir 27-May-18 12:02pm    
It works, but it is giving the opposite result, Like: 0 1 0 1. I need 1 0 1 0.
[no name] 27-May-18 12:05pm    
See Version III and ___please____ try to understand it an not just copy it!! ;)
Nabeel Munir 27-May-18 12:06pm    
Yeah that is what i want to do, I want to understand how it works. That is why i also need some explanation.
When you want to enter the array size you need to allocate dynamic memory.
C++
int *array = new int[n];
//work it 
delete array;
And you need a loop to fill the array.

Tip: use braces in your operations for clarity and avoiding bugs.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900