Run the following console application. This program should produce the union of two sets, but has several errors. Use a variety of debugging techniques to locate and correct the errors.
In C#:
Run the following console application. This program should produce the union of two sets, but has several errors. Use a variety of debugging techniques to locate and correct the errors.
NOTE: A set is a collection of like elements that does not contain any duplicates. The union of two sets is the set that contains all the elements in both sets. Formally:
A union B = {x : x Î A or x Î B}
read: A union B is the set of all x such that x is in A or x is in B
Example: A = {1,2,3,4,5} and B = {2,4,6,8}, then A union B = {1,2,3,4,5,6,8} (notice, no duplicates!)
Debug the program. When you find an error, comment out the offending line, give an explanation of the error, write a corrected line, and include a screenshot of your program running with successful output. Submit the modified files and screenshots of any breakpoints you use.
Program
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Activity_10
{
class Program
{
static void Main( string[] args )
{
//make some sets
Set A = new Set( );
Set B = new Set( );
//put some stuff in the sets
Random r = new Random( );
for(int i = 0; i < 10; i++)
{
A.addElement( r.Next( 4 ) );
B.addElement( r.Next( 12 ) );
}
//display each set and the union
Console.WriteLine("A: " + A);
Console.WriteLine("B: " + B);
Console.WriteLine("A union B: " + A.union(B));
//display original sets (should be unchanged)
Console.WriteLine("After union operation");
Console.WriteLine("A: " + A);
Console.WriteLine("B: " + B);
}
}
}
Set
Describe several debugging techniques and scenarios for their use.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
//find the errors!
namespace Activity_10
{
class Set
{
private List<int> elements;
public Set( )
{
elements = new List<int>( );
}
public bool addElement(int val )
{
if (containsElement( val )) return false;
else
{
elements.Add( val );
return true;
}
}
private bool containsElement(int val )
{
for(int i = 0; i < elements.Count; i++)
{
if (val == elements[ i ])
return true;
else
return false;
}
return false;
}
public override string ToString( )
{
string str = "";
foreach (int i in elements)
{
str += i + " ";
}
return str;
}
public void clearSet( )
{
elements.Clear( );
}
public Set union(Set rhs )
{
for(int i = 0; i < rhs.elements.Count; i++)
{
this.addElement( rhs.elements[ i ] );
}
return rhs;
}
}
}

Trending now
This is a popular solution!
Step by step
Solved in 4 steps with 6 images









