help with c++ program

Hi guys ! I need your help.I have to make a program that reads two numbers a and b and displays all the numbers that can be formed by replacing the first and the last digit of b with every digit of a step by step.
For example,if a = 19,b = 913 it should display 113,911,919,913.

This is my code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#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;
}

The problem is it doesn't work properly and I don't know how to fix it. What did I do wrong?I used two arrays va and vb that hold the values of every digit in a and b.Then,na is the number of digits of a and nb the number of digits of b.I think the program should display at least one number but it does nothing...
while (a) this newer ends;
while (b) same;

(i = na-1;i >= 0;i--)

your loop will be more efficent if you wrote it so:

(i = na - 1; i > -1; --i)
Last edited on
Thank you for the reply but it still doesn't work...
@codekiddy Thank you. I got it. It was my stupid mistake.I didn't realise it...
@codekiddy:

You're wrong about the loop performance. (i>=0) will be read as "must be positive", telling to compiler that it only needs to check a single bit (the sign bit). Your changed version is more specific ("be larger than a specific value"). Perhaps the compiler is smart enough to realize that it is the same (-1 being the "first" integer for which the sign bit is 1), but there's no guarantee. Secondly, the appearance of lonely pre or post-decrement operators is perhaps the easiest to optimize: unless it is part of a compound statement, there is no reason to keep a copy of the old value of 'i', thus both operators will do the exact same.

Also, micro-optimization in these projects in obviously useless. Chances are you're just confusing the OP.
I agree mostly with you except one thing:
both operators will do the exact same.


simply not true..

++i is more efficient than i++

EDIT:
for (register short i = 0; i > 0; --i)

is *NOT* the same as:

for (short i = 0; i > 0; i--)
Last edited on
Except that it is.
1
2
3
4
5
6
7
8
9
10
11
12
for (i = 0; i < 100; ++i) sum += i;
00401378  mov         dword ptr [i (40713Ch)],0  
00401382  jmp         main+41h (401391h)  
00401384  mov         eax,dword ptr [i (40713Ch)]  
00401389  add         eax,1  
0040138C  mov         dword ptr [i (40713Ch)],eax  
00401391  cmp         dword ptr [i (40713Ch)],64h  
00401398  jge         main+5Ch (4013ACh)  
0040139A  mov         eax,dword ptr [sum (407138h)]  
0040139F  add         eax,dword ptr [i (40713Ch)]  
004013A5  mov         dword ptr [sum (407138h)],eax  
004013AA  jmp         main+34h (401384h)  

1
2
3
4
5
6
7
8
9
10
11
12
for (i = 0; i < 100; i++) sum += i;
004013AC  mov         dword ptr [i (40713Ch)],0  
004013B6  jmp         main+75h (4013C5h)  
004013B8  mov         eax,dword ptr [i (40713Ch)]  
004013BD  add         eax,1  
004013C0  mov         dword ptr [i (40713Ch)],eax  
004013C5  cmp         dword ptr [i (40713Ch)],64h  
004013CC  jge         main+90h (4013E0h)  
004013CE  mov         eax,dword ptr [sum (407138h)]  
004013D3  add         eax,dword ptr [i (40713Ch)]  
004013D9  mov         dword ptr [sum (407138h)],eax  
004013DE  jmp         main+68h (4013B8h)  
You got such result's cos your compiler already optimized the code.
so looking into assembly has no effect :/

second your call was simple:
for (i = 0; i < 100; ++i) sum += i;
First of all, that was with optimizations disabled (because optimizations are possibly the most compiler-specific part of it all). Secondly, the code doesn't actually matter, because the meaning of 'i' remains the same: it is incremented and read. The increment is done separately before anything else. Just think about it for a second:
1
2
3
int i = 0;
++i;
cout << i;

Does it matter if you use ++i or i++? No, because the "current value" is discarded anyway. If I do this:
1
2
int i = 0;
printf("%d", i++);

..then it does matter, because the old value of 'i' has to be printed and the result would be different if I used ++i. However, in case of for loops, the increment is, again, separate.

Unless compilers are completely retarded in their implementation of pre/post in/decrements, there should be no difference in performance between the two if they are a separate statement.
I forgot to write a/=10 and b/=10 in the loop.It displays 1 1 1 9 1 9 and it's not ok...
Topic archived. No new replies allowed.