C#: Generate a cartesian product of three sets
C# Sharp LINQ : Exercise-24 with Solution
Write a program in C# Sharp to generate a cartesian product of three sets.
Sample Solution:
C# Sharp Code:
using System;
using System.Linq;
using System.Collections.Generic;
class LinqExercise24
{
public static void Main(string[] args)
{
// Declaring arrays of characters, integers, and strings
char[] charset1 = { 'X', 'Y', 'Z' };
int[] numset1 = { 1, 2, 3 };
string[] colorset1 = { "Green", "Orange" };
// Printing a prompt for the Cartesian product generation
Console.Write("\nLINQ : Generate a cartesian product of three sets : ");
Console.Write("\n----------------------------------------------------\n");
// Using LINQ to generate the Cartesian product of the three sets
var cartesianProduct = from letter in charset1
from number in numset1
from colour in colorset1
select new { letter, number, colour };
// Displaying the generated Cartesian product
Console.Write("The Cartesian Product are : \n");
foreach (var ProductList in cartesianProduct)
{
// Printing each element of the Cartesian product
Console.WriteLine(ProductList);
}
// Waiting for user input before closing the console application
Console.ReadLine();
}
}
OR
using System;
using System.Linq;
using System.Collections.Generic;
class LinqExercise24
{
public static void Main(string[] args)
{
char[] set1 = { 'X', 'Y', 'Z' };
int[] set2 = { 1, 2, 3 };
string[] set3 = { "Green", "Pink" };
Console.Write("\nLINQ : Generate a cartesian product of three sets: ");
Console.Write("\n---------------------------------------------------\n");
var set12CartesianProduct = set1.SelectMany(
letterList => set2.Select(numList =>
new { letterList, numList }));
var cartesianProduct = set12CartesianProduct.SelectMany(
(set1and2) => set3.Select(colorList =>
new { set1and2.letterList, set1and2.numList, colorList }));
Console.Write("The Cartesian Product are : \n");
foreach (var ProductList in cartesianProduct)
{
Console.WriteLine(ProductList);
}
Console.ReadLine();
}
}
Sample Output:
LINQ : Generate a cartesian product of three sets : ---------------------------------------------------- The Cartesian Product are : { letter = X, number = 1, colour = Green } { letter = X, number = 1, colour = Orange } { letter = X, number = 2, colour = Green } { letter = X, number = 2, colour = Orange } { letter = X, number = 3, colour = Green } { letter = X, number = 3, colour = Orange } { letter = Y, number = 1, colour = Green } { letter = Y, number = 1, colour = Orange } { letter = Y, number = 2, colour = Green } { letter = Y, number = 2, colour = Orange } { letter = Y, number = 3, colour = Green } { letter = Y, number = 3, colour = Orange } { letter = Z, number = 1, colour = Green } { letter = Z, number = 1, colour = Orange } { letter = Z, number = 2, colour = Green } { letter = Z, number = 2, colour = Orange } { letter = Z, number = 3, colour = Green } { letter = Z, number = 3, colour = Orange }
Flowchart:
C# Sharp Code Editor:
Contribute your code and comments through Disqus.
Previous: Write a program in C# Sharp to generate a Cartesian Product of two sets.
Next: Write a program in C# Sharp to generate an Inner Join between two data sets.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://www.w3resource.com/csharp-exercises/linq/csharp-linq-exercise-24.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics