Hello, i need to create an exe file of this code, i cant do it on my VS because
ID: 3906226 • Letter: H
Question
Hello, i need to create an exe file of this code, i cant do it on my VS because it gives me a lot of errors. If anyone can do it and send it to me i'll appreciate it.
#include <cmath>
#include <iostream>
using namespace std;
void dectobin(int *, int);
int bintodec(int *, int);
int n;
int main() {
int a, b, sc, ssor, send, s1, c, sum, allz, rem, quo;
char ch;
start:
cout << "Enter dividend in decimal format ";
cin >> a;
cout << "Enter divisor in decimal format ";
cin >> b;
if (a != 0 && b == 0)
cout << "Infinity "; // Handling Divide-by-Zero Exception
else if (a == 0 && b == 0)
cout << "Undeterminate "; // Handling case when both dividendand
// divisor are zero
else {
if (a < 0 && b < 0) {
a = abs(a);
b = abs(b);
cout << "As both dividend and divisor are negative, -ve sign "
"cancels out ";
}
if (a < 0 && b > 0) {
a = abs(a);
b *= -1;
}
double td;
td = log((double)abs(a)) / log(2.0);
n = (int)ceil(td + 1); // Finding out the number of digits required
// tostore both dividend and divisor in binary
// format
cout << " SC is " << n << endl;
sc = n;
int* Q = new int[n];
int* A = new int[n];
int* M1 = new int[n];
int* M2 = new int[n];
int* pra = new int[n];
int i;
dectobin(Q, a); // Converting the dividend into binary as Q
cout << " Q is ";
for (i = 0; i <= n - 1; i++)
cout << Q[i];
send = Q[0];
dectobin(M1, b); // Converting the divisor into binary as M1
cout << " M is ";
for (i = 0; i <= n - 1; i++)
cout << M1[i];
ssor = M1[0];
for (i = n - 1; i >= 0; i--) // Taking the 2's Complement of M1 and storing it as M2
{
if (M1[i] == 0)
M2[i] = 0;
else
break;
}
M2[i--] = 1;
while (i >= 0) {
if (M1[i]) {
M2[i] = 0;
}
else {
M2[i] = 1;
}
i--;
}
cout << " M'+1 is ";
for (i = 0; i <= n - 1; i++)
cout << M2[i];
int *AQ = 0;
AQ[2 * n]; // Creating a combined array of A and Q named AQ
for (i = 0; i < n; i++)
AQ[i] = 0;
for (i = n; i < 2 * n; i++)
AQ[i] = Q[i - n];
cout << " Initial AQ : ";
for (i = 0; i < 2 * n; i++)
cout << AQ[i];
if (ch == 'Y' || ch == 'y') // Asking the user if he wants to carry
// outanother division or exit the program
goto start;
return 0;
}
}
void dectobin(int *arr, int num) // Function to convert the passed decimal number
// into binary and return the base address of thebinary array
{
int neg, i = 0, j, temp;
if (num < 0) // Checking for a negative number
{
neg = 1;
num = abs(num);
}
while (num != 0) // Continuously dividing the number by 2 andstoring the remainder
{
*(arr + i++) = num % 2;
num /= 2;
}
for (j = i; j <= n - 1; j++)
*(arr + j) = 0;
for (i = 0; i <= (n - 1) / 2; i++) // Reversing the binary array
{
temp = *(arr + i);
*(arr + i) = *(arr + n - 1 - i);
*(arr + n - 1 - i) = temp;
}
if (neg == 1) {
*arr = 1; // Setting sign bit to 1 in case of a negative number
for (i = n - 1; i >= 1; i--) // Taking 2's Complement to store the negative number
if (*(arr + i) == 1)
break;
i--;
while (i >= 1)
*(arr + i) = !*(arr + i--);
}
}
int bintodec(int *p, int y) // Function to convert a binary array todecimal by
// taking its base address as input and returning
// thedecimal equivalent
{
int sum = 0, i;
if (*p == 1) // Taking 2's Complement of the negative number toget back its
// original magnitude
{
for (i = n - 1; i >= 1; i--)
if (*(p + i) == 1)
break;
i--;
while (i >= 1)
*(p + i) = !*(p + i--);
}
for (i = 1; i <= y - 1; i++) // Repeatedly multiplying by increasing powers
// of 2 and storing the sum after each iteration
sum += ((int)pow(2.0, y - 1 - i) * (*(p + i)));
if (*p == 1)
sum *= -1;
return sum; // Returning the equivalent decimal of the passed binary array
}
Explanation / Answer
//ERROR was in the below line of code which was generating error while compile. as AQ array is created with combination of array A and Q, forgot to define the initial size due to which error was generated.
/*
int * AQ ;
int arrsize = 2*n;
//Array to be initialized with new keyword and int type with size. which was not done in your program.
AQ = new int[arrsize];
for (i = 0; i < arrsize; i++)
AQ[i] = 0;
*/
//correction has been made to the code.
//WORKING CODE GIVEN BELOW
//Tested on codeblocks GNU C++. It is able to create .exe file
#include <iostream>
#include <cmath>
using namespace std;
void dectobin(int * , int);
int bintodec(int * , int);
int n;
int main() {
int a, b, sc, ssor, send, s1, c, sum, allz, rem, quo;
char ch='-';
//start:
do{
cout << "Enter dividend in decimal format ";
cin >> a;
cout << "Enter divisor in decimal format ";
cin >> b;
if (a != 0 && b == 0)
cout << "Infinity "; // Handling Divide-by-Zero Exception
else if (a == 0 && b == 0)
cout << "Undeterminate "; // Handling case when both dividendand
// divisor are zero
else {
if (a < 0 && b < 0) {
a = abs(a);
b = abs(b);
cout << "As both dividend and divisor are negative, -ve sign "
"cancels out ";
}
if (a < 0 && b > 0) {
a = abs(a);
b *= -1;
}
double td;
td = log((double) abs(a)) / log(2.0);
// cout << "HELLO" << td;
n = (int) ceil(td + 1); // Finding out the number of digits required
// cout << "DD" << n;
// tostore both dividend and divisor in binary
// format
cout << " SC is " << n << endl;
sc = n;
int * Q = new int[n];
int * A = new int[n];
int * M1 = new int[n];
int * M2 = new int[n];
int * pra = new int[n];
int i;
dectobin(Q, a); // Converting the dividend into binary as Q
cout << " Q is ";
for (i = 0; i <= n - 1; i++)
cout << Q[i];
send = Q[0];
dectobin(M1, b); // Converting the divisor into binary as M1
cout << " M is ";
for (i = 0; i <= n - 1; i++)
cout << M1[i];
ssor = M1[0];
for (i = n - 1; i >= 0; i--) // Taking the 2's Complement of M1 and storing it as M2
{
if (M1[i] == 0)
M2[i] = 0;
else
break;
}
M2[i--] = 1;
while (i >= 0) {
if (M1[i]) {
M2[i] = 0;
} else {
M2[i] = 1;
}
i--;
}
cout << " M'+1 is ";
for (i = 0; i <= n - 1; i++)
cout << M2[i];
// Creating a combined array of A and Q named AQ
int * AQ ;
//Define size of an array
int arrsize = 2*n;
AQ = new int[arrsize];
for (i = 0; i < arrsize; i++)
AQ[i] = 0;
for (i = n; i < 2 * n; i++)
AQ[i] = Q[i - n];
cout << " Initial AQ : ";
for (i = 0; i < 2 * n; i++)
cout << AQ[i];
//if (ch == 'Y' || ch == 'y') // Asking the user if he wants to carry
// outanother division or exit the program
// goto start;
//return 0;
}
}while(ch == 'Y' || ch == 'y');
return 0;
}
// Function to convert the passed decimal number
// into binary and return the base address of thebinary array
void dectobin(int * arr, int num)
{
int neg, i = 0, j, temp;
if (num < 0) // Checking for a negative number
{
neg = 1;
num = abs(num);
}
while (num != 0) // Continuously dividing the number by 2 andstoring the remainder
{ * (arr + i++) = num % 2;
num /= 2;
}
for (j = i; j <= n - 1; j++)
* (arr + j) = 0;
for (i = 0; i <= (n - 1) / 2; i++) // Reversing the binary array
{
temp = * (arr + i); * (arr + i) = * (arr + n - 1 - i); * (arr + n - 1 - i) = temp;
}
if (neg == 1) { * arr = 1; // Setting sign bit to 1 in case of a negative number
for (i = n - 1; i >= 1; i--) // Taking 2's Complement to store the negative number
if ( * (arr + i) == 1)
break;
i--;
while (i >= 1)
* (arr + i) = ! * (arr + i--);
}
}
// Function to convert a binary array todecimal by
// taking its base address as input and returning
// thedecimal equivalent
int bintodec(int * p, int y)
{
int sum = 0, i=0;
if ( * p == 1) // Taking 2's Complement of the negative number toget back its
// original magnitude
{
for (i = n - 1; i >= 1; i--)
if ( * (p + i) == 1)
break;
i--;
while (i >= 1)
* (p + i) = ! * (p + i--);
}
for (i = 1; i <= y - 1; i++) // Repeatedly multiplying by increasing powers
// of 2 and storing the sum after each iteration
sum += ((int) pow(2.0, y - 1 - i) * ( * (p + i)));
if ( * p == 1)
sum *= -1;
return sum; // Returning the equivalent decimal of the passed binary array
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.