C
C#14mo 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
phaseshift14mo ago
Do you know how to get 'the entered number'?
rallez
rallez14mo ago
yes i know that, like i know basics of c# but I dont have idea for this task
phaseshift
phaseshift14mo 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
rallez14mo 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
phaseshift14mo 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
Rist14mo ago
Note that numbers start repeating exactly in the middle of your output
rallez
rallez14mo 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
phaseshift14mo ago
That look overly complex
Rist
Rist14mo ago
This seems a bit overkill. Unless there were more conditions for this problem?
rallez
rallez14mo 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
Rist14mo 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
rallez14mo 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
Pascal14mo ago
This can be accomplished with a single for loop. Try applying this suggestion from @R.
rallez
rallez14mo 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
Rist14mo ago
you can just put i <= n / 2 in you for loop condition
rallez
rallez14mo ago
oh yeah
Rist
Rist14mo ago
Also, I'd recommend int.Parse instead of Convert.ToInt32 or even int.TryParse so you can handle invalid input
rallez
rallez14mo 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
Rist14mo 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
rallez14mo ago
THANKS!