Answer:
/*
* Program to write data to text file.
*/
#include<iostream>
#include<fstream>
#include<string.h>
using namespace std;
int main()
{
//Variable to name and age of a person.
string name;
int age;
cout<<"Enter your name"<<endl;
cin>>name;
cout<<"Enter your age"<<endl;
cin>>age;
//Creating object of ofstream.
ofstream file;
file.open ("outdata.txt");
//Writing data to file named outdata.txt
file<<name<<" "<<age;
file.close();
}
Output:
Enter your name
John
Enter your age
25
Content of Output.txt:
John 25
Explanation:
To write data to a file ofstream object is used. An ofstream object file is created first and then using this object a file named "outdata.txt" is created.
Finally using shift operator (<<), by means of operator overloading, name and age of person is transferred to file outdata.txt.
Answer:
Huffman code is use for encoding the language. The entropy when calculated is 1.5.
Explanation:
Using Huffman Coding scheme to encode:
The huffman coding scheme is described in the attachment.
To find entropy; we use the formula given below:
H = ∑
where H = Entropy and p = probability
p(A) = 50% = 1/2
p(B) = 25% = 1/4
p(C) = 25% = 1/4
Answer:
- import java.util.Arrays;
- public class Main {
-
- public static void main(String[] args) {
- String [] first = {"David", "Mike", "Katie", "Lucy"};
- String [] middle = {"A", "B", "C", "D"};
- String [] names = makeNames(first, middle);
-
- System.out.println(Arrays.toString(names));
- }
-
- public static String [] makeNames(String [] array1, String [] array2){
-
- if(array1.length == 0){
- return array1;
- }
-
- if(array2.length == 0){
- return array2;
- }
-
- String [] newNames = new String[array1.length];
-
- for(int i=0; i < array1.length; i++){
- newNames[i] = array1[i] + " " + array2[i];
- }
-
- return newNames;
- }
- }
Explanation:
The solution code is written in Java.
Firstly, create the makeNames method by following the method signature as required by the question (Line 12). Check if any one the input string array is with size 0, return the another string array (Line 14 - 20). Else, create a string array, newNames (Line 22). Use a for loop to repeatedly concatenate the string from array1 with a single space " " and followed with the string from array2 and set it as item of the newNames array (Line 24-26). Lastly, return the newNames array (Line 28).
In the main program create two string array, first and middle, and pass the two arrays to the makeNames methods as arguments (Line 5-6). The returned array is assigned to names array (Line 7). Display the names array to terminal (Line 9) and we shall get the sample output: [David A, Mike B, Katie C, Lucy D]
The answer to the question is true