Problem for array

Write a program that shall prompt the user to enter 15 integer values. The program then
copies the values onto another array. The condition of copying is that the values in the
second array should be distinct (no repeating values).
For example:
Array 1: 1 1 5 6 7 9 10 10 20 13 16 10 9 1 6
Array 2: 1 5 6 7 9 10 20 13 16

what i did:

#include <iostream>
#include<cctype>
using namespace std;

int main ()

{
int arr[15],arr1[15],a=0;

for(int x= 0;x<15;x++)
{
cout<<"Please enter a value"<<endl;
cin>>arr[x];
}

for(int a=0; a<15; a++)
for(int b=14; b<=0; b--)
{
if(arr[a]==arr[b])
{
arr1[a]=arr[a];
break;
}
else if(arr[a]!=arr[b])
continue;
}


while(a<15)
{

cout<<arr1[a];
cout<<" ";
a++;
}
system("PAUSE");
return 0;
}

i get an error for this one... can anyone help?

Last edited on
you have declared a as an integer in this line
int arr[15],arr1[15],a=0;

and then you declare it again in
for(int a=0; a<15; a++)

use another name to distinguish those two variables
Here i solve your problem..
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
for(int j=0; j<15; j++)
{
    for(int b=0; b<=j; b++)
    {
        if(arr[j]==arr1[b])
        {	
            repeated = true;
            continue;
        }
    }
    if(!repeated)
    {
        arr1[a++] = arr[j];
    }
}
int i =0;
while(i<a)
{

    cout<<arr1[i];
    cout<<" ";
    i++;
}
@HiteshVaghani1
the arr1 is empty , why you compare it o.o? i still cant get the result...
maybe u post a full code for me will do...thx in advance
Last edited on
It's full code.. i just didn't declare and initialize variable and array... please do it by yourself...
And yaa its working in my PC.
Last edited on

@HiteshVaghani1 i copy all ur codes and i cant see the result....
Here's the code:

#include <iostream>
#include<cctype>
using namespace std;

int main ()
{
int arr[15],arr1[15],a=0;
bool repeated;
cout<<"please enter a value: "<<endl;
for(int x=0;x<15;x++)
{
cin>>arr[x];
}
for(int j=0; j<15; j++)
{
for(int b=0; b<=j; b++)
{
if(arr[j]==arr1[b])
{
repeated = true;
continue;
}
}
if(!repeated)
{
arr1[a++] = arr[j];
}
}
int i =0;
while(i<a)
{

cout<<arr1[i];
cout<<" ";
i++;
}

system("PAUSE");
return 0;
}
@tntxtnt
if i changed it i get 1 1 1 ............ infinite loops lolz
yaa, i sort it out with problem. actually that was my mistake sorry for that..
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
38
39
40
int main ()
{
    int arr[15],arr1[15],a=0;
    bool repeated;
    cout<<"please enter a value: "<<endl;
    
    for(int x=0;x<15;x++)
    {
        cin>>arr[x];
    }
    
    for(int j=0; j<15; j++)
    {
        for(int b=0; b<=j; b++)
        {
            if(arr[j]==arr1[b])
            {	
                repeated = true;
            }
        }
        if(!repeated)
        {
            arr1[a] = arr[j];
            a++;
        }
        repeated = false;
    }

    int i =0;

    while(i<a)
    {
        cout<<arr1[i];
        cout<<" ";
        i++;
    }

    system("PAUSE");
return 0;
}
Topic archived. No new replies allowed.