C# homework

Nicedreamsbt

Lifetimer
Joined
Aug 30, 2007
Messages
269
Reaction score
0
Points
16
So I need help on my homework. I am not sure what they are asking me to do or how to do it. The question is

Modify the example of fig 23.3 to process the results by using the array returned by the task produced by task method WhenAll

where is the array being returned ? Do I need to assign it to a new array ?

My teacher wrote this with the task also

Clarification:
The original code (fig 23.3) calls WhenAll to wait, then just uses the task1 and task2 objects to determine start/stop times. This exercise is asking you to get the array returned by WhenAll and use the data in that array to calculate those times. There is some discussion about this in section 23.5.1. It is not really necessary in this case since there are only two tasks--but the idea is that the array could potentially hold a lot of tasks, so you could examine ("process") the times for all of them. Your output will basically be the same after your modification as it was before you made the change.



namespace FibonacciAsynchronous
{
public partial class AsynchronousTestForm : Form
{
public AsynchronousTestForm()
{
InitializeComponent();
}

// start asynchronous calls to Fibonacci
private async void startButton_Click(object sender, EventArgs e)
{
outputTextBox.Text =
"Starting Task to calculate Fibonacci(46)\r\n";

// create Task to perform Fibonacci(46) calculation in a thread
Task<TimeData> task1 = Task.Run(() => StartFibonacci(46));

outputTextBox.AppendText(
"Starting Task to calculate Fibonacci(45)\r\n");

// create Task to perform Fibonacci(45) calculation in a thread
Task<TimeData> task2 = Task.Run(() => StartFibonacci(45));

await Task.WhenAll(task1, task2); // wait for both to complete

// determine time that first thread started
DateTime startTime =
(task1.Result.StartTime < task2.Result.StartTime) ?
task1.Result.StartTime : task2.Result.StartTime;

// determine time that last thread ended
DateTime endTime =
(task1.Result.EndTime > task2.Result.EndTime) ?
task1.Result.EndTime : task2.Result.EndTime;

// display total time for calculations
double totalMinutes = (endTime - startTime).TotalMinutes;
outputTextBox.AppendText(
$"Total calculation time = {totalMinutes:F6} minutes\r\n");
}

// starts a call to fibonacci and captures start/end times
TimeData StartFibonacci(int n)
{
// create a TimeData object to store start/end times
var result = new TimeData();

AppendText($"Calculating Fibonacci({n})");
result.StartTime = DateTime.Now;
long fibonacciValue = Fibonacci(n);
result.EndTime = DateTime.Now;

AppendText($"Fibonacci({n}) = {fibonacciValue}");
double minutes =
(result.EndTime - result.StartTime).TotalMinutes;
AppendText($"Calculation time = {minutes:F6} minutes\r\n");

return result;
}

// Recursively calculates Fibonacci numbers
public long Fibonacci(long n)
{
if (n == 0 || n == 1)
{
return n;
}
else
{
return Fibonacci(n - 1) + Fibonacci(n - 2);
}
}

// append text to outputTextBox in UI thread
public void AppendText(String text)
{
if (InvokeRequired) // not GUI thread, so add to GUI thread
{
Invoke(new MethodInvoker(() => AppendText(text)));
}
else // GUI thread so append text
{
outputTextBox.AppendText(text + "\r\n");
}
}
}
}
 
So I need help on my homework. I am not sure what they are asking me to do or how to do it. The question is

Modify the example of fig 23.3 to process the results by using the array returned by the task produced by task method WhenAll

where is the array being returned ? Do I need to assign it to a new array ?

My teacher wrote this with the task also

Clarification:
The original code (fig 23.3) calls WhenAll to wait, then just uses the task1 and task2 objects to determine start/stop times. This exercise is asking you to get the array returned by WhenAll and use the data in that array to calculate those times. There is some discussion about this in section 23.5.1. It is not really necessary in this case since there are only two tasks--but the idea is that the array could potentially hold a lot of tasks, so you could examine ("process") the times for all of them. Your output will basically be the same after your modification as it was before you made the change.



namespace FibonacciAsynchronous
{
public partial class AsynchronousTestForm : Form
{
public AsynchronousTestForm()
{
InitializeComponent();
}

// start asynchronous calls to Fibonacci
private async void startButton_Click(object sender, EventArgs e)
{
outputTextBox.Text =
"Starting Task to calculate Fibonacci(46)\r\n";

// create Task to perform Fibonacci(46) calculation in a thread
Task<TimeData> task1 = Task.Run(() => StartFibonacci(46));

outputTextBox.AppendText(
"Starting Task to calculate Fibonacci(45)\r\n");

// create Task to perform Fibonacci(45) calculation in a thread
Task<TimeData> task2 = Task.Run(() => StartFibonacci(45));

await Task.WhenAll(task1, task2); // wait for both to complete

// determine time that first thread started
DateTime startTime =
(task1.Result.StartTime < task2.Result.StartTime) ?
task1.Result.StartTime : task2.Result.StartTime;

// determine time that last thread ended
DateTime endTime =
(task1.Result.EndTime > task2.Result.EndTime) ?
task1.Result.EndTime : task2.Result.EndTime;

// display total time for calculations
double totalMinutes = (endTime - startTime).TotalMinutes;
outputTextBox.AppendText(
$"Total calculation time = {totalMinutes:F6} minutes\r\n");
}

// starts a call to fibonacci and captures start/end times
TimeData StartFibonacci(int n)
{
// create a TimeData object to store start/end times
var result = new TimeData();

AppendText($"Calculating Fibonacci({n})");
result.StartTime = DateTime.Now;
long fibonacciValue = Fibonacci(n);
result.EndTime = DateTime.Now;

AppendText($"Fibonacci({n}) = {fibonacciValue}");
double minutes =
(result.EndTime - result.StartTime).TotalMinutes;
AppendText($"Calculation time = {minutes:F6} minutes\r\n");

return result;
}

// Recursively calculates Fibonacci numbers
public long Fibonacci(long n)
{
if (n == 0 || n == 1)
{
return n;
}
else
{
return Fibonacci(n - 1) + Fibonacci(n - 2);
}
}

// append text to outputTextBox in UI thread
public void AppendText(String text)
{
if (InvokeRequired) // not GUI thread, so add to GUI thread
{
Invoke(new MethodInvoker(() => AppendText(text)));
}
else // GUI thread so append text
{
outputTextBox.AppendText(text + "\r\n");
}
}
}
}

I'm not a fan of guessing. Do you have information related to the declaration of WhenAll function? Task appears to be a struct/class, it likely would contain that information. You have a couple of references to locations where text about the code is stored, but don't provide the information on the text. I'm not sure if the information is stored in PDF or is on a hard copy, but either way it seems relevant to answering this question.

While I don't feel that I could answer the question myself, perhaps there are others that can, assuming reviewing the information contained in the references provided don't shine a light on the solution.
 
As I understand it, you're being asked to do the same work but instead slightly differently. The areas you need to focus on are:

Code:
await Task.WhenAll(task1, task2); // wait for both to complete
You asked, where is the array being returned or defined and your answer is here:
Task.WhenAll(TResult) Method (Task(TResult)[]) (System.Threading.Tasks)

Specifically:
If none of the tasks faulted and none of the tasks were canceled, the resulting task will end in the TaskStatus.RanToCompletion state. The Result of the returned task will be set to an array containing all of the results of the supplied tasks in the same order as they were provided (e.g. if the input tasks array contained t1, t2, t3, the output task's Result will return an TResult[] where arr[0] == t1.Result, arr[1] == t2.Result, and arr[2] == t3.Result).

Now the area of the code you'll be changing is this:
Code:
DateTime startTime =
(task1.Result.StartTime < task2.Result.StartTime) ?
task1.Result.StartTime : task2.Result.StartTime;

// determine time that last thread ended
DateTime endTime =
(task1.Result.EndTime > task2.Result.EndTime) ?
task1.Result.EndTime : task2.Result.EndTime;

// display total time for calculations
double totalMinutes = (endTime - startTime).TotalMinutes;
outputTextBox.AppendText(
$"Total calculation time = {totalMinutes:F6} minutes\r\n");
}

The idea is that you won't be using task1 and task2 in the above section of code. I could give you the answer, but I think it's best you try to work it out a little more from here on your own with this information.

In the future, also try to use the CODE tags on the forums to help with code formatting. If you get stuck, post here with your progress.