Thread: Issue with the print of this program

  1. #1
    Registered User
    Join Date
    Sep 2014
    Posts
    16

    Issue with the print of this program

    For once I actually have the full program working. The only issue I am having is this. The last number of the row that is printed needs to not have a , after it. The program works if a number that is divisible by 10 is enter but if it is not there is a , after it. Can anyone help lead me down the correct way to format the function to print the way I need it.

    Here are the exact instructions for how it should work : print the values in the array. The values must be displayed with 10 integers per line and a comma after each value. The last value on each line does not have a comma after it. The last line may have fewer than10 values if the total numbers of values printed is not evenly divisible by 10. For example, if the user asked for 15 array elements to be printed, then the last line would only have 5 values; or if the user asked for 92 values, then the output would be 9 lines containing 10 values and the last output line would have only 2 values.

    Here is what I have currently for the function:

    Code:
    void printArray (int array[], int n)
    {
      for (int y = 0; y < n; y++)
     {
        cout<< array[y];
    
    
        if( (y % 10) == 9)
        {
            cout << endl;
        }
    
    
        else
            cout << ", ";
     }
        cout << endl;
    
    
       }

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Terminate the loop one iteration earlier, and output the last value separately.

    That will also address the concern (which you haven't mentioned) of an extraneous blank line if the number of values is a multiple of 10.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Sep 2014
    Posts
    16
    well that does not fix the issue I am having. So here is the example of what is happening. The user inputs 5 for how big they want the array to be, the numbers are generated randomly and stored and then output. The output at 5 currently would look like this: 55, 24, 88, 56, 12,

    I need the output to look like this: 55, 24, 88, 56, 12

    currently it only works with 10 I am not sure how to make it if they number is less than 10 no matter what row it is the last number does not have the comma after it.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Jesse Bettcher View Post
    well that does not fix the issue I am having.
    That means you did something other than follow the advice.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    Registered User
    Join Date
    Sep 2014
    Posts
    16
    So I fixed it going a different route I do know if this is exactly if you were meaning but it is what I came up with

    Code:
    void printArray (int array[], int n)
    {
      for (int y = 0; y < n; y++)
      {
         if( y + 1 == 10  || y + 1 == n || ((y + 1 ) % 10 ) == 0)
         {
            cout<<array[y]<<endl;
         }
         else
         {
            cout<<array[y]<< " , ";
         }
    
    
      }
    
    
    
    
    }

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    While your solution is one way to address the problem, it is certainly not what I suggested. No matter.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  7. #7
    Registered User
    Join Date
    Sep 2014
    Posts
    16
    so what would the code have looked like doing it the way you suggested. I was struggling to come up with what you suggested so I went with something that addressed all the possible conditions and would output correctly.

  8. #8
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    There were two parts of my one-sentence reply "Terminate the loop one iteration earlier, and output the last value separately."

    What do you think the part before the comma means?

    What do you think the part after the comma means?


    Think though those questions - they are hints that, if you think through them, are designed to point you in the direction of answering your own question.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why is it that my program does not print for case 1?
    By Jasper in forum C Programming
    Replies: 10
    Last Post: 07-02-2009, 12:57 PM
  2. C file print Issue in Line feed
    By muni in forum C Programming
    Replies: 3
    Last Post: 04-21-2008, 09:29 AM
  3. program to print itself
    By ElemenT.usha in forum C Programming
    Replies: 6
    Last Post: 02-09-2008, 05:12 AM
  4. Print to file issue
    By jed in forum C++ Programming
    Replies: 2
    Last Post: 10-18-2006, 09:54 PM
  5. C++ program to print itself
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 11-27-2001, 02:25 PM