mrnutty 761 Senior Poster

Why don't you just return std::string instead of char* ? Also you need to null terminate result1.

mrnutty 761 Senior Poster

Yes, but if you want to do it that way, you need to do manual allocation.

void foo(string** strs, int width, int height){
 //...
}
int main(){
 int width = 0, height = 0;
 cin >> height >> width;
 string **strs = new string*[height];
 for(int i = 0; i < height; ++i){
  strs[i] = new string[width];
 }
 
 foo(strs, width, height);
}

You might want to skip doing manual memory management and possible use std::vectors

typedef std::vector<string> StringArray;

void foo(std::vector<StringArray>& arrayList){
 //...
}

int main(){
  int height = 0, width = 0;
  cin >> height >> width;
  std::vector<StringArray> myList(height , StringArray(width) );
  
   foo(myList);
}
mrnutty 761 Senior Poster

It depends on the definition. The second one might not even be allowed

mrnutty 761 Senior Poster

>>Btw, if you don't know already, one of the best place to start with OpenGL is by going through the NeHe tutorials.

That's where I started a few years ago when I first started learning opengl, but now looking back, I believe it needs major updates, and the code written is not very good. They also use obsolete opengl functions. There are other up-to-date tutorial one can learn from, or better yet, read the RED book.

And btw, if you know trig/geometry/linear algebra, you should be fine! One would rarely use any higher mathematics.

mrnutty 761 Senior Poster

If your comfortable with the math, you should be fine

mrnutty 761 Senior Poster

Usually, think of lemma as a fact. Its like a theorem but just not as big fact.

mrnutty 761 Senior Poster

What is the exact problem you are trying to solve?

mrnutty 761 Senior Poster

It depends on the size of each cell in the graph. For example, if graph[0][0] is a rectangle of size 1 unit, then when the user presses right, you can move the sprite one unit to the right, and it would now be in graph[0][1]. And similary, if the user presses down, assuming he started at graph[0][0], you can move him one unit down, and now be in the location of graph[1][0] and so on

mrnutty 761 Senior Poster

You can use a try/catch block in the initializer list. Check out this site. Here is the live code as an example.

#include <iostream>

struct BadObject{
 BadObject() {}
 BadObject(int i){ throw("Why you no work 0_o?"); }
};

struct GoodObject
{
 BadObject b;
 GoodObject(int i)
   try : b( BadObject(i) )
   {}
   catch(const char *p)
   { cout << "Runtime Error: " << p << endl; }
};

int main(){
 GoodObject obj(0);

 return 0;
}
mrnutty 761 Senior Poster

Try creating simulations.

- Waves simulation
- Orbital simulations
- Pathfinding simulation

mrnutty 761 Senior Poster

yupeee:D

How do you think you should approach it then?

mrnutty 761 Senior Poster

thx 4 ur help but I only need 1 number consisted of 4 unique digits , I need to know how to get only unique digits , thx :)

Oh wait so your problem is more like generating a 4-digit number in which each digit in that number is unique?

mrnutty 761 Senior Poster

If its only 4 randomly generated numbers you need, then you can simply call rand() 4 times. It definitely should repeat just after 4 calls. Make sure you are using it correctly.

#include <iostream>
#include <ctime>
using namespace std;

int main(){
 srand( time(0) );
 for(int i = 0; i < 4; ++i){
   cout << rand()%9999+1000 << "\t";
 }
 return 0;
}
mrnutty 761 Senior Poster

What is the problem? If you can't use std::vector, then why not use std::list? Why are you using memcpy? Just use operator= for more clarity.

mrnutty 761 Senior Poster

Means? Absolute value refers to be value having no dependence on its sign right? So what is an absolute value sign?

As in the mathematical sense of absolute value. In mathematics one would use the Bar symbol as in , |x|, using the unary + symbol, as in +x, kind of amused me a little.

mrnutty 761 Senior Poster

I kind of like the idea of unary plus being an absolute value sign. Right now, the unary plus is nothing more than extra information to the user. Maybe to hint to the user that it should always be a plus or something. It has no effect.

mrnutty 761 Senior Poster

If you want to redirect the output from terminal you use the '>' operator to a file. As in ./app < inputFile > outputFile

mrnutty 761 Senior Poster

If your on linux you can use the '<' operator. As in ./app < fileName it gets the input from the file instead of direct terminal.

mrnutty 761 Senior Poster

That was my original thought too, but I dismissed it because it would be horribly sloooow.

But adding leading 0's infront assumes you know the maximum number of resource

mrnutty 761 Senior Poster

You adding it twice here :

for (int day = 0; day < 7; day += 1)
for (int temps = 0; temps < 2; temps += 1)
tot += tempav[day][0]; 
{
cout << " Total Col 1 " << tot << endl;

}

just do this:

for (int day = 0; day < 7; day += 1){
 tot += tempav[day][0]; 
}
mrnutty 761 Senior Poster

Another option is to supply your comparison method to std::sort:

int getDigitFromLine(const std::string& line){
  const string digits = "0123456789";
  size_t beg = line.find_first_of(digits);
  size_t end = line.find_first_of(digits);
  string digitString = line.substr(beg, end-beg + 1);
  return atoi( digitString.c_str() ); 
}
bool mycomp(const std::string& lhs, const std::string& rhs){
  int lhsNumber = getDigitFromLine(lhs);
  int rhsNumber = getDigitFromLine(rhs);
 return lhsNumber < rhsNumber;
}
int main(){
 //populate vector
 std::sort( vec.begin(), vec.end(), mycomp);
}
mrnutty 761 Senior Poster

std::cin.getline(const_cast<char *>(name.data()), MAX_NAME_SIZE); is undefined behavior because you are not allowed to modified the pointer-to-const-char returned by data.

You could easily just use the c-style approach and convert that into a string. Or you can do something like so.

string str(' ',MAX_SIZE);
cin.getline( &(str[0]) , str.size() );
mrnutty 761 Senior Poster

Couple of options.

1) Read it into a string and work from there
2) Split the digits and work from there
- to split the digits you can use the divide and modulus operator

And I am guessing your avoiding arrays because either your assignment says you cannot use them or the more probable reason is that you don't know how to use them well. If so then you need to seriously reconsider learning them and redoing this problem using array. BTW if you use method 1 above then internally your still using an array.

mrnutty 761 Senior Poster

What exactly do you need help with?

mrnutty 761 Senior Poster

>>So here it is, what do you think:

Honestly, its crap (take it as a helping tip). If you didn't want to use isdigit, then at least use something simpler.

bool isDigit(const std::string& str){
 const std::string digitList = "0123456789";
 bool isValid = false;
 for(int i = 0; i < str.size(); ++i){
    if( digitList.find( str[i] ) == string::npos){ //if not found
         isValid = false;
         break;
    }
 }
 return isValid;
}
mrnutty 761 Senior Poster

Um... you have to check it for all character

//check if str contains only digits
bool isDigits(const std::string& str){
 bool isValid = true;
 for(int i = 0; i < str.size(); ++i){
    if(!isdigit(str[i])){
         isValid = false;
         break;
     }
 }
 return isValid;
}

Note, this code might be ambiguous in some compilers so a better way to do this is with functors

mrnutty 761 Senior Poster

Line 14-18 set all pointers to null.
Line 21 check if driver is null
comment out line 23 and see if it runs fine.

mrnutty 761 Senior Poster

Have you learned about for-loops? If so then using for loops is the correct way to solve this problem.

mrnutty 761 Senior Poster

If the order doesn't matter then what you can do is
1) find the element A
2) If A does not exist then exit
3) Else if A does exist, then swap A with the last element in the array
4) Then simply set the last element to 0, and decrease the list's size

Then your done.

For me that notation is just a little annoying and sometimes can be in the way rather than helping me.

mrnutty 761 Senior Poster

Oh! This really makes sense. Thanks so much. By the way, what does "r" mean?

"r" stands for read. Check out the documents for fopen

mrnutty 761 Senior Poster

>> Should I include the whole path name or just the image name?

If the file is in the same directory as your source file, then you can just use the filename, else you can either use relative path or full path. However it needs to be a valid path.


>>file = fopen(filename, "C:\\Users\\annette\\My Pictures\\face.png");

You want, file = fopen(filename,"r"); and the call can be GLuint texture = LoadGLTexture("C:\\Users\\annette\\My Pictures\\face.png", 396, 734);

BoBok2002 commented: This post was helpful and well explained. I understood it clearly +2
mrnutty 761 Senior Poster

Just some few comments/questions first.

1) you might want to think twice on that hungarian notation.
2) Does the order of the elements in the string matter? For example does the last element has to be the last ? So does the element position matters relative to its neighbors?

mrnutty 761 Senior Poster

How would I do it in C++?

you can't do that with just pure C++. You will have to use external libraries like ncurses

mrnutty 761 Senior Poster

What will knowing that data structure FB used will help you in anyway? If you have a problem of picking which data structure to use, then either post it here or compare the pro's and con's of each data structure and pick the one that best suites the problem. They don't use some magical data structure that no one knows. Most data structures are based on the basic ones like trees,arrays,list.... so get to know the fundamentals first.

mrnutty 761 Senior Poster

remove the 'struct' word in line 4

mrnutty 761 Senior Poster

For P2 its easy to come up with a O(n*m) \in O(n^2) solution, but can you come up with a linear time algorithm, possibly in O(n+m), where n is the length of the p and m is the length of q?

mrnutty 761 Senior Poster

This int zero(float); has to match this int zero(float a[9][13]) . Same goes for the rest.

mrnutty 761 Senior Poster

Your code is hard to read but your problem is in line 15, your using the array and it hasn't event been initialized it. Usually when you use an array, you you use hardcoded index when you want to access all values. What you are looking for is something like so:

int main(){
 const int size = 4;
 int values[size] = {0}; //set all values to 0;
 for(int i = 0; i < size; ++i){
     int input = 0;
     cin >> input; //get user input
     //check if it matches any previously inputted values
      bool isUnique = true;
     for(int j = i - 1; j >= 0; --j){
           if(input == values[j]){ //if current value is equal to the newly inputted value
               isUnique = false; //set state to false
               break;     //exit out of this for loop;
           }
     }
    if(isUnique){ //if is unique, save it
         values[i] = input; 
     } else{
            cout << "\nError value is not unique\n";
            //reset counter
             --i;
     }
 }
}
babyhuyx commented: Amazing, Very Helpful, totally opened my mind to new way of coding +1
mrnutty 761 Senior Poster

>>Polish a turd and it's still a turd.

But its a cleaner turd? Anyways, who wants to touch a turd anyways, grosss lol.


As per the application of bubble-sort, the only place I seen it was in my introductory course. But even then I don't know if that is really a good idea. The one time I would show bubble sort is to demonstrate how not to write algorithms, or use it as a leeway to implement a better one.

mrnutty 761 Senior Poster

>>Weird and redundant, isn't it?

For now yes, but if you guys decide to not use "_T" then you can simply change the macro definition instead of changing the code a whole lot for accommodate the new type.

mrnutty 761 Senior Poster

You need to make sure that it maintains a heap structure, that is the highest priority( or the lowest) is constantly at the top. So whenever a user inserts or deletes a node, you still need to maintain that property. There are several ways of doing this, check here for details.

mrnutty 761 Senior Poster

If you have something like a factory pattern then you can enforce it, for example:

class CandyFactory{
 private:
  struct ChoclateBar(){ 
        int calories; 
        ChoclateBar(): calories(){}
   };
 public:
  shared_ptr<ChoclateBar> makeChoclateBar(){
       return shared_ptr( new ChoclateBar() )
  }
};

I don't know if thats what you were asking?

mrnutty 761 Senior Poster

>>getline(cin, first_sphere[3]);
You want: cin >> first_sphere[0] >> first_sphere[1] >> first_sphere[2] Also as suggested, move this code

float RadiusofTwoSpheres(float *sphere1, float *sphere2);
	{
		cout << "The Radius for Sphere's 1 and 2 is... " << endl;
		cout << endl;
		cout << "R = " << (float)sqrt(pow(sphere2[0] - sphere1[0], 2) + pow(sphere2[1] - sphere1[1], 2) + pow(sphere2[2] - sphere1[2], 2)) << endl;
	};

before int main(){}

Zvjezdan23 commented: Thank You so much for helping me. =) +0
mrnutty 761 Senior Poster

@op you are just having syntax issues. For your case to make a proper switch statement, you need to add the braces. Here is an example:

#include <iostream>
using namespace std;

int main()
{

  int test = 1;

  switch(test)
 {  
   case 0 : cout << "Option 0\n"; break;
   case 1: cout << "Option 1\n"; break;
 }

 return 0;
}

Notice the keyword "break". That tells the compiler that if it reaches that case, then evaluate that case and then break out of the switch statement. If you do not break out of the switch statement, then the next subsequent case statement will be evaluated.

mrnutty 761 Senior Poster

Well just giving it a try shows that it becomes "X", with quotes.

When you write ""string"", it doesn't make sense syntax wise because the first double-quote gets matched with the next double quote, and so on. Hence ""string"", gets interpreted as a empty string, followed by the identifier with name string, followed by another empty string. In your second example, you tell the interpreted to use the double-quote as a literal character and not a special character, hence "\"string\"", gets interpreted as a string which contains a double-quote followed by the characters in string, then followed by a literal double-quote.

mrnutty 761 Senior Poster

Yes I think Visual studio 2010 has it

mrnutty 761 Senior Poster

Consider using enums or contants for direction. For example:

#include <iostream>

enum Direction{WEST,NORTH,EAST,SOUTH};

//assumes proper ordering, WEST = 0, NORTH = 1, EAST = 2, SOUTH = 3
string showDirection(const Direction& d){
 const string encoding[] = {"WEST","NORTH","EAST","SOUTH"};
 return encoding[d];
}
int main(){
 Direction currentDirection = WEST;
 cout << showDirection(currentDirection);
 return 0;
}
mrnutty 761 Senior Poster

A converse of a statement P->Q is Q->P, although it does not necessarily make it true. So all you have to do is change the P and Q position. Your answer looks correct

mrnutty 761 Senior Poster

>> cin >> a >> b >> read_hw(cin, homework);

that essentially turns into cin >> cin which doesn't make sense. I agree, for your function, read_hw you should make it void. Enabling syntax such as read_hw(cin,homework) >> a >> b is obfuscated.

Just think read_hw as a regular function and call it as a regular function in its own statement. Don't try to use the returned value as there is no need. A better approach would be this :

#include <iostream>
#include <cstdlib>
#include <iostream>
#include <vector>

using namespace std;

istream& operator >>(istream& in, vector<double>& hw)
{
    if (in)
    {
        // get rid of previous contents
        hw.clear();
        // read homework grade
        double x;
        while (in >> x)
            hw.push_back(x);
        // clear the stream so that input will work for the next student
        in.clear();
    }
    return in;
}

int main()
{
    double a, b;
    vector<double> homework;
    cin >> a >> b >> homework;

    cin.get();
    return 0;
}
mrnutty 761 Senior Poster

What ^she^ said... with an emphasis on " All this implies is that you've ignored the more useful parts of the standard library for far too long."