Labdabeta 182 Posting Pro in Training Featured Poster

First of all the code for (a=4; a<5; a=a++) is undefined behaviour, you probably meant for (a=4; a<5; a++) which 'loops' a from 4 to 5 (essentially just running the loop once on the value a=4.

To use a value found inside the loop outside of it, it must be declared outside of it. Therefore your intended code was likely:

while (1) {
    <type> result;
    for (...) {
        ...
        result = ...;
    }
    //use result here all you want
}
Labdabeta 182 Posting Pro in Training Featured Poster

In your current code you populate the array with sc.nextInt(), now you want to populate them with random numbers. There is a method called Math.Random() which gives you a random number from 0 to 1 (including 0, not including 1).
You will have to 'stretch' the number you get to the right width (1-20) like so:

MatX[i][j]=(int)(Math.Random()*<Max-Min Here!>)+<Min Here!>;
Labdabeta 182 Posting Pro in Training Featured Poster

In java, x<<y shifts x y bits to the left. x>>y arithmetically shifts x y bits to the right. x>>>y logically shifts x y bits to the right.

These operations are invaluable when you need to manipulate individual bits of a value.

For example, lets say you are using a bitmask to store 10 booleans in one integer. You can use | (or) to 'set' a bit, & (and) to 'clear' a bit, and ^ (xor) to swap a bit. However, to actually use these operations you would want to have access to the bits. You could do this manually: x|0x10 to set bits 2 and 4. However, bitshift operators let you do this generically: x|(1<<2)|(1<<4) also sets bits 2 and 4. As you can tell, the second version lends itself very nicely to being put into a method, while the first version does not.

There are many more uses of the shift operators in some advanced algorithms, since many of them use the fact that any number has a binary representation to decompose the number into its components (by stripping 1's and bitshifting right). For example you can fairly quickly calculate the remainder of a number by using the method of squaring.

Labdabeta 182 Posting Pro in Training Featured Poster

The issue is that localtime and localtime_s are slightly different. The _s stands for safe, it is a memory safe version of localtime. As such it does not create it's own tm* to return, rather you have to send it one. The correct code would be:

time_t now = time(0);
tm ltm;
localtime_s(&ltm,&now);
Labdabeta 182 Posting Pro in Training Featured Poster

This is one of those sneaky bugs. Basically it is telling you that if you declare a variable in the for loop, it only lasts as long as the for loop. At first glance it looks like you only ever use 'n' inside your loop. You are wrong. Let me re-write your code with some different whitespace-ing and you will see why:

// loop to generate audio samples
for(int n=0; n < length; n++) //where n is the step |Start of For loop
    int t = n/freq; //t/freq                        |End of For loop


{//random unnecessary block
    value = amp*sin(2*pi*freq*t);
    write_CSV(n, value, fp);
    cout << n << "\t" << value <<"\n";
}

The reason for this is that loops, and if statements, only last for 1 statement (IE: until the first semi-colon). This would make them really hard to use, except that C++ provides Blocks. Basically any time you say {/*code here*/} it takes all of your code and turns it into one statement. This is true of every time you use the curly braces (as far as I know). However functions often still require them (my compiler doesn't, but ideone does).

Thus the fix is simple: move line 22 into the block:

// loop to generate audio samples
for(int n=0; n < length; n++) //where n is the step |Start of For loop
{
int t = n/freq; //t/freq
value = amp*sin(2*pi*freq*t);
write_CSV(n, value, fp);
cout << n << "\t" << value <<"\n";
} //|End of For loop …
ddanbe commented: Fine eye! +15
Ancient Dragon commented: nice explanation +14
Labdabeta 182 Posting Pro in Training Featured Poster

You have 1 syntax problem and a few redundancy problems. First lets tackle the syntax problem so that you can get your code to run.

The syntax error is that void distance(miles) is not valid C++ code. When you have parameters in a function it must have a type. What you want is void distance(double miles) which is exactly what you wrote in the class description.

This leads to the next issue, namely that you are going to end up asknig the user for the miles variable twice. Instead you want to pass miles into the function, like vmanes said. Thus your fixed distance function is:

void distance(double miles) {
    fuelLevel-=miles/mpg;
}

Finally, there is a redundancy in your constructor. While it works fine, it is very silly. Here is your constructor:

Car::Car(double mpg, double capacity): mpg(mpg), fuel_capacity(capacity){
mpg = mpg;
fuel_capacity = capacity;
fuelLevel = 0.0;
}

Which is essentially the same as:

Car::Car(double mpg, double capacity): mpg(mpg), fuel_capacity(capacity){
//:mpg(mpg) becomes:
mpg = mpg;
//:fuel_capacity(capacity) becames:
fuel_capacity = capacity;
//now the next two lines are redundant:
mpg = mpg;
fuel_capacity = capacity;
fuelLevel = 0.0;
}

A better constructor is:

Car::Car(double mpg, double capacity):  mpg(mpg), 
                                        fuel_capacity(capacity),
                                        fuelLevel(0.0)//you can set these to values too
                                        {/*no function body at all!*/}

Hope this helps!

Labdabeta 182 Posting Pro in Training Featured Poster

You have 1 syntax problem and a few redundancy problems. First lets tackle the syntax problem so that you can get your code to run.

The syntax error is that void distance(miles) is not valid C++ code. When you have parameters in a function it must have a type. What you want is void distance(double miles) which is exactly what you wrote in the class description.

This leads to the next issue, namely that you are going to end up asknig the user for the miles variable twice. Instead you want to pass miles into the function, like vmanes said. Thus your fixed distance function is:

void distance(double miles) {
    fuelLevel-=miles/mpg;
}

Finally, there is a redundancy in your constructor. While it works fine, it is very silly. Here is your constructor:

Car::Car(double mpg, double capacity): mpg(mpg), fuel_capacity(capacity){
mpg = mpg;
fuel_capacity = capacity;
fuelLevel = 0.0;
}

Which is essentially the same as:

Car::Car(double mpg, double capacity): mpg(mpg), fuel_capacity(capacity){
//:mpg(mpg) becomes:
mpg = mpg;
//:fuel_capacity(capacity) becames:
fuel_capacity = capacity;
//now the next two lines are redundant:
mpg = mpg;
fuel_capacity = capacity;
fuelLevel = 0.0;
}

A better constructor is:

Car::Car(double mpg, double capacity):  mpg(mpg), 
                                        fuel_capacity(capacity),
                                        fuelLevel(0.0)//you can set these to values too
                                        {/*no function body at all!*/}

Hope this helps!

Labdabeta 182 Posting Pro in Training Featured Poster

The issue is that you set charges=number*PRICE_CONSTANT each time, without saving the old value. Basically here is what happens if you trace charges (program tracing is a very valuable debugging tool):

Charges: ??? (since its un-initiated it could be anything, changing line 24 to double charges=0.0; would fix this)
Choice: Coffee, number: 2
Charges: 2.00 (charges=number*COFFEE;//=2*1.00=2.00)
Choice: Hot Chocolate, number: 1
Charges: 1.25 (note that the old value of 2.00 is gone)
Choice: EXIT
Charges: 1.25, TOTAL: 1.25

The solution is thus to add charges to total as you create them. Moving line 77 to just after line 71 should fix your problem.

Labdabeta 182 Posting Pro in Training Featured Poster

I am not sure exactly what you mean by #2, but I can tell you about #1. The difference is that, as far as I know, you cannot create an array variable without setting it to point to a value. Specifically when you say int a[5]={1,2,3,4,5}; it sets a to point to the address where the 1 is stored, while ensuring that 2,3,4,5 are in the following address spaces. If you were to then move where a points you would have an issue because you would lose access to your original array (and I am not sure if C would clean it up for you). However, in the context of function parameters arrays and pointers can be used almost interchangeably. For example:

void arrayTest(int a[])
{
    a[0]=7;
}

Is the same as:

void pointerTest(int *a)
{
    *a=7;
}

Just like:

int arrayAccess(int a[])
{
    return a[7];
}

Is the same as:

int pointerAccess(int *a)
{
    return *(a+7);
}

And of course a[7] and *(a+7) can be interchanged.

So the reason we declare pointers seperately is two-fold:

1) It doesn't automatically create the object we wish to point to.
2) It describes what the variable is, making code more legible.

Labdabeta 182 Posting Pro in Training Featured Poster

The issue is that char is an integer type, so a+b will add the ASCII values of a and b, which gives weird output. Converting them to strings works fine, except that strcat uses char* c-style strings and you used std::string c++ style strings.

Here you have 2 options:

Option A: C-Style strings

char result[3];//a,b,NULL
result[0]=a;
result[1]=b;
result[2]=0;//null terminate

cout<<result<<endl;

Option B: C++-Style strings

string c=(string)a;
string d=(string)b;

cout<<c+d<<endl;

For obvious reasons the C++ style is usually considered better. However the c-style version likely takes less memory. Both should work though.

Hope this helps!

Labdabeta 182 Posting Pro in Training Featured Poster

Okay, here goes:

//                  A | B | C 
TOH(3,A,B,C)//     123|   |   
{
    if (3==1)
    {//it doesn't matter what's in here anyways
    }
    TOH(3-1,A,C,B);//note that B,C got switched
    //the above actually moves 3-1=2 disks from A to C
    //              A | B | C
    //              3 |   | 12
    //we will look at how it does this next
    move from A->B;// | 3 | 12
    TOH(3-1,C,B,A);//note that A,C got switched
    //the above actually moves 3-1=2 disks from C to B
    //              A | B | C
    //                |123|
    //ta-da!
}
//now we need to look at TOH(2,A,C,B) [line 7]
//                  A | C | B <-- note the new order!
TOH(2,A,C,B)//      12|   | 
{
    if (2==1)
    {//it doesnt matter
    }
    TOH(2-1,A,B,C);//note that C,B got switched
    //the above actually moves 2-1=1 disk from A to B
    //              A | C | B
    //              2 |   | 1
    //We will also look at this later
    move from A->C;   | 2 | 1
    TOH(2-1,B,C,A);//note that order is reversed
    //the above actually moves 2-1=1 disk from B to C
    //              A | C | B
    //                | 21|
    //ta-da!
}
//now we also need to look at TOH(2,C,B,A) [line 13]
//however it should be clear that it will work exactly like the ones above
//so we can be pretty sure that it WILL correctly move 2 disks from C to B

//finally lets look at TOH(1,A,B,C) [line 26]
TOH(1,A,B,C)//<---Read: Move 1 disk from A to B
{
    if (1==1)
    {
        move from A->B;//yay! …
Labdabeta 182 Posting Pro in Training Featured Poster

Its actually not changing num at all. Instead it creates a brand new version of num in the next function call, starting from scratch. You can picture recursive functions as infinite code like this:

void printNum(int num)
{
    cout<<num;
    if (num<9)
    {
        printNum(num+1);
    }
    cout<<num;
}

//is basically the same as:
void printNum(int num)
{
    cout<<num;
    if (num<9)
    {
        //this is what printNum(num+1) does:
        int num2=num+1;
        cout<<num2;
        if (num2<9)
        {
            //this is what printNum(num2+1) does:
            int num3=num2+1;
            //etc...
        }
    }
    cout<<num;
}

This is because each function call is isolated from the others. A decent example is a recursive fibonacci example. I assume you know the fibonacci sequence. It is defined as f(x)=f(x-1)+f(x-2) and of course f(x-1)=f((x-1))=f((x-1)-1)+f((x-1)-2), etc. So a common recursive implementation of the fibonacci sequence is:

int fibonacci(int x)
{
    if (x<2)//f(1)=f(0)=1
        return 1;
    return fibonacci(x-1)+fibonacci(x-2);
}

In this example the execution for fibonacci(3) is:

fibonacci(3){
    if (3<2)
        return 1;//nope
    return fibonacci(3-1)+fibonacci(3-2);
}
//now we have called fibonacci(3-1) = fibonacci(2) so we have to find out what that is:
fibonacci(2){
    if (2<2)
        return 1;//still nope :C
    return fibonacci(2-1)+fibonacci(2-2);
}
//now we have called fibonacci(2-1) = fibonacci(1) so we have to find out what that is:
fibonacci(1){
    if (1<2)
        return 1;//yay!
    //we never get here anyways!
}
//so fibonacci(1)=1, so fibonacci(2)=1+fibonacci(2-2)
//now we have called fibonacci(2-2) = fibonacci(0) so we have to find out what that is:
fibonacci(0){
    if (0<2)
        return 1;//yay still
    //again, we dont care about what is here
}
//so fibonacci(0)=1, …
Labdabeta 182 Posting Pro in Training Featured Poster

This is a classic problem in computer science. Basically it is a neat case of induction.

For towers named A,B,C:
Start with a tower of just 1 disk. Transferring it is as easy as moving it from A->C.

If you have more than 1 disk its a bit more complicated. Start my moving all but 1 disk to the extra peg. How? By following these same instructions! Next move the biggest disk onto the final peg. Next move all the other disks from the extra peg to the final peg. This can also be done by following the same steps. Now you are done!

The key is that you change which peg is the initial, final, and extra peg, then keep recursively moving smaller towers until all you need to do is move towers of 1 disk, which is easy!

This is exactly what your code does.

1 disk: if only 1 disk, then move it.

if (d==1)
{
    cout<<"\nShift top disk from tower"<<tower1<<"to tower"<<tower2;
    return;
}

n+1 disks:
Step 1: move n disks to extra tower (tower3), while doing this the final tower (tower2) acts as the extra tower.

TOH(d-1,tower1,tower3,tower2); //TOH(n,initial,final,extra)

Step 2: move 1 disk to final tower (tower2)

cout<<"\nShift top disk from tower"<<tower1<<"to tower"<<tower2;

Step 3: move n disks from the extra tower (tower3) to the final tower (tower2) using the initial tower (tower1) as the extra.

TOH(d-1,tower3,tower2,tower1);

So for d=3 you have: (using A,B,C as the …

ddanbe commented: For the effort :) +15
Labdabeta 182 Posting Pro in Training Featured Poster

You cannot change a function's behaviour on an object, only on a class. To get the exact functionality you showed you would have to use a function pointer as a member of the test class:

class test
{// private:
    public:
    event<>(*Printed)();
    test()
    {
        Printed=[]{;}//I have yet to take a good look at the syntax for this
    }
};
test a;
a.Printed=[]{cout<<"\nMessage Printed\n";}

However this is not what people are usually referring to when they use polymorphism. Polymorphism is almost always used to represent the "is a" relationship where something changes. For example a square "is a" rectangle so this is proper practice for a square and rectangle class:

class Rectangle
{// private:
    int width,height;
    public:
    virtual int getArea(){return width*height;}
};
//time for polymorphism (technically here it is just inheritance)
class Square:public Rectangle
{// private:
    public:
    virtual int getArea(){return width*width;}
};

Because of the virtual label the following code would execute the following functions:

Rectangle r;
Square s;
Rectangle *rp;
Rectangle *rps=&s;//valid!
//Here width*height is called, as you would expect
r.getArea();
//Here width*width is called, as you would expect
s.getArea();
//Here, you still get width*height
rp->getArea();
//Here, since getArea was virtual, you get width*width
rps->getArea();

So basically to override a function in a new CLASS it is as easy as writing a new version of the function (virtual keyword is optional unless you deal with pointers to base classes). However overriding a function in a new OBJECT is very difficult.

cambalinho commented: thanks for try +2
Labdabeta 182 Posting Pro in Training Featured Poster

Yes. C++ has inheritance as well as generic types. This gives it full objective polymorphism. Polymorphism is actually one of the things for which C++ is famous http://lmgtfy.com/?q=C%2B%2B+Polymorphism .

Labdabeta 182 Posting Pro in Training Featured Poster

It is completely situational. Here is an example:
1000001

What is that number?
If you interpret it as a 7-bit unsigned number you get 65.
If you interpret it as a 7-bit sign/magnitude number you get -1.
If you interpret it as 1's complement you get -63.
If you interpret it as 2's complement you get -64.
If you interpret it as 8-bit 2's complement (very common) you get 65.
If you interpret it as an ASCII character you get 'A'.
If you interpret it as BCD (binary coded decimal) you get 41.

Basically you can represent anything which is countable using only 1s and 0s, and similarly every set of 1s and 0s can represent any number of things. The only way to know which interpretation to use is by situation.

In any given assembly language they will usually tell you exactly what things mean. Incidentally, once you assemble your code in assembly it will be turned into another number. For example in MIPS assembly (as described by my professor) you have:

add $1,$2,$3 = 0000 0000 0100 0011 0000 1000 0010 0000

The key is that n bits can represent 2^n possible 'thing's you need to define what 'thing' you want them to represent. In the case of 1s or 2s complement you just look at the first bit, if it is 1 the number is negative:

1011 in 4 bit 2's complement = -5
1011 in 8 bit …

ddanbe commented: Good advice. +15
Rahul47 commented: That was helpful. . . thankx +3
Labdabeta 182 Posting Pro in Training Featured Poster

In my programs I usually only encounter 3 types of epic one liners, they all use the ternary conditional operator extensively.
(all in c++)

Type 1: Conditional arithmetic - When I want to change how an equation behaves based on conditions. Example taken directly from the add function of my integer library: (i<s?v[i]:0)+(i<o.s?o.v[i]:0)+(carry?1:0).

Type 2: Inline switch statements - Any switch statement can be made inline with the ternary operator.

switch (x)
{
    case A:doA();break;
    case B:doB();break;
    case C:doC();break;
    default:doD();break;
}

becomes:

(x==A?doA():(x==B?doB():(x==C?doC():doD())));

Type 3: Complicated bitshifty stuff, typically to create inline hash functions. I can't think of a good example off hand, but they end up looking really cool.

However, I believe epic one liners are generally considered bad practice. That is why I try to keep a comment above each one that describes what the one-liner does.

Labdabeta 182 Posting Pro in Training Featured Poster

Now that I see it on a bigger screen the layout is growing on me. Just a comment, not sure whether to put in bugs or here, but ads underneath the transparent banner at the top come to front if you mouse over them, so it is hard to get at the menus. I'm honestly not sure if the transparent banner is worth it. It is a major contributor to the cluttered feel of the site right now. Granted it turns opaque when you mouse over it, but leaving it opaque all the time would look nicer IMO.

Labdabeta 182 Posting Pro in Training Featured Poster

I have a couple issues with the current layout.

1) Small version
When the browser is in maximized mode all of these issues are gone, however I always have my browser snapped to one side of the screen. When it is like this I lose access to "Last Updated by" and the dropdown menus, and other nice stylistic things. I think that there should be at least an option to get these back, even when the window isn't maximized.

2) A serious lack of unity
Some things have rounded edges, others are square. There are too many colours. Basically everything is cluttered. A bit of polish and tidying up will help.

Labdabeta 182 Posting Pro in Training Featured Poster

In both Firefox and Chrome, when I snap the window to the edge of the screen I no longer get the drop down menus from the bar at the top. IE: if I mouse over "Software Devolpment" I don't get the option to select a subforum. However, it works if I maximize my window. Is this intentional or a bug?

Labdabeta 182 Posting Pro in Training Featured Poster

That is the correct MANUAL way. However standard headers provide simple easy ways that are likely faster. For example atoi() converts a string like "1234" into an int like 1234 for you. Yes, you could write it yourself like so:

int atoi(char *s)
{
    int ret=0;
    for (int i=0; s[i]; ++i)
    {
        if (s>='0'&&s<='9')//if s is a digit
        {
            ret*=10;//shift
            ret+=s[i]-'0';//and add
        }
    }
    return ret;
}

The benefit of using the standard libraries is that they are fast and guaranteed to work. Both methods work fine, but as far as I know, it is generally considered best practice to use whatever standard libraries make your task easier. (although personally I prefer to rewrite the functions myself just so that I am sure that I fully understand exactly what they do)

In summary: Yes, that is the correct way (although if char x=5 then int i=(int)x; works better, but if char x='5' then int i=(int)(x-'0'); is correct).

Labdabeta 182 Posting Pro in Training Featured Poster

I was actually thinking of a more straightforward approach. Read the whole block into a string (so the string contains, for example, "12:30"). Then remove the punctuation (so "12:30" becomes "1230") then use atoi() or atol() or stringstream() to convert it to your integers. The benefit of this method is that you will not have to shift the numbers at all, and all of your dates/times should be whitespace seperated, which lends itself nicely to fstream or stringstream >> operators. Removing characters from a string is fairly simple:

//don't quote me on this, it is from memory
void removeFromString(std::string &str, char ch)//removes all occurances of ch in str
{
    int index;
    while ((index=str.find_first_of(ch))!=std::string::npos)
        str.erase(index,index+1);
}
Labdabeta 182 Posting Pro in Training Featured Poster

Take a look at the std::(o/i)fstream classes. They have everything you need. Once you have some specific code, we can help determine where you went wrong. Basically you will want to pull the numbers out of the time/dates (basically by ignoring the ./: characters) then translate them into the ints you want. there are many ways to convert strings to integers if you merely search for them (atoi and stringstream come to mind).

Labdabeta 182 Posting Pro in Training Featured Poster

I think you are still getting the errors because you are still trying to use polymorphism. If you use the const AbstractType & (inheritance) technique then you don't even need the <DefinedClass> at all. Also you don't need a pointer! Just this is how you use it:

DefinedClass dc=new DefinedClass;
new FileWritter("filename.file",dc);//it gets referenced automatically!

Also it seems as though in your definition of FileWritter (in FileWriter.h) you didnt define the functions you are trying to use. So here is a translation of your errors:

C4430: You didnt provide a type inside the <> section of your class... we're gonna assume you want ints. (fix this by just not using templates... they aren't correct here!)

C2143: Since it is still waiting on the <> section it gets confused when you throw in an & sign. Same fix as before.

C2509: What are you trying to pull? You didn't tell us you were making a constructor for FileWritter. You can't make an outside of class definition of something not already in the class. Example:

class Test
{
    public:
    void myFunctionA();
};

//This works, because the compiler knows that there is a myFunctionA inside Test
void Test::myFunctionA()
{
    //dosomething
}

//This doesn't work, because the compiler wasn't told in advance that this would be here!
void Test::myFunctionB()
{
    //dosomething
}

C2955: Again... when are you going to give us the <>... we have no idea what we are dealing with!

C2509: Again... you never told us you were …

Labdabeta 182 Posting Pro in Training Featured Poster

What you are asking about is called "type erasure" it is basically the combination of templating and inheriting. Unfortunately C++ does not support type erasure (as far as I know). As such you have to choose between templates and inheritance. Here is an example of each solution:

Inheritance based:

class AbstractClass
{// private:
string exampleData;
public:
AbstractClass();
virtual string getData()const{return exampleData;}
virtual void setData(string &o){exampleData=o;}
};

ostream &operator<<(ostream &left,const AbstractClass &right)//this should work for any class that inherits from AbstractClass
{
    left<<right.getData();
}

class FileWriter//there is only one 'T' in writer
{// private:
    ostream file;
    public:
    FileWriter(string fileName, const AbstractClass &ab){//MUST BE A REFERENCE OR POINTER!
        file.open(fileName.c_str());
        file<<ab;
    }
    ~FileWriter(){
        file.close();
    }
};

And here is the template based solution:

template <class AbstractClass>
class FileWriter
{// private:
    ostream file;
    public:
    FileWriter(string fileName, const AbstractClass &ab){//Abstract class must implement ostream&<<AbstractClass&
        file.open(fileName.c_str());
        file<<ab;
    }
    ~FileWriter(){
        file.close();
    }
};

Here are example uses of each:

Inheritance method:

class myClass:public AbstractClass
{};//normally this would have something in it. And it can modify get/setData to change its output behaviour
myClass mc;
FileWriter fw("myFile.txt",mc);

Template method:

class myClass
{};//normally this would have something in it. And it can modify the following function to change its output behaviour
ostream &operator>>(ostream &left, myClass &right)
{
    left<<"This is a myClass";//for example
}
myClass mc;
FileWriter fw("myFile.txt",mc);
Labdabeta 182 Posting Pro in Training Featured Poster

Well that is the issue with the hare psychopathy checklist. Its a decent way to get an estimate of the psychopathic levels of a person, but it is by no means conclusive. It is far to easy to cheat, and it doesn't necessarily cover everything you need to know about a person to determine if they are a psychopath. The only way to find out if you are a psychopath is to go to a real psychologist and have them run the real tests, which often involve long answers, not just yes/no. I know this because I have been to many psychologists before. As such these checklists are more for getting a ball-park estimate of your chances of being a psychopath than anything else. (There is a similar issue with all the free Meyers-Briggs tests out there, the real one involves an influx of information and then a trained professional sorta 'measures' your processes when confronted with the information).

Labdabeta 182 Posting Pro in Training Featured Poster

Firstly, to clean up your code you should use a multidimensional array... I assume you know that arrays are basically a way to prevent you from doing this (at least statically allocated ones):

int var1;
int var2;
int var3;

Instead they allow you to cobine those variables into:

int var[3];

Now look at your code. This is a section of it:

double exam1[NUMBEROFSUDENTS];
double exam2[NUMBEROFSUDENTS];
double exam3[NUMBEROFSUDENTS];
double exam4[NUMBEROFSUDENTS];
double exam5[NUMBEROFSUDENTS];

Look familiar? The solution to this is to make an array of arrays:

double exam[NUMBEROFSTUDENTS][5];

That solves your problem with the uglyness of your code. Now onto the problem of it not being right. I feel like you are not fully understanding how classes work. The whole point to a class is so that you don't have to send it all those arguments for all of its functions. Instead you can store them inside the class and then the compiler basically passes them for you! This means that this whole section is just wrong:

class student
{
public:
void setGrades(int studentId[], double exam1[], double exam2[], double exam3[], double exam4[], double exam5[], int count);
int getGrades(int studentId[], double exam1[], double exam2[], double exam3[], double exam4[], double exam5[]);
void error(int studentId[], double exam1[], double exam2[], double exam3[], double exam4[], double exam5[], int count);
void average(int studentId[], double exam1[], double exam2[], double exam3[], double exam4[], double exam5[],
double total[], int count);
void newAverage(double exam1[], double exam2[], double exam3[], double exam4[], double exam5[], int …
Labdabeta 182 Posting Pro in Training Featured Poster

Your syntactical error is that max goes out of scope, but you have many more errors that are logical. For instance that for loop will run through and gather all the data into the array. Afterwards i will be 100... so you will only be using the very last thing entered. I am not sure how you would want to fix this. I would put the input into the do-while loop and forget about the for loop completely. Here is your code with the errors highlighted by comment:

    #include <iostream>
    #include <math.h>
    #include <cmath>
    #include <climits>//for INT_MIN, INT_MAX
    using namespace std;
    int main ()
    {
    float sum = 0.0;
    float sub = 0.0;
    float variance = 0.0;
    float mult = 1.0;
    float mean = 0.0;
    float min=INT_MAX,max=INT_MIN;//create min and max here
    int i;//remove this and change all the i's to n's
    float arrayVal[100];
    int n=0;
    cout<<"input your values"<<endl;
    do
    {//note the removed for loop.
    cin>>arrayVal[i];
    sum += arrayVal[i];
    if (n == 0)
    {
    sub = arrayVal[i];
    }
    else
    {
    sub-=arrayVal[i];
    }
    mult *= arrayVal[i];
    //do not recreate max
    if (max>= arrayVal[i])
    {
    max=arrayVal[i];
    }
    //do not recreate min
    if (min <= arrayVal[i])
    {
    min=arrayVal[i];
    }
    n++; // n represents the number of values input by the user
    }
    while (i<100 && arrayVal[i]!=0);
    mean = sum/n;
    for (i=0;i<100;i++)
    {
    variance += pow((arrayVal[i]-mean),2);//using pow for squaring is sorta lame... (arrayVal[i]-mean)*(arrayVal[i]-mean) is arguably better
    }
    float standDev = sqrt(variance);
    cout<<"Sum = "<<sum<<endl;
    cout<<"Sub = "<<sub<<endl;
    cout<<"Mult = "<<mult<<endl;
    cout<<"Max = "<<max<<endl; …
Labdabeta 182 Posting Pro in Training Featured Poster

The point is that energy drinks will undoubtedly contain either caffeine or something that acts like caffeine. This will mess with your circadian rhythms, making you tired more often. Moreover, most energy drinks cause an abrupt increase in heart rate, greatly increasing your chances of getting certain heart issues, such as heart attacks!

Labdabeta 182 Posting Pro in Training Featured Poster

Both of my parents are doctors... here are some tips that work wonders:

1) STAY HYDRATED!!!
2) Try leaning your head to the left, then right. Make your head look like this \/\/\/... do it slowly-ish. This causes your jugular veins to get compressed. The brain, fearing that something is compressing them, takes a large amount of blood in to compensate. This leads to it becoming temporarily more awake.
3) Buy a false sunlight. Especially if you live in extreme latitudes (like me) you can trick your body into thinking it is daytime by shining a bright light that simulates sunlight onto you (not just your eyes, your skin too). If you live in dark countries normally, you should do this during the day as well. Also most of these lights have alarm clocks built in... I know from exprience, it is hard to sleep through a false sunlight shining on your face! (Ill provide a link to one if required)
4) Excersize (I can never spell it right)
5) Stay away from caffeine. First of all caffeine only works if you are well hydrated, if you are dehydrated it will make you tired. Secondly caffeine messes with your circadian rhythms, making you far more likely to be tired at unusual times. Finally caffeine only works for a short while, afterwards you would need more to keep you awake.
6) Get onto a solid circadian rhythm (sleep cycle). Basically set aside a specific time where …

Labdabeta 182 Posting Pro in Training Featured Poster

I was born Wednesday July 13th, 1994... it was Harrison Ford's 52nd birthday (if my math is correct).

Labdabeta 182 Posting Pro in Training Featured Poster

I got 22... but that was with a 0 in the relationship questions, but I have never really been in any serious relationships and I am pretty sure that I would eek a few more points out of those. Also I have been diagnosed with ADHD,OCD, and Aspberger's syndrome... so the test is likely fairly inaccurate for me. I have also been through a lot of psychological testing, so I am pretty sure that if I were a psychopath, one of the psychiatrists would have noticed. :)

Labdabeta 182 Posting Pro in Training Featured Poster

We need to know more... how does your find function work? what is an emp_data object? What is the problem with the code?

Labdabeta 182 Posting Pro in Training Featured Poster

As for when to use && and when to use || you should really learn about boolean algebra. That way you may be able to actually reduce some of your expressions. Basically boolean algebra deals with true/false, 1/0. It has 2 fundamental operators + (or, ||) and x (and, &&). It has many of the rules that normal algebra has. For example a(b+c)=ab+bc. Replace a with for example sign!='+' and b with sign!='-' and you can create logical expressions that do what you want them to do.

Labdabeta 182 Posting Pro in Training Featured Poster

The issue is on line 26. You check if the sign isnt + OR isnt - OR isnt etc.. you want ANDs there. :)

Labdabeta 182 Posting Pro in Training Featured Poster

I think you might be looking for the wrong software, or looking in the wrong places, i.e., using Windows-reflexes when looking for solutions to your problems.

What I meant by complicated is that it takes extra knowledge. I am well aware that installing things on my Ubuntu is far faster and takes far fewer steps than on windows (especially since most software that you would ever need can probably be gotten via the sudo apt-get install SoftwareName line). The issue is that A) you need to have the raw knowledge to know that for example you want gimp, instead of photoshop (not to mention that (at least IMO) photoshop tends to have a few more useful tools than gimp. On top of that I am really talking about when somebody says "Hey you should get <Insert software name here>!" if you want to download that... you may have some issues.

On a serious note, let's target gamers. They're going to be a big driving force in OS switch.

I agree completely. Especially as video games gain respect in the art community gaming is going to get really awesome (if interested in the artistic theory of games [not to be confused with game theory] I would have you watch Penny Arcade's Extra Credits). As it stands right now pretty much every decent game out there is either for a console or windows, with the odd one ported to apple computers. Steam IS available on linux now, but …

Labdabeta 182 Posting Pro in Training Featured Poster

Here is what I have to say. I used a mac all throughout my childhood, then got a windows computer for school and got used to it. Then when I got really into programming I put a linux partition on my laptop. So I have experience with most OSs and I try to have an unbiased view (even though personally I hate crapples). So here we go... Pro/Con list and ideal users:

Apple OSs: Computer N00bs with money

The good thing about apple computers is that, while saying that they are the most user-friendly is quite an exageration, they tend to need very little maintanance. When they do need maintenance apple stores will be able to help, although it can be very hard to find one near you. On top of that Apple OSs tend to come with a lot of default software and easy to get apps. These programs are not as well designed as more expensive ones, but because they give easy access to some basic computer skills novice computer users are capable of doing near-professional grade work. The two biggest downsides I have found are cost and efficiency. Apples are waaay overpriced. As far as I am concerned there is really no argument there... about a year ago I looked at the price for an apple laptop and found that for just the price of the hard drive upgrade I could create the upgraded version with a slightly larger form-factor. The only difference would be size/weight/lack of …

Labdabeta 182 Posting Pro in Training Featured Poster

Your code is incomplete. All you do is read 1 high and 1 low value, then output them... there is no code for anything more. To input more than one value you will need a loop of some sort, and to calculate the average you will need another. Your code isn't working because it isn't done! I feel like maybe you copied the code from somewhere and thus don't understand it, try learning c++ so you can become c++ literate, then you will find that this problem is quite simple. (A good source for learning c++ is learncpp.com)

Labdabeta 182 Posting Pro in Training Featured Poster

The issue is that reverselist as you wrote it expects a reference to a pointer of a nodetype, but you passed it num#.head_ptr which is of type pointer which will be converted to a const reference to a pointer of a nodetype (since num1,num2 are declared const in the function definition). Basically you either need to edit reverselist so that it takes const references (I doubt that will work at all!) or you can edit the binary + operator so that it takes binary references (minus the const).

Labdabeta 182 Posting Pro in Training Featured Poster

Line 113 of your header file does not match its declaration. You have an extraneous array parameter.

Labdabeta 182 Posting Pro in Training Featured Poster

There are a lot of those primes out there, but generating them will undoubtedly be slow... especially if you want the really big ones. I would suggest A) looking into an arbitrary arithmetic library (so that you can handle epicly large numbers) then B) google for lists of large numbers, there are tons of them. You could then store them in an array and randomly pick one from it. Another option is to store an array of ints representing valid exponents for Mersenne Primes, then just generate the corresponding prime.

Labdabeta 182 Posting Pro in Training Featured Poster

You are probably right, you might need to use both... luckily c++ provides fstream.h which is a combination of ifstream and ofstream. If you are just outputting to the screen though you can just use the ofstream object cout provided in iostream.

Labdabeta 182 Posting Pro in Training Featured Poster

How do you intend to compare a string to a single character? An overloaded operator does not seem valid there. Perhaps create some kind of areEqual function instead. My guess is that you have made a logic error in that you are trying to compare two variables of different types (very different types).

111100/11000 commented: I changed from "string plaintext" to "char plaintext" and it works.Thnx +0
Labdabeta 182 Posting Pro in Training Featured Poster

There are a number of issues with your code. The first one is merely for clarity and speed. Basically the isValidCharacter function can be 1 line:
state

return ((c>='A' && c<= 'Z') || (c>='a' && c<='z') || (c>='0' && c<='9') || (c=='.') || (c=='-') || (c=='+'));

Thus removing the if statement.

Your problem though lies in your loops. For example the following loop:

for (s; s<=emails.length; s--)

Unless s is greater than the emails.length this loop will not work as expected. Instead of counting down to 0 (for (s;s>=0;s--) 543210) it counts down to a number greater than it: 543210-1-2-3-4-5...-n+n... basically, until the int type wraps around to be greater than emails.length your loop will not terminate! You correct this using a break statement, but that is considered very poor programming practice!

I have no idea what you are getting at in lines 68 to 72, but I would definately suggest that it is your problem. You should usually not change the iterator character in a loop. If needed make a temporary copy of it instead.

line 78 is redundant, and another bad usage of the break statement. Put it this way, the break statement is only truly valid is switch statements (at least as far as I can think of off the top of my head, I love to be proven wrong).

I would suggest making your code confirm to proper programming pratices and then reposting if you can't solve the problem yourself. You …

Labdabeta 182 Posting Pro in Training Featured Poster

Also it didn't work because it was an example, it just so happens that you type is called Type, but you should have noticed that I was making an example by the variable names. You need to think to program. It is always tempting to copy and paste, but don't do it. Learn the concept, understand it, then apply it. Even if you find a section of useful code that you can copy and paste and it works, you will find yourself constantly searching for the example every time you need it. If you learnt what the example was trying to say then you could recreate it in seconds flat.

Labdabeta 182 Posting Pro in Training Featured Poster

I will help you out with what I can see from a quick glance. #1: Your vector stores generic 'Type' variables, so unless all of your vectors are vector<int> then you need your push_back function to take a Type value. Also you make a critical error that I used to make all the time with dynamic memory allocation. Here is how to do it properly:

Type *tempArray=new Type[arraySize];
for (size_t i=0; i<arraySize; ++i)
    tempArray[i]=sourceArray[i];//up until here we did the same thing
tempArray[arraySize++]=newValue;//this is pretty simple, you increase the array size, but return the old size to the [] operator, basically appending the value AND updating the size variable.
delete[]sourceArray;//we don't need it anymore
//here you re-allocated sourceArray and copied tempArray to it. You do not have to do it
sourceArray=tempArray;//now since the pointers are equal sourceArray will contain the contents tempArray.

You would think that since tempArray is about to go out of scope sourceArray will be invalid... you would be right if it was sourceArray=&tempArray because it is true tempArray will be deleted. The thing is that tempArray stores 1 int, corresponding to the location in memory of the array. That int is also in sourceArray. As such it still holds the same data as before!

Labdabeta 182 Posting Pro in Training Featured Poster

Here is the issue, at least what I understand of it based on a preliminary review of your code. Basically, in the handle_input function, you declare a new event, but you do not set it to anything. It is going to be filled with whatever is left on the stack, or 0 depending on your compiler. What you need to do is somehow send it the same event object that recieved the call to SDLPollEvents. Judging by your style of code, I would suggest making a global sdl event.

While I am on your style of code, I feel inclined to tell you that your extensive use of globals is not generally considered good practice. Instead of a bunch of globals, maybe create a control object like this:

class Control
{
    private:
    //your globals here
    int exampleInt;
    SDL_Event exampleEvent;
    public:
    Control();//here you could set up SDL?
    ~Control();//here you could clean up your mess (close SDL, free surfaces, delete memory, whatever)
    int getInt(){return exampleInt;}
    void setInt(int val){exampleInt=val;}
    SDL_Event getEvent(){return exampleEvent;}
    void pollEvent(){SDL_PollEvent(&exampleEvent);}
};

Hope this helps!

claywin commented: Thanks! This was very helpful! +0
Labdabeta 182 Posting Pro in Training Featured Poster

Print June 2012 to screen would be this, no?

cout<<"June 2012";
Labdabeta 182 Posting Pro in Training Featured Poster

First of all realize how many elements you are storing, which is 115x4x114x114=5 978 160. Each element is a double, which is usually 64 bits (if I remember correctly) so 5 978 160x64=382 602 240 which is 382 megabits. Though this is well within the bounds of modern RAM, do be aware that you are storing quite a lot of data. Next you can use one of three techniques:

Technique 1: Real dynamically allocated multi-dimensional arrays:

This is a tricky one, but basically what you do is you realize that each array is a pointer, your array has 4 dimensions (it is an array of arrays of arrays of arrays of doubles) as such your new array is going to need 4 asterisks! Then you loop through each one useng the new operator to allocate your memory, like so:

double ****array;
//the following loops may be in reverse order, I cannot remember if you do 115-4-114-114 or 114-114-4-115
array=new double***[115];
for (int i=0; i<115; ++i)
{
    array[i]=new double**[4];
    for (int ii=0; ii<4; ++ii)
    {
        array[i][ii]=new double*[114];
        for (int iii=0; iii<114; ++iii)
            array[i][ii][iii]=new double[114];
    }
}

and now you can use array just as if it were as posted above.

Technique 2: Real multi-dimensional vector:

This is a bit less tricky, but do note that > > has a space between the symbols! Basically you make a vector of vectors of vectors of vectors of doubles.

vector<vector<vector<vector<double> > > > array;

Technique 3: Fake multi-dimensional vector + …

Labdabeta 182 Posting Pro in Training Featured Poster

I have made a few compilers for esoteric programming languages (I have no idea why but I find them fun to write :P) and I find that even making a simple compiler can be very tricky, c++ would be near impossible to write. Luckily, you can use g++ from a console command, and as such you can compile your code without writing the compiler yourself. Syntax highlighting is not as tricky, but is still a challenge. I have written one highlighter that turns a c++ source file into an HTML output file that is properly highlighted. I took a string as input, and made an array of the same length of an enum of code states, then wrote a function that runs throught the string and sets the array to have the right highlighting. Here is an example:

#include <iostream>
using namespace std;
int main()
{
    cout<<"Hello World!"<<endl;
    return 0;
}

becomes:

ppppppppppppppppppp
kkkkkkkkkkkkkkkkkkko
kkkcccccoo
o
    ccccoossssssssssssssoocccco
    kkkkkkcno
o

where p=PREPROCESSOR, k=KEYWORD, o=OPERATOR, c=REGULAR CODE, s=STRING LITERAL, n=NUMERIC LITERAL. If you need more help I could give you some source code, but I think you should try to figure it out yourself.