# Using an array, problem

This is a discussion on Using an array, problem within the C++ Programming forums, part of the General Programming Boards category; Hey guys, basically I need to extract 2 numbers, a small and large number chosen by using cin to input, ...

1. ## Using an array, problem

Hey guys, basically I need to extract 2 numbers, a small and large number chosen by using cin to input, in an array.

Example:

How many integers? 3

Enter integer #1: 12795
Enter integer #2: -2784
Enter integer #3: -27904

The smallest digit: 0
Digit 1 can be found in integer number(s): 3

The largest digit: 9
Digit 9 can be found in integer number(s): 1, 3

*Bold indicates an entered number (input)*

So far this is my code:
Code:
```int numbers[0];
int count;
cout << "\tHow many integers do you have? ";
cin >> count;
cout << "\n";

int i;
int NumExtract;

for(i = 0; i < count; i ++){
cout << "\t\tEnter integer #" << i + 1 << " : ";
cin >> numbers[i];

}

for(i = 0; i < count; i ++){
while (numbers[i] != 0){

if (numbers[i] < 0) {
numbers[i] = -numbers[i];
}
NumExtract = numbers[i] % 10;
numbers[i] /= 10;

}

}```
This basically gets the numbers to input and everything and extracts the numbers but I'm trying to figure out how to make it so the code knows which Integer line the specified "large/small" number comes from.

Also, is a for loop good for this sort of problem?

2. I am really amazed if it's working..... as you have written int numbers[0] in your code which is a clear thing that you are declaring the array of size 0.....
So, if you will be able to store the values into the array, then what's so hard to find out that from which integer #, it's coming.....
Just use the value of i in the last for loop to show the integer #.

Also, for loop is better as this loop is used when we know the number of iterations... so it's good to use for loop here....

3. I do int numbers[0] to initialize it, then "i" takes over as the number in the array.

I know how to extract numbers from each integer # but I don't know how to make it print so it says which line it's SPECIFICALLY coming from... I'm not too good with for loops so I'm confused how to do it in this situation.

4. Originally Posted by .C-Man.
I do int numbers[0] to initialize it, then "i" takes over as the number in the array.
What do you really mean?
I think you just declared an array of size 0 here and nothing else.....
Do something like
Code:
`int numbers[3];`
so that it could be the array with size 3...

Originally Posted by .C-Man.
I know how to extract numbers from each integer # but I don't know how to make it print so it says which line it's SPECIFICALLY coming from... I'm not too good with for loops so I'm confused how to do it in this situation.
While using for loop (last one), you are using i as iterator..... so print the value of i where you finding the smallest or largest......

5. Code:
```	int count;
cout << "\tHow many integers do you have? ";
cin >> count;
int numbers[count];```

6. Code:
```while (numbers[i] != 0){

if (numbers[i] < 0) {
numbers[i] = -numbers[i];
}
NumExtract = numbers[i] % 10;
numbers[i] /= 10;

}```
What you are doing here is changing the value of array everytime... So, before this loop, first store the value of that array index into some variable and then use that....

Originally Posted by kargo
Code:
```	int count;
cout << "\tHow many integers do you have? ";
cin >> count;
int numbers[count];```
Wrong.... In C++ you can't do like this..... Unless you know how to create dynamic memory...
Static arrays are not allowed to use this...

7. Originally Posted by Mr.777
What do you really mean?
I think you just declared an array of size 0 here and nothing else.....
Do something like
Code:
`int numbers[3];`
so that it could be the array with size 3...
I mean I initialized it so inside the for loop I could have an unlimited amount of numbers no matter what I enter. Anyway, it works, that's not my problem...

Originally Posted by Mr.777
While using for loop (last one), you are using i as iterator..... so print the value of i where you finding the smallest or largest......
I don't understand what you mean by this...

8. Originally Posted by Mr.777
What you are doing here is changing the value of array everytime... So, before this loop, first store the value of that array index into some variable and then use that....

Wrong.... In C++ you can't do like this..... Unless you know how to create dynamic memory...
Static arrays are not allowed to use this...

9. Like this?

Code:
```int numInt;

for(i = 0; i < count; i ++){
cout << "\t\tEnter integer #" << i + 1 << " : ";
cin >> numbers[i];

}
numInt = i + 1;

for(i = 0; i < count; i ++){
while (numbers[i] != 0){

if (numbers[i] < 0) {
numbers[i] = -numbers[i];
}
numExtract = numbers[i] % 10;
numbers[i] /= 10;

}

}```

10. Originally Posted by kargo

Code:
```int numbers[3];
int count;
cout << "\tHow many integers do you have? ";
cin >> count;
cout << "\n";

int i;
int NumExtract;
int temp;

for(i = 0; i < count; i ++){
cout << "\t\tEnter integer #" << i + 1 << " : ";
cin >> numbers[i];

}

for(i = 0; i < count; i ++){
temp=numbers[i];
while (temp!= 0){

if (temp < 0) {
temp = -temp;
}
NumExtract =temp % 10;
temp /= 10;

}

}```

Like this.....
Now try next...

11. Ok, I understand this... but how does that help me with printing the right index when there is a number present in the index?

12. The code above is only extracting denominators everytime.....
Next you have to do this....
Declare a variable name index in the start first...
1. In the while loop there should be a conditional statement that will compare the remainder everytime with NumExtract... (Initialize NumExtract with 0 in the beginning where you declared that.)
2. Now you found the smallest of one index...... Loop through it and try to find the smallest from that unitl the loop ends.....

Note: Update the index everytime you enter in the conditional statement with the value of i...
3. In the end, you will get the minimum number of all, so print the number and index as well....

13. I'm really sorry, I'm not that good with C++ terminology can you please explain what you said through my code?

14. I helped what i could in your above given code.....

Run this code and see what more do you need to do....
Then make a logic.... write the code and then post here in case of errors.....

Page 1 of 2 12 Last