int gcd(int a, int b){
if (b==0) return a;
#ifdef DEBUG
printf("gcd(%d, %d)= ", b,a%b);
#endif
gcd(b, a%b); // where did you calculate which number was smaller?
// is a%b really what you wanted here?
}
Unimportant 18 Junior Poster
int gcd(int a, int b){
if (b==0) return a;
#ifdef DEBUG
printf("gcd(%d, %d)= ", b,a%b);
#endif
gcd(b, a%b); // where did you calculate which number was smaller?
// is a%b really what you wanted here?
}
using namespace std; // It's better if you don't pollute your global namespace
string str; // Why not declare this in main()?
string stri;
int inputString (int n); // Not sure I understand why you pass an int here
bool palindromeTest (bool isPalindrome); // why do you pass this bool?
void printMessage (bool isPalindrome); // why do you pass this bool?
int _tmain(int argc, _TCHAR* argv[])
{
int n=0;
cout<<"THE PALINDROME TEST"<<endl;
cout<<"Please enter a word or sentance to be tested: "<<endl;
std::cin >> str; // <- You are "using namespace std", don't put std::
// You aren't calling any functions, so it won't do anything.
}
I suggest you read up on variable scope, the main() function, and function parameters. You would likely find your answers with great ease.
Good luck!
To read from a file, you might try using std::ifstream
If each item is on it's own line, one method you may try, would be storing each line as a std::string, and place them in an array of std::string. Then use your rand() variables to select an array offset (such as array[rand() % 35 + 1]), at which point you could remove that entry from the array, and reduce the range of your random by 1, you may want to first swap the element you remove with the last element in your array.
Good luck!
It may be worthwhile to note that scanf ignores blank input, newline, and tab. (in the context you are using it)
What I mean by this, is that blank input is still input, not EOF.
My mistake, poster below me is correct.
Might I inquire why this would be necessary, as your return type cannot change?
Perhaps what you actually wanted was a template, I am not certain.
template <typename T>
T* foo( T t ) {return &T;}
In the code you have above, you may as well just use int, such as:
int f() {
std::cout << sizeof(int) << std::endl; // outputs 4
return sizeof(int);
}
You likely got the endianness wrong for reading data (as opposed to network endianness), but base 2 is how data is represented to a processor.
@ganesh
That's a little bit misleading, the sizeof operator won't work for sizes unknown at compile time. In this case, the size is certainly dynamic and what he'll likely get from sizeof(dynamic_array) is (4), or, the size in bytes of the pointer to its base address.
Note how this is different:
int a[20] = 0;
sizeof(a); // will return 20, because the size is known by the compiler
-1.#IND -> undefined (NaN)
-1.#INF -> infinity
I see you are doing quite a bit of math with potentially complex numbers, perhaps sqrt sometimes gives you a NaN which may be the source of your problem.
Such as -> ((beta*sqrt(divs[k][j])) * dW) + // where dW may be NaN, making the entire value NaN.
What I mean to say is that this is not corrupted data, but a mathematical problem.
Sorry I do not know what type of math you are really doing and cannot provide insight into which part may be giving you "NaN" values. The above is just a guess/example to help point you in the right direction.
Hope that helps!
If you're curious about your memory info during runtime, you may want to look at this family of functions:
GetProcessMemoryInfo
It is part of the windows API.
It accepts a process handle as a parameter, so with the correct access flags you could read the memory of the other process and do real time comparisons. You could also simply use CreateRemoteThreadEx(), shared memory segments, or message loops. It really depends on what you're trying to do.
Other than during execution, you said yourself you have no idea how much would be dynamically allocated, so it likely is not possible to know before run time. If the algorithm is purely internal mathematics, it may have a maximum value anyway, in which case you would simply use the size of the object you are allocating, plus overhead, and multiply by the maximum size of the array to get a reasonable estimate of the total memory in bytes that will be added to the heap.
Do you know how much memory will be allocated during run time?
For example, if you are resizing an array dynamically, based on packet input, the array could be a size much larger than it was at the time the program started.
If you are not using any dynamic memory, then it is technically possible to compute the size required to load your program.
Might I ask why the specific amount of memory is a concern for you?
If you mean you want no intermediate data, you may want to sort during the merge.
Before I continue I'd like to point out that you aren't actually merging your arrays, in the code you posted. You're overwriting one.
You're also incrementing i twice. Edit: I actually have no idea what you're doing with those 2 while loops, sorry. Also please use code tags.
If you don't care about the speed of sorting, you can use this method to swap the data of two variables without creating an intermediary variable:
if( a[19] < a[18] ) {
a[19] ^= a[18];
a[18] ^= a[19];
a[19] ^= a[18];
}
I don't know why you would necessarily want to do this.
You would simply need to iterate backward through your array until the if statement does not pass for a single element, you could use a size 1 smaller each time, in the worst case scenario (i<20) -> (i<19) -> (i<18)
There are better ways to sort, but that's not necessarily in the scope of this explanation.
class Collection; // <-- pointers to Category
class Category; // <-- pointers to array dvd[] and std::string description/name
struct dvd_info {
category* cat;
std::string title;
int year;
double time, price; // <-- time in seconds, use a formatting function
dvd_info() : cat(NULL), title("\0") { }
dvd_info( cat *c, std::string &t, int yr, double s, double m ) : cat(c), title(t), year(yr), time(s), price(m) { }
};
Class dvd {
public:
dvd_info* ret_info() { return &inf; } // edit or read from pointer
private:
dvd_info inf;
}
adding should be fairly straightforward, just add a function like this:
Categories::push( dvd_info &d ) { vector.push_back(d); }
/* or whatever container...
You could use std::map for easy use of strings (titles, categories) for searching in the collection(s) */
map.find( std::string("some movie") );
map["some movie"];
If you want more help than that, you have to post some code.
Please use code tags.
This is your problem:
for (int i=0; i<5; i--) // you are decrementing i, it will become -1
{
int times =5; // times will always be five
Is this really what you want to do?
else if (guess > secret_number)&&(times == i--)
A) You have no condition for guess being less than the random number
B) You are decrementing i, this -will- affect your loop
C) Times will always be 5, so in your std::cout, you will always tell the user they have 5 guesses left.
Perhaps this is what you wanted:
int times(5);
for(i=0;i<5;i++) {
--times;
std::cout << "You have " << times << " guesses remaining." << std::endl;
if( guess == secret_number ) {
std::cout << "Correct." << std::endl;
return 0; // exit program
} elseif( guess < secret_number ) {
std::cout << "Your guess was too low." << std::endl;
} elseif( guess > secret_number ) {
std::cout << "Your guess was too high." << std::endl;
}
}
return 0;
Invisi,
nArray is passed as a function parameter.
What you are doing is making another array, using memcpy to move the contents of the old array to the new one, and then deleting the old one.
You return a pointer (address of 0th element of array) to the new array from your function, this becomes nArray, which in this case is just unclear naming since you use that name in main().
You in fact, -are- deleting the array which contained your data, you obviously would not want to delete the freshly doubled in size array you made just nanoseconds before.
int* resizeArray(int* OLDArray, int oldsize, int newsize )
{
int* newArray = new int[newsize];
memcpy(newArray,OLDArray,oldsize*sizeof(int)); // I don't know why you bothered to omit this
delete[] OLDArray; // <--- Why not new array <--- why newArray?
OLDArray = newArray; // this way you don't have the modify the pointer later
return newArray; // <-- because newArray is what you are using in your code from now on
}
int main()
{
int sz(32);
int* nArray = new int[sz];
resizeArray( nArray, sz, (sz<<1) );
//code...
delete[] nArray;
nArray =0; // <-- Why are you doing this?
}
std::stringstream ss;
ss << "A line of text."
std::ofstream file("lines.txt", std::ios::out | std::ios::app);
if(file.is_open()) file << ss.str() << '\n';
file.close();
Edit: Couple notes
You may want to read about ofstream flags, they are non-trivial.
Closing the file is important.
You don't need to use a stringstream like I did.
You should definitely check to make sure you opened the file. (shown above)
You didn't post any code snippets, but it seems like you're reading or inputting uninitialized data.