Schol-R-LEA 1,446 Commie Mutant Traitor Featured Poster

The standard Datetime and Calendar classes only work with Gregorian calendar dates; however, after some searching I did find a project on Sourceforge for a multi-calendar utility module for Python, and it does specifically include the Persian calendar. Assuming it works, that may be what you need.

Schol-R-LEA 1,446 Commie Mutant Traitor Featured Poster

While I wouldn't actually be Ranma Saotome. I do often wonder how I could find my way to Nyannichuan to take a swim.

Schol-R-LEA 1,446 Commie Mutant Traitor Featured Poster

Ah, I see. Well, I can help you out I think. Fortunatel,y functions in Python are dead easy, and once you get the idea of them, you'll probably use them quite a bit.

Using a function is already familiar to you; you used the standard functions len(), print(), and sqrt() already. While len() and print() are part of the Python language itself, sqrt() is actually a part of the math library, and not a part of the core language at all. While the libraries are part of what comes with the language, they are separate from the language itself, which is why you need to use import in order to use them.

Writing your own function is almost as easy, but I should explain a little about why you would want to first. There are three main reasons for writing functions in Python (and most programming languages in general). First, they let you give a name to a particular piece of code. This allows you to ''abstract'' the idea of that piece of code, so you don't have to think about what's inside of it; you can just use the name. Second, it makes it easy to use the same piece of code in more than one place, making the program smaller and reducing the number of places you need to fix if you have to change something. Finally, a function can be ''parameterized'', that is to say, it can let you describe ''part'' of a piece of code, without …

Schol-R-LEA 1,446 Commie Mutant Traitor Featured Poster

OK, you are mostly on the right track, I would say. However, there are things that can be done to improve it. Has your professor covered defining functions yet? If so, I would certainly recommend writing the average as a a function, that way you don't have to repeat the same code in the SD section twice. In fact, I would write all of these as functions, and simply have the main program call them as needed.

Also, while it is useful to have a defined value for the input when testing, don't forget that the actual project assignment calls for getting the input from the user. You should be able to write that as a function, too.

Schol-R-LEA 1,446 Commie Mutant Traitor Featured Poster

We are certainly willing and able to help, but we'd need to know what sort of help you need. What have you done so far, and where are you stuck? Do you know the basics of how to write a function, how to use Python arithmetic, and so forth? Do you know how to calculate the mean, median. etc. by hand, and how you would apply that to writing the program?

Also, what version of Python are you using (2.x or 3.x), and is there a specific environment or IDE you are using for developing the programs?

I will caution you, though: do not expect anyone here to write the programs for you. We will assist you, but we are not a free homework-solving site. Show us that you've made an effort, and we will do what we can to aid you, but we will not and cannot do your work for you.

Gribouillis commented: indeed +14
Schol-R-LEA 1,446 Commie Mutant Traitor Featured Poster

That's correct, except that either the size of the declaration should be resd (32-bit - my mistake in my earlier post) or the size of the operand should be word (16-bit). Which you want depends on the how high the score can go - 16-bit unsigned values go only to 65536, while 32-bit unsigned go over 4 billion. If you expect the scoring to exceed 65536, then use a dword or even a qword (64-bit).

Schol-R-LEA 1,446 Commie Mutant Traitor Featured Poster

That depends on what the label userscore is for. Is it a single dword holding the value as an integer, or is it a byte buffer holding a string? In other words, assuming NASM syntax, is it:

userscore:    resw 1  ; or 'userscore dw ?' in MASM 

or

userscore:    resb 8   ; or some other arbitrary size string

If it is a string, then yes, it would need to be converted first, then added to, then converted again. However, in that case, you may find it more useful to have userscore stored as a dword in integer form and only converted to a string when being displayed.

Schol-R-LEA 1,446 Commie Mutant Traitor Featured Poster

And Schol-R-LEA I am sorry you don't like my name. I have used it for many years and really just came up with it.

Oh, no worries. I actually like it, but it just seems a bit ominous to me as someone who grew up in the 1970s and 1980s. I don't know if you even know what the MX missile was...

Schol-R-LEA 1,446 Commie Mutant Traitor Featured Poster

I think you'll find that the code you have used in that specific section is not Java, but JavaScript, a completely unrelated language despite its name. Java does not have a function keyword, among other things.

(On an unrelated note, I might add that your screen name is somewhat... unnerving to me. Perhaps I've watched too movies like WarGames and By Dawn's Early Light too many times.)

Schol-R-LEA 1,446 Commie Mutant Traitor Featured Poster

While SenthilAnand's point is correct, and excellent advice, it is not the source of the compilation error; the compiler is perfectly happy to let you perform an assignment in the conditional, it just usually isn't what you intended and will have unexpected results when run.

The real problem is that you are putting semi-colons after each if() statement's conditional, ending the if statement. Now, once again, this is for the most part accpetable syntax, until you get to the last of the else if() clauses:

    if (sales <= 0 && commission == -1)
        ;
    else if (sales <= 10000 && commission == sales * 0.2)
        ;
    else if (sales <= 40000 && commission == sales * .05 + 2000)
        ;
    else if (sales >= 40000 && commission = sales*.1 + 17000)
        ;
    {
        //calculate and display the output
        sales = sales <= 0 ;
        commission = -1 ;
        sales = sales <= 10000 ;
        commission = sales*0.2 ;
        sales = sales <= 40000 ;
        commission = sales*.05 + 2000 ;
        sales = sales >= 40000 ;
        commission = sales*.1 + 17000 ;
        printf("\nsales %d ", totalsales);
        printf("\ntotalcommissions: %.0f%% \n\n", commission);//zero decimal places
    }
    else

(Note how I have indent the code for readability, and in particular, how I made the empty bodies of the if() statements explicit.)

Now, one thing many starting C programmers aren't aware of is that you can put a block (a section of starting with an open brace and ending with a close …

ddanbe commented: The final touch! +15
Schol-R-LEA 1,446 Commie Mutant Traitor Featured Poster

I think you will find that the problem lies less with your program than it does with the way Notepad renders the codepoints. I don't know all of the details of how Notepad determines how to interpret the characters, but if I read what you are saying correctly, it basically only checks whether the the first few characters are in the ASCII range, and if they are not, it interprets the file as UTF-8; but if the first character is in the ASCII range, it interprets the file as ASCII (or more likely, either LATIN-1 or ISO-8859-1). The stream of bytes you are writing is correct; it is the interpretation of them that Notepad uses that is at fault.

If anyone knows how to force Notepad to interpret a file as UTF-8, please speak up. This video purports to explain how, but it is not a trivial process, and appears to involve registry changes.

In the meawhile, I would instead use an editor that supports multiple encodings such as Notepad++ or TextPad.

However, if the real issue is permitting your user base to edit the file manually, then that is a serious problem with no obvious solution.

Schol-R-LEA 1,446 Commie Mutant Traitor Featured Poster

You would need to call isPrime() with two numbers, one the number you are checking and the other a number smaller than that but larger than it's square root.

I would reccomend changing the name of isPrime() to primeTest() and having a second function called isPrime(), which takes only one int as its argument. This would call primeTest() with some appropriate argument automatically, so that the main() function can pass only the number being tested as the argument to isPrime().

#include <iostream>
#include <cmath>

bool isPrime(int n);
bool primeTest(int n, int d); 

int main()
{
int number;
cout <<"Enter number >= 1";
cin >> number;
if(isPrime(number))
   {
     return 
      cout << "Yes";
      else
      return
      cout << "No";

    }
    return 0;
}

isPrime(int n)
{
    if (n < 2)
    {
       return false;
    }
    else if (n == 2)
    {
       return true;
    }
    else
    {
        return primeTest(n, (int) ceil(sqrt(n));
    }
}

bool primeTest(int n, int d)
{
    if(n<2)
        return false;
    if(d == 1)
        return true;
    else 
    {
        if(n % d == 0) 
            return false;
        else
            return isPrime(n, d - 1);
    }
}

The sqrt() function returns the square root of a number as a double, and the ceil() rounds the number up to the next integer value. the (int) is a cast to make the double an int.

Schol-R-LEA 1,446 Commie Mutant Traitor Featured Poster

OK, first thing is, in C you can only declare variables at the beginning of the function, so I'm not sure why it is letting you put it there. The fact that it is inside the loop, however, means it is getting re-initialized each iteration, so it will always be zero.

Also, I assume that it is still printing out the rest of the line, correct?

Schol-R-LEA 1,446 Commie Mutant Traitor Featured Poster

Which part are you having trouble with?

Schol-R-LEA 1,446 Commie Mutant Traitor Featured Poster

It is quite simple: first, you need an unsigned int variable as a counter, which should be initialized to zero. Alter your printf() statement to print this counter as a four-digit hex value followed by a a semi-colon and a space, followed by the string you are now printing. Finally, at the end of the printing loop, add NUM_CHARS to the counter.

Schol-R-LEA 1,446 Commie Mutant Traitor Featured Poster

OK, you have some of the idea, but you seem confused about some aspects, such as parameteters and return values. Let me give some suggestions.

First off, your goal is to get the number of vowels in the function parameter, test. The parameter of a function is a value passed to it so that it can operate on that value. For example, in the function square() below,

double square(double x)
{
    return x * x;
}

the variable x is the formal parameter of square(). If I then have a function that calls square() with the argument 5,

n = square(5);

I would expect n to be assigned 25. I could also pass it a variable, like so:

m = square(n);

which would set m to 625. This is one of the main ideas behind functions: that you can use them with different arguments and get different results.

When testdriverb() calls countvowels() with the argument test (which has been set to the string "Programing language IS FUN to learn: "), it sets the value of the parameter test in countvowels() to that string.

Note that the name of the argument doesn't have to match the name of the parameter, and in fact the argument doesn't even have to be a variable at all (in most cases). If you look again at my earlier code, you'll see that I used a literal as the argument to square(), for example. You could have called the variable …

Schol-R-LEA 1,446 Commie Mutant Traitor Featured Poster

Actually, reliability is not the real issue with analog computers; indeed, they can be much more reliable at times, as they don't have issues of round-off errors and so forth that are inherent in digital computers. Analog devices have the advantage of being continous rather than discrete, and thus are better models for certain problems.

Reliability can be an issue, of course, in that most analog devices are mechanical, and subject to wear. However, well-machined parts mean that an analog computer is unlikely to wear out within it's useful lifespan.

The real limits with analog computers is that they are always special-purpose devices; no practical Turing-equivalent analog computers have ever been designed. This isn't to say it isn't possible, but given the advantages of digital computing and the experience gained with it over the past 60 years, it is unlikely that any general-purpose analog devices will be developed. The only reason to use an analog computer today is if something needs to be modelled very accurately in a continuous manner, and since current digital devices provide very high resolution (if still discrete) accuracy, that almost never arises.

Schol-R-LEA 1,446 Commie Mutant Traitor Featured Poster

Except that SQLite is a completely different database system.

Schol-R-LEA 1,446 Commie Mutant Traitor Featured Poster

Actually, you've misunderstood what the term 'SQL injection' means, and fortunately for you, your script doesn't involve any. All your code does is open a connection to a MySQL database server and create a cursor, the basic steps interacting with a database.

SQL injection is a (bad) way of building a SQLquery string, in which user input is inserted directly to a text query string and then executed:

name = raw_input("What is your name?")
cur.execute("SELECT * FROM Students WHERE Name = '%s';" % name)

The reason this is a bad idea is the same reason while it was a bad idea to use input() in Python 2.x: because the user could put anything into the input, not just their name, and the 'data' will get executed along with the query. For example, if I entered Robert'; DROP TABLE Students; --, the final query string would read

"SELECT * FROM Students WHERE Name = 'Robert'; DROP TABLE Students; --';"

which would wreak havoc on the database.

The way to avoid SQL injection is to use a prepared statement, which is a feature of the database that vetts the data automatically for you, making the above scenario less likely.

name = raw_input("What is your name?")
cur.execute("SELECT * FROM Students WHERE Name = %s;", (name))     

This may seem like a trivial difference, but when using the prepared statement, the database checks the validity of the interpolated data, making sure that the cannot execute …

Schol-R-LEA 1,446 Commie Mutant Traitor Featured Poster

The difference is that when you were creating ob in Checking, were were creating an object in each thread, which meant that they weren't shared between the different threads - each thread was synchronizing a different object. You need to have a shared object to synchronize on if you are trying to provide mutual exclusion.

By constructing the single object in main(), and more importantly, passing that single object to all three threads, all three threads share the object. That is what the synchronized block is for - for controlling access to a shared resource. If you aren't sharing the resource, then synchronized will have no effect.

Schol-R-LEA 1,446 Commie Mutant Traitor Featured Poster

I am no expert at Java threading, but I believe the main issue is that you aren't synchronizing on a single object, but rather you have three separate objects of the same type, which happen to be synchronized, but not with each other. You would need a single object which is synchronized in all three threads in order to control the order in which the threads interleave.

import java.lang.Runnable;

class Race1{

    public void show(String s){ 

        try{

            System.out.print("["+s);
            Thread.sleep(1000);
        }catch(InterruptedException e){

            e.printStackTrace();
        }
        System.out.println("]");

    }
}
class Checking implements Runnable{

    Race1 ob;
    String s;
    public Checking(String s, Race1 ob){
        this.s = s;
        this.ob = ob;
    }
    public void run(){

        synchronized(ob){
            ob.show(s);
        }
    }
}

public class RaceCondition1{

    public static void main(String args[]){
        Race1 ob = new Race1();

        Thread t1=new Thread(new Checking("Hello", ob));
        Thread t2=new Thread(new Checking("World", ob));
        Thread t3=new Thread(new Checking("Complete", ob));
        t1.start();
        t2.start();
        t3.start();
        try{

            t1.join();
            t2.join();
            t3.join();
        }catch(InterruptedException e){

            e.printStackTrace();
        }
    }
}
Schol-R-LEA 1,446 Commie Mutant Traitor Featured Poster

The first thing I would recommend in starting out with Python is finding a good Integrated Development Environment, oe IDE; while IDLE, the one that comes with Python, is passable, it is not really new-user friendly and takes a bit of getting used to. Unfortunately, 'good' is very subjective when it comes to IDEs, so you might find yourself trying a few before you come across the best fit. This thread and this one discuss the matter at length, without really giving a solid answer, for example. My personal choices are PyCharm (if you are willing to shell out for it) and Eric (which is free), but they might be a bit overwhelming to a novice. If you are planning to stick with Python 2 - not recommended, but many do - then Dr Python is excellent for novices. Still, you need to try a few out to get the one you are comfortbale with.

So why is the IDE so important, when all you really need is Notepad and an open shell window? Convenience, primarily. A good Python IDE will have both an editing window and an interpreter window in plain sight, making it easy to use the Python listener (the interactive interpreter) to test things in, while still having access to the program you are writing. Also, a good IDE makes it easier to work with more than one source module, when you get to that point in …

Schol-R-LEA 1,446 Commie Mutant Traitor Featured Poster

If you need one other suggestion which would cover both computer science and software engineering, and involves an actual open research problem, you might look into the question of compiling and linking generics (such as C++ templates) in an object format. It is a difficult issue because instantiating a generic involves generating new code based on the generic function or class at compile time - the compiler would have to be able to extract the generic from the object file without necessarily having the source code available in its original form. The advantages would be significant, however, as it would allow library writers to remove the template implementations from the headers and keep them in the source files, where they belong.

I would recommend looking at the ELF format to begin with, as it is a completely open standard and would be easier to extend, IMAO. If you succeed in this, consider doing the same with the PE format (convincing Microsoft to adopt your extensions is left as an exercise for the reader).

Schol-R-LEA 1,446 Commie Mutant Traitor Featured Poster

You probably won't be surprised to find out that this has been discussed here at length before, nor that no real conclusions were ever reached. The appeal and value of a development environment is a highly ideosyncratic thing, and what works for one programmer often doesn't suit another.

For what it is worth, I usually just stick to GNU Emacs, though Eric is also quite good in my opinion, if a bit tricky to get installed.

Schol-R-LEA 1,446 Commie Mutant Traitor Featured Poster

The purpose of the d'tor is not to free the object itself; that is done by delete, which should only be applied to dynamically allocated memory (that is, values allocated using new) in any case. Also, freeing the object does not overwrite the object; the memory itself is is still unchanged, at least for the time being, and in order to ensure that a pointer variable no longer points at that now-invalid memory location, you need to assign a new value (such as 0) to it.

The purpose of the destructor is to clean up the elements of the objects that are not automatically released when the object itself is freed: deleteing any dynamically allocated instance values, closing files, releasing semaphores, and the like.

As sepp2k said, the d'tor should almost never be called directly. However, if a class has a d'tor, it is called automatically when an object of the class is freed, whether by means of delete or when a local goes out of scope.

Schol-R-LEA 1,446 Commie Mutant Traitor Featured Poster

I would prompt you to consider a shorter if somewhat more complex solution, one which works by sorting the three values. While a full sorting function is a bit heavyweight for this purpose, consider using three temporary values, min, mid, and max, and then testing the three input values in such a way as to put the results into the three temporary values as appropriate:

sort (a, b, c):
    if a <= b
        min = a
        mid = b
    else
        min = b
        mid = a

    if mid <= c
        max = c
    else
        max = mid
        if ...
// I'll leave the rest to you...

you would then simply print out min, mid, and max.

Schol-R-LEA 1,446 Commie Mutant Traitor Featured Poster

Just to make it clear that it is in fact appearing in Netbeans 8.0, this screenshot shows it as it should appear on your screen.

Schol-R-LEA 1,446 Commie Mutant Traitor Featured Poster

That's very strange. What version of Netbeans are you using? I just installed the newest version (8.0), which does indeed have Boolean, but if you have an older version it may not support it yet. I'd be surprised if that were the case, but it could be the source of the problem.

Schol-R-LEA 1,446 Commie Mutant Traitor Featured Poster

OK, now that I can see what you are talking about, I can tell you that there is in fact a BOOLEAN option in the type pulldown, it just is off the lower edge of the menu. If you scroll down in the pulldown menu itself, you'll find it at the bottom. It is remarkably easy to miss, so don't fault yourself for it.

Schol-R-LEA 1,446 Commie Mutant Traitor Featured Poster

It shouldn't be too much for a lambda expression, so long as you recall a) that you can use a boolean expression such as in in a regular statement, and b) the string library includes a handy set of constant strings with the sets of letters, digits, and whitespace. All you should need to do is import string and apply in to your string's first character, and string.digits, and you should be able to use that as the lambda expression.

(OK, I'm sandbagging on this, as I've tested it and it works fine in Python 3.4, but I feel it is better to lead you to the code than simply give it to you. In this case, the explanation should be more than adequate for you to get the appropriate code worked out.)

Schol-R-LEA 1,446 Commie Mutant Traitor Featured Poster

I the absence of other information, I would recommend trying the -I option with the path to the header files in question.

g++ -Ipath\to\include main.cpp program1.cpp -o program1.exe

However, if they are in the path already that shouldn't be necessary, so there may be more to this than that.

Schol-R-LEA 1,446 Commie Mutant Traitor Featured Poster

That doesn't make sense - it should be there, and called BIT. I'll look into it myself and find out what is going on.

Schol-R-LEA 1,446 Commie Mutant Traitor Featured Poster

Well, technically, every data type is bit data, though not in any useful sense; I think you are confusing the terms 'bit' and 'byte'. No, what I mean is, BIT is the name of the data type in the tables.

How are you building the tables? Are you writing SQL CREATE commands directly, or using a tool for table generation? I'll show you how to create tables in SQL, but if you are generating them automatically, it may not be quite the same. I would recommend generating the tables programmatically anyway, as it gives you better control of the database, but I know it is a lot of work as well.

Clif40RD commented: Helloooo anybody there??????!!!!!! +0
Schol-R-LEA 1,446 Commie Mutant Traitor Featured Poster

Ah, yes, in SQL (not just Derby), there is a type BIT which is usually used for Boolean values.

Schol-R-LEA 1,446 Commie Mutant Traitor Featured Poster

What sort of help do you need?

Schol-R-LEA 1,446 Commie Mutant Traitor Featured Poster

You have Derby configured to work with your development tools, right? That probably means that it is going through JDBC, which makes for a nicely standard API. OK, then. What do you need help with?

Schol-R-LEA 1,446 Commie Mutant Traitor Featured Poster

While Slavi's answer is indeed a good starting point, I think we could help some more as well if you could give us some more information. Let's start with a few pertinent questions:

  • What operating system is this going to run on?
  • What database engine are you using (e.g., SqlLite, MS Access, MS-SQL, MySql, Postgres, Oracle, etc)?
  • What programming language (aside from SQL, which is nearly universal in DBMS querying) will you be using?
  • What are the Business Rules for the organization? Do you have enough information about the NPO to define the BRs?
  • What are the entities you need to have store and retrieve information about, and how are they related? What are the relevant attributes of the entities?
  • What relations - that is to say, tables - can you make to model the entities?

The starting point in most database design is defining a set of business rules, that is to say, a semi-formal description of what the organization is, what the structure of it is, and what it does. For example, some years ago I wrote the following business rules for a company I was designing a web site and database for:

Business Rules for MJTB

0) The primary business of MJTB is the processing of PURCHASE ORDERS from CUSTOMERS.
1) A PURCHASE ORDER is an agreement between MJTB and a CUSTOMER to exchange one or more ITEMS for an amount of money …

Schol-R-LEA 1,446 Commie Mutant Traitor Featured Poster

This doc page may prove useful to you.

By the way does it have to say void in the brackets on int main.

No. In fact, in C++ it shouldn't have void there.

And then for every line I want to color I Put

SetConsoleTextAttribute ( h, FOREGROUND_RED | FOREGROUND_INTENSITY );

>

Before it and

SetConsoleTextAttribute ( h, wOldColorAttrs);

after.

Actually, you should be able to set the attribute once for a block of rows, and only restore the original settings at the end of that - assuming you want to go back to the previous settings at all...

Also do I have to use printf to get the text because I am currently using the cout<< method?

I would expect that either would work, actually, though I'm not sure either of them is really correct. The actual Windows API function is WriteConsole(), but I expect that the C and C++ stream I/O functions are calling that themselves.

Oh, and why are there only 3 colors is that really the limit :/

No, it isn't; you can combine the bitfields for the colors to get different shades. For example, FOREGROUND_BLUE | FOREGROUND_GREEN should give cyan text.

Schol-R-LEA 1,446 Commie Mutant Traitor Featured Poster

First off, you aren't going to be writing hard real-time applications on Windows, or on any other operating system that uses virtual memory without specific support for unswappable pages. That's the first thing to understand: if there is a possibility of a delay in response time due to paging or swap time, real time performance is simply not possible. There are specialized operating systems around that are real-time, such as Qnx or eCos, but your typical desktop OS simply isn't suited for real-time work.

The other thing to understand is that most of the things you are probably thinking of as 'real time performance', such as music players or games, aren't, at least not in a technical sense. The term 'real time' refers not to speed, but rather to reliability and precision: a hard real-time operation is defined as one which guarantees that a given action will be taken between time t and time t', without failure - it is what you need when running an aircraft's control surfaces, for example, or the timing of actions on an assembly line. Soft real-time promises best effort response in the given time window; typical examples are the response time of a printer to a print command.

Bottom line: unless you are developing for a hardware manufacturer, you are unlikely to ever do any real-time programming.

Schol-R-LEA 1,446 Commie Mutant Traitor Featured Poster

Just to clarify to point: you have an existing source file, which contains a complete program (e.g., it has a main() function and can be compiled and run without any additional files), and you want to use a single function out of that file in another program? That explains a good deal about the confusion you are experiencing, then.

The solution is to separate the functions you want to share into a third file, with only the functions, and then create a fourth file, a header file (with the .h extension) which would contain the class declarations and function prototypes. You would then include the header - not the source file - into both the original program (sans the shared code) and the new program, as well as seeing to it that the shared file is in both builds.

So, if you had the following program:

#include <iostream>
using namespace std;

class Foo 
{
private:
    int bar;

public:
    friend ostream& operator<<(ostream& os, Foo f); 

    int baz()
    {
      // do something and return an int
    }
}    

ostream& operator<<(ostream& os, Foo f)
{
     // implement the output operator for class Foo    
}

int main() 
{
    Foo quux;

    quux.baz();
    cout << quux;

    return 0;
}    

and you wanted to use the class Foo in another program, you would remove Foo from the original program (which you really should have done in the first place for modularity's sake), and create both a header and an implementation file:

Foo.h

// this …
mike_2000_17 commented: nice +14
Schol-R-LEA 1,446 Commie Mutant Traitor Featured Poster

Apple's documentation on Workspaces and Projects should answer most of your questions; while it is aimed mainly at Objective-C, it should be the same for all supported languages. It seems fairly straightforward: you can drag and drop a file into the 'Classes' or 'Other Sources' folder, and that should be sufficient to add the files to your build. I think. If not, the explanation here should work for adding existing files.

You might want to look at this developer's tutorial. It is a bit dated, but still mostly relevant. If that's over your level at this time, try this one, though it doesn't cover the specifc question too much.

Schol-R-LEA 1,446 Commie Mutant Traitor Featured Poster

It was no waste of time, I assure you; this is what we are here for, after all.

Schol-R-LEA 1,446 Commie Mutant Traitor Featured Poster

I understand if only one or two functions are used that it is impractical, but what happens if you need almost all of the functions in the other file?

That's what Makefiles, or in the case of most IDEs, project files, are for. I don't know XCode, myself, but it should have some sort of workspace/project management facility, which will handle the building, tracking and linking for you.

Schol-R-LEA 1,446 Commie Mutant Traitor Featured Poster

Unfortunately, yes, it is. This post should help explain why.

The solution is to compile the source files in such a way that they can be linked into a single program. How you would do this is going to depend on the compiler and linker (or in many cases, the IDE) you are using. In most IDEs, including Visual Studio, Code::Blocks, and Eclipse, it can be as simple as making sure that the file is in the same project with the main program source file. If you are using a command-line compiler, you can usually list multiple source and object files in the build command. For example, with gcc you can compile the file foo.cpp to a separate object file,

 g++ -Wall foo.cpp -o foo.o

(...the -o switch says to use foo.o as the output file name, while -Wall means, 'display all the warnings that come up when compiling'...)

then you can combine it with the files bar.cpp and main.cpp like so:

 g++ -Wall foo.o bar.cpp main.cpp -o program

the GCC driver program, which for C++ is g++, will automagically call the linker for you.

Schol-R-LEA 1,446 Commie Mutant Traitor Featured Poster

As Hiroshe said, this isn't anywhere near enough information to start helping you. You'll need to not only explain the assignment, but also show us what you have tried to do already to solve it.

Do not expect us to hand you a solution out of thin air, either. Daniweb is not a free homework solving web site; we won't do your work for you. You will need to demonstrate due diligence on your own part before anyone here will give you assistance. This is even in the rules of the site:

Do provide evidence of having done some work yourself if posting questions from school or work assignments

So, what do you need help with?

Schol-R-LEA 1,446 Commie Mutant Traitor Featured Poster

Put a while() loop around it.

Schol-R-LEA 1,446 Commie Mutant Traitor Featured Poster

A lot of the answer to this is a matter of perspective, to be honest. In the end, all programs come down to machine code, and assembly language is a direct analog to machine code (mostly). However, that's not far different from saying that all matter comes down to electrons, protons and neutrons - it is true, and understanding it important, but for most purposes it is too low-level to be relevent.

The main places where assembly is directly relevant today is in the implementation of compilers, in the writing of system software such as device drivers, and - this is the important one - in debugging natively compiled code. The ability to debug at the assembly level is actually quite useful, and is too often overlooked these days. It shows you exactly what is happening inside the movement of data, which can save a great deal of effort if you know how to do it.

I would argue that assembly language is less important for its immediate applications than it is for the insight it gives to the way languages actually work - sort of like learning Latin in order to get a better grip on the Romance languages. I do recommend it, as it will make you a better programmer if you learn at least enough to read a program listing, but it is not for everyone, and it is by no means a necessity for the overwhelming majority of applications programmers.

I would add that I would …

Schol-R-LEA 1,446 Commie Mutant Traitor Featured Poster

You should be able to overload the function such that it will use one with a constant function and another with a non-const function. First, let's simplify the existing functions a bit and make them more modern:

// these two utility functions should be private and inline, 
// but I'll write them like this for now.

int BitVector::byte_index(int index)
{
    return index >> 3;
}

uint8_t BitVector::isolate(int index)
{
    assert(index >= 0 || index < size);
    return vector[byte_index(index)];
}

// Returns the bit at the specified index.
uint8_t BitVector::at(int index) 
{
    return static_cast<uint8_t>((isolate(index) >> (7 - (index & 0x7))) & 1);
}

// Replaces the bit at index with that specified by value.
void BitVector::set(int index, uint8_t value)
{
    assert(value == 0 || value == 1);
    uint8_t byteValue = static_cast<uint8_t>(isolate(index) | (1 << (7 - (index & 0x7))));

    vector[byte_index(index)] = byteValue & 0xff;
}

uint8_t& BitVector::operator[](int index) const
{
    uint8_t rtn_value = at(index);
    return static_cast<uint8_t&>(rtn_value);
}

BvHelper BitVector::operator[](int index)
{
    return BvHelper(this, index);
}

Now add a class BvHelper that handles the getter and setter cases, as described here:

class BvHelper 
{
private:
    BitVector& vec;
    int index;

public:
    BvHelper(BitVector& bv, int idx): vec(bv), index(idx)
    {}

    uint8_t& operator int () const
    {
        return vec.at(index);
    }

    uint8_t& operator = (int value)
    {
        return vec.set(index, value);
    }
}

You may need to tinker with this a bit.

ddanbe commented: deep knowledge +15
Schol-R-LEA 1,446 Commie Mutant Traitor Featured Poster

As an aside, assuming you are using ASCII character encoding (or something else with digits in order, such as UTF-8), you can simplify the section in lines 265-282 as:

if (isdigit(k[1]) && k[1] != '0')
    num = k[1] - '1';

It isn't a huge improvement, but it should make it more readable. You'll need to #include <cctype> at the start of the source file. If you actually do want it to set it when zero, simply remove the second part of the conditional cluase.

Schol-R-LEA 1,446 Commie Mutant Traitor Featured Poster

I wouldn't assume so, no. There's nothing in the standard specifying atomicity, for that or (AFAIK) anything else in C. Even if it is an atomic operation on a specific processor and with a specific compiler, it is not certain to be on others. To the best of my knowledge, there is no portable way to write any form of mutual exclusion entirely in C, or any other language for that matter (while some languages have built-in support or library support for mutex primitives, that sort of thing relies on low-level access to the processor by the language translator and/or the library writer).

In any case, the likelihood of it being implemented in an atomic fashion is very low. In the scenario you describe, it would (on any processors I know of) in the general case take at least three steps to perform: a pointer dereference, a bit set operation (probably AND), and a store back to the dereferenced memory location.

While the x86 architecture has some instructions that are guaranteed to be atomic (with the LOCK modifier), there is no (portable and reliable) way to ensure that the compiler will use them.

The practical upshot of this is, no, that isn't going to be atomic without blocking, and on a modern multicore/multiprocessor system, without memory bus locking as well.