Hi all - I'm new[] here, so be gentle...
1.
for(int i=0;i<10;i++)
out << i+1 << ": " << ( bin(left, right, arr, i) ? "yes" : "no") << endl;
This (binary search) works for int* rather than int ( won't work for i's, but for arr[i]'s).
Won't work = no output whatsoever...
Header is bool bin(int left, int right, int* arr, int find)...
2.
I have a class with a member of char* named txt, which is to be initialized with text from a file, but when I get to the class's method, it writes nothing.. (even not in the main() section!)
C'tor's init' lines:
txt = new char [Nchars];
in.read (txt,Nchars-1);
Thanks in advanced.
20 replies to this topic
#1
Posted 22 December 2011 - 05:56 AM
|
|
|
#2
Posted 22 December 2011 - 06:07 AM
Without knowing what out and in objects are, it's only a guess what can be wrong.
A conclusion is where you got tired of thinking.
#define class struct // All is public.
#3
Posted 22 December 2011 - 06:28 AM
Thery are regular i\o streams, as far as I get you.. ("ofstream out("output.txt")... "
#4
Posted 22 December 2011 - 06:49 AM
1. Does the program crash? If so, you could be reading memory past array size. 3rd parameter to bin function is a pointer to int, which is almost like an array so you can't pass i as argument.
2. You might have to open file in binary more, I'm not sure. I suppose more code would be very helpful, unless it's a secret. :)
2. You might have to open file in binary more, I'm not sure. I suppose more code would be very helpful, unless it's a secret. :)
A conclusion is where you got tired of thinking.
#define class struct // All is public.
#5
Posted 22 December 2011 - 08:06 AM
No secrets (yet...)
1.
int bin(int left, int right, int* arr, int find)
{
int med = (int)floor((left+right)/2);
if (arr[med] == find)
return arr[med];
if (arr[med] < find)
bin(med+1, right, arr, find );
else if (arr[med] > find)
bin(left, med-1 , arr, find );
else
return 0;
}
int main()
{
int arr[] = {10,20,30,40,50,60,70,80,90,100};
int left = 0;
int right = 9;
...
2.
#pragma once //current source file will be included only once in a single compilation
#ifndef TXTCUT_H // => code will be seen by the compiler only once
#define TXTCUT_H // will prevent bad implications of double includes
class Text
{
char* txt;
char** words; // supperated words in text by \n or ' '
int Nchars;
int Nwords;
public:
void set();// setting text as movies }
void get(char* del1, char* del2);
int NumTxt();
int NumWords();
Text();
~Text();
};
#endif /* TXTCUT_H */
This is the C'tor -
Text:: Text()
{
words = NULL;
Nchars=NumTxt();
// txt <- TEXT
txt = new char [Nchars];
in.read ((char*)&txt,Nchars-1);
Nwords=NumWords();
}
I thought that maybe read uses the get pointer and it should be shifted again (to ios:: beg), though my trials proved me wrong..
1.
int bin(int left, int right, int* arr, int find)
{
int med = (int)floor((left+right)/2);
if (arr[med] == find)
return arr[med];
if (arr[med] < find)
bin(med+1, right, arr, find );
else if (arr[med] > find)
bin(left, med-1 , arr, find );
else
return 0;
}
int main()
{
int arr[] = {10,20,30,40,50,60,70,80,90,100};
int left = 0;
int right = 9;
...
2.
#pragma once //current source file will be included only once in a single compilation
#ifndef TXTCUT_H // => code will be seen by the compiler only once
#define TXTCUT_H // will prevent bad implications of double includes
class Text
{
char* txt;
char** words; // supperated words in text by \n or ' '
int Nchars;
int Nwords;
public:
void set();// setting text as movies }
void get(char* del1, char* del2);
int NumTxt();
int NumWords();
Text();
~Text();
};
#endif /* TXTCUT_H */
This is the C'tor -
Text:: Text()
{
words = NULL;
Nchars=NumTxt();
// txt <- TEXT
txt = new char [Nchars];
in.read ((char*)&txt,Nchars-1);
Nwords=NumWords();
}
I thought that maybe read uses the get pointer and it should be shifted again (to ios:: beg), though my trials proved me wrong..
#6
Posted 22 December 2011 - 09:08 AM
1. What's your output? You should see 10 no's.
2. You don't open file to read from.
2. You don't open file to read from.
A conclusion is where you got tired of thinking.
#define class struct // All is public.
#7
Posted 24 December 2011 - 04:03 AM
1. No output whatsoever...
2. I've defined ifstream in("input.txt") in the main() part and externed it, if that's what you meant...
2. I've defined ifstream in("input.txt") in the main() part and externed it, if that's what you meant...
#8
Posted 24 December 2011 - 04:49 AM
1. I think you're not seeing any output because your bin function crashes if it doesn't find anything. When you're dealing with recursion, the first thing you have to check is depth (or in your case, if left > right).
2. See this question and this response. You could move in object to ctor, make it part of class or pass it to ctor as an argument.
2. See this question and this response. You could move in object to ctor, make it part of class or pass it to ctor as an argument.
A conclusion is where you got tired of thinking.
#define class struct // All is public.
#9
Posted 25 December 2011 - 02:55 PM
2. I don't quite follow. You imply that i've externed the input stream locally, though they were defined globally in the main() part, and the CPP part :
.
.
ifstream in("input.txt");
ofstream out("output.txt");
int main()
{
.
.
////////////// cpp
.
.
using namespace std;
extern ifstream in;
extern ofstream out;
Text:: ~Text()
{
.
.
The strange part is that after inits' in the C'tor -
Text:: Text()
{
words = NULL;
Nchars=NumTxt();
// txt <- TEXT
txt = new char [Nchars];
in.read ((char*)&txt,Nchars-1);
Nwords=NumWords();
}
Num of words (Nwords) is really defined again as zero.
void Text:: set()
{
int n=0,n1=0,j=0,i=0;
//////////////////////////////////// ???????????????????????????????????????????????????
n = Nwords; // n - num of words - supperated \n or ' '
out << n;
.
.
1. After redefining bin, it doesn't crash, but gives a strange output (though works line by line with the debugger).
i.e.
bool bin(int left, int right, int* arr, int find)
{
if( left > right )
return false;
int med = (int)floor((left+right)/2);
if (arr[med] == find)
return true;
if (arr[med] < find)
bin(med+1, right, arr, find );
if (arr[med] > find)
bin(left, med-1 , arr, find );
}
..
..
for(int i=0;i<11;i++)
cout << i << ": " << ( bin(left, right, arr, i ) ? "yes" : "no") << endl;
gives yes, and 9 no's..
Thanks again for the paitance for a semi-noob like me...
.
.
ifstream in("input.txt");
ofstream out("output.txt");
int main()
{
.
.
////////////// cpp
.
.
using namespace std;
extern ifstream in;
extern ofstream out;
Text:: ~Text()
{
.
.
The strange part is that after inits' in the C'tor -
Text:: Text()
{
words = NULL;
Nchars=NumTxt();
// txt <- TEXT
txt = new char [Nchars];
in.read ((char*)&txt,Nchars-1);
Nwords=NumWords();
}
Num of words (Nwords) is really defined again as zero.
void Text:: set()
{
int n=0,n1=0,j=0,i=0;
//////////////////////////////////// ???????????????????????????????????????????????????
n = Nwords; // n - num of words - supperated \n or ' '
out << n;
.
.
1. After redefining bin, it doesn't crash, but gives a strange output (though works line by line with the debugger).
i.e.
bool bin(int left, int right, int* arr, int find)
{
if( left > right )
return false;
int med = (int)floor((left+right)/2);
if (arr[med] == find)
return true;
if (arr[med] < find)
bin(med+1, right, arr, find );
if (arr[med] > find)
bin(left, med-1 , arr, find );
}
..
..
for(int i=0;i<11;i++)
cout << i << ": " << ( bin(left, right, arr, i ) ? "yes" : "no") << endl;
gives yes, and 9 no's..
Thanks again for the paitance for a semi-noob like me...
#10
Posted 25 December 2011 - 03:58 PM
I misunderstood you because you said in main() part; I thought you meant inside main function, not main.cpp. Also, please use # button (code tags) so we can see code nicely formatted. :)
1. Well, either NumTxt() function fails, and thus no data gets read, or for some reason, you didn't open file (maybe doesn't exist, don't have permission, ...). Make a simple output to see whether file opened successfully and if NumTxt returns correct values.
2. If you're searching in this array:
1. Well, either NumTxt() function fails, and thus no data gets read, or for some reason, you didn't open file (maybe doesn't exist, don't have permission, ...). Make a simple output to see whether file opened successfully and if NumTxt returns correct values.
2. If you're searching in this array:
[COLOR=#333333]int arr[] = {10,20,30,40,50,60,70,80,90,100};[/COLOR]
then 1 yes and 9 no's is correct. Your for loop goes from 0 to 10 and you have 1 10 in array.
A conclusion is where you got tired of thinking.
#define class struct // All is public.
#11
Posted 26 December 2011 - 05:54 AM
I'll bear in mind for next times... :)
2. For i from 0 to k I get "yes" for the first line (0) and "no" k-1 times.
1. It's no use hidinig it no more... Why it acts as if it is defined locally?
# int Text:: NumTxt()
#{
# in.seekg (0, ios::end);
# int length = in.tellg();
# in.seekg (0, ios::beg);
# return length;
#}
2. For i from 0 to k I get "yes" for the first line (0) and "no" k-1 times.
1. It's no use hidinig it no more... Why it acts as if it is defined locally?
# int Text:: NumTxt()
#{
# in.seekg (0, ios::end);
# int length = in.tellg();
# in.seekg (0, ios::beg);
# return length;
#}
#12
Posted 26 December 2011 - 06:47 AM
1. If I run your code:
2. I'm not sure what's wrong. Your declaration and definition seems ok. Try to pass in as an argument to Text constructor.
Code button doesn't work like that; putting # in front of code lines. :)
You can either paste code, select it and click on # button (in post editor), or you can click # button and then place code between [CODE][ /CODE] tags.
bool bin(int left, int right, int* arr, int find)
{
if( left > right )
return false;
int med = (int)floor((left+right)/2);
if (arr[med] == find)
return true;
if (arr[med] < find)
bin(med+1, right, arr, find );
if (arr[med] > find)
bin(left, med-1 , arr, find );
}
int main() {
int arr[] = {10,20,30,40,50,60,70,80,90,100};
int left = 0;
int right = sizeof(arr) / sizeof(arr[0]);
for(int i = 0; i < 11; i++)
std::cout << i << ": " << ( bin(left, right, arr, i ) ? "yes" : "no") << std::endl;
return 0;
}
I get the following output:0: no 1: no 2: no 3: no 4: no 5: no 6: no 7: no 8: no 9: no 10: yes
2. I'm not sure what's wrong. Your declaration and definition seems ok. Try to pass in as an argument to Text constructor.
Code button doesn't work like that; putting # in front of code lines. :)
You can either paste code, select it and click on # button (in post editor), or you can click # button and then place code between [CODE][ /CODE] tags.
A conclusion is where you got tired of thinking.
#define class struct // All is public.
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account


Back to top









