need help with arrays

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Dec 2004
Posts: 6
Reputation: lilpil02 is an unknown quantity at this point 
Solved Threads: 0
lilpil02 lilpil02 is offline Offline
Newbie Poster

need help with arrays

 
0
  #1
Dec 1st, 2004
Hello everyone! I sure wish I would have known about this place befroe now, it would have saved some long nights. I am having probems with arrays. The situation is this read all the lines from a file and store it into an array. Once you do this, you have a character array. All your further operations should be done on the array. create a function called "Matcher". The Matcher function should go through the entire array and find how many times the string or character pattern 'aba' occurs in the entire array.
On finding a match, the program should display the index number of the array at which the string pattern started.
Once the search is complete, the program should display how many times it found the string or character pattern 'aba' in the entire array or more appropriately, in the file.
If the number of matches is zero, the program should output "Search String NOT found".
For programming purpose use this file( "file.txt"). I tried to write it with out any help and am finding I need some. I am a little older and you know what they say about teaching an old dog new tricks.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,362
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 241
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: need help with arrays

 
0
  #2
Dec 1st, 2004
Post the code you've got between [CODE][/CODE] tags. Quid pro quo.
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 6
Reputation: lilpil02 is an unknown quantity at this point 
Solved Threads: 0
lilpil02 lilpil02 is offline Offline
Newbie Poster

Re: need help with arrays

 
0
  #3
Dec 1st, 2004
  1. #include<iostream>
  2. #include<string>
  3. #include<fstream>
  4. using namespace std;
  5.  
  6.  
  7. void main()
  8. {
  9. int i;
  10. int a;
  11. int b;
  12. string until;
  13. int arrayj[50];
  14. cout<<"Jay Pilrose"<<endl;
  15. cout<<"00519995"<<endl;
  16. ifstream file1;
  17. file1.open("db.txt", ios::in);
  18. for (int k=0; k<50; k++)
  19. {
  20. file1 >> arrayj[k];
  21. }
  22.  
  23. int count=0;
  24. int arrayi[50];
  25. for (i=0; i<50; i++)
  26. {
  27. while (i<50)
  28. {
  29. if(i=a)
  30. {
  31. i++;
  32. if(i=b)
  33. {
  34. i++;
  35. if(i=a)
  36. {
  37. cout<<"The sequence starts at "<<endl;
  38.  
  39. }
  40. }
  41. else
  42. i++; until; i=a;
  43. }
  44. }
  45. }
  46.  
  47.  
  48.  
  49.  
  50. }
I am pretty sure this won't do what it is supposed to but it may do something.
Another try looked much the same but used && operators and compared them on the same line. I have a hard time knowing how and what to pass in a function. I would use a void function for the matcher part but I am not sure that is the best way to go about it.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,362
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 241
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: need help with arrays

 
0
  #4
Dec 1st, 2004
>if(i=a)

This is assigning the value of a to i and then evaluating whether i is nonzero. Use if(i==a).

Why do you take input into an array of int if it is supposed to be an array of char?

>void main()

Use int main().
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 6
Reputation: lilpil02 is an unknown quantity at this point 
Solved Threads: 0
lilpil02 lilpil02 is offline Offline
Newbie Poster

Re: need help with arrays

 
0
  #5
Dec 1st, 2004
So by changing the int arrayj to char arrayj I will get characters instead of numbers? That was one problem I had with it. I am trying to use a book that uses all numbers in the examples but then asks you to use characters in the problems. Will the code that I have now work in a function?
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 6
Reputation: lilpil02 is an unknown quantity at this point 
Solved Threads: 0
lilpil02 lilpil02 is offline Offline
Newbie Poster

Re: need help with arrays

 
0
  #6
Dec 1st, 2004
I have many questions so please be patient. I am also looking in help as we speak.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,362
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 241
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: need help with arrays

 
0
  #7
Dec 1st, 2004
Try adding some debugging statments to help you as you go. Just use cout to display something you may be curious about. This will be one way to answer your own questions as you go.

Are you required to do user input char by char, or are you allowed to use other standard functions such as getline?
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 6
Reputation: lilpil02 is an unknown quantity at this point 
Solved Threads: 0
lilpil02 lilpil02 is offline Offline
Newbie Poster

Re: need help with arrays

 
0
  #8
Dec 1st, 2004
The book does't say to do it any special way it just says to load a file into an array and then use the array to do everything else. The file is supposed to be created in notepad. When you say debugging statements you mean like this //and the text following turns green.?
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,362
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 241
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: need help with arrays

 
0
  #9
Dec 1st, 2004
No. Debugging with cout would be like this:
#include <iostream>
#include <fstream>
#include <cstdio>
using namespace std;

char array [ 1000 ];

int main()
{
   ifstream file(__FILE__);
   const char seq[] = "123";
   file.getline(array, sizeof array, '\0');
   const size_t len  = strlen(array);
   cout << "len = " << len << endl; // DEBUG
   const size_t size = strlen(seq);
   cout << "size = " << size << endl; // DEBUG
   for ( size_t i = 0; i < len - 2; ++i )
   {
      if ( strncmp(&array[i], seq, size) == 0 )
      {
         cout << "The seq starts at " << i << endl;
         cout << &array[i] << endl;
         break;
      }
   }
   return 0;
}
They are extraneous lines that during development will at least tell you what is going on in a program at a certain point. And lines that are generally removed after you've debugged and gotten portions working.

In the above example, these two lines produce this output.
  1. len = 616
  2. size = 3
This can be helpful information while you're getting things working. For example, you might find that a buffer size of 50 is not big enough.
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 6
Reputation: lilpil02 is an unknown quantity at this point 
Solved Threads: 0
lilpil02 lilpil02 is offline Offline
Newbie Poster

Re: need help with arrays

 
0
  #10
Dec 1st, 2004
So they are lines that are not integral to the program but they output values? This book dosen't say anything about them. Those will be helpful, if I can get the program to work.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC