473,554 Members | 3,120 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 17945
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
7802
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
1830
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)...
1
6403
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
3533
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
5017
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...
33
1828
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
10572
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...
8
11798
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
2143
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 ... -...
0
8023
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...
1
7547
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...
0
7879
tracyyun
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...
1
5426
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...
0
3548
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...
0
3536
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2009
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
1
1117
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
828
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...

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.