473,702 Members | 2,679 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

check 2 D array columns then rows for duplicates

41 New Member
Hi I am stuck on checking if a 2D array has duplicates.

I have to write a function to check if a column has duplicates and then another function to check if the rows have duplicates from a file.

How would I go about this... I have the array reading a file correctly is it just the check for duplicates that I am stuck on... :)
Mar 2 '09 #1
40 17986
donbock
2,426 Recognized Expert Top Contributor
What is your definition of a "duplicate" ?
> Two cells that have the same value?
> Identical rows?
> Identical columns?
... or something else?
Mar 2 '09 #2
kylie991
41 New Member
a row that contains more than the same character once...

eg.

123456789 has no duplicates
123456799 has a duplicate

I am stuck on how to check a 2D array for duplicates. But I need to write one function to check the columns for any duplicates and another function to check the rows. If I get help to do at least one of them then I will work on the other myself..

below is my program currently.

Expand|Select|Wrap|Line Numbers
  1. int loadFile(char data[9][9]) 
  2.  ifstream infile; 
  3.    string filename; 
  4.    float list = 0; 
  5.    cout << "Enter file name " << endl; 
  6.    cin >> filename; 
  7.    infile.open(filename.c_str()); 
  8.    if (infile.fail()) 
  9.    { 
  10.       cout << "File not found " << endl; 
  11.       return EXIT_FAILURE; 
  12.    } 
  13.    for(int i=0;i<9;i++) 
  14.      for(int j=0;j<9;j++) 
  15.        infile >> data[i][j]; 
  16.    return EXIT_SUCCESS; 
  17.  
  18. int displayFile(char data[9][9]) 
  19.     for(int i=0;i<9;i++) 
  20.      { 
  21.      for(int j=0;j<9;j++) 
  22.        cout << data[i][j] << " "; 
  23.      cout << endl; 
  24.      } 
  25. }
Mar 2 '09 #3
donbock
2,426 Recognized Expert Top Contributor
A "duplicate" is ...
@kylie991
So you want to be able to check each row and each column for duplicate entries.

Consider your example rows. Start with the first character ("1") and compare it to each character to its right. If no matches, then take the second character ("2") and compare it to each character to its right. And so on. That's a general solution.

However, the specifics of your problem might allow for a simplification. If the permissible values in the array are restricted to a small range (for example, 1 to 9) then you can check off each permissible value as you scan the row or column. You have a duplicate if any permissible value has more than one check mark.
Mar 2 '09 #4
kylie991
41 New Member
thanks for that.. I understand that is what I have to do but when I try to write the function I must be getting the coding wrong.. because it wont work.... I was thinking of creating a temp variable but that wont work either as I need to know if there is a duplicate in the whole row eg.

123426 // 2 is in that row twice but if I had a temp to check the array it wouldnt pick it up unless it was right after it...??

for now we can assume the column max is 9 and row max is 9 (in total 81 characters)...

I think what I am confused about is how to check it...

How woud I write the function for check cols... Am I kind of on the right track??? Do I need to implement a copy of the array???

int checkCols(char data[9][9]) //not complete
{
float checkCols = 0;
for(int i=0;i<9;i++)
{
for(int j=0;j<9;j++)
{

if (data[i][j] == checkCols) // i know this is wrong but I know I need to compare this with something once I have the code above sorted.

}
cout << endl;
}
}
Mar 2 '09 #5
donbock
2,426 Recognized Expert Top Contributor
I suggest you take a look at Arrays Revealed.

For example, this function definition isn't right:
Expand|Select|Wrap|Line Numbers
  1. int checkCols(char data[9][9]) {...}
Mar 2 '09 #6
kylie991
41 New Member
what do you mean?? Do you mean I should have int checkCols(char data[8][8])??

I'm confused...

I am just finding this really hard because anytime I try to sort this array it goes back to the top of my program and says that float list; is not being used....

Expand|Select|Wrap|Line Numbers
  1. #include <iostream> 
  2. #include <string> 
  3. #include <fstream> 
  4.  
  5. using namespace std; 
  6.  
  7. void printMenu();
  8. int loadFile(char data[9][9]);
  9. int displayFile(char data[9][9]);
  10. //int checkCols(char data[9][9]);
  11. void bubbleSort(char data [9][9]);
  12. void ShowArray(char data [9][9]);
  13.  
  14. int main() 
  15.  
  16.  char choice = '*';
  17.      while (choice != 'Q')
  18.  
  19.  
  20.  
  21.      {
  22.          char data[9][9];
  23.          printMenu();
  24.          cin >> choice; 
  25.          switch (toupper(choice))
  26.          {
  27.                 case 'L' :  loadFile(data); 
  28.                            break;
  29.                 case 'D' : displayFile(data); 
  30.                            break;
  31.                 case 'R' :  // complete this
  32.                 case 'C' :  bubbleSort(data);
  33.                             ShowArray(data);
  34.                            break;
  35.                 case 'M' :  // complete this
  36.                 case 'Q' : break;
  37.                 default :  cout << "Invalid option " << endl; cin.ignore(100,'\n');
  38.          }
  39.  
  40.      }     
  41.  
  42.     return 0;
  43.  
  44.  
  45.  
  46.  system("pause");
  47.  
  48. void printMenu()
  49. {
  50.      cout << "\n\tSudoku Checker " << endl << endl;
  51.      cout << "\t L\t Load file " << endl;
  52.      cout << "\t D\t Display " << endl;
  53.      cout << "\t C \t Check columns " << endl;
  54.      cout << "\t R \t Check rows " << endl;
  55.      cout << "\t M \t Check minigrids" << endl;
  56.      cout << "\t Q\t Quit " << endl;
  57.      cout << " Rows and columns are labelled from 0 to 8 " << endl;
  58.      cout << endl;
  59. }
  60. int loadFile(char data[9][9]) 
  61.  ifstream infile; 
  62.    string filename; 
  63.    float list = 0; 
  64.    cout << "Enter file name " << endl; 
  65.    cin >> filename; 
  66.    infile.open(filename.c_str()); 
  67.    if (infile.fail()) 
  68.    { 
  69.       cout << "File not found " << endl; 
  70.       return EXIT_FAILURE; 
  71.    } 
  72.    for(int i=0;i<9;i++) 
  73.      for(int j=0;j<9;j++) 
  74.        infile >> data[i][j]; 
  75.    return EXIT_SUCCESS; 
  76.  
  77. int displayFile(char data[9][9]) 
  78.     for(int i=0;i<9;i++) 
  79.      { 
  80.      for(int j=0;j<9;j++) 
  81.        cout << data[i][j] << " "; 
  82.      cout << endl; 
  83.      } 
  84. }
Mar 2 '09 #7
kylie991
41 New Member
can someone pls help?? I am stuck :(
Mar 2 '09 #8
donbock
2,426 Recognized Expert Top Contributor
@kylie991
Follow the link provided in message #4.
Mar 3 '09 #9
kylie991
41 New Member
I did read it but it hasnt helped. I do understand the concept of arrays it is the finding duplicate files that is confusing me... the task is...
* each column is checked for duplicate values. If a column contains a duplicate value then the message "column "c " is incorrect " is displayed. If all columns are correct then the message "All columns are correct " is displayed.
Mar 3 '09 #10

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

Similar topics

8
7810
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?
5
1833
by: Mr. B | last post by:
The following code is how I check for duplicates in a List box. This is simple enough as there is only one column of stuff to check. ' Check for Duplicates ' Search listbox (from last to first) For cntr = lbDwgList.Items.Count - 1 To 1 Step -1 ' If next item is a duplicate -> Remove It If lbDwgList.Items(cntr) = lbDwgList.Items(cntr - 1) Then _ lbDwgList.Items.RemoveAt(cntr) Next
1
6430
by: Sam | last post by:
Hello all I have a two dimensional array (the dimensions are not known) that needs to be passed to fortran from c++, allocate the dimensions of the array in fortran code, do some filling up of the array in fortran and then accessing it in my c++ code. Say in my c++ code I have; extern "C" { void foo_(float **, int &, int &); }
6
3540
by: Doug | last post by:
I have data that looks something like this when returned from a stored procedure (3 columns, X rows): Key1 Name1 Value1 Key1 Name2 Value2 Key1 Name3 Value3 Key1 Name4 Value4 Key2 Name1 Value1 Key2 Name2 Value2 Key3 Name1 Value1
4
5029
by: Haydnw | last post by:
Hi, I'd like to put a load of database results (several rows for 5 fields) into a two-dimensional array. Now, this may be a really stupid question, but can someone give me a pointer for how to do it? I can bind data to datagrids and lists and stuff all day long, but can't seem to grasp this one. Any pointers to a useful article / demo (or just any useful pointers!) would be much appreciated. Thanks,
33
1867
by: Peace | last post by:
I am having trouble writing code to turn an array into a delimited string. Dim splitout As String splitout = Join(DataArray(rows, columns), " ") rows and columns are integers representing rows and columns quanitity of datasets. Could someone help show what I am doing wrong? When ever I write splitout to to a text file nothing is there.
3
10576
by: Eric Lilja | last post by:
Assignment: Create a 3x4 2-dimensional array of integers. Populate each element using some non-random scheme so that no two elemens contain the same value. Then create two functions for printing the 2D array. One should only work for set column size, accepting only the number of rows as an additional argument, one should accept both the number of rows and columns as arguments. So, I came up with: #include <stdio.h>
8
11826
by: per9000 | last post by:
Hi all, I have a two-dimensional array of data, f.x int's. We can imagine that the array is "really large". Now I want the data in it and store this in a one-dimensional array. The obvious way to do this is a nested for-loop - but we all know O(n^2) is bad. So I am looking for something like ArrayList.ToArray(), or Matlabs A(:). C#
1
2164
by: tskmjk55 | last post by:
Recently, I have a requirement to develop a vb.net application wherein the input excel sheet data which has an average of 5000 records should be checked for Internal duplicates (duplicates within the same sheet) and external duplicates (duplicates which exist outside this sheet). I have gone through lot of logics..some of which are ... - Common and currently testing out.. - First insert the excel sheet data into DB..then query by...
0
8739
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
8652
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
9234
Oralloy
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...
0
9089
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
4412
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...
0
4667
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3107
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
2402
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2036
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.