473,387 Members | 1,760 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

not getting the required output

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
10 1489
ilikepython
844 Expert 512MB
#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
4,677 Expert Mod 4TB
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
4,677 Expert Mod 4TB
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
844 Expert 512MB
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
JosAH
11,448 Expert 8TB
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
844 Expert 512MB
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
844 Expert 512MB
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
4,677 Expert Mod 4TB
I don't think so, but I definately might have.
Nope, I missed one. Sorry!
Aug 15 '07 #9
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
JosAH
11,448 Expert 8TB
Jos you beat me to it! ;)
Man, ain't I fast today ;-)

kind regards,

Jos
Aug 15 '07 #11

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

Similar topics

5
by: Philip Ronan | last post by:
OK, here's my 2p worth: === Q. Why am I getting the error message 'Headers already sent'? A. PHP produces this error message when you try to set a header for a web page after you have already...
21
by: Michael Bierman | last post by:
Please forgive the simplicy of this question. I have the following code which attempts to determine the color of some text and set other text to match that color. It works fine in Firefox, but does...
1
by: Bruce Wood | last post by:
I'm having a devil of a time calling DeviceCapabilities() in order to get the list of paper names / codes / sizes for a printer. Here is my code and the input it produces: static extern Int32...
5
by: Scott Chang | last post by:
Hi all, I copied a set of VC++ version 6 source code of the 'cppdll'(2 projects) from a website and put the cppdll.cpp, cppdll.def, cpp.h, (as the 1st project) and test.cpp (as the 2nd project)...
10
by: Peter Afonin | last post by:
Hello, I have a simple client-side form that is checking the domain availability on the domain registrar's server: <FORM action="https://www.webnames.ru/scripts/RegTimeSRS.pl" method="post">...
8
by: cypher543 | last post by:
This has been driving me insane for the last hour or so. I have search everywhere, and nothing works. I am trying to use the subprocess module to run a program and get its output line by line. But,...
1
by: leescriven | last post by:
Hi, Thi is my first post on this forum, and I'm hoping that there is a guru out there who can help me with an annoying problem I am having. I have written a Stored Procedure that relies heavily...
4
by: preeti13 | last post by:
Hi friends i have a probelm i am try to pass the value to the employeeid parameter but getting th error please help me how i can do this i am getting the error here is my code using System;...
2
by: preeti13 | last post by:
Hi guys i am here with my another probelm please help me.trying insert the value into the data base but getting the null value error .I am getting thsi error Cannot insert the value NULL into...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.