C
C#2y ago
rallez

print out all pairs of natural numbers whose sum isequal to the entered number n

Write an algorithm that prints out all pairs of natural numbers whose sum is equal to the entered number n. Example: n = 7 Paris: 0 + 7 1 + 6 2 + 5 3 + 4 I have no clue how to introduce it into c# lang
20 Replies
phaseshift
phaseshift2y ago
Do you know how to get 'the entered number'?
rallez
rallezOP2y ago
yes i know that, like i know basics of c# but I dont have idea for this task
phaseshift
phaseshift2y ago
Well get started on it, then come back with where you're actually stuck Are you sure you're stuck on the c#? Or perhaps it's actually the algorithm?
rallez
rallezOP2y ago
My issue is actually algorithm. I can't figure it out i was able to print all pairs but they were not unique so for n = 7 i got 0 + 7 1 + 6 2 + 5 3 + 4 4 + 3 5 + 2 6 + 1 7+ 0 instead of 0 + 7 1 + 6 2 + 5 3 + 4
phaseshift
phaseshift2y ago
Aha Well there's two relationships that can solve they problem for you Compare one number in the pair to the other, which is bigger? That's one way. Or count how many pairs you've made compared to the input
Rist
Rist2y ago
Note that numbers start repeating exactly in the middle of your output
rallez
rallezOP2y ago
okay i have something like that and i think its works well could you review it please?
// Zadanie 7
Console.Write("Podaj n: ");
int n = Convert.ToInt32(Console.ReadLine());
int[] tab = new int[n + 1];
for (int i = 0; i < n + 1; i++)
{
tab[i] = i;
}
var pary = Parowanie.Extract(tab, n);
foreach (var para in pary)
{
Console.WriteLine($"{para.Item1} + {para.Item2}");
}

if (n % 2 == 0)
{
Console.WriteLine("{0} + {1}", n / 2, n / 2);
}

public class Parowanie
{
public static List<Tuple<int,int>> Extract(IEnumerable<int> input, int n)
{
int polowa = n / 2;
var posortowane = input.Where(n => n <= polowa).OrderBy(s => s);

var pary = new List<Tuple<int,int>>();
foreach (var obecna in posortowane)
{
var wynik = n - obecna;
if (input.Where(n => n > obecna).Contains(wynik))
pary.Add(Tuple.Create(obecna, wynik));
}

return pary;
}
}
// Zadanie 7
Console.Write("Podaj n: ");
int n = Convert.ToInt32(Console.ReadLine());
int[] tab = new int[n + 1];
for (int i = 0; i < n + 1; i++)
{
tab[i] = i;
}
var pary = Parowanie.Extract(tab, n);
foreach (var para in pary)
{
Console.WriteLine($"{para.Item1} + {para.Item2}");
}

if (n % 2 == 0)
{
Console.WriteLine("{0} + {1}", n / 2, n / 2);
}

public class Parowanie
{
public static List<Tuple<int,int>> Extract(IEnumerable<int> input, int n)
{
int polowa = n / 2;
var posortowane = input.Where(n => n <= polowa).OrderBy(s => s);

var pary = new List<Tuple<int,int>>();
foreach (var obecna in posortowane)
{
var wynik = n - obecna;
if (input.Where(n => n > obecna).Contains(wynik))
pary.Add(Tuple.Create(obecna, wynik));
}

return pary;
}
}
phaseshift
phaseshift2y ago
That look overly complex
Rist
Rist2y ago
This seems a bit overkill. Unless there were more conditions for this problem?
rallez
rallezOP2y ago
// Zadanie 7
int n;
Console.Write("Podaj n: ");
n = Convert.ToInt32(Console.ReadLine());
int[] tab = new int[n + 1];
for (int i = 0; i < n + 1; i++)
{
tab[i] = i;
}

var pary = new List<Tuple<int,int>>();
foreach (var obecna in tab)
{
var wynik = n - obecna;
if (tab.Where(n => n > obecna).Contains(wynik) || obecna == wynik)
pary.Add(Tuple.Create(obecna, wynik));
}

foreach (var para in pary)
{
Console.WriteLine($"{para.Item1} + {para.Item2}");
}
// Zadanie 7
int n;
Console.Write("Podaj n: ");
n = Convert.ToInt32(Console.ReadLine());
int[] tab = new int[n + 1];
for (int i = 0; i < n + 1; i++)
{
tab[i] = i;
}

var pary = new List<Tuple<int,int>>();
foreach (var obecna in tab)
{
var wynik = n - obecna;
if (tab.Where(n => n > obecna).Contains(wynik) || obecna == wynik)
pary.Add(Tuple.Create(obecna, wynik));
}

foreach (var para in pary)
{
Console.WriteLine($"{para.Item1} + {para.Item2}");
}
Rist
Rist2y ago
You're still overcomplicating it. Also you can define tuples like (int, int) and you can create them like this: (1, 2) You don't really need to store your values anywhere, you just need to output them. Try to compute the values you need to show right there in your for loop.
rallez
rallezOP2y ago
// Zadanie 7
int n;
Console.Write("Podaj n: ");
n = Convert.ToInt32(Console.ReadLine());
int[] tab = new int[n + 1];
for (int i = 0; i < n + 1; i++)
{
tab[i] = i;
}

foreach (var num in tab)
{
if (tab.Where(n => n > num).Contains(n - num) || num == n - num)
Console.WriteLine($"{num} + {n - num}");
}
// Zadanie 7
int n;
Console.Write("Podaj n: ");
n = Convert.ToInt32(Console.ReadLine());
int[] tab = new int[n + 1];
for (int i = 0; i < n + 1; i++)
{
tab[i] = i;
}

foreach (var num in tab)
{
if (tab.Where(n => n > num).Contains(n - num) || num == n - num)
Console.WriteLine($"{num} + {n - num}");
}
what you think?
Pascal
Pascal2y ago
This can be accomplished with a single for loop. Try applying this suggestion from @R.
rallez
rallezOP2y ago
// Zadanie 7
int n;
Console.Write("Podaj n: ");
n = Convert.ToInt32(Console.ReadLine());
int[] tab = new int[n + 1];
for (int i = 0; i < n + 1; i++)
{
tab[i] = i;
if (tab.Where(n => n < i).Contains(n - i) || i == n - i)
Console.WriteLine($"{i} + {n - i}");
}
// Zadanie 7
int n;
Console.Write("Podaj n: ");
n = Convert.ToInt32(Console.ReadLine());
int[] tab = new int[n + 1];
for (int i = 0; i < n + 1; i++)
{
tab[i] = i;
if (tab.Where(n => n < i).Contains(n - i) || i == n - i)
Console.WriteLine($"{i} + {n - i}");
}
int n;
Console.Write("Podaj n: ");
n = Convert.ToInt32(Console.ReadLine());

for (int i = 0; i <= n; i++)
{
if (i <= n / 2)
{
Console.WriteLine($"{i} + {n - i}");
}
}
int n;
Console.Write("Podaj n: ");
n = Convert.ToInt32(Console.ReadLine());

for (int i = 0; i <= n; i++)
{
if (i <= n / 2)
{
Console.WriteLine($"{i} + {n - i}");
}
}
Rist
Rist2y ago
you can just put i <= n / 2 in you for loop condition
rallez
rallezOP2y ago
oh yeah
Rist
Rist2y ago
Also, I'd recommend int.Parse instead of Convert.ToInt32 or even int.TryParse so you can handle invalid input
rallez
rallezOP2y ago
// Zadanie 7
Console.Write("Podaj n: ");
int n = Convert.ToInt32(Console.ReadLine());

for (int i = 0; i <= n / 2; i++)
{
Console.WriteLine($"{i} + {n - i}");
}
// Zadanie 7
Console.Write("Podaj n: ");
int n = Convert.ToInt32(Console.ReadLine());

for (int i = 0; i <= n / 2; i++)
{
Console.WriteLine($"{i} + {n - i}");
}
what is the difference betweene parse and convert ?
Rist
Rist2y ago
Convert.ToInt32 will return 0 if the input is null, int.Parse will throw an exception int.Parse also doesn't require using System, but that's not much of an advantage and if you use int.TryParse you can display an error message when input isn't an integer
rallez
rallezOP2y ago
THANKS!
Want results from more Discord servers?
Add your server