Aug 13, 2012 at 4:14am UTC
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 Aug 13, 2012 at 4:22am UTC
Aug 13, 2012 at 5:03am UTC
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
Aug 13, 2012 at 12:19pm UTC
@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 Aug 13, 2012 at 12:20pm UTC
Aug 13, 2012 at 12:22pm UTC
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 Aug 13, 2012 at 12:24pm UTC
Aug 13, 2012 at 12:48pm UTC
@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;
}
Aug 13, 2012 at 1:10pm UTC
@tntxtnt
if i changed it i get 1 1 1 ............ infinite loops lolz