I was wondering how to compare one array into two arrays but with some rules:
In my code I need to type 8 digits to make the cycle – OK, I did it. Now I need to compare the array = m[3] to two different arrays:
first array must be with (if m[i]%2==0) ...
second array must be with (if m[i]%2!=0) ...
So if I type from keyboard those three rows in my Main array (m[3]):
12345678
12345689
12344331
After typing them, I need to set the in those two different arrays and here I think I need to make the char(string) to integer to make the check with %, or to somehow do the check only on the last digit (it will work the same way).
So here goes the next step after typing the 3 rows:
arrA=12345678
arrB=12345689 12344331
#include <iostream>
#include <conio.h>
#include <string>
#include <cstdlib>
#include <stdlib.h>
using namespace std;
int main()
{
int i,n;
char m[3];
for(i=1; i<=3; i++)
{
cout<<i<<". Fak nomer: "<<endl;
do
{
cin>>m[i];
gets(m);
}
while (strlen(m)!=7);
cout<<"As integer: "<<atoi(m);
}
}
From what I understand, you are trying to read three positive integers into an array called
m, but you want to ensure the following:m[0]) is evenm[1]) is oddIt would be a lot easier if
mcan be an array of integers, then a conversion from a string to an int is not required. To read the three integers from the console, use this:By making
man array of integers, checking for even or odd becomes easier: