I am creating a multiplication table, when you input a number and click the calculate button it should display. I have tried watching a few tutorials on YouTube and have checked out some coding forums however I can only find people using the Console Application however I am using the Windows Form Application
1 * 1 = 1
2 * 1 = 2
3 * 1 = 3
4 * 1 = 4
5 * 1 = 5
6 * 1 = 6
7 * 1 = 7
8 * 1 = 8
9 * 1 = 9
10 * 1 = 10
However, when I run the program it only displays
1 * 10 = 10
Here is my code;
private void btnCalc_Click(object sender, EventArgs e)
{
int n, i;
n = Int32.Parse(txtNum.Text);
for (i = 1; i <= 10; ++i)
txtCalc.Text = Convert.ToString(n + " * " + i + " = " + n * i);
}
This loop keeps setting the control's text to a different value over and over, leaving you to see only the final value.
for (i = 1; i <= 10; ++i)
{
txtCalc.Text = Convert.ToString(n + " * " + i + " = " + n * i);
}
A straightforward solution is:
string text = "";
for (i = 1; i <= 10; ++i)
{
text += Convert.ToString(n + " * " + i + " = " + n * i);
}
txtCalc.Text = text;
You will still run into some formatting issues you'll need to solve, but you'll get the fundamental info in there.
You're overwriting the text over and over again. What you want to do is append new text every time through the loop. Try something like:
txtCalc.Text = "";
for (i = 1; i <= 10; ++i)
{
txtCalc.Text += Convert.ToString(n + " * " + i + " = " + n * i) + Environment.NewLine;
}
your txtCalc.Text... overwrites the field in every iteration. You probably want something like this:
txtCalc.Text = "";
for (i = 1; i <= 10; ++i)
{
txtCalc.Text += Convert.ToString(n + " * " + i + " = " + n * i);
}
Related
I've been struggling with this pretty tricky problem for the past week :( I have to find all combinations of numbers that sum up to a given natural number using recursion. I'm not allowed to use LINQ or anything else besides "using system"
For example if the input is 7 then the output should look like this:
1 + 1 + 1 + 1 + 1 + 1 + 1
2 + 1 + 1 + 1 + 1 + 1
2 + 2 + 1 + 1 + 1
2 + 2 + 2 + 1
3 + 1 + 1 + 1 + 1
3 + 2 + 1 + 1
3 + 2 + 2
3 + 3 + 1
4 + 1 + 1 + 1
4 + 2 + 1
4 + 3
5 + 1 + 1
5 + 2
6 + 1
The numbers from the combination should be listed exactly in that order so for an input of 3 for example the output should be exactly as follows:
1 + 1 + 1
2 + 1
For an input of 4 the output should look like this:
1 + 1 + 1 + 1
2 + 1 + 1
2 + 2
3 + 1
For every new list of combinations we increment the first number in the list and then we continue with the remaining part of the previous list until the sum will be equal with the input.
Just positive numbers are allowed between 1 (1 included) and input - 1.
My code so far gives me the following output for the same given input of 7:
+ 1
+ 1 + 1 + 2
+ 1 + 1 + 1
+ 2 + 2 + 3
+ 1 + 1 + 1
+ 1 + 1 + 2
+ 2 + 1
+ 1 + 1 + 2
+ 2 + 2 + 1
+ 3 + 3 + 4
+ 1 + 1 + 1
+ 1 + 1 + 2
+ 1 + 1 + 1
+ 2 + 2 + 3
+ 2 + 1
+ 1 + 1 + 2
+ 1 + 1 + 1
+ 2 + 2 + 3
...
Can you please help me with some suggestions?
static string GenerateCombinations(int n)
{
string combinationList = "";
for (int index = 1; index < n - 1; index++)
{
string intermediaryList = GenerateCombinations(n - index, index) + " + " + index;
combinationList += intermediaryList;
}
return combinationList + "\n";
}
static string GenerateCombinations(int n, int index)
{
string combinationList = "";
for (int i = 1; i < n - 1; i++)
{
if (i <= index)
{
string intermediaryList = GenerateCombinations(n) + " + " + index;
combinationList += intermediaryList;
}
}
return combinationList;
}
static void Main()
{
int n = Convert.ToInt32(Console.ReadLine());
Console.WriteLine(GenerateCombinations(n));
}
Here is a solution not using collections (only using System;) and generating the output in the required order.
public static void PrintCombinations(int n)
{
PrintRest("", 0, n, n - 1);
}
private static void PrintRest(string listStart, int startSum, int n, int max)
{
for (int i = 1; i <= max; i++) {
string list = listStart.Length > 0
? listStart + " + " + i.ToString()
: i.ToString();
int sum = startSum + i;
if (sum == n) {
Console.WriteLine(list);
} else if (sum < n) {
PrintRest(list, sum, n, i);
}
}
}
You would call it as
PrintCombinations(7);
It starts by taking all possible start summands and calling itself to construct the rest of the sum. The combinations up to the current point are passed as string parameter listStart. The sum it represents is passed as int startSum. The target sum is n. max is the biggest summand allowed.
Try following :
class Program
{
static List<string> combinationList = new List<string>();
const int SUM = 7;
static void Main(string[] args)
{
List<int> numbers = new List<int>();
GenerateCombinations(numbers, 0);
combinationList.Sort();
Console.WriteLine(string.Join("\n", combinationList));
Console.ReadLine();
}
static void GenerateCombinations(List<int> numbers, int sum)
{
int start = 1;
if (numbers.Count > 0) start = numbers[0];
for (int i = start; i <= SUM; i++)
{
int newSum = sum + i;
if (newSum > SUM) break;
List<int> newList = new List<int>(numbers);
newList.Insert(0,i);
if (newSum == SUM)
{
combinationList.Add(string.Join(" + ", newList));
break;
}
else
{
GenerateCombinations(newList, newSum);
}
}
}
}
Since you are looking for a recursive solution, let's do it recursively without Linq and other mean.
Let's start from the basic: when given 0, we have an empty solution:
private static int[][] Solutions(int value) {
if (value <= 0)
return new int[][] { new int[0] };
//TODO: other cases for 1, 2, ...
}
Time to do the next step: if we know how to solve for some n - 1 (n - 1 >= 0) we can solve for n as follow:
all solutions start from m (m < n) are in form
`m + solutions for n - m which uses m .. 1 only`
E.g.
6 + 1 <- starts from 6, solves for 7 - 6 = 1, uses 6..1 only
5 + 2 ...
5 + 1 + 1
4 + 3
4 + 2 + 1
4 + 1 + 1 + 1 ...
3 + 3 + 1 <- starts from 3, solves for 7 - 3 = 4, uses 3..1 only
3 + 2 + 2 <- starts from 3, solves for 7 - 3 = 4, uses 3..1 only
3 + 2 + 1 + 1 <- starts from 3, solves for 7 - 3 = 4, uses 3..1 only
3 + 1 + 1 + 1 + 1 ...
2 + 2 + 2 + 1
2 + 2 + 1 + 1 + 1
2 + 1 + 1 + 1 + 1 + 1 ...
1 + 1 + 1 + 1 + 1 + 1 + 1 <- starts from 1, solves for 7 - 1 = 6, uses 1..1 only
This recursion can be encoded as
private static int[][] Solutions(int value, int startWith = -1) {
if (value <= 0)
return new int[][] { new int[0] };
if (startWith < 0)
startWith = value - 1;
List<int[]> solutions = new List<int[]>();
for (int i = Math.Min(value, startWith); i >= 1; --i)
foreach (int[] solution in Solutions(value - i, i)) {
int[] next = new int[solution.Length + 1];
Array.Copy(solution, 0, next, 1, solution.Length);
next[0] = i;
solutions.Add(next);
}
// Or just (if we are allow a bit of Linq)
// return solutions.ToArray();
int[][] answer = new int[solutions.Count][];
for (int i = 0; i < solutions.Count; ++i)
answer[i] = solutions[i];
return answer;
}
Demo
var result = Solutions(7);
// A pinch of Linq for demonstration
string report = string.Join(Environment.NewLine, result
.Select(solution => string.Join(" + ", solution)));
Console.Write(report);
Outcome:
6 + 1
5 + 2
5 + 1 + 1
4 + 3
4 + 2 + 1
4 + 1 + 1 + 1
3 + 3 + 1
3 + 2 + 2
3 + 2 + 1 + 1
3 + 1 + 1 + 1 + 1
2 + 2 + 2 + 1
2 + 2 + 1 + 1 + 1
2 + 1 + 1 + 1 + 1 + 1
1 + 1 + 1 + 1 + 1 + 1 + 1
Issue is that TotalRows is about 71800 where the workList only returns 718 which is only the first result of the Task. I have the WaitAll there but it seems to finish as soon as the first task is done.
TotalRows = GetRowCount();
var lastRecord = 0;
List<tmpWBITEMALL> workList = new List<tmpWBITEMALL>();
for (int i = 0; i < 100; i++)
{
var tmpI = i;
gatherTmpTasks.Add(Task.Factory.StartNew(() =>
{
var context = new AS400_PIM5ContextDataContext();
context.CommandTimeout = 0;
int amount = (TotalRows / 100);
int tmplastRecord = lastRecord;
Interlocked.Add(ref lastRecord, amount);
Console.WriteLine("Getting rows " + tmplastRecord+ " to " + (tmplastRecord + amount));
var pagedResult = context.ExecuteQuery<tmpWBITEMALL>("SELECT * FROM (SELECT ROW_NUMBER() OVER ( ORDER BY Id ) AS RowNum, * from tmpWBITEMALL) AS RowConstrainedResult WHERE RowNum >= " + tmplastRecord+ " AND RowNum < " + amount + " ORDER BY RowNum");
lock (listLock)
workList.AddRange(pagedResult);
context.Dispose();
}));
}
Task.WaitAll(gatherTmpTasks.ToArray());
Console.WriteLine("total work: " + workList.Count + " tasks: " + gatherTmpTasks.Count);
So as reference gatherTmpTasks.Count returns 100 but workList.Count is only 718 where as listLock is just a static new object(). If didn't notice already I am using LINQ to SQL
Anyone have ideas why my list isn't the same size as TotalRows?
" AND RowNum < " + amount: amount is always 718, so you are asking the query to always return things between tmplastRecord and 718, NOT inbetween tmplastRecord and tmplastRecord + amount. I think you just need to change to " AND RowNum < " + (tmplastRecord + amount)
Wise man
for (int k = 0; k < 32;k=k+1)
{
for (int j = 1; j < 32; j++)
{
double Score = (user0[k] * user0[j] + user1[k] * user1[j] + user2[k] * user2[j] + user3[k] * user3[j] + user4[k] * user4[j] + user5[k] * user5[j] + user6[k] * user6[j] + user7[k] * user7[j] + user8[k] * user8[j] + user9[k] * user9[j] + user10[k] * user10[j] + user11[k] * user11[j] + user12[k] * user12[j] + user13[k] * user13[j] + user14[k] * user14[j] + user15[k] * user15[j] + user16[k] * user16[j] + user17[k] * user17[j] + user18[k] * user18[j] + user19[k] * user19[j] + user20[k] * user20[j] + user21[k] * user21[j] + user22[k] * user22[j] + user23[k] * user23[j] + user24[k] * user24[j] + user25[k] * user25[j] + user26[k] * user26[j] + user27[k] * user27[j] + user28[k] * user28[j] + user29[k] * user29[j] + user30[k] * user30[j] + user31[k] * user31[j]) / ((Math.Sqrt(user0[k] * user0[k] + user1[k] * user1[k] + user2[k] * user2[k] + user3[k] * user3[k] + user4[k] * user4[k] + user5[k] * user5[k] + user6[k] * user6[k] + user7[k] * user7[k] + user8[k] * user8[k] + user9[k] * user9[k] + user10[k] * user10[k] + user11[k] * user11[k] + user12[k] * user12[k] + user13[k] * user13[k] + user14[k] * user14[k] + user15[k] * user15[k] + user16[k] * user16[k] + user17[k] * user17[k] + user18[k] * user18[k] + user19[k] * user19[k] + user20[k] * user20[k] + user21[k] * user21[k] + user22[k] * user22[k] + user23[k] * user23[k] + user24[k] * user24[k] + user25[k] * user25[k] + user26[k] * user26[k] + user27[k] * user27[k] + user28[k] * user28[k] + user29[k] * user29[k] + user30[k] * user30[k] + user31[k] * user31[k])) * (Math.Sqrt(user0[j] * user0[j] + user1[j] * user1[j] + user2[j] * user2[j] + user3[j] * user3[j] + user4[j] * user4[j] + user5[j] * user5[j] + user6[j] * user6[j] + user7[j] * user7[j] + user8[j] * user8[j] + user9[j] * user9[j] + user10[j] * user10[j] + user11[j] * user11[j] + user12[j] * user12[j] + user13[j] * user13[j] + user14[j] * user14[j] + user15[j] * user15[j] + user16[j] * user16[j] + user17[j] * user17[j] + user18[j] * user18[j] + user19[j] * user19[j] + user20[j] * user20[j] + user21[j] * user21[j] + user22[j] * user22[j] + user23[j] * user23[j] + user24[j] * user24[j] + user25[j] * user25[j] + user26[j] * user26[j] + user27[j] * user27[j] + user28[j] * user28[j] + user29[j] * user29[j] + user30[j] * user30[j] + user31[j] * user31[j])));
if (Score > simScore)
{
simScore = Score;
}
}
System.Console.WriteLine("Score =" + simScore);
}
I think you can ignore the long equation.
I need this program to compare 1 book to 32 other books.
The for (int k = 0; k < 5;k=k+1)loop is for the one selected book to be compared to 32 others (then give the greatest similarity rating).
The for (int j = 1; j < 32; j++) loop is to allow all the different books to be compared to the the book selected (book k).
The problem is that a book cannot be compared to itself because I think it ruins the equation and I get non-sensical values for the similarity ratings.
How can I omit a book?
(For example: When comparing k=3 (book 3) to other books, how can I make it so that j will not use the same reference?
Start the second loop from one past the first. Comparing the pair (a,b) is the same as comparing (b,a), so you don't need to start from the beginning with each subsequent book.
for (var k = 0; k < 5; k++)
{
for (var j = k + 1; j < 5; j++)
{
....
// if you need the complete set, you can store both
rating[j][k] = ...
rating[k][j] = ...
}
}
The most straightforward way would be to put an if statement that only evaluates the body of the second loop if k != j. I'm not familiar with C#, so there may be other ways of doing it that I'm not aware of, but this is how I would do it in Python.
How about using continue?
for (int k = 0; k < 32; k++) {
for (int j = 0; j < 32; j++) {
if (j == k)
continue;
// Compare
if (isMatch)
break;
}
}
I have created a custom Jquery Timer. but i am facing little problem i dont know that is not working for me. below is my code.
function show(Hos, mins, secds) {
var hours = Hos;
var minutes = mins;
var seconds = secds;
var dn = "AM";
if (hours > 12) {
dn = "PM"
hours = hours - 12
}
if (hours == 0)
hours = 12
document.getElementById('<%= Label1.ClientID %>').innerHTML = hours + ":" + minutes + ":" + seconds + " " + dn
if (parseInt(seconds) == 59) {
seconds = 0;
if (parseInt(minutes) == 59) {
if (parseInt(hours) == 12) {
hours = 0;
} else {
hours = parseInt(hours) + 1;
}
} else {
minutes = parseInt(minutes) + 1;
}
} else {
seconds = parseInt(seconds) + 1;
}
setTimeout("show('" + hours + "','" + minutes + "','" + seconds + "'" + " )", 1000)
}
This code is working fine i am passing the hours,mins,seconds first time from the code behind using c#.Now my problem is i want to add "0" if the seconds is less than 9 and minutes less than 9 and hours less than 9. i have tried the following trick but i dont know why its not working for me..
if (seconds <= 9) {
seconds = '0' + parseInt(seconds);
}
Please help me..Actually what happens when i tried this . its concatenate 0 with seconds upto 9 but as 9 comes it restarts from 1. That is the problem.
seconds = '0' + parseInt(seconds) will do string addition, since '0', is a string.
When you add a string to a number in javascript, the number will be appended to the string.
Remove the quotes for 0, then it will be treated like number.
P.S. I guess this is the problem you are trying to crack
i have resolve this problem using some trick that is as follow:
function show(Hos, mins, secds) {
var hours = Hos;
var minutes = mins;
var seconds = parseInt(secds, 10);
if (hours == 0)
hours = 12
if (parseInt(seconds, 10) < 10) {
seconds = "0" + parseInt(seconds, 10);
}
if (parseInt(hours, 10) < 10) {
hours= "0" + parseInt(hours, 10);
}
if (parseInt(minutes, 10) < 10) {
minutes= "0" + parseInt(minutes, 10);
}
document.getElementById('<%= Label1.ClientID %>').innerHTML = hours + ":" + minutes + ":" + seconds + " "
if (parseInt(seconds) == 59) {
seconds = 0;
if (parseInt(minutes, 10) == 59) {
if (parseInt(hours, 10) == 24) {
hours = 0;
} else {
hours = parseInt(hours, 10) + 1;
}
} else {
minutes = parseInt(minutes, 10) + 1;
}
} else {
seconds = parseInt(seconds, 10) + 1;
}
setTimeout("show('" + hours + "','" + minutes + "','" + seconds + "'" + " )", 1000)
}
I would like to ask you about this programing part, is it everything ok?
the task was:
Write the pseudocode or flow diagram and code for the theorem of Pythagoras - for right-angle triangle with three ribs (a, b, and c) of type integer
int KendiA = 0;
int KendiB = 0;
int H = 0;
string Trekendeshi = null;
int gjetja = 0;
for (KendiA = 1; KendiA <= 15; KendiA++)
{
for (KendiB = 1; KendiB <= 15; KendiB++)
{
for (H = 1; H <= 30; H++)
{
if ((Math.Pow(KendiA, 2) + Math.Pow(KendiB, 2) == Math.Pow(H, 2)))
{
gjetja = gjetja + 1;
Trekendeshi = gjetja + "\t" + KendiA + "\t" + KendiB + "\t" + H;
Console.WriteLine(Trekendeshi);
}
}
}
}
It's much easier to find pythagorean triples than to iterate through every set of three. Have a look at http://en.wikipedia.org/wiki/Formulas_for_generating_Pythagorean_triples for instance.
Also, it's better to square integers by multiplying it with itself rather than using pow(i,2).
Your code works well but it prints repeated ones. Count KendiB from KendiA solves your problem
for (KendiA = 1; KendiA <= 15; KendiA++){
for (KendiB = KendiA; KendiB <= 15; KendiB++){
Here is simplest way
for(int i=2; i<10; i++){
int a = 2*i;
int b = i*i-1;
int c = i*i+1;
System.out.println(a + " " + b + " " + c);
}