Saith 74 Junior Poster

That is a lot of if-else statements. Think you can rewrite this with a more logical approach?

Note: This is not to bash you! If you can think of creating this with another approach, it may help with other types of OOP. Consider using a class for each player as either X or O, and using enum. ::hint hint:: :D
2nd Note: Oh, sorry. This was posted ~2 weeks ago. Likely should not have replied to this thread by now.

Saith 74 Junior Poster

I guess you are right but in this program he doesnt really need to change it. He created the object before asking for input and it was unnecessary.

You think so? I think what he did was just fine. First creating the empty object and then setting the values to the users choice. This set up will be handy when he learns about pointers and dynamic arrays/classes/structures that are soon to come. Note that there is a second object that was made with the constructor of 4 parameters.

I suppose it comes down to personal preference with this specific example that the OP has given.

sergent commented: You are correct! +0
Saith 74 Junior Poster

Good to hear you don't want your homework done for you, it's definitely a plus on my end to see.

There are a number of things wrong with the code, but overall, not a bad start.

You may want to give us a reason why it may be buggy? Tends to boil down to syntax/logical/both. If it's syntax, just start from the top and work your way down.

I'll help you out for a minute or two.

First, your structure definition looks good. I see no problems there. What I do see wrong is the calling of a Record object that you are trying to pass through functions. Like classes, which you will likely learn next, you will have a class/structure and an object of that class that you will be manipulating.

A quick side-track moment. Think of a class/structure as the human race. Think of an object as one particular person. When you want to change someones eyes color, you need to change that particular persons eye color, not the entire human race. You do this in C++ first by creating a data type (class/structure/human race) followed by the variable(object/person); now you may change one particular persons eye color (or any other variable) through that object you just created.

ie int number; // int is a data type, number is it's variable
Record FooFighers; // Record is the data type (class), FooFighters is it's object.

then, to send FooFighters through a …

Saith 74 Junior Poster

Hi, I am working on an exercise in operator overloading. I've created a matrix class and I am supposed to overload operators so I can do arithmetic on matrices efficiently.

This is where the fun part comes into play :D Enjoy it and let it roll.

My directions say that I am supposed to make two matrix arrays using the class constructor that has 2 parameters and a third matrix array that will be used to store the result of arithmetic using the default constructor (1 parameter).

Sounds about right.

Since I am going to use these arrays to overload operators they are going to need to be data members of the class (I think).

Yes. The data that you will be using for your particular class assignment will be class member values of the matrix class that you created.

However, I thought that classes were supposed to be as representative of real life things as possible so making a matrix class with multiple arrays doesn't make sense to me (a matrix is only one matrix).

Nice thinking. It wouldn't make much sense to see one matrix having more than one matrix inside when you are trying to create and have one matrix in all.

Am I misunderstanding classes or is there a different way to make additional matrices using the class constructor I am not thinking of? Thanks all, here is the code in question.

I think you have part of it down. Once you …

Saith 74 Junior Poster

So one should write like std::cin and such?
Seems annoying / ugly =(
Should I be doing that? (std::), do experienced programmers do that? =(

Yes, you can write like that. I have read a few C++ books and both have been shown. I suppose it comes down to preference, as I am a limited C++ programmer myself, but what you may do instead is sometime similar to this.

/*
	Purpose: thread1529731
	Name: Saith
	Date: 4/8/11
	*/

#include<iostream>

int main() {
	using namespace std;

	int x;

	cin >> x;
	cout << x;

	return 0;
}

The previous code will only use the

using namespace std;

in that block of code so you can continue to use cin without the need of std::cin; but for that block only, if you try to use cin in any other function without the leading std:: the compiler will give an error. Likewise, you may put that namespace in any other block of code and will be limited to that block only.

also I don't quite understand your swap function >.<
Shouldn't you do swap(&x, &y) iny our main function?

There is a swap function in the std. The compiler will use the std function swap() that takes two arguments and swaps the values for each.

The only time you need to use the & for call-by-reference is when you are -declaring- a function and when you are -defining- a function, you -do not- need the & when …

Narue commented: Props for showing namespace directives in a nested scope. +25
Saith 74 Junior Poster
rational rational ::operator +(rational r){


rational sum;
sum.numerator =numerator+sum.numerator;
sum.dnomenator=dnomenator+sum.dnomenator;
cout<<"The sum afer adeng both fraction  is  :"<<endl;
return sum;

}

Few things wrong with the logic of this.
1. You need to add two separate objects together, not one object and the sum of two objects.

Ex.
You want A + B = C
not A + C = C

This is shown with both additions in the previous code.

2. You need to find a common denominator between both fractions. If they are already equal, then you can just add the numerator and KEEP THE SAME denominator. If both denominators are different, you need to find a common denominator, then change the numerators respectively prior to adding the numerators.

Finally, you can use your reduction function to simply your answer.

Saith 74 Junior Poster

Please reread what I wrote, then check your answer with mine. Do they look similar? Do they have the same amount of parameters? You also declined to add the friend function declaration in the class definition, without it, the class will not know where to look for the + operator.

Here is a quick tutorial that should help with your needs.

http://www.cplusplus.com/doc/tutorial/classes2/

Saith 74 Junior Poster

The code you posted would indicate that fourvector is a class derived from the class threevector. The constructor of fourvector simply forwards the one_two_three parameter to its base class constructor.

Remark: "I have seen this syntax used for the constructor of the fourvector:"
Run! Just run away from this code! There are so many things that are wrong with it that its not worth wasting time correcting it. Just run, before it poisons your mind.

Funny that you mention that. I think I have seen this type of example somewhere from a -textbook-. Kind of "funny" (being facetious) that a text book would give a bad example as the one you are saying to run away from.

I suppose this wouldn't be the first time I've head of this occurring. Difference between what you "can do" and what you "should really be doing".

Out of curiosity, why would this set up be so bad?

Saith 74 Junior Poster

Remember cin.ignore has two parameters.

cin.ignore(1000, '\n');

This is to clear the input stream useful with while mixing cin.get(param1) and cin >> variable1.

Example 1

/*
	Purpose: threads/354626
	Name: Saith
	Date: 3/20/11
	*/

#include<iostream>
using namespace std;


int main(){

	int var1;
	char var2;

	cout << "Input some number.\n";
	cin >> var1;
	cout  << "Your number is "<< var1 << endl;

	cout << "Input another number.\n";
	cin.get(var2);
	cout  << "Your number is "<< var2 << endl;

	return 0;
}

Input:
1
// c // would have been the second input

Output:
1
[blank space]


and will not bother pausing for a keystroke due to a character already in cin (the '\n' character). If you add the cin.ignore(para1, para2); syntax, the '\n' character will be removed from the stream input along with any other value.

Example 2

/*
	Purpose: threads/354626
	Name: Saith
	Date: 3/20/11
	*/

#include<iostream>
using namespace std;


int main(){

	int var1;
	char var2;

	cout << "Input some number.\n";
	cin >> var1;
	cin.ignore(1000, '\n');
	cout  << "Your number is "<< var1 << endl;

	cout << "Input another number.\n";
	cin.get(var2);
	cout  << "Your number is "<< var2 << endl;

	return 0;
}

Input:
1
c
Output:
1
c

Ancient Dragon commented: Nice :) +36
Saith 74 Junior Poster

Luckily, us "smart ones" (really the ones that truly want to find the answer by digging for it) will filter through those kind of responses and will find what we are looking for.

I've been a Daniweb member now for ~2 months, ~2 months longer than any other forum, and I still consider this a very reputable site in so far as to searching this forum prior to any other and spending my time to help others.

No matter where you go, there will be good and the bad. Seems like this site does a good job filtering those bad down to a minimum. I say keep up the good work to the admin's and keep up with the great responses to those that are giving positive criticism.

AndreRet commented: Agreed! +0
Saith 74 Junior Poster

First, as Crutoy as responded, you want to populate the array.

Use nested for-loops. A generic one you may find would include something similar to this.

/*
	Purpose: thread347853 - filling a multidimensional array
	Name: Saith	
	Date: 2/17/11
	*/

#include<iostream>
using namespace std;


int main() {

	const int vertical = 2;
	const int horizontal = 2;
	int list[vertical][horizontal];

	for(int index = 0; index < vertical; index++){		// will fill your array vertically
		for(int index2 = 0; index2 < horizontal; index2++){		// fill your array horizontally
			cout << "Please input [" << index << "]["<< index2<<"]" << endl;
			cin >> list[index][index2];
		}
	}
	cout << "OUTPUT "<< endl;
	// And to output them, you would do the same thing.

	for(int index = 0; index < vertical; index++){		
		// will output your array vertically

		for(int index2 = 0; index2 < horizontal; index2++){		
			// output your array horizontally
			
			cout << list[index][index2] << " ";
		}
		cout << endl;		
		// as the index increases (vertically) a new line will be make for the next set of numbers
	}

	return 0;
}

If you compile and test this code, it may help you understand how/why multi-dimensional work. You can really cut down on the amount of coded lines. Imagine, if not 10x10 but 1000 x 1000, trying to code in for each possibility. Yeah. Good luck on that one! Good luck to you on this current project.

-Saith

Saith 74 Junior Poster

I copied and pasted what you had.

The only solution I came up after compiling with no errors is:

#include<vector> in the header file

Oh, and get rid of
std::

in

std::vector<int> v1;

So its just

vector<int> v1;

Saith 74 Junior Poster

First, do what jonsca suggested by creating a new variable, adding to the variable of the current grade to get a running total of all grades added together, then divide by NUM_SCORES (sum)/NUM_SCORES will give you your average.

To extend on that, you will need to add an additional for-loop to get the average from the students that you did not initially input from the user. That for loop will sum all the grades together as I explained earlier and then again, divide by NUM_SCORES. You can make a local variable to save the averages for each student, or add a member function to the original structure, Student.


You may also make a function to pass as an argument the student object so that you don't have to rewrite the function three times, or as many student as there are. You just have to create the function once, then call the function as many times as you want to, no matter how many students.

Saith 74 Junior Poster

OH
I see

check your IF STATEMENT.. GET RID OF THE ;

if(begllts <= intarray[m] && intarray[m] <= endllts); //<--- that last ;, Get rid of it BAD, NEWBIE BAD .. :D

Akill10 commented: well spotted!! +1
Saith 74 Junior Poster

Okay. I just compiled your code and debugged a bit. The reason why are getting an error when running your code is due to the divide by zero effect. It's GG.

You are wondering why numOfClasses = 0 by the time you call the second function, it's because numOfClasses actually equals zero.

Why? Because in the first function, you passed the value argument, pass-by-value, and not pass-by-reference. You told the function that numOfClasses equals 3 through cin, but that assignment to numOfClasses was only valid in that functions block of code. Once the block ends, numOfClasses value is now wiped and replaced by the value prior to the function call, zero. Making all the parameters for that first function, pass-by-reference, you will save those variables past the end of the block from the function. You will also need those variables saved past that first block to be used in the third function.

Also, in your third function, the output function to the screen with the final results. There are no syntax errors, but there are logical errors. Your output of letter grade outputs the LAST LETTER GRADE entered. NOT the average letter grade. So if you inputted A, A, C. Your letter grade is C, which should be B.

Also, for -just in case-, you want to equate totalPoint to zero prior to using totalPoints += qp; as it also means, totalPoints = totalPoints + qp; and if there is some random junk from a previous …

jonsca commented: Good explanation, thanks for not just giving the code to the OP +6
Saith 74 Junior Poster

Seem like your coding structure is a little off, perhaps something like this?

This is just a snippet of what I just wrote, but I did write the entire program to make sure the snippets work properly. The output seems correct with the desired input in the array. I won't copy and paste the entire program of what I have written just to do someone elses homework, but snipped what I did write to help that person understand how I came up with that I did.

I recommend using comments as I have done here in your own program, just in case someone else needs to read what you were trying to do. Also, allows you to remember what you did at a later date.

/* This truly brought me back to the days when I first learned how to do functions. Let's start
from the beginning.

What I'm about to write is all in assuming from what I gethered from the given code.

Find:

Average of all Positive Numbers
Average of all Negative Numbers
Average of all Numbers;
and Max Number in the list.

with a given array. Make function use out of all required findings. */


#include <iostream>
using namespace std; 


// declarations purposely deleted from this snippet

int main()
{

int Ave, AveP, AveN, Max;		// integers record required averages
const int ARRAY_SIZE = 10;

int numbers[ARRAY_SIZE]={4, -30, 0, 7, 42, -20, 18, 400, -123, -6};	// array that will be manhandled

/* Display(numbers, …
jonsca commented: It's good that you did not give a complete solution. +6
Saith 74 Junior Poster

Will do in the future, Alexchen. I'm an absolute noob to forums, and just started today at this site. Thank you for the advice!

Saith 74 Junior Poster

Instead of having the user input a statement, have the program ask a question and the user reply with yes or no. At which point, if the answer is yes, you lead down one road of possibilities to what the answer is. Otherwise, head the other direction. Having the user to input an answer for the program to comprehend might be a bit much for a simple '20 Questions'.

e.g. Answer: Pencil

Q1. Person(A), Place(B) or Thing(C).
Input: C
Q2. Solid Object?(A) Non-solid Object (B)
Input: A
Q3. Human daily use?(A) Or not daily use?(B)
Input: A
Q4. Used for communication?(A) Or not used for communication(B)
Input: A
Q5. Do you write on it?(A) Or do you not write on it. (B)
Input: B
Q6. Do you write with it?(A) Or do you not write with it? (B)
Input: A
Is the answer: A pencil?

That's very simplistic and very narrowed approach to the answer but -more of the set up that I was thinking-. This would be one approach, definitely may not be the best approach, but one that could possibly do the job. Good luck!