DO NOT DEAL WITH THIS MEMBER
The following code won't show the correct answer.
(Suppose to show the answer to highest number - lowest number.)
code:
Code string texti = textBox1.Text;
string[] strengur = texti.Split(',');
int[] tolur = new int[texti.Length];
int mismunur = 0 ;
//var hætta
for (int i = 0; i < strengur.Length; i++)
{
tolur[i] = Convert.ToInt32(strengur[i]);
}
//test
MessageBox.Show(tolur.Max()+","+tolur.Min());
//test
mismunur = tolur.Max()-tolur.Min();
MessageBox.Show("Lesnar voru inn tölurnar:\n"+texti+"\n En mismunurinn á hæstu og lægstu tölunni er:"+mismunur);
Change this line:
Code int[] tolur = new int[texti.Length];
to
Code int[] tolur = new int[tolur.Length];
DO NOT DEAL WITH THIS MEMBER
(Feb 3, 2012 01:24 PM)Silicon Wrote: Change this line:
Code int[] tolur = new int[texti.Length];
to
Code int[] tolur = new int[tolur.Length];
Just got an error "the use of an unassigned variable"
By the way that line I use to be sure the array has enough "compartments" for the numbers in the textbox1 which are separated by a ",".
Everything works, even the .max number and the .min numbers are correct it's just that the subtraction doesn't seem to work. =/
//full code with irrelevant stuff
Code using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Verk_3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
if (textBox1.Text != null)
{
string texti = textBox1.Text;
string[] strengur = texti.Split(',');
int[] tolur = new int[texti.Length];
int mismunur = 0 ;
//var hætta
for (int i = 0; i < strengur.Length; i++)
{
tolur[i] = Convert.ToInt32(strengur[i]);
}
//test
MessageBox.Show(tolur.Max()+","+tolur.Min());
//test
mismunur = tolur.Max()-tolur.Min();
MessageBox.Show("Lesnar voru inn tölurnar:\n"+texti+"\n En mismunurinn á hæstu og lægstu tölunni er:"+mismunur);
}
}
}
}
}
This is the working code for UInt64 numbers which mean that you can insert From 0 to 18,446,744,073,709,551,615 which equals 2^64 − 1 numbers.
I have also commented the code so to be understandable.
This is Consoled based algorythm just change it and modify it into your app!
Code //First of all we need UInt64 to calculate really long numbers.
string Input = "18446744073709551615,100";
/*We split the numbers in the text by ',' (comma)
and put them in an array named Numbers*/
string[] Numbers = Input.Split(',');
//This will help us to convert the string into number.
UInt64[] ConvertedNum = new UInt64[Numbers.Length];
/*We convert the string into an integer and put it
in the tolur array.*/
for (int i = 0; i < Numbers.Length; i++)
{
ConvertedNum[i] = Convert.ToUInt64(Numbers[i]);
}
//We Find the Difference between the max and the min Number.
UInt64 Difference = ConvertedNum.Max() - ConvertedNum.Min();
Console.WriteLine("Were read into the numbers:\n" + Input +
"\nBut the difference between the highest and lowest number is:\n" + Difference);
Console.ReadKey();
Execution Result:
Code Were read into the numbers:
18446744073709551615,100
But the difference between the highest and lowest number is:
18446744073609551615
@Ghost. Should we read your sound and well in code commented answer as a sign you're irritated by the lack of self involvement of those lazy "students" expecting us to do their homework ?
DO NOT DEAL WITH THIS MEMBER
Feb 3, 2012 06:54 PM
(This post was last modified: Feb 3, 2012 06:57 PM by svavart.)
(Feb 3, 2012 05:12 PM)Gh0st-Zer0 Wrote: This is the working code for UInt64 numbers which mean that you can insert From 0 to 18,446,744,073,709,551,615 which equals 2^64 − 1 numbers.
I have also commented the code so to be understandable.
This is Consoled based algorythm just change it and modify it into your app!
Code //First of all we need UInt64 to calculate really long numbers.
string Input = "18446744073709551615,100";
/*We split the numbers in the text by ',' (comma)
and put them in an array named Numbers*/
string[] Numbers = Input.Split(',');
//This will help us to convert the string into number.
UInt64[] ConvertedNum = new UInt64[Numbers.Length];
/*We convert the string into an integer and put it
in the tolur array.*/
for (int i = 0; i < Numbers.Length; i++)
{
ConvertedNum[i] = Convert.ToUInt64(Numbers[i]);
}
//We Find the Difference between the max and the min Number.
UInt64 Difference = ConvertedNum.Max() - ConvertedNum.Min();
Console.WriteLine("Were read into the numbers:\n" + Input +
"\nBut the difference between the highest and lowest number is:\n" + Difference);
Console.ReadKey();
Execution Result:
Code Were read into the numbers:
18446744073709551615,100
But the difference between the highest and lowest number is:
18446744073609551615
The teacher didn't go in dept about what the usual "int" was capable to do so thanks ;), also thanks for taking time to comment the code. Too many that decide not to.
tolur = numbers
texti = text
strengur = string
Forgot to include.
(Feb 3, 2012 06:46 PM)E25EB8582FF05BF Wrote: @Ghost. Should we read your sound and well in code commented answer as a sign you're irritated by the lack of self involvement of those lazy "students" expecting us to do their homework ? Well he did the half job so i just help a little to complete! Of course i'm not the guy that support lazy Students! I'm a Univercity student and i hate guys that C/P other work and present them us theirs!
Sorry for my last post, I was tired and I changed it to the wrong variable.
Change this line:
Code int[] tolur = new int[texti.Length];
to:
Code int[] tolur = new int[strengur.Length];
If you set it to texti.Length, you are setting the array size to the amount of characters in the text box. What if you had a 10-digit-long number, but only that number? Then you'd have 10 spaces in the array, but only working with 1 number. Because strengur is the text split up via ,, you are setting it to the number of items separated by ,s in the text.
DO NOT DEAL WITH THIS MEMBER
Feb 4, 2012 04:56 AM
(This post was last modified: Feb 4, 2012 04:58 AM by svavart.)
(Feb 3, 2012 06:59 PM)Gh0st-Zer0 Wrote: Well he did the half job so i just help a little to complete! Of course i'm not the guy that support lazy Students! I'm a Univercity student and i hate guys that C/P other work and present them us theirs!
I'm at the early stage of learning the programming basics with c# and there are already plenty of students to lazy to do their own work. Unfortunately for them there is a program that cross-checks all the students projects when you've made it to advanced programming.
(Feb 4, 2012 03:43 AM)Silicon Wrote: If you set it to texti.Length, you are setting the array size to the amount of characters in the text box. What if you had a 10-digit-long number, but only that number? Then you'd have 10 spaces in the array, but only working with 1 number. Because strengur is the text split up via ,, you are setting it to the number of items separated by ,s in the text. Thanks, I understand what you mean now, feel a bit stupid for not noticing that. ;)
(Feb 4, 2012 04:56 AM)svavart Wrote: I'm at the early stage of learning the programming basics with c# and there are already plenty of students to lazy to do their own work. Unfortunately for them there is a program that cross-checks all the students projects when you've made it to advanced programming. Well if it's like that. . .
Good Then! 🙂
|