By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,984 Members | 1,086 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,984 IT Pros & Developers. It's quick & easy.

not getting the required output

P: 6
Expand|Select|Wrap|Line Numbers
  1. #include<iostream.h>
  2. #include<conio.h>
  3.  
  4. void main()
  5. {
  6.    int a[10],b[10],i,j,k=0,fg=0;
  7.    for(i=0;i<5;i++)
  8.    {
  9.       cout<<"enter no.";
  10.       cin>>a[i];}
  11.       for(i=0;i<5;i++)
  12.       {
  13.          for(j=0;j<5;j++)
  14.          {
  15.             if(a[i]==a[j]&& i!=j)
  16.             {
  17.                fg=1;
  18.                break;
  19.             }
  20.          }
  21.          if(fg==0)
  22.          {
  23.             b[k]=a[i];
  24.             k++;
  25.          }
  26.       }
  27.       for(i=0;i<k;i++)
  28.          cout<<b[i];
  29.       getch();
  30.    }
  31.  

this is the program to remove duplicate values from an array.

if my input is 2,2,3,4,5
then i should get output 3,4,5
but i am getting garbage value

but if i put my input 1,2,3,3,4
then i get the output 1,2,4 which is right

why i am not getting the output in first case.please if the program is wrong then let me know the right program
Aug 15 '07 #1
Share this Question
Share on Google+
10 Replies


ilikepython
Expert 100+
P: 844
#include<iostream.h>
#include<conio.h>
void main()
{int a[10],b[10],i,j,k=0,fg=0;
for(i=0;i<5;i++)
{cout<<"enter no.";
cin>>a[i];}
for(i=0;i<5;i++)
{for(j=0;j<5;j++)
{if(a[i]==a[j]&& i!=j)
{
fg=1;
break;}}
if(fg==0)
{b[k]=a[i];
k++;}}
for(i=0;i<k;i++)
cout<<b[i];
getch();}



this is the program to remove duplicate values from an array.

if my input is 2,2,3,4,5
then i should get output 3,4,5
but i am getting garbage value

but if i put my input 1,2,3,3,4
then i get the output 1,2,4 which is right

why i am not getting the output in first case.please if the program is wrong then let me know the right program
The first thing I saw is a pile of meesy code. Please indent and use code tags:
Expand|Select|Wrap|Line Numbers
  1. #include<iostream.h>
  2. #include<conio.h>
  3.  
  4. void main()
  5. {
  6.     int a[10],b[10],i,j,k=0,fg=0;
  7.     for(i=0;i<5;i++)
  8.     {
  9.         cout<<"enter no.";
  10.         cin>>a[i];
  11.     }
  12.     for(i=0;i<5;i++)
  13.     {
  14.         for(j=0;j<5;j++)
  15.         {
  16.             if(a[i]==a[j]&& i!=j)
  17.             {
  18.                 fg=1;
  19.                 break;
  20.             }
  21.         }
  22.         if(fg==0)
  23.         {
  24.             b[k]=a[i];
  25.             k++;
  26.         }
  27.     }
  28.     for(i=0;i<k;i++)
  29.         cout<<b[i];
  30.     getch();
  31. }
  32.  
Aug 15 '07 #2

sicarie
Expert Mod 2.5K+
P: 4,677
The first thing I saw is a pile of meesy code. Please indent and use code tags:
Expand|Select|Wrap|Line Numbers
  1. #include<iostream.h>
  2. #include<conio.h>
  3.  
  4. void main()
  5. {
  6.     int a[10],b[10],i,j,k=0,fg=0;
  7.     for(i=0;i<5;i++)
  8.     {
  9.         cout<<"enter no.";
  10.         cin>>a[i];
  11.     }
  12.     for(i=0;i<5;i++)
  13.     {
  14.         for(j=0;j<5;j++)
  15.         {
  16.             if(a[i]==a[j]&& i!=j)
  17.             {
  18.                 fg=1;
  19.                 break;
  20.             }
  21.         }
  22.         if(fg==0)
  23.         {
  24.             b[k]=a[i];
  25.             k++;
  26.         }
  27.     }
  28.     for(i=0;i<k;i++)
  29.         cout<<b[i];
  30.     getch();
  31. }
  32.  
ilikepython-

Did you add a brace in there? I'm missing one, but I might have deleted it when I was trying to figure out the mess of code he made.
Aug 15 '07 #3

sicarie
Expert Mod 2.5K+
P: 4,677
hpbrothers-

You've already been asked by a Mod to use code tags, and now yo uhave by an Expert. I'm making it an official warning - read and follow the Posting Guidelines. If you want to post, you have to play by the rules.
Aug 15 '07 #4

ilikepython
Expert 100+
P: 844
ilikepython-

Did you add a brace in there? I'm missing one, but I might have deleted it when I was trying to figure out the mess of code he made.
I don't think so, but I definately might have.
Aug 15 '07 #5

Expert 10K+
P: 11,448
Shouldn't that variable 'fg' be reset every time the inner loop is started; e.g. insert
'fg= 0;' at line 14 of the code above.

kind regards,

Jos
Aug 15 '07 #6

ilikepython
Expert 100+
P: 844
Expand|Select|Wrap|Line Numbers
  1. #include<iostream.h>
  2. #include<conio.h>
  3.  
  4. void main()
  5. {
  6.    int a[10],b[10],i,j,k=0,fg=0;
  7.    for(i=0;i<5;i++)
  8.    {
  9.       cout<<"enter no.";
  10.       cin>>a[i];}
  11.       for(i=0;i<5;i++)
  12.       {
  13.          for(j=0;j<5;j++)
  14.          {
  15.             if(a[i]==a[j]&& i!=j)
  16.             {
  17.                fg=1;
  18.                break;
  19.             }
  20.          }
  21.          if(fg==0)
  22.          {
  23.             b[k]=a[i];
  24.             k++;
  25.          }
  26.       }
  27.       for(i=0;i<k;i++)
  28.          cout<<b[i];
  29.       getch();
  30.    }
  31.  

this is the program to remove duplicate values from an array.

if my input is 2,2,3,4,5
then i should get output 3,4,5
but i am getting garbage value

but if i put my input 1,2,3,3,4
then i get the output 1,2,4 which is right

why i am not getting the output in first case.please if the program is wrong then let me know the right program
You never set fg back to 0 when you set it to 1. The program works for me if I add fg = 0; after the first loop:
Expand|Select|Wrap|Line Numbers
  1.     for(i=0;i<5;i++)
  2.     {
  3.         fg = 0;
  4.         for(j=0;j<5;j++)
  5.         {
  6.             if(a[i]==a[j]&& i!=j)
  7.             {
  8.                fg=1;
  9.                break;
  10.             }
  11.          }
  12.          if(fg==0)
  13.          {
  14.             b[k]=a[i];
  15.             k++;
  16.          }
  17.       }
  18.  
Aug 15 '07 #7

ilikepython
Expert 100+
P: 844
Shouldn't that variable 'fg' be reset every time the inner loop is started; e.g. insert
'fg= 0;' at line 14 of the code above.

kind regards,

Jos
Jos you beat me to it! ;)
Aug 15 '07 #8

sicarie
Expert Mod 2.5K+
P: 4,677
I don't think so, but I definately might have.
Nope, I missed one. Sorry!
Aug 15 '07 #9

P: 6
You never set fg back to 0 when you set it to 1. The program works for me if I add fg = 0; after the first loop:
Expand|Select|Wrap|Line Numbers
  1.     for(i=0;i<5;i++)
  2.     {
  3.         fg = 0;
  4.         for(j=0;j<5;j++)
  5.         {
  6.             if(a[i]==a[j]&& i!=j)
  7.             {
  8.                fg=1;
  9.                break;
  10.             }
  11.          }
  12.          if(fg==0)
  13.          {
  14.             b[k]=a[i];
  15.             k++;
  16.          }
  17.       }
  18.  

thanks it is now working for me also
Aug 15 '07 #10

Expert 10K+
P: 11,448
Jos you beat me to it! ;)
Man, ain't I fast today ;-)

kind regards,

Jos
Aug 15 '07 #11

Post your reply

Sign in to post your reply or Sign up for a free account.