Wrox Programmer Forums
|
BOOK: Beginning C# 3.0 : An Introduction to Object Oriented Programming ISBN: 978-0-470-26129-3
This is the forum to discuss the Wrox book Beginning C# 3.0 : An Introduction to Object Oriented Programming by Jack Purdum; ISBN: 9780470261293
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning C# 3.0 : An Introduction to Object Oriented Programming ISBN: 978-0-470-26129-3 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 23rd, 2010, 05:58 PM
Friend of Wrox
 
Join Date: Feb 2009
Posts: 194
Thanks: 5
Thanked 3 Times in 3 Posts
Default Chapter 8 exercise 1

I am having some trouble completing this exercise and I would appreciate some input.

Currently I have this code:

Code:
 private void btnCalculate_Click(object sender, EventArgs e)
    {
        double i;
        bool flag;
        double startingHeight;
        double endHeight;

        //Check for user input
        if (txtStartingHeight.Text == "" || txtEndHeight.Text == "")
        {
            MessageBox.Show("Please enter starting and ending heights", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            return;
        }

        if (ddlGender.SelectedItem == null)
        {
            MessageBox.Show("Please select a geneder", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            return;
        }

        //Validate users input
        flag = double.TryParse(txtStartingHeight.Text, out startingHeight);
        if (flag == false)
        {
            MessageBox.Show("Please enter only numbers", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            txtStartingHeight.Focus();
            return;
        }

        flag = double.TryParse(txtEndHeight.Text, out endHeight);
        if (flag == false)
        {
            MessageBox.Show("Please enter only numbers", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            txtEndHeight.Focus();
            return;
        }

        //Process
        double range = endHeight - startingHeight; //Get the length of the array
        Array[] heights = new Array[(int)range];

        if ((string)ddlGender.SelectedValue == "Male")  //Check which calculation to perform
        {
            for (i = startingHeight; i < endHeight; i++)    //This will iterate through each height of the loop
            {
                                         
            }
        }


        }
    }
So I have done some validation for user input and I have several variables which should hold everything I need (for now at least, I also think I am performing some unnecessary steps but lets look past those for now).

The bit which I am getting stumped on is that I want to perform the calculation for the weight in the for loop and then I guess populate each result into an ArrayList (or similar)

But I can't quite get this to work.

So what I think I should be doing is something like
Code:
for (i = startingHeight; i < endHeight; i++)    //This will iterate through each height of the loop
            {
                4.0 * i -128 //Can't get this working :-(                         
            }
store result in an array which I can then loop through to display the results, I just can't seem to write the part that does the actual calculation I think I am having trouble with syntax for this and types.

And it is really bugging me, I think I might have a face palm to my forehead moment if someone can point out the answer or a page reference as to how I would do this..I would be very grateful.
__________________
Follow me on twitter.

Where I work.

Connect with me on LinkedIn

Blog
 
Old March 2nd, 2010, 03:11 PM
Friend of Wrox
 
Join Date: Sep 2008
Posts: 234
Thanks: 0
Thanked 32 Times in 30 Posts
Default Weights-heights exercise

See if this helps:


Exercise 1 Solution
The code has a user interface with two textboxes for getting the starting and ending
heights for the table and a listbox or ListView object to present the results. The
button click event code does most of the work and one solution using a listbox is
shown here.

const double MININCHES = 36;
const double MAXINCHES = 96;

private void btnCalc_Click(object sender, EventArgs e)
{
bool flag;
int i;
int j;
double start;
double end;
double male;
double female;
double[,] idealWeights;
string buff;
//=================== Input ==========================
flag = double.TryParse(txtStart.Text, out start); // Table start
if (flag == false)
{
MessageBox.Show("Numeric only.");
txtStart.Focus();
return;
}
flag = double.TryParse(txtEnd.Text, out end); // Table end
if (flag == false)
{
MessageBox.Show("Numeric only.");
txtEnd.Focus();
return;
}
//=================== Validate Inputs ================
if (start < MININCHES || start > MAXINCHES) // Check table limits
{
MessageBox.Show("Table can only span " + MININCHES.ToString() +
" to " + MAXINCHES + " inches.");
txtStart.Focus();
return;
}
if (end < MININCHES || end > MAXINCHES)
{
MessageBox.Show("Table can only span " + MININCHES.ToString() +
" to " + MAXINCHES + " inches.");
txtStart.Focus();
return;
}
if (end <= start) // Can we display anything?
{
MessageBox.Show("Starting value must be less than ending value");
txtStart.Focus();
return;
}
// Define the array for table data
idealWeights = new double[2, (int) (end - start) + 1];

//================== Process ======================
female = 3.5 * start - 108; // Set initial table values
male = 4.0 * start - 128;

for (i = (int)start, j = 0; i <= (int)end; i++, j++)
{ // Since linear relationships...
idealWeights[0, j] = (female += 3.5);
idealWeights[1, j] = (male += 4.0);
}

//================== Display =======================
for (i = (int)start, j = 0; i <= (int)end; i++, j++)
{
buff = string.Format("{0,5}{1,15}{2,15}", i, idealWeights[0, j], idealWeights[1, j]);
lstResults.Items.Add(buff);
}
}

The program validates the input values for the table. Once the start and end values are
determined, those variables can be used to set the array size:

idealWeights = new double[2, (int) (end - start) + 1];

Note that, because start and end are double data types, you must cast those values to
an int to use them to set the array size.

The code then calculates the initial idea weights for males and females. However, since the code increments the value by 1 on each pass through the loop, you can simply add 3.5 to the current female value and 4.0 to the current male value to derive the new table value. Because adding numbers is a little faster than multiplication, you get a small performance improvement. Also note how we use the shorthand addition operators and the array reference to store the new values:

idealWeights[0, j] = (female += 3.5);
idealWeights[1, j] = (male += 4.0);

The second for loop simply displays the results in a listbox object. Obviously, you could move everything into a single loop and even do away with the arrays if you wanted to.
__________________
Jack Purdum, Ph.D.
Author: Beginning C# 3.0: Introduction to Object Oriented Programming (and 14 other programming texts)
 
Old March 2nd, 2010, 03:26 PM
Friend of Wrox
 
Join Date: Feb 2009
Posts: 194
Thanks: 5
Thanked 3 Times in 3 Posts
Default

Thanks for the response. I did end up looking at the back in the end and got the answer so that's all good.

Cheers,
__________________
Follow me on twitter.

Where I work.

Connect with me on LinkedIn

Blog





Similar Threads
Thread Thread Starter Forum Replies Last Post
Chapter 5 exercise 3 Will BOOK: Beginning Microsoft Visual C# 2008 ISBN: 978-0-470-19135-4 2 September 27th, 2009 02:41 PM
Chapter 3 - Exercise 3 AndyN BOOK: Beginning Cryptography with Java 3 August 16th, 2006 03:09 PM
Chapter 5 - Exercise 1 scgtman BOOK: Beginning Visual Basic 2005 Databases ISBN: 978-0-7645-8894-5 3 May 16th, 2006 08:10 PM
Chapter 3 Exercise 3 Matt WAXON BOOK: Beginning PHP, Apache, MySQL Web Development ISBN: 978-0-7645-5744-6 3 July 4th, 2005 02:19 AM
Chapter 8, Exercise 4 cjo BOOK: Beginning ASP.NET 1.0 0 November 3rd, 2003 02:26 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.