rubberman 1,355 Nearly a Posting Virtuoso Featured Poster

These sort of problems are much more amenable to understanding if you first write out your algorithm either mathematically, or in many cases, in plain language pseudo-code. Example for an insertion sort (your final code is not using an insertion sort, because it is sorting after the array is generated), you generate a number, and insert it into the list. The insert function will find the correct position and if it is not at the bottom, move everything below where it goes down one element, and put the new member there. So, in plain language pseudo-code:

main:
while not done
   generate number N
   insert N in list
   check if done
end while

insert N:
    # First do empty list optimization
   if list size == 0
       list[0] = N
       list size = 1
   else
       for i = 0, while insert not done and i < list size, i = i + 1
           if list[i] > N # we found the spot
               move list from i to list size - 1 down one element
               list[i] = N
               insert done = true
           else if i == list size - 1  # We are at end of list, N goes here
               list[list size] = N
               list size = list size + 1
               insert done = true  # this is redundant because the increment at the
                            # top of the loop will kick us out, but say what you mean
                            # anyway, it will eliminate an entire class of bugs
           end if
       end for
   end if

I'm leaving the move list down function to you... :-)

rubberman 1,355 Nearly a Posting Virtuoso Featured Poster

Please see this page for more algorithm information: http://en.wikipedia.org/wiki/Quicksort

And if the poster is really clever, they will read the man page for the qsort() function...

rubberman 1,355 Nearly a Posting Virtuoso Featured Poster

griswolf is heading in the right direction. Use an XML structure for your collection, which is basically what a singly linked list is, an ordered collection of similar things/objects. That's very easy in XML form:

<list>
    <element>
        <field1>1</field1>
        <field2>my name</field2>
    </element>
    <element>
        <field1>2</field1>
        <field2>your name</field2>
    </element>
    .
    .
    .
</list>

So, you walk through the list, writing each element and its data members as you go. To reconstitute the list, each time you find the opening <element> tag (or whatever you name it), you allocate a buffer of the appropriate structure type, then for each field in the element, you parse out the data as a string, and convert if necessary, setting the appropriate structure member field. If it is the first element, it becomes the head of the list, and each subsequent element in the list is linked to the previous one. Voila! You have now reconstituted your list from the text (xml) file.

FWIW, I have had to do this numerous times in my work as a software engineer, so perhaps it is second nature to me. In any case, if you do use XML, there are a lot of good C and C++ xml parsing tools such as xerces to use.

rubberman 1,355 Nearly a Posting Virtuoso Featured Poster

Actually, instead of exit() in mike_2000_17's code as "last resort", use ::abort() - that will generate a core dump and if your program was compiled with the -g option and not stripped, then you can do a post-mortem in the debugger and look at variables, stack, etc. Or, just run it in the debugger in the first place. Some consider debugging an art form, but it is one of the most valuable skills you will ever learn as a programmer. Learn how to user your debugger, and utilize it. Many have settings that allow you to detect buffer overflows, uninitialized variables, and a lot of other useful stuff.

rubberman 1,355 Nearly a Posting Virtuoso Featured Poster

Does anyone actually know what the question is? Does the OP?

Probably not, considering the non-existent quality of the "spec". As someone once said "If it was easy, anyone could do it."...

rubberman 1,355 Nearly a Posting Virtuoso Featured Poster

Look up Sieve of Eratosthenes: http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes
That will allow you to build an array of prime numbers. Then you just need to see if your number is divisible by Prime(N) where N <= 1/2 X where X is your number. Another algorithm is if your number is even, divide by two until it isn't, then if the result ends in 5, divide by 5 until the last digit isn't 5, then factor that result. Example:

X = 100
X is even, so 2 is a factor.
X / 2 = 50
50 / 2 = 25, ends in 5 so 5 is a factor
25 / 5 = 5, 5 is prime, so no more factors
Results: factor(100) = {2, 5}

What about X = 101?
101 / 2 = 50 (drop fraction), but 50 is not prime
Is 50 prime? No, because it is divisible by 2 and 5
Is 49 prime? Yes. Is 101 divisible by 49? No
Is 47 prime? Yes. Is 101 ... ? No.
Is 43 prime? Yes. ... No.
Is 41 prime? Yes. ... No.
Is 39 prime? No. Divisible by 3 and 13.
Is 37 prime? Yes. ... No.
Is 33 prime? No. Divisible by 3 and 11.
Is 31 prime? Yes. Is 101 divisible by 31? No.
Is 29 prime? Yes. ... ? No.
Is 27 prime? No ... divisible …

rubberman 1,355 Nearly a Posting Virtuoso Featured Poster

Get/Read Comer and Stevens "Internetworking with TCP/IP, Volume 3, Client-Server Programming". From what I am reading, you are just guessing what to do. On the client side, you open socket, bind it to the server address, connect to the server, then loop into send-message/get-reply. You can use select() on the client side, usually to wait for the socket to be able to send more data, or wait for the reply to come back from the server, or determine that there was an exception/error. If you are communicating with multiple servers, then using select() to wait for one of them to reply before reading the message is appropriate. In any case, you connect() after you select() and that is just wrong. You should only connect once to each server, then send your messages and use select (usually with a timer) to be informed when data is available to read.

One side note: if you use a thread for each connection, then you can send-message/get-reply without using select unless the socket buffer is full, in which case select() can tell you when the socket is available to take more data, or you can just use blocking send() and read() calls.

rubberman 1,355 Nearly a Posting Virtuoso Featured Poster
char buffer[500];
sprintf(buffer, "======================\nMonth : %d\n:Day : %d\n", st.month, st.day);
rubberman 1,355 Nearly a Posting Virtuoso Featured Poster

It may be helpful to post the error output of the compiler...

rubberman 1,355 Nearly a Posting Virtuoso Featured Poster

What platform (OS + version) and compiler (type and version) are you using?

rubberman 1,355 Nearly a Posting Virtuoso Featured Poster

The functions you use, like readdata() and printvalues() are in another translation unit (fancy name for source file or library). Normally you include the necessary headers where the functions are declared, and link the program with the object file or library that contains the implementation of the function. Something like this:

#include <stdlib.h>
#include <someheader.h>

int main(void)
{
   // Calling functions declared in someheader.h
   somefunction();
   otherfunction();
   return 0;
}

Now, when you like the executable, you either need to link the object file, such as somefunctions.o (somefunctions.obj for Windows), or the library that contains them, such as libsomefunctions.so (libsomefunctions.dll for Windows).

rubberman 1,355 Nearly a Posting Virtuoso Featured Poster

Each do loop needs a while condition, such as:

bool done = false;
do
{
   // Stuff to do
   if (no_more_stuff_to_do)
   {
       done = true;
   }
}
while (!done);

// Or

while (!done)
{
    // Stuff to do
    if (no_more_stuff_to_do)
    {
       done = true;
    }
}
rubberman 1,355 Nearly a Posting Virtuoso Featured Poster

What mike_2000_17 said, plus this:

// Your code ---->
	p = &b;    // Ok
	*b.l = 23; // Bad: member 'l' has not been initialized with a valid pointer
	*b.m = 23; // Bad: static member 'm' has not been initialized with valid pointer

	cout << "with ptr = " << *b.l << *(p->l) <<endl; // Bad - see above.

// Better code ---->
        static int ll = 23;
	p = &b;
	b.l = &ll;
	b.m = &ll;

	cout << "with ptr = " << *(b.l) << *(p->l) <<endl;
rubberman 1,355 Nearly a Posting Virtuoso Featured Poster

From your description, you are using 8-bit values, and want to extract the high and low nibbles (4 bits), turn them into 4-bit values, then add them together? Is that correct? If so, then what you want to is a mask and shift operation. Example:

unsigned char ch = 11111010b; // unsigned 8-bit value
unsigned char top = (ch & 0xF0) >> 4; // Mask off bottom 4 bits and shift right
unsigned char bottom = (ch & 0x0F);   // Mask off top 4 bits - no need to shift
unsigned int results = top + bottom;  // Add top and bottom values
rubberman 1,355 Nearly a Posting Virtuoso Featured Poster

Try something you'd personally find useful, such as a CD/DVD cataloging application. Since you want to program in C++ (my favorite language - been using it for 20 years), try to "think objectively". Think about the things you are modeling, and write the classes for those things with the behavior they should have. Each class should have its own header file and source file - don't jam everything together in one huge source file. That just makes modifications and bug fixes more difficult.

Make sure your class constructors properly initialize all member variables that do not have constructors of their own.

Avoid the NIH (Not Invented Here) syndrome - use standard library functions as much as possible. They have been thoroughly tested, and trust me when I say that writing your own vector or string classes may be educational, you will spend a lot of time implementing something that isn't as good, fast, or efficient.

Passing arguments to functions, where in C you would use a pointer, use a reference instead. It will eliminate an entire class of bugs when you try to dereference a null pointer. The compiler will catch that stuff for you.

Always declare destructors as virtual, and always have a destructor, even if it does nothing. Some day you will want to use that class as a base class, or have it derived from some other class, and this will enable you to delete pointers to either base or derived and it …

rubberman 1,355 Nearly a Posting Virtuoso Featured Poster

1. Break your program into logical units as functions, such as the code for processing menu a or menu b.
2. As suggested, instead of a great big set of if/else statements, use a switch().
3. If you want your program to be portable, don't use MS specific screen formatting functions like clrscr(). Instead, if you really want to have a nice text display, use the ncurses library instead. It works everywhere.
4. Keep your braces lined up and indent your code between them. It will help you find a lot of the bugs that exist in your program now.

rubberman 1,355 Nearly a Posting Virtuoso Featured Poster

You can compile a completely empty source file, or one that only has preprocessor statements like #include, #define, etc. You will possibly get a warning, but it shouldn't be fatal. You are correct in that two main() functions would not be viable. Even c++ which allows function overloading (two functions with same name, but different signatures) doesn't allow two main() functions - I just tested this on my system.

That said, there are a lot of nasty little bugs in this program, including the string_to_integer() function. You might want to read it through again carefully, especially with regard to array access/indexing in main().

rubberman 1,355 Nearly a Posting Virtuoso Featured Poster

Without seeing what you are doing in assign3.c, this is not helpful. Also, what compiler (include version) and platform are you building on?

rubberman 1,355 Nearly a Posting Virtuoso Featured Poster
void cubes(int n)
{
    if (n >= 1)
    {
        cubes(n-1);
        cout << (n * n * n) << endl;
    }
}
rubberman 1,355 Nearly a Posting Virtuoso Featured Poster

Line 54: string used = "";
string used is also a function argument. This is a conflict because you are also declaring a local variable string named "used".

rubberman 1,355 Nearly a Posting Virtuoso Featured Poster

Ok, I've changed it a lot myself. I'm implementing this algorithm; http://compprog.files.wordpress.com/2008/01/dijkstra.c . but i want it for a given source to given target.

Sorry, but I don't get what you mean with "i want it for a given source to given target." - please explain. The source shown, while not what I'd let my engineers get away with releasing into production, will work (from the little I was able to study it). What is your actual intention here?

rubberman 1,355 Nearly a Posting Virtuoso Featured Poster

Here is the real test to see if the virtual function is working properly:

#include <iostream>
using std::cout;
using std::endl;

class Parent
{
public:
 virtual void MyFunction() { cout << "Parent" << endl;}
};
 
 
class Child : public Parent
{
public:
 virtual void MyFunction() { cout << "Child" << endl;}
};

int main(void)
{
    Child myKid;
    Parent& mySelf = myKid;
    myKid.MyFunction();
    mySelf.MyFunction();
    return 0;
}

The expected output is:

Child
Child

rubberman 1,355 Nearly a Posting Virtuoso Featured Poster

Well, it should work. What compiler are you using?

In any case, I tried it with GNU C++ 4.4.4 and it works fine - I get "Child" for output.

rubberman 1,355 Nearly a Posting Virtuoso Featured Poster

School exercise? First, state the problem clearly before you start coding. Second, write out the algorithm you are going to use as pseudo-code. Third, keep the actual code as simple as possible.

Sieve: generate array of prime numbers. A prime number is divisible only by 1 and itself. The numbers 1, 2, and 3 are prime. All even numbers other than 2 are not prime. So, we only have to consider odd numbers from 4 through the limit value.

Refinement: other than 5, all numbers that end in 5 are divisible by 5, hence not prime.

So, we have eliminated all numbers > 3 that end in 0, 2, 4, 6, or 8. We have also eliminated all numbers > 5 that end in 5. So, we only have to consider numbers > 5 that end in 3, 7, or 9. These are the only numbers that we need to check for divisibility by any of the previously discovered prime numbers < 1/2 of the number being considered.

Once we have generated our array of indicators (can be a bit-array for size, or numeric for speed) where 0 == !prime and 1 == prime, we can now create an algorithm to find prime factors for any number quite easily. I will leave that as an exercise for the reader... :-)

rubberman 1,355 Nearly a Posting Virtuoso Featured Poster

What mike said is generally correct. However, it is also dangerous! Never dereference a pointer like this without first making sure it is not a valid pointer. This is why we have reference objects in C++ since it eliminates the need to check the pointer first. IE:

int int_function_1(int* y)
{
   int x = *y; // BAD! If y is null, you are toast!
   return x+1;
}

int int_function_2(int* y)
{
   int x = 0;
   if (y != 0)
   {
       x = *y; // Better - we know that y is not null
   }
   return x + 1;
}

int int_function_3(int& y)
{
    int x = y; // Best - we know that y references a real integer
               // unless the caller of this function was a real meathead!
    return x + 1;
}

Also, in mike's example of doing array references (legal code), this is also dangerous in that you must know two things. First that the memory that y points to is really an array (if we are looking past the 0th element), and second that the array is big enough and properly initialized for the element we are accessing. In any case, this sort of code is a very big security hole.

rubberman 1,355 Nearly a Posting Virtuoso Featured Poster

Way too much cruft to sort through quickly here. Remember the motto "KISS" - so, simplify your code down to the bare essentials, and take all those big loops and refactor them into function calls. That way, each function point (call) can be much more easily analyzed in detail, and side effects are minimized.

rubberman 1,355 Nearly a Posting Virtuoso Featured Poster

In the class definitions, class Child is not derived from class Parent, at least in the example shown. IE: it should look like this:

class Parent
{
public:
 virtual void MyFunction() {// do something}
};
 
 
class Child : public class Parent
{
public:
 virtual void MyFunction() {// do something else}
};
rubberman 1,355 Nearly a Posting Virtuoso Featured Poster

Well, you can enable the setuid bit on the date command (chmod +s /bin/date), then use the system("date time_val") to set the date+time in your C/C++ program.

rubberman 1,355 Nearly a Posting Virtuoso Featured Poster

Short answers,

1. Same as you would in a C application.
2. Ditto - you can't.

Long answers.

1. Why? Call system("ntpdate ntp_server_address") - this will set the system time to the global clock. Guaranteed accurate. If you want to set it to your own value, use system("date date+time_value").
2. The problem here is that you will need to input the password for the root account, and that cannot be done if the process is being run in the background. So, for security reasons, you really can't do that. In a shell, running the command "su -" will request the password for the root account, and if the calling process is backgrounded (no stdin), then it will fail.

rubberman 1,355 Nearly a Posting Virtuoso Featured Poster

Red Hat 2.x is a long-buried, dead corpse... Download and install current version 5.5 at least. If you don't want to pay the RH licensing fees, try CentOS - it is free and 100% compatible - only the logos have been changed. Go to www.centos.org and download.

rubberman 1,355 Nearly a Posting Virtuoso Featured Poster

Huh? Please explain better - provide some details about what you are trying to accomplish, and why.

rubberman 1,355 Nearly a Posting Virtuoso Featured Poster

This is not helpful. You really need to provide the source code you are building for this, along with some description of what the application does (or that you want it to do), and what your design is comprised of.

rubberman 1,355 Nearly a Posting Virtuoso Featured Poster

Each mobile platform has very different coding requirements. What device do you want to use? If you are interested in Android development, you can go to the google/android web site and download their SDK (software development kit) which includes an Android phone emulator. That way you can test out your application without needing to purchase a $500 unlocked phone.

rubberman 1,355 Nearly a Posting Virtuoso Featured Poster

Not much information here. What hardware do you need drivers for?

rubberman 1,355 Nearly a Posting Virtuoso Featured Poster

What happens if you remove the battery, plug in the A/C adapter, and turn on the system? If it still doesn't work, the motherboard is fubar. If it works, then the battery is kaput.

rubberman 1,355 Nearly a Posting Virtuoso Featured Poster

This has happened to me when I spilled water on my keyboard. It caused some of the carbon on the key contact to run and short out, resulting in repeating keys and/or the wrong key being detected. I had to replace the keyboard.

rubberman 1,355 Nearly a Posting Virtuoso Featured Poster

Unfortunately it is true that many (if not most) hospitals are almost entirely Windows shops, even the systems that monitor critical medical devices. A number of them were effectively shut down recently when they were hit with a particularly nasty virus - only emergency operations in life-threatening situations were allowed to go forward. This type of computer monoculture is incredibly dangerous. To use Windows for any safety-critical system is, in my professional opinion, a matter of malpractice. FWIW, I am a 20+ year member of the IEEE and director of a major IEEE consultant's network.

rubberman 1,355 Nearly a Posting Virtuoso Featured Poster

@ Bobber47
Actually, I think that WinCE does run on ARM processors, but I would agree that Win7 is another kettle of fish. Sounds like the FUD spinners are hard at work! :-)

rubberman 1,355 Nearly a Posting Virtuoso Featured Poster

A wise decision? I think not. Microsoft has a wonderful track record, of sticking the knife in their partners' backs. This is one deal that Amazon will come to regret. That's just my humble opinion.