473,396 Members | 1,996 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

want to search in a text file if it contains the words present i a 2d array..

i hav a text file anagrams.text.......and i hav a 2d array r[6][4]....i want to search in the text file anagrams.txt the words present in the 2d array and if present print them....thanx....(im trying to implement genetic algorithm on anagrams)..
.................................................. .................................................. ...................
Expand|Select|Wrap|Line Numbers
  1. #include <time.h>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <conio.h>
  5. #include <iostream>
  6. #include <string.h>
  7. using namespace std;
  8.  
  9. char reply='y';
  10. int j=0;
  11. main()
  12. {
  13.   while(reply=='y')
  14.   {
  15.   char s[3];
  16.  
  17.   char r[6][4];
  18.   char arr[6][4];
  19.  
  20.   cout<<"\nenter the 4 letter word";
  21.   cin>>s;
  22.   cout<<"\nthe 1st letter  is\t"<<s[0]<<"\n";
  23.   cout<<"\nthe 2nd letter  is\t"<<s[1]<<"\n";
  24.   cout<<"\nthe 3rd letter  is\t"<<s[2]<<"\n";
  25.   cout<<"\nthe 4th letter  is\t"<<s[3]<<"\n";
  26.  
  27.     cout<<"\n\n";
  28.  
  29.  
  30.   int rolls=24;
  31.  
  32.   srand((unsigned int)time((time_t *)NULL));
  33.   cout<<"\nthe possible allel combinations are \n\n"<<"\n";
  34.  int a=1;
  35.  int b=1;
  36.  
  37.   while(rolls--)
  38.   {
  39.      int i=((rand()%4)+0);
  40.  
  41.     if(i==0)
  42.     {
  43.        r[b][a]=s[0];
  44.        arr[b][a]=s[0];
  45.  
  46.        cout<<r[b][a];
  47.      }
  48.  
  49.     else
  50.     if(i==1)
  51.     {
  52.  
  53.        r[b][a]=s[1];
  54.        arr[b][a]=s[1];
  55.        cout<<r[b][a];
  56.        }
  57.     else
  58.     if(i==2)
  59.     {
  60.        r[b][a]=s[2];     
  61.        arr[b][a]=s[2];
  62.       cout<<r[b][a];
  63.  
  64.     }
  65.  
  66.     else
  67.     if(i==3)
  68.     {
  69.     r[b][a]=s[3];
  70.     arr[b][a]=s[3];
  71.     cout<<r[b][a];
  72.  
  73.     }
  74.  
  75.     a++;
  76.  
  77.  
  78.     if(a>4)
  79.     {
  80.  
  81.       a=1;
  82.       b++;
  83.     }
  84.  
  85.  
  86.  
  87.     j++;
  88.     if(j==4)
  89.     {
  90.       printf(" \n\n");
  91.       j=0;
  92.     }
  93.  
  94.  
  95.  
  96.   }
  97.  
  98.  
  99.  
  100.  
  101.  
  102.  
  103. // crossover
  104.  
  105. cout<<"\n\n after cross over\n\n";
  106.  
  107. arr[1][3]=r[2][3];
  108. arr[1][4]=r[2][4];
  109. arr[2][3]=r[1][3];
  110. arr[2][4]=r[1][4];
  111. arr[3][3]=r[4][3];
  112. arr[3][4]=r[4][4];
  113. arr[4][3]=r[3][3];
  114. arr[4][4]=r[3][4];
  115. arr[5][3]=r[6][3];
  116. arr[5][4]=r[6][4];
  117. arr[6][3]=r[5][3];
  118. arr[6][4]=r[5][4];
  119. int h=0;
  120. int m=1;
  121. int n=1;
  122. int roll=24;
  123. while(roll--)
  124. {
  125.  
  126.  
  127.  
  128. cout<<arr[m][n];
  129.  
  130. n++;
  131.  
  132.  
  133.     if(n>4)
  134.     {
  135.  
  136.       n=1;
  137.       m++;
  138.     }
  139.  
  140.  
  141.  
  142.     h++;
  143.     if(h==4)
  144.     {
  145.       printf(" \n\n");
  146.       h=0;
  147.     }
  148.  
  149.  
  150. }


cout<<"want to try again ? (y/n)";
cin>>reply;
}
getch();
}


------------------------------------------------------------------------------------------------------------------

anagrams.txt
---------------------------------------------
Expand|Select|Wrap|Line Numbers
  1. abed
  2. bade
  3. bead
  4. abet
  5. beat
  6. beta
  7. abut
  8. tabu
  9. tuba
  10. acme
  11. came
  12. mace
  13. acre
  14. care
  15. race
  16. ales
  17. leas
  18. sale
  19. seal
  20. amen
  21. mane
  22. mean
  23. name
  24. anew
  25. wane
  26. wean
  27. arcs
  28. cars
  29. scar
  30. asps
  31. pass
  32. saps
  33. spas
  34. awls
  35. laws
  36. slaw
  37. bard
  38. brad
  39. drab
  40. bats
  41. stab
  42. tabs
  43. brag
  44. garb
  45. grab
  46. code
  47. coed
  48. deco
  49. dale
  50. deal
  51. lade
  52. lead
  53. dare
  54. dear
  55. read
  56. diet
  57. edit
  58. tide
  59. tied
  60. demo
  61. dome
  62. mode
  63. diet
  64. edit
  65. tide
  66. tied
  67. does
  68. dose
  69. odes
  70. dues
  71. sued
  72. used
  73. ears
  74. eras
  75. sear
  76. rose
  77. sore
  78. ores
Apr 16 '10 #1
2 1696
Banfa
9,065 Expert Mod 8TB
You need to revisit your text book/reference material and read about how arrays work. An array

int array[4];

Has the number of entries given in the definition, 4 in this case, and they are numbered from 0 so valid indexes for this case are 0, 1, 2, 3

A string is and array of characters with a terminating 0, that is the nul character or '\0' at the end of the array. To hold a string of 4 characters say "made" you need an array of 5 characters

char word[5];

4 to hold the printable characters and a final one for the terminator.

You code is full of instances of writing outside the array boundaries because the index is too high. This results in undefined behaviour which is bad because anything can happen.
Apr 16 '10 #2
@Banfa
ya it works now...my array indexes were not proper....thanx.
Apr 17 '10 #3

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

Similar topics

0
by: Phil Powell | last post by:
The table already has a fulltext index and from there I can use the MySQL fulltext search query to get results as well as the relevancy score. The problem I have is that MySQL has a default...
3
by: Fred Yarbrough | last post by:
We have a simple html page that contains a long list of documents that users can hyperlink to. The entries are currently in alphabetical order and we would like to implement some type of page...
8
by: Rob Meade | last post by:
Lo all, Ok - I'm adding site search functionality to a database driven website. I have a list of 390 stop/ignore words, having looked at ASPFAQ already I see that the example uses an array,...
3
by: Trevor Fairchild | last post by:
I'm trying to execute a full-text query from a vb.net web application. The problem I have is that in SQL Server, the syntax for a full-text search is SELECT * FROM table WHERE CONTAINS( *, '...
4
by: Jane Doe | last post by:
Hi, I need to search and replace patterns in web pages, but I can't find a way even after reading the ad hoc chapter in New Rider's "Inside JavaScript". Here's what I want to do: function...
7
by: Arnold | last post by:
I need to read a binary file and store it into a buffer in memory (system has large amount of RAM, 2GB+) then pass it to a function. The function accepts input as 32 bit unsigned longs (DWORD). I...
5
by: JP SIngh | last post by:
Hi All This is a complicated one, not for the faint hearted :) :) :) Please help if you can how to achieve this search. We have a freetext search entry box to allow users to search the...
91
by: jerger | last post by:
I want to help teach to a minority group in Milwaukee, so I want to create a dictionary program that translates a sentence (like a homework problem or teacher instructions), from English into Hmong....
0
by: Skywick | last post by:
Hi I am trying to do a full text search with a column name for the search term. I can do this using LIKE with: SELECT tblContent.ID FROM tblContent INNER JOIN #keywords ON tblContent.words...
0
by: Atos | last post by:
Binary search is used to locate a value in a sorted list of values. It selects the middle element in the array of sorted values, and compares it with the target value; that is the key we are...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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...
0
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...
0
agi2029
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,...

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.