C# grade array with histogram
I am studying fundamental programming in C# in Sweden and I was wondering if you could give me a hand with understanding an easy example.
My goal is to fill an array with random numbers and then to show asterisk (*) or any symbol that many times as is the random generated number.
This what I mean:
Student 1 has a grade: 4 : * * * *
Student 2 has a grade: 9 : * * * * * * * * *
etc.
This is the code I came up with so far:
using System;
using System.Text;
namespace Array_1_10
{
class Program
{
static void Main(string[] args)
{
//declar and create an int array object with 5 elements
string tempStars = "";
int[] grades = new int[11];
// initiate the array using Random cla开发者_如何学运维ss methods
Random grade = new Random();
for (int j = 1; j < 11; j++)
grades[j] = grade.Next(1, 9);
//Read and display the array's elements
for (int j = 1; j < 11; j++)
{
tempStars += "*" + " ";
tempStars += "";
Console.WriteLine("Student {0} has got: {1} : {2} ", j, grades[j], tempStars);
}
}
}
}
It fills the array but the asterisks go from 1 to 10 no matter what number is generated, like this:
Student 1 has a grade 5 : *
Student 2 has a grade 1 : * *
etc.
Can you please help me with this? Thank you very much. Vojtech
using System;
using System.Text;
namespace Array_1_10
{
class Program
{
static void Main(string[] args)
{
//declar and create an int array object with 5 elements
int[] grades = new int[11];
// initiate the array using Random class methods
Random grade = new Random();
for (int j = 1; j < 11; j++)
grades[j] = grade.Next(1, 9);
//Read and display the array's elements
for (int j = 1; j < 11; j++)
{
string tempStars = ""; //initialize string each time
for (int lp = 0 ; lp < grades[j] ; lp++)) { // build the string
tempStars += "*" + " ";
}
Console.WriteLine("Student {0} has got: {1} : {2} ", j, grades[j], tempStars);
}
}
}
}
Here's a more concise version which uses StringBuilder:
using System;
using System.Text;
namespace SO7626386
{
class Program
{
static void Main(string[] args)
{
var grades = new int[11];
// initiate the array using Random class methods
var grade = new Random();
for (int j = 1; j < 11; j++)
{
grades[j] = grade.Next(1, 10);
}
//Read and display the array's elements
for (int j = 1; j < 11; j++)
{
Console.WriteLine("Student {0} has got: {1} : {2} ", j, grades[j], new StringBuilder().Insert(0,"* ",grades[j]).ToString());
}
}
}
}
Trace this part of your code:
for (int j = 1; j < 11; j++)
{
tempStars += "*" + " ";
tempStars += "";
Console.WriteLine("Student {0} has got: {1} : {2} ",
j, grades[j], tempStars);
}
In each iteration you will add new asterisk to tempStars
no matter how many *
should be assign, So you change it.
for example first define tempStars
in your for loop, second add *
as the size of grades[j] to your tempStars
variable then print it.
As an additional solution, that you can research freely:
for (int j = 1; j < 11; j++)
{
string tempStars = new String('*', grades[j]).Replace("*", "* ");
Console.WriteLine ....
I WON'T explain it to you, because it's quite easy to research it on MSDN.
It's interesting because it shows what is the precedence between new
and .
(to be more clear, in C# you don't need to write this: (new String('*', grades[j])).Replace("*", "* ")
, in other languages you would have to add the brackets.)
You need to change your code where you display the number oF stars to take into account the grade. So the code from for loop down would be:
For(j=1; j<11; j++)
{
StringBuilder ab = new StringBuilder(grades[j]);
For(int i=0; i<grades[j]; i++)
{
sb.Append(" *");
}
Console.WriteLine("Student {0} has grade {1} : {2}", j, grades[j], sb.ToString());
}
The extra for loop is to build a string with the amount of stars the student has got. You should use a stringbuilder for these kinds of operations as it's much more efficient than creating lots of strings.
One of thing to note is that the code initialises the stringbuilder to the correct string length. This saves the string builder class the work of resizing it's array under the hood that it uses to build the string.
The way you've written your code, you're only printing a single asterisk in the first loop, and then adding an additional asterisk in each iteration of the loop. You need an inner loop that prints the correct amount of asterisks, and then you need to clear the tempStars variable in the outer loop after printing it.
Try changing the portion of your code that displays the asterisks accordingly: (untested)
string tempStars = "";
for (int j = 1; j < 11; j++)
{
for (int i = 0; i < grades[j]; i++)
tempStars += "*" + " ";
Console.WriteLine("Student {0} has got: {1} : {2} ", j, grades[j], tempStars);
tempStars = "";
}
This should be better:
using System;
using System.Text;
namespace Array_1_10
{
class Program
{
string stars(int count)
{
if (count == 0) return "";
string st = "*";
for (int i = 1; i < count; i++)
{
st += " *";
}
return st;
}
static void Main(string[] args)
{
//declar and create an int array object with 10 elements
int[] grades = new int[10];
// initiate the array using Random class methods
Random grade = new Random();
for (int j = 0; j < 10; j++)
grades[j] = grade.Next(1, 9);
//Read and display the array's elements
for (int j = 0; j < 10; j++)
Console.WriteLine("Student {0} has got: {1} : {2} ", j, grades[j], stars(grades[j]));
}
}
}
精彩评论