Answer to Question #211770 in C++ for Need it ASAP

Question #211770

Topic: Basics of C++ Programming Language "ARRAY IN C++"


Codes discussed will be posted here. Write your conclusion on the File stream discussion. 



Filename: Ch8_CodeDetection.cpp


Code:

-->>https://drive.google.com/file/d/1iH_fVlO2SY4LOh3wqqKorsaggZeqGXWC/view?usp=sharing


*Note need proper codes and conclusion on how it's done and about the results. And please give the proper solution and explanation.


Note: Place your conclusion on why did it turn out like that and give your reasons why the results turned like that, one paragraph will do for the explanation and conclusion.


Note: Please place the proper conclusion and explanations and results of the codes which were given.


1
Expert's answer
2021-06-29T06:14:44-0400
//************************************************************
// Author: D.S. Malik
//
// Program: Check Code
// This program determines whether a code is transmitted
// correctly.
//************************************************************
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
const int MAX_CODE_SIZE = 250;


void readCode(ifstream& infile, int list[], int& length, bool& lenCodeOk);
void compareCode(ifstream& infile, ofstream& outfile,const int list[], int length);


int main()
{
	//Step 1
	int codeArray[MAX_CODE_SIZE]; //array to store the secret code
	int codeLength; //variable to store the length of the secret code
	bool lengthCodeOk; //variable to indicate if the length of the secret code is less than or equal to 250
	
	ifstream incode; //input file stream variable
	ofstream outcode; //output file stream variable
	
	char inputFile[51]; //variable to store the name of the input file
	char outputFile[51]; //variable to store the name of the output file
	cout << "Enter the input file name: ";
	cin >> inputFile;
	cout << endl;


	//Step 2
	incode.open(inputFile);
	if (!incode)
	{
		cout << "Cannot open the input file." << endl;
		return 1;
	}
	
	cout << "Enter the output file name: ";
	cin >> outputFile;
	cout << endl;
	outcode.open(outputFile);
	readCode(incode, codeArray, codeLength,	lengthCodeOk); 
	
	//Step 3
	if (lengthCodeOk) 		//Step 4
	compareCode(incode, outcode, codeArray,	codeLength);
	else
	cout << "Length of the secret code "<< "must be <= " << MAX_CODE_SIZE<< endl; //Step 5
	incode.close();
	outcode.close();
	return 0;
}


void readCode(ifstream& infile, int list[], int& length,bool& lenCodeOk)
{
	lenCodeOk = true;
	infile >> length; //get the length of the secret code
	if (length > MAX_CODE_SIZE)
	{
		lenCodeOk = false;
		return;
	}
	//Get the secret code.
	for (int count = 0; count < length; count++)
	infile >> list[count];
}


void compareCode(ifstream& infile, ofstream& outfile,const int list[], int length)
{
	//Step a
	int length2;
	int digit;
	bool codeOk;
	codeOk = true; //Step b
	infile >> length2; //Step c
	if (length != length2) //Step d
	{
		cout << "The original code and its copy "<< "are not of the same length."<< endl;
		return;
	}	
	outfile << "Code Digit Code Digit Copy"<< endl;
	for (int count = 0; count < length; count++) //Step e
	{
		infile >> digit; //Step e.1
		outfile << setw(5) << list[count]<< setw(17) << digit; //Step e.2
		if (digit != list[count]) //Step e.3
		{
			outfile << " code digits are not the same"<< endl;
			codeOk = false;
		}
		else
		outfile << endl;
	}
	if (codeOk) //Step f
		outfile << "Message transmitted OK."<< endl;
	else
		outfile << "Error in transmission. "<<"Retransmit||"<<endl;
}


Explanation:

The given code is described in 4-steps as follows:

Steo-1: An input file is to be in on prompt. The input file may be text file e.g D:\\Msg.txt

If the Input file is not present on disk, then the code will exit out with error that the file dos not exist.

Step-2: Next, an output file name is to be enterd in. This file is created after the code is successfully run.


Step-3: Here, the input file is read. The number of characters are counted and compared to MAX_CODE_SIZE=250.

If the no. of characters in input file are more than MAX_CODE_SIZE, the program will generate error and terminate.


Step-4: The input file length is compared in the compare function, where the input file is again compared with length of secret code.

If the length <= MAX_CODE_SIZE, then the same is transmitted else error is generated and program is terminated with error.


Conclusion:

The program is transmitting the secret code into a output file as eneterd on prompt. This file is created when the program runs successfully.

The input file is read from the disk if it exists otherwise the error is generated and program is terminated. If the input file is read successfully,

the code checks the secret message length with MAX_CODE_SIZE=250. If the length is OK, then it is passed on or transmitted via the link and the same is written to output file.


The code is working fine. I have edited the code in a correct format in a readable format.


Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS