Answer:
/ declare the necessary header files.
#include <iostream>
#include <string>
#include <vector>
using namespace std;
// declare the main function.
int main()
{
// declare a vector.
vector<string> words;
vector<int> counts;
// declare variables.
int size;
string str;
cin >> size;
// start the for loop.
for(int i = 0; i < size; ++i)
{
// input string.
cin >> str;
words.push_back(str);
}
// start the for loop.
for(int i = 0; i < size; ++i)
{
int count = 0;
// start the for loop.
for(int j = 0; j < words.size(); ++j)
{
// check the condition.
if(words[j] == words[i])
{
count++;
}
}
counts.push_back(count);
}
// start the for loop.
for(int i = 0; i < size; ++i)
{
// display result on console.
cout << words[i] << "\t" << counts[i] << endl;
}
return 0;
}
Explanation:
b) onto but not one-to-one
c) neither one-to-one nor onto
Answer:
Let f be a function
a) f(n) = n²
b) f(n) = n/2
c) f(n) = 0
Explanation:
a) f(n) = n²
This function is one-to-one function because the square of two different or distinct natural numbers cannot be equal.
Let a and b are two elements both belong to N i.e. a ∈ N and b ∈ N. Then:
f(a) = f(b) ⇒ a² = b² ⇒ a = b
The function f(n)= n² is not an onto function because not every natural number is a square of a natural number. This means that there is no other natural number that can be squared to result in that natural number. For example 2 is a natural numbers but not a perfect square and also 24 is a natural number but not a perfect square.
b) f(n) = n/2
The above function example is an onto function because every natural number, lets say n is a natural number that belongs to N, is the image of 2n. For example:
f(2n) = [2n/2] = n
The above function is not one-to-one function because there are certain different natural numbers that have the same value or image. For example:
When the value of n=1, then
n/2 = [1/2] = [0.5] = 1
When the value of n=2 then
n/2 = [2/2] = [1] = 1
c) f(n) = 0
The above function is neither one-to-one nor onto. In order to depict that a function is not one-to-one there should be two elements in N having same image and the above example is not one to one because every integer has the same image. The above function example is also not an onto function because every positive integer is not an image of any natural number.
Answer:
See explaination
Explanation:
Taking a look at the The Logic function, which states that an output action will become TRUE if either one “OR” more events are TRUE, but the order at which they occur is unimportant as it does not affect the final result. For example, A + B = B + A.
Alternatively the Most significant bit which is also known as the alt bit, high bit, meta bit, or senior bit, the most significant bit is the highest bit in binary.
See the attached file for those detailed logic functions designed with relation to the questions asked.
The decimal number which is equivalent to binary number 11111011 is 251.
Binary numbers are base-2 numbers, which means they are composed of only two digits: 0 and 1.
Each digit in a binarynumber represents a power of 2, starting from the rightmost digit.
The rightmost digit represents 2⁰ (which is 1), the next digit represents 2¹(which is 2), the next represents 2² (which is 4), and so on.
Given the binary number 11111011:
1 × 2⁷ + 1 × 2⁶ + 1 × 2⁵ + 1 × 2⁴ + 1 × 2³ + 0 × 2² + 1 × 2¹ + 1 × 2⁰
Simplifying each term:
128 + 64 + 32 + 16 + 8 + 0 + 2 + 1
= 251
Hence, 251 is the decimalnumber which is equivalent to binary number 11111011.
To learn more on Binary numbers click here:
#SPJ3
Answer:
251
Explanation:
Answer:
pkt = int(input("Pocket Number: "))
if pkt == 0:
print("Green")
elif pkt >= 1 and pkt <=10:
if pkt%2 == 0:
print("Black")
else:
print("Red")
elif pkt >= 11 and pkt <=18:
if pkt%2 == 0:
print("Red")
else:
print("Black")
elif pkt >= 19 and pkt <=28:
if pkt%2 == 0:
print("Black")
else:
print("Red")
elif pkt >= 29 and pkt <=36:
if pkt%2 == 0:
print("Red")
else:
print("Black")
else:
print("Error")
Explanation:
The program was written in Python and the line by line explanation is as follows;
This prompts user for pocket number
pkt = int(input("Pocket Number: "))
This prints green if the pocket number is 0
if pkt == 0:
print("Green")
If pocket number is between 1 and 10 (inclusive)
elif pkt >= 1 and pkt <=10:
This prints black if packet number is even
if pkt%2 == 0:
print("Black")
Prints red, if otherwise
else:
print("Red")
If pocket number is between 11 and 18 (inclusive)
elif pkt >= 11 and pkt <=18:
This prints red if packet number is even
if pkt%2 == 0:
print("Red")
Prints black, if otherwise
else:
print("Black")
If pocket number is between 19 and 28 (inclusive)
elif pkt >= 19 and pkt <=28:
This prints black if packet number is even
if pkt%2 == 0:
print("Black")
Prints red, if otherwise
else:
print("Red")
If pocket number is between 29 and 36 (inclusive)
elif pkt >= 29 and pkt <=36:
This prints red if packet number is even
if pkt%2 == 0:
print("Red")
Prints black, if otherwise
else:
print("Black")
Prints error if input is out of range
else:
print("Error")
Answer:
int main()
{
int A1= {5,3,4,8,9,0,7};
int SArri1 = sizeof(A1);
printf("Original array: ");
for (int i=0; i < SArri1;i++)
printf("", A1[i] );
int i, sum = 20, n= 0;
printf("\nArray pairs whose sum equal to 20: ");
for (int i=0; i<SArri1; i++)
for (int j=i+1; j<SArri1; j++)
if (A1[i]+A1[j] == sum)
{
Printf(“\n”, array1[i])
Printf(“,”,array1[j]);
n++;
}
printf("\nNumber of pairs whose sum equal to 20: ",n)
return 0;
}
Explanation:
First of all, you should create the array of integers, and put random numbers. Then you have to save in a constant the size of the array (in this code is called SArr1) With a for you can print all the numbers that are on the array because then you will print all array pairs whose sum is equal to a specified numer. in this code we are going to use 20.
then with a condition (if) you are going to compare one of number of the array with all the others and check if its equal to 20. If yes, it going to print the numbers that answer to that condition and it's going to add +1 to the variable n (for this you will need to use two bucles for)
Then you can print the number of pairs whose sum is equal to 20 by printing n
Answer:
Following are the code to this question:
public class Pig //Defining class Pig
{
private String name; //Defining string variable name
private int age; // Defining integer variable age
private double weight; // Defining double variable weight
Pig (String name, int age, double weight) //Defining parameterized constructor
{
super(); //using super key
this.name = name; //holding value in name variable
this.age = age; // holding value in age variable
this.weight = weight; // holding value in weight variable
}
String getName() //Defining method getName
{
return name; //return name value
}
void setName(String name) // Defining method setName
{
this.name = name; //hold name value
}
int getAge() // Defining method getAge
{
return age; //return value
}
void setAge(int age) // Defining method setAge
{
this.age = age; // hold age value
}
double getWeight() //Defining method getWeight
{
return weight; //return weight value
}
void setWeight(double weight) //Defining method setWeight
{
this.weight = weight; //hold weight value
}
void display() //Defining method display
{
System.out.println("Name:" + name + " Age:" + age + " Weight:" + weight); //print value
}
public static void main(String[] ar) //Defining main method
{
Pig onc = new Pig("Jig",5,14.5); //creating class object and called parameterized constructor
onc.display();//calling display method
}
}
Output:
please find the attachment.
Explanation:
In the given java program, a class "Pig" is declared, in which three name, age, and weight is defined which differs in datatypes, in the next step, parameterized constructor, get and set method, and display method declared, which can be described as follows: