Newbie Here having trouble with assigning values to arrays. PLS help!

I've spent a better part of a couple of days trying to figure this out and I'm lost!


I have a value of type int stored to a variable. I am trying to take this value and extract each digit by using % 10 on the variable then dividing / 10 to move to the next digit until I have reached the first digit, starting from the last.

so for a value, Example: 12345 . I get 5, 4, 3, 2, 1 in that order.


 
int* digit = new int[count];


I have used come code to dynamically assign the values to an array. But, this this where I'm lost. there is pointer to an array I believe, I don't understands how this really works.

I know I can find the first and last digit buy keeping track with a count variable during a loop.

but can I compare the values in the array to find the largest and smallest digit
with this "pointer array". sorry I don't understand what is employed here, forgive my explanation.


but can I compare the values in the array to find the largest and smallest digit

Yes. Do you mean you don't know how to access the values in the array?

If I write:

int my_array[5];

... my_array is now a pointer. It points to a memory address where the first of my 5 ints is stored.
If I want to get the actual value of an int stored there, I need to dereference the pointer:

int value = my_array[0]; // get 1st value

You can use this same notation even if you've used new:

1
2
int* digit = new int[5];
int value = digit[0];


Regards, keineahnung
Sounds like you need a tutorial on arrays: http://www.cplusplus.com/doc/tutorial/arrays/
I believe I don't get the concept of pointers. I get how to access the array, but I think I was trying to work with a "pointer"? array? i kept get weird values when I tried to work with it.

so I was able to do exactly keineahnung suggested but I used

1
2
const int MAX_LENGTH_NUMBER = 64;
	int digit[MAX_LENGTH_NUMBER];


here is what I have, I can get it to do what I want, but can not make it work without the return; after the first if statement. it leaves the function and doesn't print out the greatestInt. either way its not working correctly yet.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

int greatestInt;
	greatestInt = -10;
	

	for(int indexTwo = countTwo -1 ; indexTwo >= 0; indexTwo--) {
		
		digit[indexTwo] = modifyIVal3 % 10;
		modifyIVal3 = modifyIVal3 / 10;
		cout << "digit value" << digit[indexTwo] << endl;
		if(greatestInt > digit[indexTwo]) {
		return;
                } else (digit[indexTwo] > greatestInt) {
		greatestInt = digit[indexTwo];
		} 
		
	}
cout << "Greatest digit: " << greatestInt << endl;
was not using the conditions correctly, but got it to work

1
2
3
4
5
6
7
8
9
10
11
12
int greatestInt =-10;
	for(int indexTwo = count -1 ; indexTwo >= 0; indexTwo--) {

		digit[indexTwo] = modifyIValTwo % 10;
		modifyIValTwo = modifyIValTwo / 10;
		if ((greatestInt) >= (digit[indexTwo])){
			greatestInt = greatestInt;
		} 
		if ((digit[indexTwo]) >= (greatestInt)){
			greatestInt = digit[indexTwo];
		}
	}
Topic archived. No new replies allowed.