this is my program by C# but there is error I can not solve it. please help me t
ID: 3767128 • Letter: T
Question
this is my program by C# but there is error I can not solve it. please help me to sove it .
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
namespace HW8
{
class Program
{
void fun(ref int[]arr)
{
for(int i=0;i<=100;i++)
{
arr[i]=i;
}
}
void fun1(int[]arr1)
{
for(int i=0;i<=100;i++)
{
arr1[i]=i;
}
}
static void Main(string[] args)
{
Stopwatch sw1 = new Stopwatch();
Stopwatch sw2 = new Stopwatch();
int[] arr3 = new int[100];
Program p = new Program();
sw2.Start();
for(int i =0;i<=100000;i++)
{
p.fun1(arr3);
}
sw2.Stop();
sw1.Start();
for (int i=0; i<=100000;i++)
{
p.fun(ref arr3);
}
sw1.Stop();
Console.WriteLine("Function called a 100000 times");
Console.WriteLine("when array is passed by reference, execution time= " + sw1.Elapsed.TotalMilliseconds + "millisec");
Console.WriteLine("When array is passed by value, excution time= " + sw2.Elapsed.TotalMilliseconds + "millisec");
Console.ReadLine();
}
}
}
Explanation / Answer
Solved
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
namespace HW8
{
class Program
{
void fun(ref int[] arr)
{
for (int i = 0; i < 100; i++)
{
arr[i] = i;
}
}
void fun1(int[] arr1)
{
for (int i = 0; i < 100; i++)
{
arr1[i] = i;
}
}
static void Main(string[] args)
{
Stopwatch sw1 = new Stopwatch();
Stopwatch sw2 = new Stopwatch();
int[] arr3 = new int[100];
Program p = new Program();
sw2.Start();
for (int i = 0; i <= 100000; i++)
{
p.fun1(arr3);
}
sw2.Stop();
sw1.Start();
for (int i = 0; i <= 100000; i++)
{
p.fun(ref arr3);
}
sw1.Stop();
Console.WriteLine("Function called a 100000 times");
Console.WriteLine("when array is passed by reference, execution time= " + sw1.Elapsed.TotalMilliseconds + "millisec");
Console.WriteLine("When array is passed by value, excution time= " + sw2.Elapsed.TotalMilliseconds + "millisec");
Console.ReadLine();
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.