473,785 Members | 2,640 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

not getting the required output

6 New Member
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 1527
ilikepython
844 Recognized Expert Contributor
#include<iostre am.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 Recognized Expert Moderator Specialist
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 Recognized Expert Moderator Specialist
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 Recognized Expert Contributor
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 Recognized Expert MVP
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 Recognized Expert Contributor
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 Recognized Expert Contributor
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 Recognized Expert Moderator Specialist
I don't think so, but I definately might have.
Nope, I missed one. Sorry!
Aug 15 '07 #9
hpbrothers
6 New Member
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

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

Similar topics

5
2543
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 started sending out the content of the page. Web content is always delivered with a few headers at the top, ending with a blank line. For example, a web page might start like this:
21
3991
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 nothing in IE. I'd be greatful for any assistance. Also, if I will have problems the code on Opera or Safari, I'd appreciate any pointers--I don't have a Mac to test Safari. THanks very much, Michael
1
11890
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 DeviceCapabilities( string device, string port, Int16 capability, out IntPtr outputBuffer,
5
2932
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) into my Microsoft VC++ .NET 2002 - Windows XP Pro PC: //----cppdll.cpp---- #include "cppdll.h" //prevent function name from being mangled
10
1945
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"> <input type="hidden" name="thisPage" value="pispCheckDomain"> <input type="hidden" name="username" value="test"> <input type="hidden" name="password" value="test"> domain_name: <input type="text" name="domain_name"><br>
8
6521
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, it always waits for the process to terminate and then return the output all at once. Can someone please show me some code that actually works for this sort of thing? It doesn't even have to use the subprocess module. Don't worry if the code...
1
7432
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 on dynamic SQL to export a series of table/views to CSV format, then zip the files up, then it creates an series of ftp commands to put the newly created zip file onto the relevent ftp server. The list of files and destination server are held in a...
4
2099
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; using System.Collections; using System.ComponentModel; using System.Data; using System.Drawing; using System.Web; using System.Web.SessionState; using System.Web.UI;
2
2432
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 column 'EmployeeID', table 'Accomplishments.dbo.Accomplishment'; column does not allow nulls. INSERT fails. The statement has been terminated. and my code is this using System; using System.Collections;
0
9645
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9481
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8978
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7502
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6741
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5383
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4054
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3655
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2881
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.