Lab 4

Lab Description

In this laboratory experiment, the differences between C++ coding and HLA coding will be observed and understood. The objective is to create a calculator code that can perform certain arithmetic operations such as factorials, exponentation to any power of two or multiply using any value the user wants as an input. In order to perform this experiment, previous coding kills for C language will be required. The goal is to enter a value and receive a result based no the math operation the user wishes to take. The screen returns to giving the user another option to choose or simply close the program by pressing the final option, which in this experiment is by pressing 4.

There is certainly differences between C++ coding and HLA coding. In C++, there is no need to find a register to place data into compared to HLA coding. The coding is based on various statements like strings, functions, classes, loops, and vice versa. While HLA does support these functions, each operation must return a result to the main register and start from the beginning again, like what occured in lab 3. In C++ coding, there commands just pay attention to the value given first. If it agrees with the condition of the loop, it executes the output otherwise it looks at the next condition, especially if loops are involved. If continues this step unless the value does not work with the code.

Code

#include	// includes input and output stream such as cin and cout
using namespace std;
int Loop();

int n, choice;
static int EndValue;




int main()// function protocol... like "begin program"
{

// EndValue == 0; because of the static int.


	while(choice!=4)
	{
		Loop();
	}
	
}


int Loop()
{

	cout << "This is Rafael's and Cynthia's C++ version of the second calculator."<<"\n" <<  "Enter the value for the operation you would like to use." << "\n" << "(1) factorial of n" << "\n" << "(2) Product Factorial" << "\n" << "(3) the expenential of 2^(n)" << "\n" <> choice; // gets choice
  while(choice>4||choice<1) // checks if choice was not from list 
{
	cout << "This is Rafael's and Cynthia's C++ version of the second calculator."<<"\n" <<  "Enter the value for the operation you would like to use." << "\n" << "(1) factorial of n" << "\n" << "(2) Product Factorial" << "\n" << "(3) the expenential of 2^(n)" << "\n" << "(4) Exit the program." <> choice; // gets choice
}


   if(choice==1) // for choice one do the code bellow
{
	cout << "Enter the value to use in this operation." << "\n Value" <> n; // gets the value to factorial sum
	
	while(n>0)//while n is greater than 0 do the bellow
	{
		EndValue = EndValue+n; // adds n to zero then adds the reduced n to the old n
		n= n-1;
		}
	cout << "The Value is  " << EndValue << "\n";
	EndValue=0;
	n=0; // Resets the values for the loop so that the new values will not be changed
}


  if(choice==2)
{
	cout << "Enter the value to use in this operation." << "\n Value" <> n; // gets the value to factorial sum
	EndValue=1;
	while(n>0)
	{
		EndValue = EndValue*n;
		n= n-1;
		}
	cout << "The Value is  " << EndValue << "\n";
	EndValue=0;
	n=0; // Resets the values for the loop so that the new values will not be changed
}


  if(choice==3)
{

	cout << "Enter the value to use in this opperation." << "\n Value" <> n; // gets the value to factorial sum
	
	EndValue = 1;
	while(n>0)
	{
		EndValue = EndValue*2;
		n = n-1;
		}
	cout << "The Value is  " << EndValue << "\n";
	EndValue=0;
	n=0; // Resets the values for the loop so that the new values will not be changed
}


  if(choice==4)
{
	cout << "Lab test complete. " << "\n" << "The end." << "\n";
}
  while(choice!=4)
{
	Loop();
}

}

Screenshot

lab4sc

Leave a Reply

Your email address will not be published. Required fields are marked *