Hi I have a problem with some code. Basically what the program is supposed to do is to take in a minimum exponent and a maximum exponent and a base. Then its is suppose to create a table displaying the base to the power of the minimum exponent to the max exponent.I have got the positive exponents to work but I cannot get the negative exponents to work. I cannot use the pow() function.

ex.(this is what it should look like)
input:
Base = 2
max exponent = 3
min exponent = -2

output:
.25
.5
1
2
4
8
----------------------
however mine outputs (with the same data of course)

.5
1
.5
1
.5
1
2
4
8
16
32
------------------------
I do not know why

Here is the code so far

#include "StdAfx.h"
#include <iostream>
#include <math.h>
using namespace std;

 
void ExpPow(double &res, int base, int count);
void negexp(double &res, int base, int count);
 
 
int main()
{
	int base;
	int expow;
	int expomax;
	int count;
	double res = 1;
	int count1;
 
	cout << "Please enter a maxiumum exponent value" << endl;
		cin >> expomax;
	cout << "Please enter a minimum exponent value" << endl;
		cin >> expow;
	cout << "Please enter a base" << endl;
		cin >> base;

	if(cin.fail())
	{
		cout << "Error input" << endl;
	}
	
	for(count1 = expow; count1 <= expomax; count1++)
	{if(count1 <= 0)
	{
		for(count = 1; count < abs(expow); count++)
		{
			 negexp(res,base,count);
		}
		for(count = expow; count <= expomax; count++)
		{
			negexp(res,base,count);
			cout << res << endl;
			cout << "                     " << expow << endl;
			count1 = count;
		}
	}
	}
	{
		if(count1 >0)
		{
			for(count = 1; count < expow; count++)
			{
				ExpPow(res, base, count);
			}
			for(count = expow; count <= expomax; count++)
			{
			    ExpPow(res, base, count);
				cout << res << endl;
				cout << "                     " << expow << endl;
			}

		}
	}

	return 0;
}
 
void ExpPow(double &res, int base, int count)
{
	res = res * base;
}
void negexp(double &res, int base, int count)
{
	res = 1/abs((res * base));
}

Any help is appreciated, thank you.

P.S. forget the 'cout << " " << expow << endl;'

Instead of correcting your code (which is in the mess), I would rather simplify the solution. Let break the problem in small pieces and solve it one by one. First of all you want to create a table displaying the base to the power of the minimum exponent to the max exponent.

for (int i=expow; i<=expomax; i++) {
   std::cout << expo(base, i) << std::endl;
}

First problem is solved. Second problem is that you need a expo function to return value of base power to i .

double expo(int base, int to)
{
    double result = 1;

    if (to == 0) {          
        // everything to the power of 0 will be 1
        return 1;
    } else if (to > 0) {
        while(to > 0) {
            result *= base;
            to--;
        }
        return result;
    }
}

Second problem is half-solved, but what if to is negative (negative exponent). Then, expo function must able to handle the negative exponent as well.

double expo(int base, int to)
{
    double result = 1;

    if (to == 0) {          
        // everything to power of 0 will be 1
        return 1;
    } else if (to > 0) {
        // positive exponent
        while(to > 0) {
            result *= base;
            to--;
        }
        return result;
    } else {
        // negative exponent
        to = -to;
        while(to > 0) {
            result *= base;
            to--;
        }
        return 1/result;
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.