0

i have to use an array for this and when i run my program my computer starts to spit out number can someone help me. the critia is that it can read from base 2-9 and will give me the answer in base 10.

#include <iostream>
using namespace std;
int main ()
{
	int myarray[8];
	int answer=0;
	int base;
	int num;
	int i;
	cout<< "please put number in from right to left";
	cin>>num;
	
	for(i=0;i>=0;i++)
	{
		cout<<(answer+ myarray[i]*base^(7-i));
	}
	return 0;
}
4
Contributors
3
Replies
4
Views
8 Years
Discussion Span
Last Post by Freaky_Chris
0

You're given the base right, cause otherwise you can't figure it out case 11 could be 3 base-2, or 4 base-3, or 5 base-4, etc.

The variable 'i' will always be greater then or equal to 0 if you start it at zero and increase it.

As far as you implementation, I see what you are trying to do, but I think they way you are doing it is wrong. I think you'd want answer += myarray[i]*base^(7-i); in the for loop and just one cout << answer; after the loop.

0

You'll also need to read your book again to find out what the ^ operator really does.
It isn't "raise to power of".

0

If you want to raise something to the power of something then you have two choices,
A) include <cmath> and use its pow() function.
B) Write and recursive, or none recursive power function of your own.

The reason it is spitting out random numbers is because you are calling an integer array when you have not initialized it at any point, see the problem?

Chris

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.