Duoas 1,025 Postaholic Featured Poster

Ah, I see the problem.

On line 52 you are assuming something about the input with (next >= 0) . (According to the instructions it should be (next > 0) .)

If you wish to use a semaphore item to terminate input, it must be passed as argument to the function.

However, a better idiom is to forget the semaphore stuff. Users consider it unnatural. Rather, accept a blank line as end of input. For this you'll need to #include <string> and <sstream>.

#include <iostream>
#include <limits>
#include <sstream>
#include <string>
using namespace std;

...

template<class T>
void fill_array(T values[], int size, int& number_used, const string& itemname );

...

int main()
  {
  ...

	fill_array(int_values, SIZE_DECLARED, number_used, "integer");
	fill_array(double_values, SIZE_DECLARED, number_used, "floating-point number");
	fill_array(char_values, SIZE_DECLARED, number_used, "character");

  ...
  }

template<class T>
void fill_array(T values[], int size, int& number_used, const string& itemname )
{
	T next, search_value;
	int index = 0, search_result;
	cout << "Enter a maximum of " << size << ' ' << itemname << "s: \n";
	cout << "Press ENTER on a blank line to end.\n";

	string s;
	while (getline( cin, s ) && !s.empty() && (index < size))
	{
		stringstream ss( s );
		while ((ss >> next) && (index < size))
		{
			values[index] = next;
			index++;
		}
	}
	number_used = index;

	cout << "Enter a " << itemname << " to search for: ";
	cin >> search_value;
	cin.ignore( numeric_limits <streamsize> ::max(), '\n' );
	search_result = search_array(values, number_used, search_value);
	display_result(search_value, search_result);
}

There are a …

Duoas 1,025 Postaholic Featured Poster

You still haven't indicated what OS you are using, but doesn't it do that for you anyway? On each one of Windows, Mac, and Linux you can set yourself a secure password and disable all other user accounts but yours.

That way, only the person who knows the password (you) can log onto your laptop. (Which is the same thing you are stated as doing.)

If it is just for leaving it lying around for a moment, you can always trigger the screensaver, which again on each system mentioned can password-protect the computer.

If you want a quick script to trigger the screensaver on demand I can give you one. There are also programs online that will do it if you move the mouse into a corner of the display or something like that...

Duoas 1,025 Postaholic Featured Poster

You may already have NCurses installed.

Try the following program to see if you do.

// hello.cpp

#include <curses.h>

int main() {
  initscr();
  raw();
  noecho();
  nonl();
  intrflush( stdscr, FALSE );
  keypad( stdscr, TRUE );

  mvaddstr( 10, 10, "Hello, world" );
  mvaddstr( 11, 10, "Press the 'any' key." );

  wgetch( stdscr );

  endwin();
  return 0;
  }

Compile with g++ -o hello hello.cpp -lncurses .

The code is valid C code also, so it doesn't matter if you use gcc or g++.

If it complains that it doesn't know what libncurses is, try -lcurses, and if that fails, use sudo apt-get install ncurses-dev and that should (hopefully) get you everything you need.

Hope this helps.

Duoas 1,025 Postaholic Featured Poster

Like iamthwee indicated, there is no standard way to do that. You'll have to do something OS-specific.

I suggest you check out the Curses library.

POSIX: NCurses
Win32: PDCurses

Hope this helps.

Duoas 1,025 Postaholic Featured Poster

Win32
Linux
Mac OS X

Hope this helps.

Duoas 1,025 Postaholic Featured Poster

Please don't shout.

Adding a background depends on the toolkit you are using.

If you are using straight Win32 API, you'll need to process the WM_PAINT message sent to the form and draw the bitmap before you let any other controls draw.

If you are using a toolkit, please tell us which one you are using.

Duoas 1,025 Postaholic Featured Poster

I understood what you want. Perhaps you should spend some more time reading through the theory links I gave you.

Variables have to exist somewhere, and the place for local variables is on the stack as part of a function's call frame. You can't separate functions and local variables.

High-level languages make creating variables deceptively simple --magical even. In assembly you've got to know a lot more about their storage. MASM and TASM make it easy with the LOCAL directive.

Global variables work the same way: they are located in the data segment and addressed with the EDS:[reg+n] syntax, just like local variables are [EBP+n].

Finally, I also gave you a link to a forum where people who eat and breathe this stuff all day frequent. They'll be able to tell you all sorts of little technical details that I'll have forgotten or would have to look up.

There is nothing simple about assembly language. You just have to dive in and expect a steeper learning curve than usual. I'll continue to check in now and again if you get stuck.

Good luck!

Duoas 1,025 Postaholic Featured Poster
Duoas 1,025 Postaholic Featured Poster

I think you need to spend some time tracing through your algorithm. I'm not sure exactly what it is supposed to do, but I don't think it is doing what you want it to do...
(Given "HELLO", the list it builds is H, E, L, LL, ...)

In any case, your problems are with strncpy().
There is no guarantee that the result string (aux) will be null-terminated, and the way you are doing it, it definitely will not be null-terminated. Make sure to put a '\0' at the end of the string.

Hope this helps.

PS. I know some libraries (notably MS) typedef away *s from pointers, but that is really bad style. If you are using a variable, you should not have to find a typedef to know whether it is a pointer or not, and it makes reading the code harder. It took me a minute to verify that the last three lines of Insert() were doing what they were supposed to be doing...

Duoas 1,025 Postaholic Featured Poster
#include <algorithm>
#include <functional>
...

transform(
  a.begin(),
  a.end(),
  b.begin(),
  result.begin(),
  minus <pair <foo, bar> > ()
  );

You'll have to define what the difference between two pairs<> is... (by overloading the - operator)

Hope this helps.

Duoas 1,025 Postaholic Featured Poster

> I wasn't asking you to do my homework...

Yes, actually, you did:
> I am looking for assembler language in lc3 to sort an array...

If you wanted help developing an algorithm, you would have asked for help with fixing what you already tried to write.

So it took you three weeks to decide to be indignant about demanding free code?

You'll notice that I still offered to actually help you. But if you believe that my response was "attitude" you are going to have a hard time dealing with people in real life. Good luck.

Duoas 1,025 Postaholic Featured Poster

No exception because after SetLength(0) the variable is already nil, and FreeMem has a built-in safeguard against freeing nil pointers.

Duoas 1,025 Postaholic Featured Poster

I know what you mean. When I was first learning C I spent three or four days once trying to figure out an error -- which turned out to be a missing } in an #included file...

An extra pair of eyes if always useful for these kinds of things.

:)

Duoas 1,025 Postaholic Featured Poster

Ah, it is because seating is declared as a 2D array, but you are only indexing one dimension. Hence the error.

Did you mean to treat seating as a 1D array in your function? I'm not sure how you are trying to index the seating array in your function...

If I am reading it right (from just a quick glance over your code) it should be: if(seating[openRow][openSeat + potential] == '#') Hope this helps.

Duoas 1,025 Postaholic Featured Poster

You got it.

Duoas 1,025 Postaholic Featured Poster

You'll have to google for each of these.

The grid classes have methods to insert and delete rows.

To display things other than text you'll need to set the DefaultDrawing property to false and override the OnDrawCell event.

Good luck!

Duoas 1,025 Postaholic Featured Poster

The compiler believes that seating is an integer. The OP placed the cast there to try to get rid of the error message.

Can we see the class definition where seating is declared? Is there anything else between the class and the variable? Did you #include the header in the .cpp file?

Hope this helps.

Duoas 1,025 Postaholic Featured Poster

C++FAQ-Lite 13.15

Hope this helps.

Duoas 1,025 Postaholic Featured Poster

I don't know how you got that far...

You never initialize coeffs. You must set its length before playing with any elements. Or push elts onto the end.

Hope this helps.

Duoas 1,025 Postaholic Featured Poster

In C and C++, nothing is automatically initialized. (I'm pretty sure.)

You can use the STL fill_n() function to initialize it for you:

#include <algorithm>  // fill_n() and copy()
#include <iostream>   // cout, of course
#include <iterator>   // ostream_iterator<>
#include <limits>     // numeric_limits<>

using namespace std;

int main()
  {
  int *nums = new int[ 10 ];

  cout << "Before initialization: ";
  copy( nums, nums +10, ostream_iterator <int> ( cout, " " ) );
  cout << endl;

  fill_n( nums, 10, 42 );

  cout << "After initialization: ";
  copy( nums, nums +10, ostream_iterator <int> ( cout, " " ) );
  cout << endl;

  cout << "Press ENTER to finish";
  cin.ignore( numeric_limits <streamsize> ::max(), '\n' );

  delete[] nums;
  return 0;
  }

Hope this helps.

Duoas 1,025 Postaholic Featured Poster

In the combo box 'select item' event handler, use ShellExecute() to start VLC with the name of the file to play as argument.

You can use the returned process handle with WaitForSingleObject() (or any of the wait functions) to wait for VLC to finish.

Hope this helps.

Duoas 1,025 Postaholic Featured Poster

No. That will cause a system exception.

You have very little control over exactly how much memory is used by your application. I believe there are compiler directives to specify the maximum heap size, but I've never had occasion to use them under Win32.

Duoas 1,025 Postaholic Featured Poster

Do you mean that when you use the IDE to assign the icon to your application that the resulting EXE's icon is missing its transparency?

It could be that the IDE is too old to handle alpha transparency (bit-transparency should work just fine).

In which case you will have to use a program like Resource Hacker to update the main icon directly.

Alas. Hope this helps.

Duoas 1,025 Postaholic Featured Poster

We're glad to have been helpful (if indeed you found the thread enlightening...)

@dougy83
You are correct about how XOR works. You must have knowledge not encoded in the number to untangle it.

And I don't think it is hijack to explore the OP's questions... so don't worry about it -- No abuse headed your way from me.

Duoas 1,025 Postaholic Featured Poster

All objects in Delphi, including 'string', are allocated on the heap. When you resize a dynamic array the unused objects are automatically destructed for built-in types only -- so in this case, yes.

When you set the length of a dynamic array to zero the dynamic array object itself is also freed and its value becomes nil.

However, deallocating an object does not necessarily mean that Delphi returns memory to the system.

Hope this helps.

Duoas 1,025 Postaholic Featured Poster

Listen, don't get all hot under the collar. I have not said anything offensive to you.

numbers

Show me a "two".

You can show me two of something, but two, by itself, does not exist. It is an abstract concept -- a way of thinking about real, extant things.

turing

After you've spent as many years as I have dealing with computational theory you can lecture me on the relevance of Turing machines. Whatever it is you got out of the Wikipedia article (and I commend you for going there), the solvability of these kinds of questions at the very heart of Church and Turing's conjecture.

If a problem is not representable by a (universal) Turing machine, it cannot be solved. Meaning that, if you want to remove state from the program, you have essentially made it unsolvable.

In plainer English: if you are not allowed to remember all the values in the array (up to and including the value currently being examined), you simply cannot tell me whether a given value is a repeat of a former one. There is no way around that.

copies

Conceptually, the XOR method makes a copy of the array the exact same way the prime numbers method does. Both result in a single number. Both can be used to reproduce the original array [1]. The way they achieve that goal is different, and the feasibility of the XOR method is conditional on the input …

Duoas 1,025 Postaholic Featured Poster

> This is making a (sorted) copy ... of the array

Yes, that is correct. The same is true of the XOR method. The problem cannot be solved unless you are permitted to have state. That is a fundamental concept in Turing machines.

The original problem indicated that you may have one or more (presumably a lot fewer than values in the array) temporary variables, but you may not create another array. So could I use a linked list? I think the intent of the question is that you must solve the problem without creating another list of values.

A number is not a list of any kind. (Though it can be represented as a list of digits. Or prime factorizations.)

If you really want to be technical, numbers don't exist. :-O :-/ :)

Duoas 1,025 Postaholic Featured Poster

Well, you can get all the original numbers back, including all duplicates, triplicates, etc, but you cannot get the original order of the elements back. So:

Yes: if element order is not important.
No: if element order is important.

Click on "fundamental theorem of arithmetic" in post #11.

You should be aware, though, that using primes is far more expensive than just using a temporary array would be... prohibitively so for large arrays. This kind of question is usually an algorithms thought experiment. Knowing something can be done doesn't necessarily mean it should be done. In this case, the restrictions on the algorithm's data structures are unrealistic (usually). But being restricted to one pass over the array itself is not.

Hope this helps.

[edit] I haven't looked over the original post since my first reply. I was only responding to subsequent posts. If you are restricted to using all given numbers and duplicating only one, then XOR will work perfectly. ;)

Duoas 1,025 Postaholic Featured Poster

> Yes I know...
I know you know... But I thought I'd give the OP more. ;)

There is no POSIX equivalent to GetModuleFileName(). You have to use getcwd() as soon as your program starts or at least before anything changes the current directory.

Hope this helps.

Duoas 1,025 Postaholic Featured Poster

Unfortunately what you are trying to do can't be done directly.

The only time the sizeof/sizeof idiom works is on the original array[] variable. Pointers and function arguments etc don't know anything about the original array...

The two usual solutions are:

  1. Pass the size of the array as an argument.
  2. Place a null value at the end of the array (just like a string ends with '\0').

Sorry there isn't a better answer...

Duoas 1,025 Postaholic Featured Poster

...but that is not guaranteed. It is usually a safe bet that it contains at least the simple name of the executable ("foo.exe"). It may also contain either an absolute path or a relative path.

On POSIX systems (Unix, Linux, Mac OS X, etc), use getcwd() and combine the result with argv[0].

On Win32, use GetModuleFileName().

Hope this helps.

Duoas 1,025 Postaholic Featured Poster

Actually, I came back to re-edit my post. The first time I said that XOR wouldn't work, but I wasn't sure and didn't want to think about it, so I edited it to give you the benefit of the doubt (that is, assuming that you thought about it).

I was outside reading and it just passed through my brain that XOR can't work, because choosing 999 elements out of 1000 possibilities 1000 times means at least one duplicate. Stipulating only one duplicate, then XOR cannot work because the result will indicate two numbers: the duplicate and the one that was not chosen. Since you don't know which is which, it cannot work. (In fact, you can't even tell which two numbers are indicated!)

The solution you just posted accesses each element of the array N times, where the array is N items long. The OP specified that each element can be accessed only once... meaning O(N), but yours is O(N^2). Oops.

Wow, those were some pretty ridiculous algorithms you guys came up with.

Well, if you want to avoid making friends, you might have succeeded. Considering your algorithm ignores every requirement of the problem, it seems me blithely arrogant to call other attempts "ridiculous". Prove a superior solution first.

And learn some math.

Duoas 1,025 Postaholic Featured Poster

Bits != Primes.

No matter what you do you must keep information about all items in the array at once. XOR does that by counting the number of times a bit is toggled. Your array is in the loop. If you want to argue code == data then all arguments fail by default.

For the constricted case given of 999 values out of a choice of 1000 then XOR works fine... Primes will always work because of the fundamental theorem of arithmetic.

The original problem states that it is OK to use other variables and not arrays. An atomic number is not an array.

Hope this helps.

Duoas 1,025 Postaholic Featured Poster

Sarehu
The purpose of "style guides" is twofold:

  1. Readability -- Physical layout of source code
  2. Interoperability -- Structural layout of data (functions, variables, etc)

Failure on either account costs significant amounts in terms of cold, hard cash. Business concerns make or break in terms of the amount of time you waste on a project.

I dislike tabs because they don't play nice. Most editors I've used (and I've tried just about every one you can name to me) handle tabs either incorrectly or just plain differently than the editor the guy four cubicles down does. In one case incorrect tab handling actually changed the meaning of my code. But sure, small potatoes.

Lighten up.

Duoas 1,025 Postaholic Featured Poster

Use only spaces, and indent 2 spaces at a time.

Yes! Vindication!

Bwaa hah hah hah ha haah


PS I don't like tabs.

Duoas 1,025 Postaholic Featured Poster

Nope.

(Sorry.)


[edit]
I could be wrong. Someone might have made a hack to do this...
It might be worth you while to ask over at the Tek-Tips Delphi forum.

Duoas 1,025 Postaholic Featured Poster

Ah, I think I know what is happening.

The file stream is not required to keep the actual disk file up-to-date with the fstream's state at all times. It can wait until it is convenient to write data to the file.

So it looks to me like there is a race condition occurring between the two applications to access the file data.

You can force it to synchronize the disk file and the fstream's data buffer by using the flush function/manipulator, and for FILE*s use fflush().

So, when the button is clicked, the following should happen:

  1. Make sure to fflush before calling ShellExecute().
  2. Remember that ShellExecute starts the indicated process but does not wait for it to terminate. You must use one of the wait functions to wait for the program to terminate. For example:
    bool ExecTheChildProc()
      {
      // Both files should be flushed or closed before calling this function
    
      // Execute the child process...
      HINSTANCE hChild = ShellExecute( ... );
    
      // ...and wait for it to terminate
      // (You can specify a specific number of milliseconds to wait
      // before returning so that you can get control back periodically
      // and make sure your application doesn't freeze.)
      DWORD result;
      while (true) switch (WaitForSingleObject( hChild, 500 ))
        {
        case WAIT_OBJECT_0:
          // Success. The child is terminated
          // and the files are ready to be read.
          return true;
    
        case WAIT_TIMEOUT:
          // Half a second has passed.
          // Make sure the application stays responsive.
          ProcessMessages();
          break;
    
        default: …
Duoas 1,025 Postaholic Featured Poster

Seconded. The remote control shouldn't have any clue what is going on inside the TV.

Duoas 1,025 Postaholic Featured Poster

That still isn't anywhere near enough information. What DB are you using? What commands are you using to access it? What software library are you using to interface with it?

Duoas 1,025 Postaholic Featured Poster

What do you mean by 'it returns the wrong answers'? How are you returning data from the console application?

Duoas 1,025 Postaholic Featured Poster

For the array problem:

Without using an intermediary array of some kind (via recursion or on a stack or whatever) the only other way I can think to do it is with math.

You'll need a bignum library, and a list of the first N prime numbers (where N is 1 << bits_per_element).

Start with a temporary bignum with value zero. For each element, add the eth prime number (where e == bitvalue of element) to the temporary.

Now decompose the temporary and determine which prime(s) is(are) composited more than once.

;-)

Duoas 1,025 Postaholic Featured Poster

OK, I told you I'd get you an example. I don't want to spend a lot of time finding the libraries to do it and coding one, but I do have an old program I wrote many many moons ago, when I was still new to programming. It is in Turbo Pascal, but you can translate the important parts to Turbo C++ easily enough. Just a couple notes for you:

  • uses Graph; corresponds to #include <graphics.h> Individual Borland graphics routines are named similarly (I think TC puts them all in lowercase though).
  • foo^ is the same as *foo
  • ReadKey corresponds to getch
  • { and } correspond to /* and */
  • Mark and Release are old Borland heap control procedures. I don't think TC has corresponding procedures... better for you to free() the image data manually.
  • The arrow keys move the background around. ESC quits.
  • The procedures of interest to you are: InitiateGraphics, FlipPages, ClearPage, and the main code block.
  • It is fairly heavily commented in my newbie style.

Ok, so here it is. (I use 80 column limits, but DaniWeb is shorter, so what you see following looks messy. Just click the "Toggle Plain Text" button to cut and paste something more readable.)

program GPImage;
uses Crt,Graph;
var
  GraphDriver, GraphMode    : integer;                { hardware refrences }
  WX1,WY1, WX2,WY2, MX2,MY2 : integer;{ window x1,y1, x2,y2, center of scr }
  CurrentPage               : integer;      { active, hidden graphics page }

  IMAGE_SIZE …
Duoas 1,025 Postaholic Featured Poster

Dog and Cat don't relate to each other, and should probably not be friends. (No joke intended, though it is funny anyway...)

"Friends" in the C++ sense mean that some foreign object has access to your object's internals. This is useful, and quite often, but should not be used indiscriminately.

As for inheritance, both Dog and Cat could derive from a common ancester, say "Mammal" or some such. Then they could have methods in common, like Speak() and Eat().

It would probably be worth your while to spend some time over at the C++FAQ-Lite.

Hope this helps.

[edit] Hmm, too slow again... :P

Duoas 1,025 Postaholic Featured Poster

To get a filename without path or extension you need to do some string manipulations.

Here are three procs I use all the time (well, in C++... I haven't used C in a while):

#include <stdlib.h>
#include <string.h>

/* ExtractFilePath()
 *   Returns a new string containing the directory information in the
 *   path, including the terminating path separator.
 *   Don't forget to free() the result when you are done with it!
 * arguments
 *   path       The path to dissect, which may or may not contain
 *              directory information.
 *   separator  The path separator char to use if not '/'. You can specify
 *              0 for the default.
 * returns
 *   A newly malloc()ed string, or NULL if a new string could not be
 *   allocated or path is NULL.
 */
char* ExtractFilePath( const char* path, char separator )
  {
  char* p;
  char* result = NULL;
  if (!path) return NULL;
  if (!separator) separator = '/';

  p = strrchr( path, separator );
  if (p == NULL) p = (char*)path;
  else           p++;

  result = (char*)calloc( p -path +1, 1 );
  if (result) strncpy( result, path, (const char*)p -path );

  return result;
  }

/* ExtractFileName()
 *   Takes a complete path and returns a new string containing the
 *   filename part.
 *   Don't forget to free() the result when you are done with it!
 * arguments
 *   path       The path to dissect, which may or may not contain
 *              directory information.
 *   separator  The path separator char to use if not '/'. You …
Duoas 1,025 Postaholic Featured Poster

I don't know where you read that, but they're wrong. Multiple display pages is a hardware feature, so you can use it no matter what programming language you use.

Give me a little bit to provide your example..

Duoas 1,025 Postaholic Featured Poster

Dev-C++ uses MinGW.

And AD only said he has seen the problem, not that he tried compiling OPs code.

The problem likely stems from the 16-bit executable trying to do something in a way that XP doesn't like. It is picky about stuff like that. I've re-written old programs to compile with a modern compiler just so they would run properly.

What it sounds like is that the program is not terminating properly, and the DOS emulator (that comes with Windows to run 16-bit applications) is snagging it in memory because of that.

I could be totally wrong. Let me give it a compile and see what happens...

[EDIT] It behaves properly for me on XP using GCC 4.x.

Duoas 1,025 Postaholic Featured Poster

It has been a long time since I played with BGI graphics, and I did it in Turbo Pascal. If you want I'll whip something up to demonstrate page flipping... This is a standard technique, often called "double buffering".

Duoas 1,025 Postaholic Featured Poster

Argh. Sorry, I made a dumb mistake. iter->first is the key, so it is erasing the item with the named key...

Duoas 1,025 Postaholic Featured Poster

The first should result in a compilation error and the second deletes the element at *iter.

You need to find a good reference. The two I tend to use are:
http://www.cppreference.com/ (very simple and curt)
http://www.cplusplus.com/reference/ (nicely detailed when the first won't do)

So a map::erase() reads thus and thus.

Hope this helps.

Duoas 1,025 Postaholic Featured Poster

The "hex" file is really just a plain-vanilla text file.

Each byte is represented by a two-digit hexadecimal value: 00..FF.

The first number is the relative address of the first byte listed on that line. In the example you listed, the first byte listed on the first line is at offset 0 --> 00000000h: . There are 16 bytes listed per line, so the next line is at offset 16 --> 00000010h , and the line after that is at offset 32 --> 00000020h , etc.

So, all you have to do is open your input file as a file of byte, then read it 16 bytes at a time (16 hexadecimal codes per line of output, remember). If you are using Delphi, you will find the IntToHex function (in the SysUtils unit) very helpful.

Good luck!