Thread: help with a c++ program

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    26

    help with a c++ program

    Hello!
    I have to make a program that reads two numbers a and b and then displays all the numbers that can be formed by replacing the first and the last digit of b with every single digit of a.
    For example,if a is 19 and b = 913 it should display the numbers:
    113,911,919,913.

    There is my piece of code :
    Code:
    #include<iostream>
    using namespace std;
    
    int main()
    {
       int a,b,i = 0,j = 0,na = 0,nb = 0,va[12],vb[12];
       
       cout <<"a = ";
       cin >> a;
       
       cout <<"b = ";
       cin >> b;
       
       while (a) {
            va[i] = a%10;
            na++;
            i++;
        }
        
        while (b) {
            vb[j] = b%10;
            nb++;
            j++;
        }
       
        for (i = na-1;i >= 0;i--) {
                vb[0] = va[i];
                vb[na] = va[i];
         for (j = nb - 1; j >= 0;j--){
                cout << vb[j] << " " ;
          }
          }
     return 0;
    }
    I used 'na' and 'nb' to calculate the number of digits of a and b. Also,I used 2 arrays 'va[12]' and 'vb[12]' to hold the values of every digit in the number.
    What's wrong? It doesn't work properly...
    Thanks in advance !

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Code:
    while (a) {
            va[i] = a%10;
            na++;
            i++;
    }
    When does a become 0 in this loop, causing it to terminate?

    Code:
    while (b) {
            vb[j] = b%10;
            nb++;
            j++;
    }
    Likewise with b.

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    In the following code:
    Code:
       while (a) {
            va[i] = a%10;
            na++;
            i++;
        }
    When will this loop end? When will "a" ever be zero?

    Jim

  4. #4
    Registered User
    Join Date
    Aug 2011
    Posts
    26
    Quote Originally Posted by jimblumberg View Post
    In the following code:
    Code:
       while (a) {
            va[i] = a%10;
            na++;
            i++;
        }
    When will this loop end? When will "a" ever be zero?

    Jim
    Of course,it was so obvious. I forgot the a/=10. I usually don't make these kind of mistakes but this time I just couldn't figure it out. I thought that there was a/=10.
    Sorry for the dumb question.I'm feeling a little embarassed right now...

  5. #5
    Registered User
    Join Date
    Aug 2011
    Posts
    26
    I've just run my program and it displays 111 and 919 instead of 113,919,911 and 913. How should I fix this problem?

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    Post your current code. Do you see 111 and 919 or 1 1 1 and 9 1 9?

    Jim

  7. #7
    Registered User
    Join Date
    Aug 2011
    Posts
    26
    Quote Originally Posted by jimblumberg View Post
    Post your current code. Do you see 111 and 919 or 1 1 1 and 9 1 9?

    Jim
    No. Actually,I see 1 1 1 and 9 1 9. What should I do? It's because of that space " "...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help converting array program to link list program
    By hsmith1976 in forum C++ Programming
    Replies: 0
    Last Post: 02-14-2010, 09:50 PM
  2. Replies: 1
    Last Post: 03-03-2009, 04:47 PM
  3. Get program to copy itself into program files, then start on startup.
    By guitarist809 in forum Windows Programming
    Replies: 6
    Last Post: 03-03-2008, 09:42 AM
  4. Replies: 5
    Last Post: 08-16-2007, 11:43 PM
  5. Replies: 18
    Last Post: 11-13-2006, 01:11 PM