473,698 Members | 2,181 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 17985
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
41 New Member
a row that contains more than the same character once...


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; 
  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
2,426 Recognized Expert Top Contributor
A "duplicate" is ...
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
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
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
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> 
  5. using namespace std; 
  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]);
  14. int main() 
  16.  char choice = '*';
  17.      while (choice != 'Q')
  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.          }
  40.      }     
  42.     return 0;
  46.  system("pause");
  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; 
  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
41 New Member
can someone pls help?? I am stuck :(
Mar 2 '09 #8
2,426 Recognized Expert Top Contributor
Follow the link provided in message #4.
Mar 3 '09 #9
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

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: 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
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 &); }
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
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,
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.
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>
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#
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...
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: 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: 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: 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...
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...
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
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
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...

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.