I recently make a program to calculate sum and avg of a given array. The program also want to print the histogram or stars pattern match with value of index.
Like this:
This is the code I wrote so far:
private void process_Click(object sender, EventArgs e)
{
string temp2 = null;
string temp3 = null;
float sum=0;
int countingNumber = 1;
string message;
int count = Convert.ToInt32(inputArray.Text);
int[] varray = new int[count];
for (int i = 0; i < count; i++)
//for (int j = 1; j <= count; j++)
{
varray[i] = Convert.ToInt32(Interaction.InputBox(message = "enter the value of array number " + countingNumber));
sum = sum+ varray[i];
temp += countingNumber + " "+varray[i] + Environment.NewLine;
temp2 += countingNumber + " " + varray[i] + " *" + Environment.NewLine;
box1.Text = Convert.ToString("Index Value" + Environment.NewLine + temp);
boxSum.Text = Convert.ToString(sum);
boxAvg.Text = Convert.ToString(sum/count);
countingNumber++;
}
for (int stars = 0; stars <= i; stars++)
{
temp3 = " ";
box2.Text = Convert.ToString("Element Value Histogram" + Environment.NewLine + temp2+temp3);
}
}
}
My code won't print the stars match with the value. Could anybody help me?
Try replacing this line:
temp2 += countingNumber + " " + varray[i] + " *" + Environment.NewLine;
with this line:
temp2 += countingNumber + " " + varray[i] + " " + new String('*', i) + Environment.NewLine;
I have a loop that is generating about 150 unique strings. How can I add these strings to an array?
Here is my loop:
for (int intCounter = 0; intCounter < projectCount; intCounter ++)
{
var projectname = project.value[intCounter].name;
var releaseUri = "http://tfs1:8080/tfs/defaultcollection/" + projectname + "/_apis/release/releases?api-version=3.0-preview.2&searchText=911&minCreatedTime=" + date + "T00:00:00.00Z";
Console.WriteLine(releaseUri);
}
The Console.WriteLine(releaseUri) prints each url. but I would like to store the releaseUri in an array.
Lists are normally better than arrays.
var releaseUris = new List<string>();
foreach(var project in projects)
{
var releaseUri = $"http://tfs1:8080/tfs/defaultcollection/" + project.projectname + "/_apis/release/releases?api-version=3.0-preview.2&searchText=911&minCreatedTime=" + project.date + "T00:00:00.00Z";
releaseUris.Add(releaseUri);
}
#ShaneP,
You will need to declare an array outside of the for loop like so.
string[] releaseUriArray = new string[projectCount];
for (int intCounter = 0; intCounter < projectCount; intCounter ++)
{
var projectname = project.value[intCounter].name;
var releaseUri = "http://tfs1:8080/tfs/defaultcollection/" + projectname + "/_apis/release/releases?api-version=3.0-preview.2&searchText=911&minCreatedTime=" + date + "T00:00:00.00Z";
// Here you are adding the releaseUri strings to the releaseUriArray
releaseUriArray[intCounter] = releaseUri;
}
// print your uris from the array here
for (int intCounter = 0; intCounter < projectCount; intCounter ++)
{
var releaseUri = releaseUriArray[intCounter];
Console.WriteLine(releaseUri);
}
You can use an array in this case as you know the number of elements. Initialize it and set the items as you go
var arr = new string[projectCount];
for (int intCounter = 0; intCounter < projectCount; intCounter ++)
{
var projectname = project.value[intCounter].name;
var releaseUri = "http://tfs1:8080/tfs/defaultcollection/" + projectname + "/_apis/release/releases?api-version=3.0-preview.2&searchText=911&minCreatedTime=" + date + "T00:00:00.00Z";
Console.WriteLine(releaseUri);
arr[intCounter] = releaseUri;
}
If you now projectCount then you can create an array with needed element numbers and just set its items by index.
var urls = new string[projectCount];
for (int intCounter = 0; intCounter < projectCount; intCounter ++)
{
var projectname = project.value[intCounter].name;
var releaseUri = "http://tfs1:8080/tfs/defaultcollection/" + projectname + "/_apis/release/releases?api-version=3.0-preview.2&searchText=911&minCreatedTime=" + date + "T00:00:00.00Z";
urls[i] = releaseUri;
}
Or you can just use an dynamic array and add elements using Add() method to be able to change array size after initialization.
var urls = new List<string>();
for (int intCounter = 0; intCounter < projectCount; intCounter ++)
{
var projectname = project.value[intCounter].name;
var releaseUri = "http://tfs1:8080/tfs/defaultcollection/" + projectname + "/_apis/release/releases?api-version=3.0-preview.2&searchText=911&minCreatedTime=" + date + "T00:00:00.00Z";
urls.Add(releaseUri);
}
Also you no need to use loops and can solve your problem with just 1 string of code using LINQ:
var urls = project
.value
.Select(p => "http://tfs1:8080/tfs/defaultcollection/" + p.projectname + "/_apis/release/releases?api-version=3.0-preview.2&searchText=911&minCreatedTime=" + date + "T00:00:00.00Z")
.ToArray();
One easy way to do this would be to create a template Uri that has placeholders for project name and date (using {0} and {1} in the string), then with some Linq extension methods (Select and ToList) and string.Format, you can and generate your strings from an Enumerable.Range:
// Project name and date will be inserted where {0} and {1} are below
string uriTemplate = "http://tfs1:8080/tfs/defaultcollection/{0}/_apis/release/" +
"releases?api-version=3.0-preview.2&searchText=911&minCreatedTime={1}T00:00:00.00Z";
string[] releaseUris = Enumerable.Range(0, projectCount)
.Select(i => string.Format(uriTemplate, project.value[i], date))
.ToArray();
I can't figure out why the code don't work properly, when i click on save button show me Yokoso Log(1) then second save is showing Yokoso Log(1).txt(2).txt .....
//Create txt and write
string logPath = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Yokoso Log\\Yokoso Log");
TextWriter txtwrite = new StreamWriter(logPath);
int count = 1;
Find:
if (File.Exists(logPath))
{
logPath = logPath + "(" + count.ToString() + ").txt";
count++;
goto Find;
}
else
{
File.Create(logPath);
for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
{
for (int j = 0; j < dataGridView1.Columns.Count; j++)
{
txtwrite.Write("\t" + dataGridView1.Rows[i].Cells[j].Value.ToString() + "\t" + "|");
}
txtwrite.WriteLine("");
txtwrite.WriteLine("____________________________________________________________________");
}
txtwrite.Close();
MessageBox.Show("Log create successfully (directory desktop).");
}
}
What you are trying to do is something like this:
var currentPath = logPath;
while (File.Exists(currentPath))
{
currentPath = logPath + "(" + count.ToString() + ").txt";
count++;
}
File.Create(currentPath);
...
here you are creating a file
TextWriter txtwrite = new StreamWriter(logPath);
then when you you check for the file sure enough there is a file
if (File.Exists(logPath))
this is what I think you mean to do
string logPath = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Yokoso Log\\Yokoso Log");
int count = 1;
while (File.Exists(logPath))
{
logPath = logPath + "(" + count.ToString() + ").txt";
count++;
}
using (TextWriter txtwrite = new StreamWriter(logPath))
{
for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
{
for (int j = 0; j < dataGridView1.Columns.Count; j++)
{
txtwrite.Write("\t" + dataGridView1.Rows[i].Cells[j].Value.ToString() + "\t" + "|");
}
txtwrite.WriteLine("");
txtwrite.WriteLine("____________________________________________________________________");
}
}
MessageBox.Show("Log create successfully (directory desktop).");
I am reading all the string values from a table and adding to an array as follows.
da2.Fill(ds, "DNAiusTwitter_Table");
int counts = ds.Tables[0].Rows.Count;
for (int i = 0; i < counts; i++)
{
names[i, 0] = ds.Tables[0].Rows[i][3].ToString();
}
How can I get string append = 'name1','name2','name3','name4'; How can I pass those values to this below code:
for (int i = 0; i < 1; i++)
{
HtmlGenericControl scriptTagLinks = new HtmlGenericControl("script");
scriptTagLinks.Attributes["type"] = "text/javascript";
string scrip1 = "$(function () {$(\"[src='" + names[i, 0] + "']\"" + ").pinit();});";
var scrip = "var tweetUsers=['" +append + "'," + "'" + splitted[3]+"']";
scriptTagLinks.InnerHtml = scrip;
this.Controls.Add(scriptTagLinks);
}
If you don't need the array later in code, I would use a StringBuilder (System.Text namespace), it has better memory allocation if your table changes in size.
da2.Fill(ds, "DNAiusTwitter_Table");
int counts = ds.Tables[0].Rows.Count;
StringBuilder appendString = new StringBuilder();
for (int i = 0; i < counts; i++)
{
appendString.AppendFormat("{0},", ds.Tables[0].Rows[i][3].ToString());
}
This will add all the data to the builder, then in the second code snippet, do the following to convert the builder to a string stripping off the additional comma at the end. Also I don't think you need the for loop (loop through 1?) in the second snippet or is the 1 in the for loop a typo?
var scrip = "var tweetUsers=['" + appendString.ToString().TrimEnd(new char[] { ',' }) + "'," + "'" + splitted[3]+"']";
use string.join() method
Example
string.Join(your_array, ",")
how can i save the number that user enter in textbox in a 2 dimension array?
for example:
i have this numbers in textbox:45,78
and now i want to save 45,32 like this: array[0,0]=45 and array[0,1]=78
how can i do that?thanks,so much
edited:
oh,when i entered 1,2,3,4,5,6,7,8,9 in textbox and it takes [2,2]=56
private void button10_Click(object sender, EventArgs e)
{
int matrixDimention = 2;
int[,] intValues = new int[matrixDimention + 1, matrixDimention + 1];
string[] splitValues = textBox9.Text.Split(',');
for (int i = 0; i < splitValues.Length; i++)
intValues[i % (matrixDimention + 1), i % (matrixDimention + 1)] = Convert.ToInt32(splitValues[i]);
string a=intValues[2,2].ToString();
MessageBox.Show(a);
}
when i take:
string a=intValues[2,1].ToString();
it shows 0
Have a look at using String.Split Method (Char[]) and Convert.ToInt32 Method (String)
Something like
string textBox = "45,78";
int[,] values = new int[1,2];
string[] textBoxSplit = textBox.Split(',');
values[0, 0] = Convert.ToInt32(textBoxSplit[0]);
values[0, 1] = Convert.ToInt32(textBoxSplit[1]);
EDIT
Example using List and Linq
string textBox = "45,78,1,2,3,4,5,6,7,8,9,10,11,12";
List<int> list = new List<int>(textBox.Split(',').Select(x => Convert.ToInt32(x)));
EDIT 2
Longwinded example using List and foreach
string textBox = "45,78,1,2,3,4,5,6,7,8,9,10,11,12";
List<int> list2 = new List<int>();
string[] splitVals = textBox.Split(',');
foreach (string splitVal in splitVals)
list2.Add(Convert.ToInt32(splitVal));
EDIT
Enter the matrix
string textBox = "1,2,3,4,5,6,7,8,9";
int matrixDimention = 2;
int[,] intValues = new int[matrixDimention + 1, matrixDimention + 1];
string[] splitValues = textBox.Split(',');
for (int i = 0; i < splitValues.Length; i++)
intValues[i/(matrixDimention + 1), i%(matrixDimention + 1)] = Convert.ToInt32(splitValues[i]);
EDIT
Follow the white rabbit
string textBox = "1,2,3,4,5,6,7,8,9";
int matrixDimention = 2;
int[,] intValues = new int[matrixDimention + 1, matrixDimention + 1];
string[] splitValues = textBox.Split(',');
for (int i = 0; i < splitValues.Length; i++)
intValues[i/(matrixDimention + 1), i%(matrixDimention + 1)] = Convert.ToInt32(splitValues[i]);
string displayString = "";
for (int inner = 0; inner < intValues.GetLength(0); inner ++)
{
for (int outer = 0; outer < intValues.GetLength(1); outer++)
displayString += String.Format("{0}\t", intValues[inner, outer]);
displayString += Environment.NewLine;
}
MessageBox.Show(displayString);
try this assuming array is a string array
str[] input = textBox.Text.Split(',');
if(input.Length > 1)
{
arr[0,0] = input[0];
arr[0,1]= input[1];
}