# Thread: a little help with int array and for looping

1. ## a little help with int array and for looping

Hi@all;

I think I don't need to write explanation about my code because it is very easy.

Can anyone tell me when I run my prog. why the prog. gives me correct answer only once and after that doesn't give the correct answers for others?

I know what is wrong but I don't know how to correct it? How can I divide each element in my array to 100 and print them?

Code:
```#include <iostream>
using namespace std;
int main()
{
int array[2];
for (int i=0; i<2; i++)
cin >>array[i];
for (int j=0; j<2; j++)
cout <<array[j]/100<<endl;

return 0;
}```
Thanks

2. Originally Posted by Apropos
Code:
```#include <iostream>
using namespace std;
int main()
{
int array[2];

for (int i=0; i<2; i++)
cin >>array[i];
for (int j=0; j<2; j++)
cout <<array[j]/100<<endl;

return 0;
}```

That's a no no. Absolutely no need to have two for loops for such a simple task. And therein lies your problem, I believe. You only need ONE for loop, and use { }'s to enclose all the actions taken through each cycle of the loop.

Code:
``` for (int i=0; i<2; i++){
cin >>array[i];
cout <<array[j]/100<<endl;
}```

3. Actually, regardless to how simple your code, you really do need to explain intents and purposes of code. And what the problem is other than 'Doesn't give correct answers'. Otherwise how are we to know what it is you are expecting from your code?

However I'm going to take a shot in the dark here and guess you're expecting floating point answers to integer math equations.

4. That's a no no. Absolutely no need to have two for loops for such a simple task. And therein lies your problem, I believe. You only need ONE for loop, and use { }'s to enclose all the actions taken through each cycle of the loop.

Code:
Code:
``` for (int i=0; i<2; i++){
cin >>array[i];
cout <<array[j]/100<<endl;
}```
No, first of all your code there Krak doesn't initialize j so wouldn't even compile. Second maybe the specs for the program require that the entire array be read in first before printing the answers in which case you do need two loops.

I agree with Scribbler, its hard to say what the error is in the program since it seems to work just fine. The only thing I can gather is that you are entering in a number that is not divisible by 100 in which case you will get a non-decimal answer due to integer arithmetic. For example, 250/100 in integer math will not give you 2.5 but will instead give you 2. Change your definition of array to float instead of int and see if you get the answers you were looking for.

5. Hi
@Krak
Thanks for your reply but I need to two loops in my prog because after getting elements of my array from the keyboard, I tried to devide each element to 100 after that I tried to print index number and print * according to result of division line by line. I wrote my code and didn't work so I thought that I did make a mistake about my array. Here is my complete program;

Code:
```#include <iostream>
using namespace std;
int main()
{
int array[2];
for (int j=0; j<2; j++)
cin >>array[j];
for (int i=0; i<array[i]/100; i++)
cout <<i<<'x'<<endl;

return 0;
}```
example;

input;

300
400

output;

0***
1****

thanks everybody who tried to help me

P.S; I read the article about loops at http://www.cprogramming.com/tutorial/lesson8.html but I didn't write my program

6. It looks like you have a logic error there in your second loop. What you really need is a nested loop to do what you are asking for, the first to keep track of the line number and the array value, and the second to print out your x's. Try something like this for your second loop:

Code:
```for (int i=0; i<2; i++)
{
cout <<i;
for (int k=0; k<array[i]/100; k++)
cout<<'x';
cout<<endl;
}```

7. Thank you @ PJYelton