Hello, I'm having a little problem with my code...I have the program written but its just not doing what I want and needs a minor tweak and I've been trying to fix it for ohurs and can't find a solution. I just need to write a function that removes the duplicates in an array. heres my code for main -
#include <iostream>
-
#include "accfuncs.cpp"
-
using namespace std;
-
-
void sort(int a[], int h, int a2[], int size);
-
void printarray(int [], int); //the printarray function declaration
-
int main ()
-
{
-
int numbs[20], numbs2[20], r, idx; //declared numbs array and a integer sym\
-
bolizing the index.
-
cout << "Please enter in a series of numbers ending it with a 0: ";
-
-
// For loop that takes input from the user of up to 20 numbers and stores them
-
// in the numbs array. Also if the user enters a 0 it ends the loop.
-
for(idx = 0; idx < 20; idx++) {
-
cin >> numbs[idx];
-
if(numbs[idx] == 0) {
-
break; //user entered 0 signifying end of loop
-
}
-
}
-
int n = idx; //saves the size of the array to the n variable
-
sort(numbs, r, numbs2, n);
-
printarray(numbs2, r - 1); //calls the print array function and itterates i\
-
t
-
//using numbs
-
cout << endl; // new line
-
}
-
and here my other file which contains the functions: -
using namespace std;
-
-
//printarray definition
-
void printarray(int a[], int size){
-
for(int i = size; i > 0; i--)//loop that prints out all values in an array
-
cout << a[i];
-
}
-
-
void sort(int a[], int h, int a2[], int size){
-
h = 0;
-
for (int i = 0; i < size; i++){
-
for ( int j = 0; j < size; j++){
-
if (a[i] == a2[j]){break;}
-
else{
-
a[i] = a2[h];
-
h++;}
-
}
-
}
-
}
-
I believe the problem is somewhere in my sort function..Im not supposed to srt it tho just simply remove the dupes and when I look at it I feel it should do just that but it's not :
If user enters 5 26 22 26 7 8 7 9 5
it should print: 5 26 22 7 8 9 (print each number once)
any help would be greatly appreciated because im seriously stuck
12 2675 JosAH 11,448
Recognized Expert MVP
Hello, I'm having a little problem with my code...I have the program written but its just not doing what I want and needs a minor tweak and I've been trying to fix it for ohurs and can't find a solution. I just need to write a function that removes the duplicates in an array. heres my code for main -
#include <iostream>
-
#include "accfuncs.cpp"
-
using namespace std;
-
-
void sort(int a[], int h, int a2[], int size);
-
void printarray(int [], int); //the printarray function declaration
-
int main ()
-
{
-
int numbs[20], numbs2[20], r, idx; //declared numbs array and a integer sym\
-
bolizing the index.
-
cout << "Please enter in a series of numbers ending it with a 0: ";
-
-
// For loop that takes input from the user of up to 20 numbers and stores them
-
// in the numbs array. Also if the user enters a 0 it ends the loop.
-
for(idx = 0; idx < 20; idx++) {
-
cin >> numbs[idx];
-
if(numbs[idx] == 0) {
-
break; //user entered 0 signifying end of loop
-
}
-
}
-
int n = idx; //saves the size of the array to the n variable
-
sort(numbs, r, numbs2, n);
-
printarray(numbs2, r - 1); //calls the print array function and itterates i\
-
t
-
//using numbs
-
cout << endl; // new line
-
}
-
and here my other file which contains the functions: -
using namespace std;
-
-
//printarray definition
-
void printarray(int a[], int size){
-
for(int i = size; i > 0; i--)//loop that prints out all values in an array
-
cout << a[i];
-
}
-
-
void sort(int a[], int h, int a2[], int size){
-
h = 0;
-
for (int i = 0; i < size; i++){
-
for ( int j = 0; j < size; j++){
-
if (a[i] == a2[j]){break;}
-
else{
-
a[i] = a2[h];
-
h++;}
-
}
-
}
-
}
-
I believe the problem is somewhere in my sort function..Im not supposed to srt it tho just simply remove the dupes and when I look at it I feel it should do just that but it's not :
If user enters 5 26 22 26 7 8 7 9 5
it should print: 5 26 22 7 8 9 (print each number once)
any help would be greatly appreciated because im seriously stuck
Take baby steps and separate responsibilitie s; here's a little function that
searches an array (up to element 'n') for a value 'v': -
int isPresent(int a[], int v, int n) {
-
for (int i= 0; i < n; i++)
-
if (a[i] == v) return true;
-
return false;
-
}
... now suppose an array contains the first occurances of elements so far and
suppose you're scanning another array for those first occurances of elements.
I don't think it'll come as a surprise that the following (pseudo) code can do
the job: -
void uniques(int a[], int unique[], int size) {
-
int u= 0; // no uniques found yet
-
for (int i= 0; i < size; i++)
-
if (!isPresent(unique, a[i], u)) {
-
// add a[i] to array unique
-
// and print it
-
}
-
}
kind regards,
Jos
for the isPresent function though i don;t know what number im searching for...for the int v...shouldnt i have 2 for loops then to iterate all the number of the array -
int isPresent(int a[], int n) {
-
for (int i= 0; i < n; i++){
-
for (int v = 1; v < n; v++)
-
if (a[i] == a[v]) return true;
-
return false;
-
}
-
}
-
Im sorry but Im new to C++ this is only my second semester ever using it and I just got the basics barely down.
Ganon11 3,652
Recognized Expert Specialist
for the isPresent function though i don;t know what number im searching for...for the int v...shouldnt i have 2 for loops then to iterate all the number of the array -
int isPresent(int a[], int n) {
-
for (int i= 0; i < n; i++){
-
for (int v = 1; v < n; v++)
-
if (a[i] == a[v]) return true;
-
return false;
-
}
-
}
-
Im sorry but Im new to C++ this is only my second semester ever using it and I just got the basics barely down.
In JosAHs version, int n would be the item you are searching for. You would probably call isPresent within a loop, so that every number in a ends up being n in the function at one point. With this taken into consideration, JosAHs solution will work for you.
Your code, however, is closer to what I'd use - with some tweaking, it could work. You're right that you would need two loops - in JosAHs example, the second for loop (the outer one) is supposed to be outside the function, in main(). In your example, you will have both together. Also, your v loop will not work as planned. Suppose you are on the second iteration of the outer loop - that is, i = 1. Since you start the next loop at v = 1, true is returned - a[i] == a[v], because a[i] == a[1] == a[v].
In JosAHs version, int n would be the item you are searching for. You would probably call isPresent within a loop, so that every number in a ends up being n in the function at one point. With this taken into consideration, JosAHs solution will work for you.
Your code, however, is closer to what I'd use - with some tweaking, it could work. You're right that you would need two loops - in JosAHs example, the second for loop (the outer one) is supposed to be outside the function, in main(). In your example, you will have both together. Also, your v loop will not work as planned. Suppose you are on the second iteration of the outer loop - that is, i = 1. Since you start the next loop at v = 1, true is returned - a[i] == a[v], because a[i] == a[1] == a[v].
so where should i start v at....0? also im not sure which loop goes in main and how I get n to be every value in the array?
heres my main -
#include <iostream>
-
#include "accfuncs.cpp"
-
using namespace std;
-
-
int isPresent(int a[], int v, int n);
-
void uniques(int a[], int unique[], int size);
-
void printarray(int a[], int size);// function declaration that prints array
-
int main ()
-
{
-
int numbs[20], numbs2[20], idx; //declared numbs array and a integer symbol\
-
izing the index.
-
cout << "Please enter in a series of numbers ending it with a 0: ";
-
-
// For loop that takes input from the user of up to 20 numbers and stores them
-
// in the numbs array. Also if the user enters a 0 it ends the loop.
-
for(idx = 0; idx < 20; idx++) {
-
cin >> numbs[idx];
-
if(numbs[idx] == 0) {
-
break; //user entered 0 signifying end of loop
-
}
-
}
-
int n = idx; //saves the size of the array to the n variable
-
for (i = 0; i < n; i++){
-
isPresent(numbs, n, numbs[i]);
-
}
-
uniques(numbs, numbs2, n);
-
printarray(numbs2, n); //call function and print array (numbs)
-
cout << endl; // new line
-
}
-
and heres the function file -
void printarray(int a[], int size){
-
for(int i = 0; i < size; i++)//loop that prints out all values in an array
-
cout << a[i];
-
}
-
int isPresent(int a[], int v, int n) {
-
for (int i= 0; i < n; i++)
-
if (a[i] == v) return true;
-
return false;
-
}
-
void uniques(int a[], int unique[], int size) {
-
int u = 0;// no uniques found yet
-
for (int i= 0; i < size; i++)
-
if (!isPresent(unique, a[i], u)) {
-
a[i] = unique;
-
}
-
}
-
When I try to compile it I get the following errors and Im unsure of what they mean....I tried 80 different things and I always get compiling errors and then when I don't it prints some wacky output but I know the code works without the two function uniques and isPresent. But all it does is take input from the user and outputs it back to the screen without removing the duplicates and that s what Im trying to accompish but Im having minor problems. ALl it needs is a little tweak but Im to inexperienced to see what it is that needs changed. Im up for any help possible. Thank You
heres the compiler errors with that exact code: accfuncs.cpp: In function `void uniques(int*, int*, int)':
accfuncs.cpp:23 : error: invalid conversion from `int*' to `int'
accountant.cpp: In function `int main()':
accountant.cpp: 28: error: `i' was not declared in this scope
update i fixed the error in main i didnt have the int i in the for loop. but still need help with what i said before.
heres my main -
#include <iostream>
-
#include "accfuncs.cpp"
-
using namespace std;
-
-
int isPresent(int a[], int v, int n);
-
void uniques(int a[], int unique[], int size);
-
void printarray(int a[], int size);// function declaration that prints array
-
int main ()
-
{
-
int numbs[20], numbs2[20], idx; //declared numbs array and a integer symbol\
-
izing the index.
-
cout << "Please enter in a series of numbers ending it with a 0: ";
-
-
// For loop that takes input from the user of up to 20 numbers and stores them
-
// in the numbs array. Also if the user enters a 0 it ends the loop.
-
for(idx = 0; idx < 20; idx++) {
-
cin >> numbs[idx];
-
if(numbs[idx] == 0) {
-
break; //user entered 0 signifying end of loop
-
}
-
}
-
int n = idx; //saves the size of the array to the n variable
-
for (i = 0; i < n; i++){
-
isPresent(numbs, n, numbs[i]);
-
}
-
uniques(numbs, numbs2, n);
-
printarray(numbs2, n); //call function and print array (numbs)
-
cout << endl; // new line
-
}
-
and heres the function file -
void printarray(int a[], int size){
-
for(int i = 0; i < size; i++)//loop that prints out all values in an array
-
cout << a[i];
-
}
-
int isPresent(int a[], int v, int n) {
-
for (int i= 0; i < n; i++)
-
if (a[i] == v) return true;
-
return false;
-
}
-
void uniques(int a[], int unique[], int size) {
-
int u = 0;// no uniques found yet
-
for (int i= 0; i < size; i++)
-
if (!isPresent(unique, a[i], u)) {
-
a[i] = unique;
-
}
-
}
-
When I try to compile it I get the following errors and Im unsure of what they mean....I tried 80 different things and I always get compiling errors and then when I don't it prints some wacky output but I know the code works without the two function uniques and isPresent. But all it does is take input from the user and outputs it back to the screen without removing the duplicates and that s what Im trying to accompish but Im having minor problems. ALl it needs is a little tweak but Im to inexperienced to see what it is that needs changed. Im up for any help possible. Thank You
heres the compiler errors with that exact code: accfuncs.cpp: In function `void uniques(int*, int*, int)':
accfuncs.cpp:23 : error: invalid conversion from `int*' to `int'
accountant.cpp: In function `int main()':
accountant.cpp: 28: error: `i' was not declared in this scope
You're saying "a[i] = unique". "unique" is an array, and you can't assign an array to "a[i]" I believe what you need is a variable to store the current index in "unique". So then you can do "unique[variable_that stores_index] = a[i];" That way you fill unique with the numbers that are not duplicates.
Hope that helped.
You're saying "a[i] = unique". "unique" is an array, and you can't assign an array to "a[i]" I believe what you need is a variable to store the current index in "unique". So then you can do "unique[variable_that stores_index] = a[i];" That way you fill unique with the numbers that are not duplicates.
Hope that helped.
WOW that worked only one problem...its getting rid of the duplicates but im gettin some crazy extra output after i enter in some numbers. Their is one minor problem in the code and I can't find it. it compiles fine and does what i want but then additional numbers get output that weren't in the input. heres my code...Please any ideas will help... when i enter in:
3
3
4
5
6
6
7
0
output:
3456758900360
heres my main: -
#include <iostream>
-
#include "accfuncs.cpp"
-
using namespace std;
-
-
int isPresent(int a[], int v, int n);
-
void uniques(int a[], int unique[], int size);
-
void printarray(int a[], int size);// function declaration that prints array
-
int main ()
-
{
-
int numbs[20], numbs2[20], idx; //declared numbs array and a integer symbol\
-
izing the index.
-
cout << "Please enter in a series of numbers ending it with a 0: ";
-
-
// For loop that takes input from the user of up to 20 numbers and stores them
-
// in the numbs array. Also if the user enters a 0 it ends the loop.
-
-
int isPresent(int a[], int v, int n);
-
void uniques(int a[], int unique[], int size);
-
void printarray(int a[], int size);// function declaration that prints array
-
int main ()
-
{
-
int numbs[20], numbs2[20], idx; //declared numbs array and a integer symbol\
-
izing the index.
-
cout << "Please enter in a series of numbers ending it with a 0: ";
-
-
// For loop that takes input from the user of up to 20 numbers and stores them
-
// in the numbs array. Also if the user enters a 0 it ends the loop.
-
for(idx = 0; idx < 20; idx++) {
-
cin >> numbs[idx];
-
if(numbs[idx] == 0) {
-
break; //user entered 0 signifying end of loop
-
}
-
}
-
int n = idx; //saves the size of the array to the n variable
-
for (int i = 0; i < n; i++){
-
isPresent(numbs, n, numbs[i]);
-
}
-
uniques(numbs, numbs2, n);
-
printarray(numbs2, 7); //call function and print array (numbs)
-
cout << endl; // new line
-
}
-
-
heres the functions file -
using namespace std;
-
-
//printarray definition
-
void printarray(int a[], int size){
-
for(int i = 0; i < size; i++)//loop that prints out all values in an array
-
cout << a[i];
-
}
-
int isPresent(int a[], int v, int n) {
-
for (int i= 0; i < n; i++)
-
if (a[i] == v) return true;
-
return false;
-
}
-
void uniques(int a[], int unique[], int size) {
-
int u = 0;// no uniques found yet
-
for (int i= 0; i < size; i++)
-
if (!isPresent(unique, a[i], u)) {
-
unique[u] = a[i];
-
u++;
-
}
-
}
-
disregard that last post and for some reaosn i couldn't delete it that was when I was trying some changes of my own...heres the right code i have
its getting rid of the duplicates but im gettin some crazy extra output after i enter in some numbers. Their is one minor problem in the code and I can't find it. it compiles fine and does what i want but then additional numbers get output that weren't in the input. heres my code...Please any ideas will help... when i enter in:
3
3
4
5
6
7
7
8
0
output: 3456780-1075255112
heres my main: -
#include <iostream>
-
#include "accfuncs.cpp"
-
using namespace std;
-
-
int isPresent(int a[], int v, int n);
-
void uniques(int a[], int unique[], int size);
-
void printarray(int a[], int size);// function declaration that prints array
-
int main ()
-
{
-
int numbs[20], numbs2[20], idx; //declared numbs array and a integer symbol\
-
izing the index.
-
cout << "Please enter in a series of numbers ending it with a 0: ";
-
-
// For loop that takes input from the user of up to 20 numbers and stores them
-
// in the numbs array. Also if the user enters a 0 it ends the loop.
-
-
int isPresent(int a[], int v, int n);
-
void uniques(int a[], int unique[], int size);
-
void printarray(int a[], int size);// function declaration that prints array
-
int main ()
-
{
-
int numbs[20], numbs2[20], idx; //declared numbs array and a integer symbol\
-
izing the index.
-
cout << "Please enter in a series of numbers ending it with a 0: ";
-
-
// For loop that takes input from the user of up to 20 numbers and stores them
-
// in the numbs array. Also if the user enters a 0 it ends the loop.
-
for(idx = 0; idx < 20; idx++) {
-
cin >> numbs[idx];
-
if(numbs[idx] == 0) {
-
break; //user entered 0 signifying end of loop
-
}
-
}
-
int n = idx; //saves the size of the array to the n variable
-
for (int i = 0; i < n; i++){
-
isPresent(numbs, n, numbs[i]);
-
}
-
uniques(numbs, numbs2, n);
-
printarray(numbs2, n); //call function and print array (numbs)
-
cout << endl; // new line
-
}
-
-
heres the functions file -
using namespace std;
-
-
//printarray definition
-
void printarray(int a[], int size){
-
for(int i = 0; i < size; i++)//loop that prints out all values in an array
-
cout << a[i];
-
}
-
int isPresent(int a[], int v, int n) {
-
for (int i= 0; i < n; i++)
-
if (a[i] == v) return true;
-
return false;
-
}
-
void uniques(int a[], int unique[], int size) {
-
int u = 0;// no uniques found yet
-
for (int i= 0; i < size; i++)
-
if (!isPresent(unique, a[i], u)) {
-
unique[u] = a[i];
-
u++;
-
}
-
}
-
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Michelle |
last post by:
hi,
i have created an array from recordset containing user names
eg. (davidp, davidp, evenf, patricka, rebeccah)
which i have sorted in alphabetical order, but i need to
identify duplicates in this array and the number of times it has
been duplicated.
can someone help?
|
by: M B HONG 20 |
last post by:
Hi all -
I was wondering if Javascript has a way to easily remove duplicates
from a string. For example, if I had a string:
"car truck car truck truck tree post post tree"
it should turn into:
"car truck tree post"
|
by: Drew |
last post by:
I have a permission tracking app that I am working on, and I have made the
insert page for it. I am having issues on how to prevent duplicates from
getting entered.
Currently the interface for the app has a mixture of select boxes, list
boxes and checkboxes. The form submits the page to processAIMR.asp and then
does the inserting. I am using a loop to insert a new record for each
checkbox checked or listbox entry selected.
My...
|
by: vbportal |
last post by:
Hi,
I would like to add BitArrays to an ArrayList and then remove any duplicates
- can someone please help me forward. I seem to have (at leaset ;-) )2
problems/lack of understanding (see test code below):
(a)When adding BitArrays to the ArrayList and then looping through the
ArrayList I seem to access only the latest added BitArray and I'm not exactly
clear on best way to access each BItArray in the ArrayList
(b)When I try to remove...
|
by: tyrfboard |
last post by:
I've been searching for awhile now on how to remove duplicates from a
table within an Access db and have found plenty of articles on finding
or deleting duplicates. All I want to do is remove them from within an
SQL query - leaving one of the records behind of course.
I have a mailing list comprised of a union query that gets records from
two separate tables.
I want to be able to run a query that removes one (or more) of the
duplicated...
| |
by: almurph |
last post by:
Folks,
I have an array of chars nad would like to remove any repeating
terms. Any ideas as how to do this?
Thanks,
Al.
|
by: Niyazi |
last post by:
Hi all,
What is fastest way removing duplicated value from string array using vb.net?
Here is what currently I am doing but the the array contains over 16000
items. And it just do it in 10 or more minutes.
'REMOVE DUBLICATED VALUE FROM ARRAY +++++++++++++++++
Dim col As New Scripting.Dictionary
Dim ii As Integer = 0
|
by: Mokita |
last post by:
Hello,
I am working with Taverna to build a workflow. Taverna has a beanshell where I can program in java. I am having some problems in writing a script, where I want to eliminate the duplicates in an array (String).
for (int k=0; k<myLength; k++){
boolean isthere=false;
for (int l=0; l<sizeres; l++){
if (res.equals(my)){
|
by: Thekid |
last post by:
I'm trying to figure out a way to find if there are duplicates in an array. My idea was to take the array as 'a' and make a second array as 'b' and remove the duplicates from 'b' using 'set' and then compare a to b. If they're different then it will print out 'duplicates found'. The problem is that even after trying different arrays, some with duplicates some without, that 'b' rearranges the numbers. Here's an example:
a='1934, 2311,...
|
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...
|
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,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |