a little help with int array and for looping

This is a discussion on a little help with int array and for looping within the C++ Programming forums, part of the General Programming Boards category; Hi@all; I think I don't need to write explanation about my code because it is very easy. Can anyone tell ...

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    29

    Post 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. #2
    People Love Me
    Join Date
    Jan 2003
    Posts
    412
    Quote 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. #3
    Registered User Scribbler's Avatar
    Join Date
    Sep 2004
    Location
    Aurora CO
    Posts
    266
    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.
    Last edited by Scribbler; 02-05-2005 at 11:20 AM.

  4. #4
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    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.
    Last edited by PJYelton; 02-05-2005 at 11:16 AM.

  5. #5
    Registered User
    Join Date
    Dec 2004
    Posts
    29
    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
    Last edited by Apropos; 02-05-2005 at 11:54 AM.

  6. #6
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    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. #7
    Registered User
    Join Date
    Dec 2004
    Posts
    29
    Thank you @ PJYelton

    I got your point. I appreciate for your help.

Popular pages Recent additions subscribe to a feed

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