In: Computer Science
Implement C++ program for each of the following.
Let D = [-48, -14, -8, 0, 1, 3, 16, 23, 26, 32, 36] Each answer is
either True or False. If False, give example
of number(s) that make it False. i.e. The Counterexample. ∀x∈D, if
x is odd then x > 0
Let D = [-48, -14, -8, 0, 1, 3, 16, 23, 26, 32, 36] Each answer is
either True or False. If False, give example
of number(s) that make it False. i.e. The Counterexample. ∀x∈D, if
x is less than 0 then x is even.
Let D = [-48, -14, -8, 0, 1, 3, 16, 23, 26, 32, 36] Each answer is
either True or False. If False, give example
of number(s) that make it False. i.e. The Counterexample. ∀x∈D, if
x is even then x <= 0;
Let D = [-48, -14, -8, 0, 1, 3, 16, 23, 26, 32, 36] Each answer is
either True or False. If False, give example
of number(s) that make it False. i.e. The Counterexample. ∀x∈D, if
the ones digit of x is 2, then the tens
digit is 3 or 4.
Let D = [-48, -14, -8, 0, 1, 3, 16, 23, 26, 32, 36] Each answer is
either True or False. If False, give example
of number(s) that make it False. i.e. The Counterexample. ∀x∈D, if
the ones digit of x is 6, then the tens
digit is 1 or 2.
The above problem can be solved by following these steps-
STEP 1- We will first declare and initialize the array as given in the question and find out its size so that we can iterate over its elements.
STEP 2- Here we will deal with the first conditon:if x is odd then x > 0. To find out the numbers which do not satisfy this condition, we check if a number is odd or not. If its odd, in the nested if, we check if number>0. If number is greater than 0, move on to next element, otherwise print the number. We also keep a flag variable to keep a count of such counter examples. If we do not get any counter examples, print None as output.
STEP 3- Here we will deal with the second conditon:if x is less than 0 then x is even. To find out the numbers which do not satisfy this condition, we check if a number is less than 0. If yes, in the nested if, we check if number is even or not. If number is even, move on to next element, otherwise print the number. We also keep a flag variable to keep a count of such counter examples. If we do not get any counter examples, print None as output.
STEP 4- Here we will deal with the third conditon:if x is even then x <= 0. To find out the numbers which do not satisfy this condition, we check if a number is even. If yes, in the nested if, we check if number <=0. If number<=0, move on to next element, otherwise print the number. We also keep a flag variable to keep a count of such counter examples. If we do not get any counter examples, print None as output.
STEP 5- Here we will deal with the fourth conditon:if the ones digit of x is 2, then the tens digit is 3 or 4. To find out the numbers which do not satisfy this condition, we check if ones digit is 2. If yes, in the nested if, we find out the tens digit and check if it is 3 or 4. If yes, move on to next element, otherwise print the number. We also keep a flag variable to keep a count of such counter examples. If we do not get any counter examples, print None as output.
STEP 6- Here we will deal with the fourth conditon:if the ones digit of x is 6, then the tens digit is 1 or 2. To find out the numbers which do not satisfy this condition, we check if ones digit is 6. If yes, in the nested if, we find out the tens digit and check if it is 1 or 2. If yes, move on to next element, otherwise print the number. We also keep a flag variable to keep a count of such counter examples. If we do not get any counter examples, print None as output.
C++ Code-
#include <bits/stdc++.h>
using namespace std;
//driver method
int main()
{
//initialize the array of numbers
int D[] = {-48, -14, -8, 0, 1, 3, 16, 23, 26, 32, 36};
//calculate the size of array
int size = sizeof(D)/sizeof(D[0]);
cout<<"\nExample of number(s) that make condition \"If x is
odd, then x>0\" false.\n";
//if x is odd, then x>0
int flag = 0; //flag to keep a count of numbers not satisfying the
condition
//iterate over all the elements
for(int i = 0;i<size;i++){
//if number x is odd
if(D[i]%2 == 1){
//then, number x>0
//nested if
if(D[i]>0){
//do nothing as the number satisfies the condition
continue;
}
else{
//increment flag and print the number
flag++;
cout<<D[i]<<endl;
}
}
}
//if all numbers satisfy the condition, that means flag is 0, print
none as output
if(flag == 0){
cout<<"None\n\n";
}
flag = 0; //flag to keep a count of numbers not satisfying the
condition
cout<<"\nExample of number(s) that make condition \"If x is
less than 0, then x is even\" false.\n";
//if x is less than 0, then x is even
//iterate over all the elements
for(int i = 0;i<size;i++){
//if number x is less than 0
if(D[i]<0){
//then number x is even
//nested if
if(D[i]%2 == 0){
//do nothing as the number satisfies the condition
continue;
}
else{
//increment flag and print the number
flag++;
cout<<D[i]<<endl;
}
}
}
//if all numbers satisfy the condition, that means flag is 0, print
none as output
if(flag == 0){
cout<<"None\n\n";
}
flag = 0; //flag to keep a count of numbers not satisfying the
condition
cout<<"\nExample of number(s) that make condition \"If x is
even then x <= 0\" false.\n";
//if x is even then x <= 0
//iterate over all the elements
for(int i = 0;i<size;i++){
//if x is even
if(D[i]%2 == 0){
//then x<=0
//nested if
if(D[i]<=0){
//do nothing as the number satisfies the condition
continue;
}
else{
//increment flag and print the number
flag++;
cout<<D[i]<<endl;
}
}
}
//if all numbers satisfy the condition, that means flag is 0, print
none as output
if(flag == 0){
cout<<"None\n\n";
}
flag = 0; //flag to keep a count of numbers not satisfying the
condition
cout<<"\nExample of number(s) that make condition \"If the
ones digit of x is 2, then the tens digit is 3 or 4\"
false.\n";
//if the ones digit of x is 2, then the tens digit is 3 or 4.
//iterate over the elements
for(int i = 0;i<size;i++){
//if ones digit is 2
if(D[i]%10 == 2){
int number = D[i];
number/=10; //divide the number by 10 to remove ones digit
//then tens digit is 3 or 4
//nested if
if(number%10 == 3 || number%10 == 4){
//do nothing as the number satisfies the condition
continue;
}
else{
//increment flag and print the number
flag++;
cout<<D[i]<<endl;
}
}
}
//if all numbers satisfy the condition, that means flag is 0, print
none as output
if(flag == 0){
cout<<"None\n\n";
}
flag = 0; //flag to keep a count of numbers not satisfying the
condition
cout<<"\nExample of number(s) that make condition \"If the
ones digit of x is 6, then the tens digit is 1 or 2\"
false.\n";
//if the ones digit of x is 6, then the tens digit is 1 or 2.
//iterate over elements of array
for(int i = 0;i<size;i++){
//if ones digit is 6
if(D[i]%10 == 6){
//remove ones digit and then taking mod 10 will give us tens
digit
int number = D[i];
number/=10;
//then tens digit is 1 or 2
//nested if
if(number%10 == 1 || number%10 == 2){
//do nothing as the number satisfies the condition
continue;
}
else{
//increment flag and print the number
flag++;
cout<<D[i]<<endl;
}
}
}
//if all numbers satisfy the condition, that means flag is 0, print
none as output
if(flag == 0){
cout<<"None\n\n";
}
return 0;
}
To help in better understanding of comments and indentation, given below is image of code -
OUTPUT-
If this answer helps, please give an up vote and Feel free to comment for any query.