mrnutty 761 Senior Poster

>>// output: 4 (?!) Shouldn't this be an error?
It is. And its a bad one, which went undetected in a sense.

mrnutty 761 Senior Poster

Just go for C++. Learning C afterwards will be easy. And you will see
the real difference between the two.

mrnutty 761 Senior Poster

its used to indicate if the number is prime or not.

mrnutty 761 Senior Poster

Hint, initialize your variables.

mrnutty 761 Senior Poster

Your base case is wrong. When should your function return or Stop ?

mrnutty 761 Senior Poster

I think a good start would be to create a linked list. Since you don't
know templates yet, you don't have to create a template linked list.

Do you know what a linked list is?

mrnutty 761 Senior Poster

It depends on what "i" is. If its a class then there if no way of knowing
exactly what it does unless you see the code for it. If "i" is a primitive type data, then look at its assembly code and see which one is the fastest. It might vary with compilers.

mrnutty 761 Senior Poster

Oh, you mean you want the number in hex inside the string? I don't see a good reason for that. Whenever you need to print in hex, just convert the string into a int or a float and set the stream to print out in hex.

mrnutty 761 Senior Poster

>> Well that's simple thanks, tho was more wondering how to convert it to a string?

You can use the stringstream.

#include<iostream>
#include<string>
#include<sstream>

using namespace std;

int main()
{
    const float PI = 3.14159265f;
    string  pie = "";
    stringstream convert;  //create stream object
    convert << PI;    //insert out value into our stream
    convert >> pie; //get the value in the stream as a string object

   return 0;
}

Or for practice, try to create a function that does that without the sstream.

mrnutty 761 Senior Poster

If anyone is worried about which makes his code faster :

A) ++a;
B) a++;
C) a = a + 1;
d) a += 1;

Then I am more worried about his code.

mrnutty 761 Senior Poster

Before I say something, what do you think this statement is doing :

struct word_list * new = NULL;
mrnutty 761 Senior Poster
int n = 15;
cout << hex;
cout << n << endl;
Excizted commented: thank you. +1
mrnutty 761 Senior Poster

A conditional expression with more than 2 statement is just the same
as two separate conditional expression.

For example :

while( gameIsRunning )
{
        if( keyPressed )
            if( keyPressedIsX){ .. }
}

Is the same as :

while(gameIsRunning)
{
    if(keyPressed && keyPressedIsX) { ... }
}

So you see, you can think of a conditional loop with more than
1 expression as 2 or more.

Here are more examples :

do{
}while( playerIsAlive && ScoreIsNotNegative);
int grade = 0;
grade = scanner.nextInt();
if( grade >= 90 ) System.print(" You got an A" );
else if( grade >= 80 ) System.print("You got an B");
else System.print("You didn't get a B or an A\n";
mrnutty 761 Senior Poster

Yes. The main use of glut is to create a window and handle events. Windows API can do the same, so you can use win32 with opengl. In fact
in NEHE tutorial, thats what they use.

mrnutty 761 Senior Poster

You can do something like this :

#include <iostream>
#include <string>
#include <stdexcept>

using namespace std;

class Trouble : public std::exception
{
private:
	string errMsg;
public:
	Trouble(const wstring msg)
		 //convert from wstring to string
		: exception( string(msg.begin(), msg.end() ).c_str() )
	{
		errMsg = string( msg.begin(), msg.end() );	
               //testing
              throw *this;	
	}
};

int main()
{ 	
	try{
		Trouble(L"testing");
	}
	catch(Trouble& e){
		cout << e.what() << endl;
	}
	return 0;
}

Although its probably not portable.

mrnutty 761 Senior Poster
"OpenGL is the industry's most widely used, supported and best documented 2D/3D graphics API making it inexpensive & easy to obtain information on implementing OpenGL in hardware and software"

Its a graphics library that provides an interface for the user to draw 2d
or 3d.

Glut and SDL are similar.

"GLUT (pronounced like the glut in gluttony) is the OpenGL Utility Toolkit, a window system independent toolkit for writing OpenGL programs. It implements a simple windowing application programming interface (API) for OpenGL. GLUT makes it considerably easier to learn about and explore OpenGL programming. GLUT provides a portable API so you can write a single OpenGL program that works across all PC and workstation OS platforms."

glut and sdl provide an interface for the user to handle creating window.
They help it make it easier to program applications. For example, using
glut, you can handle creating a window, and communicating with the
mouse and keyboard, and use opengl to just draw stuff.

Just using, SDL you can do 2d graphics as well.

mrnutty 761 Senior Poster

To firstPerson

If I want to string array2D[][MAX_COL] as a parameter in the function

in main() how would I declare that variable?
string array2D[][Max_COL] // gives error saying undetermined size

and how would I initialize that function at the beginning of the code?

Thanks

Like this :

const int ROW = 5;
const int COL = 5;
void foo(string array2d[ROW][COL]){
}
int main()
{
    string mainArray[ROW][COL];
     foo(mainArray);
}
mrnutty 761 Senior Poster

You have the answer in your face. All you have to do is copy it into code.

>>The examples below is based on the number: 129
Ex: Round down 129 to nearest 40 gives: 120 in this way:
40 + 40 + 40 = 120


Try to do exactly that, in code.

If we are rounding down to the nearest 10. We can do this

int num = 129;
int roundedDown = 1;
//round down to the nearest 10
while( roundedDown < num  - 10 ){
 roundedDown  += 10;
}
mrnutty 761 Senior Poster

For this problem, if you get 1, other will be simple. So focus on 1
function first.

>>Round down to nearest 5 gives: 125

Before we go further.

What happens if we say this :

Round down to nearest 5 for number 124 ?
Round down to nearest 5 for number 3 ?

Can you give more input/output and a little more info?

Edit : I see

mrnutty 761 Senior Poster

>>it'd be neat if I can return a 2-d array

It would also be slow and maybe dangerous depending on some things.

Why not just pass in a 2d array, and fill it with the data from the file?

mrnutty 761 Senior Poster

From what I see , you do not need to return a 2d array. Consider just
doing this :

void set_element(string array2D[][MAX_COL], const int MAX_ROW)
{	
	for (int i = 0 ; i < MAX_ROW ; i++)
	{
		for (int j = 0 ; j < MAX_COL ; j++)
		{
			array2D[i][j]=" ";
		}
	}
}
mrnutty 761 Senior Poster

Thanks for the replies,is this because *p1 is derefrenced here so contents can be added?

Yep, just think about it like this, if ptr is a pointer-to-int, then *ptr,
is the value that it points to , so if it points to an address, which
has the value 4, then *ptr == 4 is true.

int a = 4;
int * p = &a;

int t = *p; // same effect  as int t = 4;
mrnutty 761 Senior Poster
int a = 1;
int b = 2;
int *p1 = &a;
int *p2 = &b;
int *p3 = *p1 + *p2;
mrnutty 761 Senior Poster

It works if you do it right :

#include<iostream>
#include<ctime>

using namespace std;

class Random{
private: const unsigned int MAX;
public:
	Random(const unsigned int maxLimit) : MAX(maxLimit) {}
	int getRandom(){
		return rand() % MAX;
	}
};

int main(){
	srand(time(0));
	Random myRand(100);

	for(int i = 0; i < 25; i++){
		if(i && i % 5 == 0 ) cout << endl;
		cout.width(3);
		cout << myRand.getRandom() << " ";
	}
	cout<<endl;
	return 0;
}
mrnutty 761 Senior Poster

>>CEquipment* equipment = &(random_from<CEquipment>(equipments));

This is what you are doing :

int * pointer = 0; //declared somewhere
void foo(){
  int a = 3;
   pointer= &a; //what you are doing
}//end of the brackets

now at the end of the brackets, a gets destroyed, and what does
pointer points too?

mrnutty 761 Senior Poster

First make a palindrome that is case and punctuation sensitive :

You code revised a little :

#include <string.h>

bool palindrome (const char *s)
{
	int index = 0, length = 0;
	length = strlen (s);
        int halfLen = length/2;
	for (index = 0 ;  index  < halfLen;  i++){
		If (s[index] != s[length- index-1]) 
                   return false;
	}
	return true;
}

Now you need to first add a couple of things to it.
1) Makes the sentence all lower or all upper case
2) Deletes punctuation from the string
3) and add other restriction.

mrnutty 761 Senior Poster
void FindMaxMin(int x, int y, int z, int &max, int &min)
{
	//FindMax
    if (x > y && x > z)
    {max = x;}
 
	if (y > x && y > z)
	{max = y;}
 
	else
	{max = z;}
 
 
	//FindMin
    if (x < y && x < z)
    {min = x;}
 
	if (y < x && y < z)
	{min = y;}
 
	else
	{min = z;}
 
}

still wrong.

mrnutty 761 Senior Poster
float FindMaxMin(int x, int y, int z, float &max, float &min)
{
	//FindMax
    if (x > y && x > z)
    {return x;}
 
	if (y > x && y > z)
	{return y;}
 
	else
	{return z;}
 
 
	//FindMin
    if (x < y && x < z)
    {return x;}
 
	if (y < x && y < z)
	{return y;}
 
	else
	{return z;}
 
}

Its code like this make me wanna pull my hair.

mrnutty 761 Senior Poster

Yes, :

char space = ' ';
for(unsigned int i = 0; i < str.size(); i++){
    if(str[i] == space){ 
           str[i] = '_'
    }
}
mrnutty 761 Senior Poster

Fix to :

if(winner == 0)

Also make use of functions.

mrnutty 761 Senior Poster

Thanks!

I've got #include <string> and thought that "xyz" would be interpretted as a string versus the single quotation marks ' ' which indicate its a const char* :-S ?

Nope, then it would be ambiguous.

mrnutty 761 Senior Poster

>>output("1", "k"); // ?

should be a string :

output(string("1"), string("k")); // ?

Whats happening is that "1" and "k" is being interpreted as a const char*

mrnutty 761 Senior Poster

whats the problem?

mrnutty 761 Senior Poster

Appreciate your assistance! I also need to incorporate at least one function call within my code and that has me completely lost....Any help?

You already did, the displayTitle(); is a function call.

You also might want to make a function that returns the average, given an array and its size.

mrnutty 761 Senior Poster

Well it will ask you questions of what you have covered.

Salem commented: Aparently, this wasn't obvious to the OP - it does not bode well for the exams... ;) +17
mrnutty 761 Senior Poster

What is enemies?

mrnutty 761 Senior Poster

Is there a particular problem? For you operator >, you can just do :
return !(a < b );

mrnutty 761 Senior Poster
#include <iostream>
using namespace std ;

void displayTitle ()
{
     cout << "Active Duty Navy personnel Program" << endl ;
}

int main ()
{

    displayTitle () ;
    
    int Age[50] ;    // age of personnel
    int I ;          // number of ages entered
    int sum ;        // sum of ages
    float Average ;  // average of ages entered
    sum = 0 ;
    Average = 0 ;
    for (I = 0 ; I < 50 ; I++)
    {
       cout << "Enter current age of AD Navy Personnel" << ( I + 1 ) << endl ;
       cin >> Age[I] ;
        
       if ( Age[I] >=18 ){
          sum = sum + Age[I] ;
       }
       else {
             I = I - 1 ; // the same element the Age array will be read 
        }
    Average = sum/50 ;
    for ( I = 0 ; I < 50 ; I ++ )
   {
        cout << "Age of Personnel" << ( I + 1 ) << "is : " 
        << Age[I] << endl ;
       }
       cout << "The current average age of AD Navy Personnel is : " 
        << Average << endl ;
           
    cin.get () ;
    return 0 ;
}
mrnutty 761 Senior Poster

Your highest function should return an INT, and that int should be
the index of the highest element in that array. Change that first.

Next, your highest function is almost correct, except that your max should be the index to the highest value, and your if statement should check every element against the array content of the max value.
So it should be something like this :
//returns the index to the highest element in arr

int highest(student arr[], int cnt, int i)
{
	int max = 0;
 
	for (int j=0;j<cnt;j++){
		if (arr[j].gr[i] > arr[max].gr[i])
			max = j;
             }

	return max;
}
mrnutty 761 Senior Poster
//items are equal 
		else if(list1=list2)

or

// is items equal ?
	else if(list1== list2)
mrnutty 761 Senior Poster

First your temp variable is not initialized but you use it.

Second check this and see if it help, look particularly at the logic inside the loop.


Third you have the x declared before the for loop and inside it. So that
might be your problem.

Doing this works so far :

bool add (int a[], int b[], int sum[])
{

    int x = 0, temp = 0;
    for(x = MAX_DIGITS-1; x>=0; x--)  // first index of array is 0 in C/C++
    {
     
        sum[x]=a[x]+b[x];  

        if(temp==1)
         sum[x]=sum[x]+1;
        
        
        if(sum[x]>=10)
        {
            temp=1;
            sum[x]=sum[x]-10;
        }

        else 
        {
            temp=0;
        }
            
    }

    if(( x<0 )&&(temp==1)) // = is assignment == is comparison
        return false;
    else
        return true;

}
mrnutty 761 Senior Poster

This part :

for(int index = 0; index < dim; index++){
		aux = matrix[i][index];
		matrix[i][index] = matrix[j][index];
		matrix[j][index] = aux;
		}

What do you think its doing? Were you trying to swap ?

mrnutty 761 Senior Poster

Try degubbing it. Print out everything in the map before and after you
make that search call.

mrnutty 761 Senior Poster

Is it in the same directory? Show some code?

mrnutty 761 Senior Poster

>>But this looks ugly and I had to use a separate variable.

I think that looks beautiful. In fact you can just make a function that does that, and so it will be in a sense a one liner.

I don't think there is a standard implementation of getch in C

mrnutty 761 Senior Poster

so for instance say i did dataTable->at(key) would i be referring to the list or vector? or both?

dataTable is a vector.

dataTable is a list

dataTable.front() is the int.

And also I don't think mixing up vectors and list would be a good
idea, unless it really really makes sense to do so.

mrnutty 761 Senior Poster

>>"The vector holds a list of data type int? "

the word list has ambiguity.

A better term(IMO) would be : The vector contains a container in which the contained container is of type int and the contained container is a stl's list container.

mrnutty 761 Senior Poster

From :

while ((cin >> ar[i]) && (i < arSize))

To :

while ((i < arSize) && (cin >> ar[i]) )
mrnutty 761 Senior Poster

>>The you can just concat title with first name.

That was my objection. Those two can not be joined.

Are you talking about conceptually or technically?

mrnutty 761 Senior Poster
lastElem =remove_if(v1.begin(), v1.end(), islowerCase);//line1

Line 1 : remove_if(...), if the containers contains a lower case character
the function removes it. It does this, and returns the a pointer that
points to the new end since some elements has been deleted.

ostream_iterator<char> screen(cout, "");//line2

Line 2 : creates an iterator. Just like the vector::iterator, ostream_iterator is an iterator. It can insert elements into the
ostream object, cout. When you do :

cout << 'a';

Its almost the same as if you do :

screen = 'a';

Its just doing it in a different way.

copy(v1.begin(), lastElem, screen);//line3

Remember what I said, if you do screen = 'a'; It will print out a into the
console. So the above code roughly does screen = vec[0] ... vec[lastElement]. That means everything inside the vector is getting
printed to the screen.


Also check this out for some reference : remove_if , istream_iterator using std::copy