different variables for functions

in the code below i call the function split on the int a and b. i should like to savethe values in the global array a if it is called with a. and otherwise in b. could someone help me on that?

i have another problem and that is that the pow function is not working when i use 2 ints. so i cast them to doubles and after that i cast it to int again which seems quite pointless. so could someone tell me what would be better to do there.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <cmath>

using namespace std;

int a[4];
int b[4];

int split(int number){
	int length=getLength(number);//simply gets the length of the ints e.g.: length of 1234=4
	for (int i=length;i>0;i--) {
		double rang=pow(10.0,static_cast<double>(i));
		int remainder=number%static_cast<int>(rang);
		a[length-i]=(number-remainder)/rang;
                // or in case this is called with b:
                //b[length-i]=(number-remainder)/rang;
	}
}

int main() {
	int a=1234;
	int b=4321;
	split(a);
	split(b);
	system("pause");
	return 0;
}
You can pass in a pointer to the array you want to fill to the split function which would then become

 
int split(int number,int* array)


You have two global pointers to arrays a and b declared at the top. You then hide these names in main by declaring local integers a and b there. You need to give these some different names.

You are using pow to calculate a number (rang) that goes 10000, 1000, 100, 10 so you can see that you could declare another variable int rang = 10000 before the loop and then divide it by 10 on each iteration
(rang /= 10). This is a method for getting rid of the pow function
Topic archived. No new replies allowed.