Alex Edwards 321 Posting Shark

I'm fairly confused as to why you use an inventory4 class than an Inventory4 class... It just adds to the confusion.

I'd also like to know how you got that compiler error. What line did you attempt to use Inventory5 (or Inventory5_5 ?)

Alex Edwards 321 Posting Shark

I looked up the method class and thought that it might be useful, but unfortunately Method objects must be public to be invoked.

I'm just curious of when I'd actually really need to use the Method class. Maybe to create an array that used a type of algorithm to fire different methods?

Sorry if this is too vague. I'm just really curious.

Alex Edwards 321 Posting Shark

Multiple Choice: What is the value of the pointer after this syntax--

int array[] = {10, 20, -10, -20, 5}, *p = array;
p = &p[2];
array[1] = 5;
p++;

A) 10
B) 20
C) -10
D) -20
E) 5

-------------------

Multiple Choice: What is the difference between--
// information about pointers int x[] = {1, 2}, y[] = {3, 4}, *p = x, *q = y;

*p++ = *q++;
p++ = q++;

A) The first line increments the values and copies the values from q to p whereas the second line increments the addresses and assigns the address of q to p

B) The first line copies the values from q to p then increments the address whereas the second line assigns the address of q to p and then increments the address pointed by both pointers.

C) The first line increments the addresses and copies the values from q to p whereas the second line increments the addresses and assigns the address of q to p

D) The first line assigns the value of q to p then increments the values of q and p whereas the second line assigns the address of q to p then increments the address pointed by both pointers.

E) There is no difference

Alex Edwards 321 Posting Shark

Hi guys,

This may well have been covered in another post and, if so, feel free to direct me to that one. Also, I hope that I am in the correct section!

I am approaching the end of my semester and face the upcoming exams. Obviously most of the work for computing is achieved by doing the homework throughout the semester and learning as you go, which I hasten to add I have done! However, I would also like to perform really well in the exam and demonstrate what I know. Therefore, my question has two parts:

1 - What strategies do you recommend in preparation for these kind of examinations? This may sound a little vague, but what I mean is: hardest topics first, last or mix it up? suggestions for breaking down the structure of a session. Also, any hints for good exam technique would be greatly appreciated, eg. do you do easy questions first or just standard order?

2 - I hope this is not an inappropriate question, but are there collections of questions to practise and, if so, is this ok? For example, things like: write an iterative function that finds the max value in an array, write a recursive function which does y. I would love to be able to just practise basic functions to really drill the concepts into my head.

Thanks very much for your time, guys. I hope that this sort of stuff is not a problem to ask. I really am …

Alex Edwards 321 Posting Shark

The below logic is as such--

*declares a static variable to hold the current max argument.
*compares the argument with an indice in the array and replaces the static variable with a value only if it is greater
*process continues through the array recursively

#include <cstdlib>
#include <iostream>

using namespace std;

int maxArray( int anArray[], int n )
{
	static int temp = anArray[0];

	if(n == 1)
	{
		int beta = temp;
		temp = 0;
		return beta;
	}
	else if(n == 0)
	{
		cout << "Improper arg! size cannot be zero!" << endl;
		return 0;
	}
	else
	{
		if(anArray[n - 1] > temp)
			temp = anArray[n - 1];

		return maxArray(anArray, n - 1);
	}
}

int main(int argc, char *argv[])
{
    int myArray[] = {1, 9, 2, 5, 10, 99, 54, 7, 2};    
    
    cout <<"Max value in myArray is: "<< maxArray(myArray, 9) << endl;
    
    cin.get( );
    return EXIT_SUCCESS;
}
Alex Edwards 321 Posting Shark

I just briefly glanced your code and noticed an error...

if( anArray[n] > anArray[n] )

Why are you comparing the same element to itself? And also why are you returning the same element in either case?

Your line should look like--

if( anArray[n] > anArray[n+1] )
return anArray[n]
else
return anArray[n+1]

However this is only assuming that you have some kind of rule for n (what is n supposed to be? the size of the argument? The last indice? It's not clear! )

Alex Edwards 321 Posting Shark

I figured as much.

This project reminds me of the game "doodlebugs" except I had to perform that code in Java.

Alex Edwards 321 Posting Shark

Seems like you'll either have to suffice with while loop that has some kind of waiting case, or you can learn to use threads.

I don't know if there is a built-in timer to pause a process (such as a method) being read for a set amount of milliseconds, or any time argument. If one exists I'd use that.

Alex Edwards 321 Posting Shark

I need a few answers before I can help you further.

First, how is the object moving? User input with keyboard (like pressing the right arrow key will make the object shift to the right, etc)?

If this is the case what I'd do is "catch" the users input (such as, the key they used) and figure out how this effects the values of your object.

For example, if the boards dimensions are 7x9 (7 rows down, 9 columns over) then it would look like this

---------
---------
---------
---------
---------
---------
---------

and an object within it can "visually" move around when I use a key command. If I press the right button then the column property of the object is incremented and the board is normally updated with dashes but includes the update for the object itself.

Since you know the dimensions of your object, you can probably treat it like a boxed-object (an object that exists in some kind of invisible bordering) and restrict movement if--

->the objects current position on the screen, plus its height (or length) does not exceed the board.

i.e...

---------
---------
---------
-----*---
-----**--
-----***-
---------

this triangle can move one more time to the right. It's position is (according to your syntax) Shape::the_board.area[y][x] where [y][x] is the dash right above the first asterisk. This has no character value but …

Alex Edwards 321 Posting Shark

Wouldn't using a preprocessor definition work?

For example--

#include <cstdlib>
#include <iostream>

#define COLUMN(arg) arg

using namespace std;

int main(int argc, char *argv[])
{

    typedef int column[5]; //anything marked with column before its variable name will be
                                     //an int array of size 5
    
    column COLUMN(a); //an int array of size 5, variable name a. Using preprocessor definition
                                  //so a is still associated with the word column
    column COLUMN(b); //an int array of size 5, variable name b... 
    column COLUMN(c); //an int array of size 5, variable name c...
    
    COLUMN(a)[0] = 1; //using these as examples for proof that the array definitions were legitiment
    COLUMN(b)[0] = 2;
    COLUMN(c)[0] = 3;

    cout << COLUMN(a)[0] << endl; //further debugging to test the typedef and preprocessor macros
    cout << COLUMN(b)[0] << endl;
    cout << COLUMN(c)[0] << endl;

    cin.get( );
    return 0;
}

--but you might have to write a ridiculous amount of variables which would make this seem redundant.

Then again if it's easier for you to reference the variable in a familiar manner, like this one, then I suppose it's alright.

Alex Edwards 321 Posting Shark

No problem, but I don't believe I really posted a solution.

You'll have to do some kind of comparison to ensure that the starting amount of the triangle, and the ending (the "tips") are recognizable when entering parameters for the triangle.

Either that or use a cstring object/function that performs visual spacing so that the triangle looks more like a triangle.

Another suggestion would be to add a starting case and ending case for the triangle, so that when you begin creating the triangle (and finish), tips are ensured to show.

By using my program if you enter something like 2 for height and 9 for base you'll get a funky looking triangle. I doubt that's something you want to suffice with.

Instead have the first and last call be something like (first line must have at most (1/5)base amount of stars.

Alex Edwards 321 Posting Shark

In C++ main has to return an int (or possibly some kind of comprehensible value), not void.

#include<iostream>
int main()
{
char cReply;
int iNum, iSquare;
std::cout << "Do you want to find the square of a number (y/n)?";
std::cin >> cReply;
while(cReply == 'y')
{
std::cout << "Enter a number:";
std::cin >> iNum;
iSquare = iNum * iNum;
std::cout << "The square of "<<iNum<<"is"<<iSquare<<std::endl;
std::cout << "Do you want to find the square of another number (y/n)?";
std::cin >> cReply;
}

return 0;
}
Alex Edwards 321 Posting Shark

I also can't believe how badly I goofed with using char* instead of char... No wonder the compiler was looking for a char** in some occassions, it was replacing T *value with char **value and expecting that type of argument.

Alex Edwards 321 Posting Shark

Hey thanks a million for the code-edit, but I was really hoping for an explanation of why I'm receiving the error.

The reason I use preprocessor macros is because it really short-lines long winded code. I didn't realize that using __typeof__(element) was a nonstandard c++ convention.

I thought the way Java defined instanceof was based off of __typeof__(e) which is the reason I thought there would be no harm in using it to label the actual type of the element for easier macroing.

I'm actually greatful that you took the time to edit the code, but I really wish for some type of explanation for my elusive error...

Alex Edwards 321 Posting Shark
#include <cstdlib>
#include <iostream>

using namespace std;

#define INPUT(a) cin >> a
#define PRINTLN(a) cout << a << endl
#define PRINT(a) cout << a << flush

class Triangle
{
      private:
             int *height, *base;
             
      public:
             Triangle(){PRINTLN("Enter the height"); height = new int; INPUT(*height); PRINTLN("Enter the base"); base = new int; INPUT(*base);};
             Triangle(int &h, int &b){height = new int(h); base = new int(b);};
             ~Triangle(){delete height; delete base;};
             void display();
             double area(){return (.5 * (*height) * (*base));};
};

void Triangle::display()
{
     
  if((*height) < (*base))
  {
      for(int i = 0; i <= (*height); i++)
      {          
           for(int x = 0; x < (static_cast<double>(*base)/(*height)) * i; x++)
                 PRINT("*");
                 
           PRINTLN("");
      }
  }
  else
  {
      for(int i = 0; i <= (*base); i++)
      {          
           for(int x = 0; x < (static_cast<double>(*height)/(*base)) * i; x++)
                 PRINT("*");
                 
           PRINTLN("");
      }
  }   
     
};

int main(int argc, char *argv[])
{    
    Triangle *t = new Triangle();
    
    t->display();
    PRINTLN(t->area());
    
    t->Triangle::~Triangle();
    delete t;

    system("PAUSE");
    return EXIT_SUCCESS;
}

--Basically I used the Whiever-is-greater / whichever-is-lower ratio method to determine the shape of the triangle.

Hopefully this helps XP

Alex Edwards 321 Posting Shark

In times like this I'd either write pseudo-code or a process of how I want the code to work.

such as...

//The maximum amount of stars printed is the argument the user/programmer decides
//the stars stack onto each other until they reach max value, then descend to zero

knowing this, you should be able to write code to display the asteriks.

For the ascending cycle...

int VALUE = /*enter value here*/;

for(int i = 0; i < VALUE; i++)
{
   cout << "*" << flush;
}

but this isn't enough, we're simply printing the asterik out VALUE times. We need to make it seem like a stack.

Notice that the diamonds increment in a dimensional manner--

(0)
* (1)
** (now 3)
*** (now 6)
**** (now 10)

for each "row" there are an equal amount of asteriks.

for(int i = 0; i < VALUE; i++)
{
     for(int j = 0; j < i; j++)
        cout << "*" << flush;

     cout << "" << endl;
}

This would solve the ascending problem. Now for descending you'd have to do something similar, but in reverse. Plus you can't use the less-than-VALUE expression because you can't print the max row twice so...

for(int i = (VALUE - 1); i > 0; i--)
{
     for(int j = 0; j < i; j++)
        cout << "*" << flush;

    cout << "" << endl;
}

would complete the diamond

Alex Edwards 321 Posting Shark

Hello, I'm having an issue with a line of code that really doesn't seem that it should be giving me an error.

Before posting, I've googled this error and looked at related links with no help whatsoever. Most of the "solutions" were for correctly-placed brackets or different names (since some names were accidentally reused from the library, or were keywords). Anywho, here's the code--

#include <cstdlib>
#include <iostream>

#define TYPE(a) __typeof__(a)
#define ID(a) typeid(a).name()
#define ID_SAME(a,b) typeid(a).name() == typeid(b).name()
#define SWAP(a,b) TYPE(a) temp; temp = a; a = b; b = temp
#define PRINTLN(a) cout << a << endl
#define DO_SORT(varName, a, b, c, d) (*(varName)).sortValues<TYPE(a)>(a, b, c, d)

using namespace std;

template<class T>
class MySort
{
      private:

      public:
             MySort(){};
             ~MySort(){};
             void sortValues(T *value, int start, int size, void (*functocall)(void*, void*));
             //This method accepts a user-defined function and sorts the value.
             //The pointer is expected to have more than just one value, so it will be an array of some type.
             
              bool isSorted(T *value, int start, int size)
              {
                   for(int i = start; i < size - 1; i++)
                   {
                        if(value[i] > value[i + 1])
                           return false;
                   }
                   
                   return true;
              };             
};

template<class T>
void MySort<T>::sortValues(T *value, int start, int size, void (*functocall)(void*, void*))
{
    if(start < size)
    {
       while(!isSorted(value, start, size))
       {
            for(int i = start; i < size - 1; i++)
            {
               void *left = (value + start), *right = (value + (start + 1));
               (*functocall)((left), (right));
            }
       }
    }
    else 
       PRINTLN("Start point cannot be greater …