help

I have a problem... this code outputs a[] and b[] like it needs to, but it must output c[] it shows weird numbers (0x0018f.....)
basicly a and b arrays ar random, first numbers in c array ar a arrays and then goes b array numbers.... what could be my mistake?
Sorry for bad english ;(

#include <iostream.h>
#include <conio.h>
#include <stdlib.h>

void main(){
randomize();
int a[10],b[10],c[20],i,n,t;

cout<<"Ievadi masivu elementu skaitu: ";
cin>>n;

for(i=0; i<n; i++){
a[i]=random(10)+1;
cout<<a[i]<<" ";}
cout<<"\n";
for(t=0; t<n; t++){
b[i]=random(10)+1;
cout<<b[i]<<" ";
}
cout<<"\n";
for(i=0; i<n; i++){
c[i]=a[i]; cout<<c<<" ";
}
cout<<"\n";
for(i=n; i<2*n; i++){
c[i]=b[i];
cout<<c<<" ";
}
getch();
}
Instead of

cout<<c<<" ";

you should write i

cout<< c[i] <<" ";

Also main shall be declared as

int main()
still something wrong

the part when c array = a array elements goes well, but when program needs to output other part from c array where it must show b array numbers then itself outputs somekind other random numbers

example
n=4
a - 3 8 10 4
b- 5 8 9 10
c- 3 8 10 4 1638144 0 0 1 (underlined part is incorect)


closed account (3qX21hU5)
Helped by putting them in code format. Always use the code format (the <> button on the bottom when posting and right when replying) when posing code helps make it easier to read.

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
#include <iostream.h>
#include <conio.h>
#include <stdlib.h>

void main(){
    randomize();
    int a[10],b[10],c[20],i,n,t;

    cout<<"Ievadi masivu elementu skaitu: ";
    cin>>n;

    for(i=0; i<n; i++){
        a[i]=random(10)+1;
        cout<<a[i]<<" ";
    }
    
    cout<<"\n";
    for(t=0; t<n; t++){
        b[i]=random(10)+1;
        cout<<b[i]<<" ";
    }
    
    cout<<"\n";
    for(i=0; i<n; i++){
        c[i]=a[i]; cout<<c<<" ";
    }
    
    cout<<"\n";
    for(i=n; i<2*n; i++){
        c[i]=b[i];
        cout<<c<<" ";
    }
getch();
}


Last edited on
You are writing to the same elements in c in both loops. If you want c to contain the values of a followed by the elements of b you should change the second loop.
c[n + i] = b[i];

If this is the whole program you don't need to have a c array because you can just output the content of a and b directly.
Last edited on
Topic archived. No new replies allowed.