psyking's Profile User Rating: -----

Reputation: 20 Tradesman
Group:
Authors
Active Posts:
165 (0.1 per day)
Joined:
17-January 10
Profile Views:
9,782
Last Active:
User is offline Nov 08 2013 12:17 PM
Currently:
Offline

Previous Fields

Country:
Who Cares
OS Preference:
Linux
Favorite Browser:
FireFox
Favorite Processor:
Intel
Favorite Gaming Platform:
PC
Your Car:
Ford
Dream Kudos:
150

Latest Visitors

Icon   psyking has not set their status

Posts I've Made

  1. In Topic: Magic Square Program

    Posted 4 Dec 2012

    View Posttmarr80, on 04 December 2012 - 03:22 PM, said:

    int sum_row(int i)
    /*   Pre: i is a valid row of the square
        Post: Returns the row sum of the ith row  */
    {
    	int sum = 0;
    	for(int j=0; j<size; j++)
    	{
    	    sum = sum+grid[j][j];
    	}
    	return sum;
    }
    
    

    The problem with this code for this method is that it isn't returning the sum for that specific row, it's returning the diagonal going from top left to bottom right of the square. For this one, you would need to have your sum change like this: sum += grid[i][j]; where i is the row that the has been requested and j is the current column.

    View Posttmarr80, on 04 December 2012 - 03:22 PM, said:

    int sum_col(int i)
    /*   Pre: i is a valid col of the square
        Post: Returns the col sum of the ith col  */
    {
    	int sum = 0;
    	for(int j=0; j<size; j++)
    	{
    	    sum = sum+grid[j][j];
    	}
    	return sum;
    }
    
    

    Here, it is the same as above, but instead it should be reversed (i.e. sum += grid[j][i];).

    View Posttmarr80, on 04 December 2012 - 03:22 PM, said:

    int sum_other()
    /*   Post: Returns the non-main diagonal sum  */
    {
    	int sum = 0;
    	for(int j=0; j<size; j++)
    	{
    	    sum = sum+grid[j][j];
    	}
    	return sum;
    }
    
    

    Here, you don't need to change how the grid works, just how the for loop is initially started. Instead of assigning 0 to j, assign size-1, and instead of checking if j is less than the size, check if it is greater than or equal to 0, and instead of incrementing at the end of each pass, decrement at the end of each pass j--


    That should hopefully help you fix your problems.
  2. In Topic: problem: While loops with multi-conditions

    Posted 3 Dec 2012

    View Postanwind, on 03 December 2012 - 12:41 AM, said:

    Thank you, how stupid of me.

    Don't worry about it, everyone has those kinds of days (I've had plenty of them).
  3. In Topic: problem: While loops with multi-conditions

    Posted 3 Dec 2012

    View Postanwind, on 03 December 2012 - 12:00 AM, said:

    #include <iostream>
    using namespace std;
    
    // Function of reverse
    class Odometer
    { 
    public:
    
    	void odometer()
    	{	
    		milesDriven = 0;
    		Mpg = 0;
    		Igallons=0;
    	}
    
    

    Here, your problem is you are not defining your constructor correctly. It's definition has no type (i.e.
    class ExampleClass
    {
    public:
        ExampleClass(){
            // do something
        }
    };
    
    
    ) and it's name needs to match exactly the name of the class (case-sensitive). Because it is not correct, your variables do not really get initialized, which could potentially cause a bug in your program.

    View Postanwind, on 03 December 2012 - 12:00 AM, said:

    	void fuelleft(double gallonsleft)
    	{
    		gallonsleft = Igallons - milesDriven / Mpg;
    	}
    
    


    This method accepts a double, then sets the local variable 'gallonsleft' to Igallons - milesDriven/Mpg. Later in your code, you pass Gallonsleft to this method, expecting Gallonsleft to be something other than 0 when the method returns. However, this will not be the case, since you are passing the variable by value and not by reference. Passing the variable by reference will change the value stored in the address of the variable, meaning that Gallonsleft will be changed.

    View Postanwind, on 03 December 2012 - 12:00 AM, said:

    	void Imiles(double milesD)
    	{
    		milesDriven = milesD + milesDriven;
    	}
    
    	void Odometer::set_mpg(double mpg)
    	{
    		mpg = Mpg;
    	}
    
    


    Here is good as well.

    View Postanwind, on 03 December 2012 - 12:00 AM, said:

    	void Odometer::set_gallons(double gallons)
    	{
    		gallons = Igallons;
    	}
    
    


    Here, you are trying to assign the number of gallons remaining in the tank. However, the assignment is wrong, because in C/C++ the variable that you are trying to assign a value to must be on the left side of the equals ('=') sign. The variable Igallons will always remain as zero (0) because you are always assigning Igallons to the local variable gallons.

    View Postanwind, on 03 December 2012 - 12:00 AM, said:

    
    private:
    	double milesDriven;
    	double Mpg;
    	double Igallons;	
    
    };
    
    
    int main()
    {
    	Odometer CarOdometer;
    	double milesdriven = 0;
    	double MPG=5;
    	double Gallons=10;
    	double Gallonsleft=0;
    
    
    


    View Postanwind, on 03 December 2012 - 12:00 AM, said:

    	while (( Gallons >= 10 ) && ( Gallons <= 30))
    	{
    		cout << "Enter gallons in tank (10-30): " << endl;
    		cin >> Gallons;
    		if (Gallons <10 || Gallons > 30)
    			cout << "Error: "<< endl;
    	}
    		CarOdometer.set_gallons(Gallons);
    
    


    You will always get stuck on this while loop while you are entering the correct information. As soon as you enter a number that is less than 10 or greater than 30, the while loop will exit. What you should be checking for in the while loop is if they have entered incorrect numbers, then the while loop should keep going; otherwise, it should exit (i.e. Gallons <10).

    View Postanwind, on 03 December 2012 - 12:00 AM, said:

    	while ((MPG >= 5) && (MPG <= 70))
    	{
    	cout << "Enter Fuel efficiency (MPG) (5-70): " << endl;
    	cin >> MPG;
    	if (MPG <5 || MPG > 70)
    		cout << "Error: "<< endl;
    	}
    		CarOdometer.set_mpg(MPG);
    
    


    You have the same problem here as you did in the while loop checking for the number of gallons entered. As long as the user enters a "correct" number (i.e. between 5 and 70) the loop will always continue.

    View Postanwind, on 03 December 2012 - 12:00 AM, said:

    	while (milesdriven = -1)
    	{
    		cout << "Enter miles for trip (-1 to finish, no more trips) : " << endl;
    		cin >> milesdriven;
    
    		CarOdometer.Imiles(milesdriven);
    	}
    
    


    Here, you have a different problem for the while loop, as you have created an infinite loop by doing an assignment operation every time the loop checks if it should continue. What you are trying to do is to check if milesdriven is not equal to -1 (i.e. != -1).

    View Postanwind, on 03 December 2012 - 12:00 AM, said:

    
    	CarOdometer.fuelleft(Gallonsleft);
    	cout << "Gallons left in the tank: "<< Gallonsleft <<" gallons" << endl; 
    
    
    	return 0;
    }
    
    


    Here, when you call the method fuelleft, you enter the variable Gallonsleft, and later expect it to have changed from 0 gallons remaining to however many gallons remaining -- hence the line immediately following. However, as I stated earlier, this is only going to happen if you pass the variable by reference (i.e. &Gallonsleft) and if the method accepts a double passed by reference (which it currently does not), meaning that your output for that final line will always be: "Gallons left in the tank: 0 gallons".

My Information

Member Title:
D.I.C Head
Age:
22 years old
Birthday:
February 8, 1992
Gender:
Location:
Everywhere there is a computer connected to the internet
Interests:
Running Cross Country and Track, running my own business, programming websites and software, and other things as well.
Full Name:
Null

Contact Information

E-mail:
Private

Friends

Comments

psyking has no profile comments yet. Why not say hello?