# Grand Total of Square Root Question

Page 1 of 1

## 6 Replies - 2724 Views - Last Post: 16 February 2012 - 02:39 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=266956&amp;s=5bf93aadafd0df7b76050d4cbd98655b&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 mrbrightside

Reputation: 0
• Posts: 7
• Joined: 23-January 12

# Grand Total of Square Root Question

Posted 16 February 2012 - 01:09 PM

Hi I'm writing a code for a program that output the number, the square of the number, the cube of the number and finally the square root of the number and at the end gives me a grand total of the square roots.

I'm having problem with the grad total of square roots calculation: The code I have so far is below. Can anyone please help with to calculate the grad total of square roots? What should I use for the calculation? Thanks in advance.

My code is below:

```using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace extraworkassignment

{

class Program

{

static void Main(string[] args)

{

int
Square,
Cube;

Console.WriteLine("\tNumber\tSquare\tCube\tSquareroot\tTotal");

//loop 12 times calculating and displaying values

for (int Number = 1; Number <= 12;

Number++)

{
Square = Number * Number;

Cube = Square * Number;

Math.Sqrt(Number);

Total = Square * Math.Sqrt(Number);

Console.WriteLine("\t{0}\t{1}\t{2}\t{3}\t{4}", Number, Square, Cube, Math.Sqrt(Number), Total);

}// end of calculate and display

}

public static double Total { get; set; }
}

}

```

Is This A Good Question/Topic? 0

## Replies To: Grand Total of Square Root Question

### #2 Curtis Rutland

• （╯°□°）╯︵ (~ .o.)~

Reputation: 5102
• Posts: 9,283
• Joined: 08-June 10

## Re: Grand Total of Square Root Question

Posted 16 February 2012 - 01:37 PM

Well, first, you haven't declared Total, so you won't be able to use it. Then initialize it to zero, and just add the square root to it each time.

Oh, and line 35 is pointless. You're performing a sqrt calculation, but you're not assigning it to anything, so it makes no sense.

### #3 tlhIn`toq

• Xamarin Cert. Dev.

Reputation: 6528
• Posts: 14,435
• Joined: 02-June 10

## Re: Grand Total of Square Root Question

Posted 16 February 2012 - 02:07 PM

Curtis Rutland, on 16 February 2012 - 02:37 PM, said:

Well, first, you haven't declared Total, so you won't be able to use it.

Isn't it that property on line 51?

The code does work, does run and does total. The alignment needs a little help is about all I can see.

MrBrightSide: You need to be more clear about what the problem is.
"I'm having trouble with the total" tells us nothing. In what why are you having trouble? Wrong answers? Answer is always zero? Exception errors?

Reminder to new folks: This is homework and we don't provide completed code for homework. Rather we try to help the student comprehend the concept they are struggling with so they can write their own code. They are being graded on their skill not ours.

This is the 'Help me with my homework' article you wanted.
Stages of asking for homework help on a forum.

rookie What this shows us is that you aren't familiar with breakpoints and how to debug your own code. Learning to debug one's own code is an essential skill. Sadly, one that apparently few college courses teach. Silly if you ask me.

Placing breakpoints and walking through the code line by line allows you to actually WATCH it execute. Visualizing what your code does will let you see why it behaves the way it does.

It would be well worth your time to do the tutorials on FAQ 2. A couple hours learning this skill will save you hundreds of hours of confusion in one project alone.

What does this error message mean?
FAQ 2: How do I debug
FAQ 3: How do I make Form1 talk to Form2

FAQ (Frequently Asked Questions - Updated Feb 2012
Spoiler

I would also suggest you take the time to clean up your code lines.
I know it sounds picky and unimportant, but if it is hard to read, hard to determine which blocks are within other blocks due to lack of indenting etc. etc. it just compounds the difficulty in debugging.

Look at your code block above, then look at the same code after it is cleaned up? Which would you rather try to debug?

```using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace extraworkassignment
{

class Program
{

static void Main(string[] args)
{

int Square, Cube;

Console.WriteLine("\tNumber\tSquare\tCube\tSquareroot\tTotal");

//loop 12 times calculating and displaying values
for (int Number = 1; Number <= 12; Number++)
{
Square = Number * Number;
Cube = Square * Number;
Math.Sqrt(Number);

Total = Square * Math.Sqrt(Number);

Console.WriteLine("\t{0}\t{1}\t{2}\t{3}\t{4}", Number, Square, Cube, Math.Sqrt(Number), Total);

}// end of calculate and display

}
public static double Total { get; set; }
}

}

```

### #4 tlhIn`toq

• Xamarin Cert. Dev.

Reputation: 6528
• Posts: 14,435
• Joined: 02-June 10

## Re: Grand Total of Square Root Question

Posted 16 February 2012 - 02:19 PM

You do realize your Total is a RUNNING TOTAL of every line, right?
Is that what you wanted? If not, you need to re-zero it during each iteration of your loop. Otherwise you get what you see in that screen shot, where each iteration just adds to the results from the previous time through the loop.

### #5 mrbrightside

Reputation: 0
• Posts: 7
• Joined: 23-January 12

## Re: Grand Total of Square Root Question

Posted 16 February 2012 - 02:22 PM

Thank you for your reply and feedback on the code, I'll try to clean it up...just starting on coding with C# and it's been mid-blowing already...I refuse to give up though...I will learn this..

To be clear about my question is that I need help with the grand total of square root, I don't know what to use to calculate and it also need to display in the bottom of all those numbers with the calculation total of all square root. What would I use to calculate the total amount and display it in the bottom?

This post has been edited by tlhIn`toq: 16 February 2012 - 02:29 PM
Reason for edit:: Please don't quote the entirity of the previous post: We can already see it.

### #6 tlhIn`toq

• Xamarin Cert. Dev.

Reputation: 6528
• Posts: 14,435
• Joined: 02-June 10

## Re: Grand Total of Square Root Question

Posted 16 February 2012 - 02:31 PM

YOu are already creating a grand total. The variable TOTAL.
In what way is that not the total you want?

### #7 Curtis Rutland

• （╯°□°）╯︵ (~ .o.)~

Reputation: 5102
• Posts: 9,283
• Joined: 08-June 10

## Re: Grand Total of Square Root Question

Posted 16 February 2012 - 02:39 PM

tlhIn`toq, on 16 February 2012 - 03:07 PM, said:

Curtis Rutland, on 16 February 2012 - 02:37 PM, said:

Well, first, you haven't declared Total, so you won't be able to use it.

Isn't it that property on line 51?

The code does work, does run and does total. The alignment needs a little help is about all I can see.

Good call. Didn't scroll down that far. I'm not used to seeing people declare properties below where they're used, though it's perfectly valid code.