C :: Printing Duplicates Found In Array?
May 8, 2013
I'm having some trouble printing the duplicates found in an array. Specifically, when the value is at more than 2 positions. So if the value 3 is at position 1, 10, and 11 it'll print three messages instead of two:
value 3 at position 1 is also at position 10
value 3 at position 1 is also at position 11
value 3 at position 10 is also at position 11
instead of
value 3 at position 1 is also at position 10
value 3 at position 1 is also at position 11
This is real simple problem, but I can't seem to figure it out. I've been trying to implement another array to 'remember' the encountered position, but I haven't had any luck.
Code:
for(i = 0; i < num_count; i++){for (j = i + 1; j < num_count; j++) {if (num[i] == num[j]){printf("
value %d at position %d is also at position %d", num[i], i, j);}}}
View 8 Replies
ADVERTISEMENT
Jul 21, 2014
I have an array of structures with structure def that looks like.
Code:
struct {
char * name;
char * school;
int age;
}
there are multiple people with same name but different ages so i want to list them like.
Name.
age 1
age 2 and so on
The array is sorted by name already.
View 3 Replies
View Related
Mar 9, 2013
I have same question as posted by holla and Iam not sure about merging the contents of 2 sorted arrays into another array without duplication of values.
View 9 Replies
View Related
Nov 9, 2014
I was instructed to write a binary search function which would return true if an element, inputted by the user, was found in the array, and false if it was not. I'm not sure why, but my function always returns false. My code is as follows.
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
//binary search function
bool search (int array[], int item)
[Code] ....
Why my code does not fulfill it's purpose???
View 7 Replies
View Related
Dec 7, 2013
Question: How to find a duplicate numbers and numbers found once in array.
View 7 Replies
View Related
Apr 3, 2013
Code: /*
generals is the first array. Max 10 elements.
numGenerals is the element count of generals.
genBuff is the second array; it is to be checked/pruned.
genCount is the element count of genBuff.
genBuff will be a max of 171, but be pruned to no more than 10, and no more than the complement of the element count of generals.
*/
[Code] ....
(I do have comments in the actual source, different from above).
I have two int arrays. They hold values from 0 to 170. The first one will never be more than 10. The second will be at most 171, but will be whittled down to at most 10, usually less. 171 is worst case, most users of this particular program will probably be reasonable and not try to add all 171 (max is 10 anyway). The first array is the original array. The second array is a temporary array. Any value in the second array that is also found in the first array, is removed from the second array, since all values in the first one must be unique. After this pruning process, both arrays will collectively contain no more than 10 unique elements; the elements from the second will be added to the first.
So right now I have three nested loops. I figured with the miniscule array sizes it wouldn't be a big deal. I can think of a way to remove one or two of them, but I want to be sure that I'm still writing clean, legible, good-practice code. The first loop walks through the first array. For each element in the first array, there is a second loop to walk through the second array to check for duplicates. If a duplicate is found, the third loop walks through the second array to overwrite the duplicate while preserving the second loop's position (j).
Is this dumb? I know that the big O gets worse and worse the deeper you go with nested loops. Even though the arrays are really tiny, is this still a thing to avoid?
View 5 Replies
View Related
Feb 24, 2013
I am having problems with this function:
bool HashTable::insert(char const * const key, const Player& aPlayer) {
//calculate the insertion position (the index of the array)
size_t index = calculateIndex(key);
for(int i=0; i < capacity; i++) {
[Code] ....
The inserting part works just fine, but the checking for duplicates where I compare the two values is crashing my program.
View 1 Replies
View Related
Nov 18, 2014
I have a program that's supposed to read in a file with comma seperated values. This file contains duplicates. The goal is to write a new file that does not contain any of the duplicates. I've successfully written the code to read in a file and create a new, identical file, but I'm failing at deleting the duplicates.
The format of each line in the file is: index,first_name,last_name,address,city,state,zip_code
Example: 1,John,Savage,579 Lone Street,Providence,RI,02903
As a requirement for the assignment, I've defined a Person class:
//File: Person.h
struct Person {
string index;
string first_name;
string last_name;
[Code] .....
This code writes the file I want (overlooking the duplicates) if I implement my equality operator as follows:
bool operator ==(const Person &a, const Person &B)/>
{
return false; //placeholder
}
Obviously this doesn't get the job done, since it will never detect a duplicate. The problem is that whenever I try to write any meaningful code, the program writes an empty file. The idea I've been trying to implement is to compare each of the members of Person like this:
bool operator ==(const Person &a, const Person &B)/>
{
//if two Person objects have equivalent names, they are duplicates
return ( (a.first_name == b.first_name) && (a.last_name == b.last_name) )
}
At first I thought the program was working just as before, but then deleting each line of the file as the result of an error in my code. However, I tried troubleshooting the problem by adding in
cout << a.last_name;
to parts of my code so I could see the value in certain places. Whenever I add this line or try to access a member of Person, the program writes a blank file.
View 3 Replies
View Related
Oct 12, 2014
I'm making the n puzzle game (15 puzzle), but having a problem generating random numbers without the duplicates.
Random num = new Random();
int rand = num.Next(1, 15);
b.Text = rand.ToString();
eliminating the duplicate numbers and only allowing 1-15 once in each button.
View 14 Replies
View Related
Dec 13, 2013
I want to create a randomly ordered array of integers where there are no duplicates. Is there a way of doing this in one iteration? Or maybe even a standard function for this?
I'm looking for something like this:
A2 = [3 2 1 6 7 8 4 5]
View 12 Replies
View Related
Aug 25, 2014
I have this structures:
Code:
typedef struct {{
GList *presets;
} Settings;
typedef struct Preset preset;
struct Preset
{gchar *name;
[Code]...
I want to remove duplicates items in list if them have same freq value.
Example output with duplicate freq:
PHP Code:
name freq unammed 87.80 Radio ZU 92.20 Napoca FM 104.50 unammed 92.20 Rock FM 102.20
Want output:
PHP Code: Europa FM 92.20 unammed 87.80 Radio ZU 92.20 Napoca FM 104.50 Rock FM 102.20
write code to remove duplicates.
View 10 Replies
View Related
Nov 18, 2013
I'm trying to sort my vector, find duplicate values using unique and erase them. Program can compile successfully but the duplicates are not removed.
output:
x: 3
y: 2
x: 6
y: 4
x: 3
y: 2
vector<Point2D> p2dvector;
void readData()
[Code].....
I also have operator== in my Point2D class which I understand it is required to use unique for vector.
bool operator==(const Point2D& lhs, const Point2D& rhs)
{
return lhs.x == rhs.y;
}
View 10 Replies
View Related
Nov 21, 2013
I have an array, ary[size+1] and the original values entered and then another value, x, entered. I found the index of x that makes the array nondecreasing order
Code:
void InsertX (int ary[], int size, int x)
{
int i=0;
int j;
int index;
while(ary[i]<x)
[Code]...
But i can't figure out how to print the new array with x in it, using its index
View 6 Replies
View Related
May 8, 2013
how to print a box using 2d arrrays. i have to create a tetris game which a im stuck at this stage... so far i have tired i can only come out with this..my coding done so far is as follows:
Code:
#include <stdio.h>
#define ROW 15
#define COLUMN 15
void disp_box (char b[ROW][COLUMN])
}
[code]....
View 2 Replies
View Related
Mar 18, 2014
I have a vector of
strings.vector input;
bob
sam
bob
sammom
aardvark
money
aardvark
wanted
I need to remove the duplicates but each part of the vector corresponds to the next location. They are pairs.
ex. bob corresponds to the its definition, which is sam.
I need to keep the first instance, so keep bob and sam, but remove the second instance of bob, so remove bob and sammon. Only the first instance of the pair need to kept.
It doesn't matter if the sam and sammon don't match, all that matters is the first part of the pair.
The vector is already in alphabetical order. I can't use the algorithm library.
View 4 Replies
View Related
Mar 8, 2013
I have a text file that needs to be read by command line arguments. The text are all numbers and can have multiple numbers on one line separated by a space. I cannot use an array or sort the numbers.So say I have a text file, listNums.txt:
12
473 8 29
30 1
8
248 17 55
29 84
5
Basically I need to read one number, find out if its odd or even by dividing by 2, search the odd or even doubly linked list that it would go into to see if its in there, if its not then add it to the bottom of the list.
View 2 Replies
View Related
Oct 25, 2013
I want to make a program that asks the user for a message and then print out a large graphic of that message. For example, if the user types "he" I want to print out
H..................H EEEEEEEEE
H..................H E
H..................H E
H..................H E
HHHHHHHHHH EEEEEEEEE
H..................H E
H..................H E
H..................H E
H..................H EEEEEEEEE
(treat the periods as spaces. I only put them there because it wouldn't separate the H's correctly.)
I will loop this to continue until the user types quit.
1. How would I set this up to store the user input characters into an array?
2. How would I print out the stored data in the shape of the word?
View 4 Replies
View Related
Nov 4, 2013
I am trying to print out prime from array. I have viewed other sources that are similar and tried to use as reference, but only to confuse myself. This is what i have so far.
void get_output(int&Max,int array[]) {
//array[i]-i;
for(i=1;i<=10;i++) // allow 10 outputs on a line. {
for(int i=0;i<Max;i++) {
prime=true;
[Code] ......
View 4 Replies
View Related
Feb 18, 2014
I am writing a code with C where I will give an integer and get the binary conversion of it.I tried to fill the binary digits into an integer array.But when I normally print it will give the proper output.But when I try to print the contents of the array it will not produce the proper result.
My code would be as follows.
#include <stdio.h>
#include <conio.h>
int main()
[Code]....
View 5 Replies
View Related
Apr 10, 2015
I have a program where I need to read in a 96 number .txt file and perform some operations on it.However, the numbers it reads in are all inncorrect. I got it to work in XCode, but it doesnt work in DevC++ which is where the teacher will be running it. Do I have any major error?
#include <stdio.h>
#include <stdlib.h>
#define rows 8
#define cols 12
void makeArray(FILE*,int[][cols]);
int getScore(int[rows][cols],int,int);
[Code]......
View 14 Replies
View Related
Oct 10, 2014
I have an array of structure that takes family information (name, age, and state) after it takes the users input it prints back all of the information that was input and then prints the family members that just live in Texas. After that I am trying to implement a function to average out the ages of all family members but I cant seem to get it right, whenever it gets to that part of the program it just outputs 0. I also tried adding an & sign prior to FAMILY[i].age and got an error of int from ptr with no cast.
Also I realize that the code only shows adding of the family members ages which is fine for now as I am concentrated on getting that to work in the first place.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//Global Variables
int i=0, a=0, sum=0;
[Code].....
View 8 Replies
View Related
Aug 28, 2014
I am doing a data structures program on C++ that is suposse to consist of declaring an array of size five for my credit card structure, that allows the user to input information of five diferent credit cards. I am having a problem with my program, because it runs but after I input the information of the credit card, it does not print out the values that I had input.I dont know where I went wrong with the code or if it is that my approach to solving it is wrong. My code is the following:
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
struct TarjetaCredito
[code].....
View 2 Replies
View Related
Apr 11, 2014
I need to print all the values in a square 2d array that are above the diagonal line. So if I have this 6x6 array for instance:
{0,0,0,0,0,0},
{1,1,1,1,1,1},
{2,2,2,2,2,2},
{3,3,3,3,3,3},
{4,4,4,4,4,4},
{5,5,5,5,5,5}
I'd have to print the elements in italics. I've tried this:
for (i = 1; i < a.GetLength(0); i++) {
for (j = i-1; j >=0; j--)
Console.Write("{0,-2}", a[i, j]);
Console.WriteLine();
}
with the array mentioned above but for some reason it keeps printing the elements BELOW the line, when I need it to print those above it. I know that for arrays we usually start loops with a value of 0 but seeing as there should be nothing to print at [0,0] I started the loop at 1.
View 2 Replies
View Related
Jan 31, 2014
what if we want to print both the diagonal elements!!!!
View 1 Replies
View Related
Feb 17, 2014
I am trying to print the array of string using this code but the compiler is printing null in the output:
The code is as follows:
char *arr[10];
int i;
for(i=0;i<10;i++)
scanf("%s",*(arr+i));
for(i=0;i<10;i++)
printf("%s
",*(arr+i));
View 3 Replies
View Related
Nov 2, 2013
I have a piece of code in C with header files included. I run it on Mac OS X Maverick with XCode 4.6.2 installed. GCC is also installed. Note that Command Line Tools in XCode are already installed.
When I compile it, the error I receive says something like this:
add.c:1:19: error: stdio.h: No such file or directory add.c:2:20: error: stdlib.h: No such file or directory add.c:3:20: error: unistd.h: No such file or directory
However when I run it on Ubuntu, it compiles without a problem.What to do?
View 2 Replies
View Related