Right now I can’t even get a cout statement to work. How the hell is that possible? I have the includes necessary (like conio and whatnot) but it doesn’t work.
It’s okay on the program that has a predefined array, and I cout something. But I need to generate an array. So using the same damn code, but inserting a for loop that’ll randomly generate the array contents makes borland hate cout. At first I thought that it might have something to do with using the vector class for my array (we have to use this; I’m accustomed to just having int a and whatnot), and maybe doing
cout << a[j] << endl;
wasn’t the correct syntax for that class. Or maybe it needed to be combined into the first loop, so instead of
for(int i = 0; i <=20; i++)
{
a[ i ] = (rand()-16384)/170;
}
for(int j = 0; j <=20; j++)
{
cout << a[ j ] << endl;
}
I could just have
for(int i = 0; i <=20; i++)
{
a[ i ] = (rand()-16384)/170;
cout << a[ i ] << endl;
}
I mean, criminy! I don’t even NEED to have the array displayed in my finished program, I’m just doing it so I can see it’s generating the array. It won’t let me see anything. And it’s all from the cout. I tried typing this:
for(int i = 0; i <=20; i++)
{
a[ i ] = (rand()-16384)/170;
}
cout << "Hello
";
So the “hello” has nothing to do with the array or the for loop at all. Yet I still get the same error message (“Project algorithm1.exe raised exception class EAccessViolation with message ‘Access violation at address 00402A8E. Write of address FFFFFFDD’. Process stopped. Use Step or Run to continue.”). And if I try to close Borland (C++ Builder 5), or even just the cpp file, about half the time it will ask me if I want to terminate a debugging process. I click yes to close the thing, and it gives me “unable to terminate process: access is denied”. argh! It won’t let me shut the damn program down unless I go into task manager and click “end program” several times.
HOW THE HELL AM I SUPPOSED TO ANALYSE THESE ALGORITHMS IF I CAN’T EVEN GET THEM TO RUN IN THE FIRST PLACE?
I tried your last example, with Hello at the end of the for loop, and it worked fine for me. (Haven’t used C in a while, so I had to look up some stuff.) cout is in iostream.h, rand is in stdlib.h, if that helps any.
I didn’t get an error once I included both of those.
If the cout is really your problem, can’t you leave it out and use watches in Borland to see the contents of your array? (Or, heaven forbid, println statements?)
Sorry to not be more help. Been using VB for 6 months, and learning C #. Bleh.
Here’s the program in its entirety (it’s not really that long)(and for some reason the formatting is a bit messed, so it’s not nicely and neatly indented properly):
Yes, I know, I have a function called maxSubSum1 called in the main, and I didn’t show you that function; it’s commented out in my program because I’m not focusing on that right now. As far as Borland can see, this is all I have (note that the maxSum and everything is commented out here as well). I kept that part in here just to show that there IS a purpose for this durned array.
Oh, and IMightBeGiant, thanks for mentioning the stdlib.h! I had forgotten that. I added it in and I still get the same message though. But thanks still.
This version of the code works, right? If it’s C++ and not C why are you using the .h files instead of namespaces? Is that one of the requirements of the assignment, or what?
Do you have any compilers other than Borland available to try your buggy code out on? G++ or ::shudder:: MSVC++?
By the by, you can use code tags to surround stuff that you want in a fixed width font:
since this is only allocating 8 elements in the array.
In C and C++ the compiler only knows about the starting element of an array, and calculates a particular element by calculating the offset from the starting location. It will happily let you use any index during compilation, but at runtime it’ll run right off the end of the array into other areas of memory. Hence, the access violation which probably happens on the 9th iteration of the loop.
Yeah, I had a brainfart with the size of the array. I made a const int MAXSIZE, so I can adjust the size for the testing I need, then made the loop have i <= MAXSIZE, and it works now.
But actually, that wasn’t the problem that was giving me the awful exception error. Do you know what it was? The fact that I used ‘i’ in the loop! Seriously! I had someone look at it and they said, "try changing ‘i’ to something else. This was odd to me, since I’ve almost always used ‘i’ for loops (it’s often used in programming examples). But lo and behold, I change ‘i’ to ‘m’, and it works fine!
(although it wouldn’t have worked fine if y’all hadn’t pointed me to the incorrect array size; thanks again!)
Oh, and I use #include <conio.h> et al instead of just #include <conio> out of habit. My first programming class was C, and all of my professors used C before C++, so all of their programs have conio.h, etc out of habit as well. Eventually I’ll break that habit, I swear.
That’s what I was thinking, too. She used to take some programming classes, and when she used loops, she always used a,b,c for the variables. So when she looked at that, she just thought it was odd that I’d use ‘i’ instead. Basically, she said, “just for the hell of it, change ‘i’ to something else and see what happens.”
Neither of us can figure out exactly why that was the problem. Perhaps there’s an ‘i’ variable reserved in vector.h that I don’t know about (I’ve never used that include before). But honest to God, it’s the oddest thing I’ve seen since I’ve started programming.
No! The fact that you used ‘i’ instead of ‘m’ is not your problem. I use ‘i’ for subscripts and loop variables all the time. If you changed the size of your array and still have the problem, I can’t say what the problem is, but changing ‘i’ to ‘m’ just hid the problem; it didn’t solve it.
Stick with it, you’ll see lots and lots of weirder stuff.
I once had a bug caused by overlapping memory structures that only occurred on Wednesdays, and only when running on workstations with a terminal ID that began with the letter ‘E’. Now that’s a bugger to troubleshoot!