Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have to make console program that asks the user to enter number, then the program has to make square of stars (*) equal to the number that the user input.
Console.WriteLine("enter number and press ENTER");
int userNumber = int.Parse(Console.ReadLine());
int rowNumber = 0;
int lineNumber = 0;
int counter = 0;
while(counter < userNumber) {
Console.Write("*");
while(lineNumber < userNumber) {
Console.WriteLine("*");
lineNumber++;
}
counter++;
}
Console.WriteLine();
example:
user input :5
*****
* *
* *
* *
*****
you can do the following:
explanation for the code:
Square is formed of 2 equal sides, so in order to draw the square using *, you will think of it like a matrix
you need a loop to draw the rows and another loop to draw the columns, and both loop has upper limit which is the number entered by the user.
the rule of drawing is like this i will put star if i am in the first row or the last one or in the first column or last, so by addressing this i used the if statement ( i==0 || i== number-1 || j==0 || j== number -1) where i is the row and j is the column, and if this condition is not satisfied, print space
using System;
public class Program
{
public static void Main()
{
Console.WriteLine("Please enter a number:");
var number=Convert.ToInt32(Console.ReadLine());
for(int i=0; i < number; i++)
{
for(int j=0; j < number; j++)
{
if(i==0 || i == number-1 || j==0 || j == number-1)
Console.Write("*");
else
Console.Write(" ");
}
Console.Write("\n");
}
}
}
here a working DEMO
hope this will help you
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 months ago.
Improve this question
The scope is to output the numbers from 1 to (input number) with the multiples of 3 being a * each. E.g. for 8 as input: 1,2,*,4,5,*,7,8. I tried to do this with a for loop but it has no output for some reason.
using System;
using System.Collections.Generic;
namespace HelpPleaseStackOverFlow
{
class Program
{
static void Main(string[] args)
{
int number = Convert.ToInt32(Console.ReadLine());
int x;
for (x = 1; x <= number; )
if (x % 3 == 0)
{
Console.WriteLine("*");
}
else;
{
Console.WriteLine(x);
x = x++;
}
}
}
}
I tried converting the x variable (integer) to a string and then making it the * that should be printed out, in the first half of the IF statement and the last part with the ELSE basically the same.
I also tried to make the for statement like this:
for (x = 1; x <= number; x++)
with the x++ at the end but this just game me the result of ,,8. With an input of 7; 8 should not be part of this and I have no idea why it put 8 as an output.
most of the erroros are that you are lacking some {} and ()
String input = Console.ReadLine();
int number;
int.TryParse(input, out number);
for (int x = 1; x <= number; ++x)
{
if ((x % 3) == 0)
{
Console.WriteLine("*");
}
else
{
Console.WriteLine(x);
}
}
Console.ReadLine();
Hope it helps.
So i have a project where i need to store 8 integers, between 1-10, and recall them into a histogram
The histogram is made up of the character * only. (forgive bad formatting, this is my first post)
The program works, but i think my if statements for int restriction (1-10) could be simplified.
My goal is to achieve this output with as little code as possible
The whole code, and nothing but the code (and comments)
Console.WriteLine("Hello and Welcome!\nEnter 8 values between 1 and 10\n*Press Any Key to continue*");
Console.ReadKey();
//create list to store values
List<int> values = new List<int>();
//loop to collect values
for (int i = 1; i < 9; i++)
{
//label for data validation start point
retry:
Console.WriteLine("Please Enter Value " + i, "Between 1 - 10");
//variable assigned to user input
value = Console.ReadLine();
//Convert string to integer
if (!int.TryParse(value, out validValue))
{
Console.WriteLine("~Incorrect Data Input~"); goto retry; };
if (validValue < 1) { Console.WriteLine("~Incorrect Data Input~"); goto retry; };
if (validValue > 10) { Console.WriteLine("~Incorrect Data Input~"); goto retry; };
values.Add(validValue);
}
for (int i = 0; i < 8; i++ )
{
Console.WriteLine();
for(int id = 0; id < values[i]; id++)
Console.Write("*");
}
Console.ReadKey();
This is the area im thinking could be cleaner
if (validValue < 1) { Console.WriteLine("~Incorrect Data Input~"); goto retry; };
if (validValue > 10) { Console.WriteLine("~Incorrect Data Input~"); goto retry; };
Im open to any suggestions as to how i could clean this or any part of the project up.
There is a more difficult aspect to this project, like a do this if you want to show off kind of thing, where i would need the histogram to be displayed vertically.
I figure this would be done with 2d arrays? i havent really got a clue other than needing 80 spaces and *'s in the right spaces lol.
Ive only been coding for a month or so and it being learner based, any help or suggestions would be welcomed
Thank you
Chris
Here's a sample of how you could reconstruct your logic with a couple of pointers.
Console.WriteLine("Hello and Welcome!\nEnter 8 values between 1 and 10\n*Press Any Key to continue*");
Console.ReadKey();
//create list to store values
List<int> values = new List<int>();
//loop to collect values (no need to run from 1 - 9; 0 - 8 works fine.)
for (int i = 0; i < 8; i++)
{
//label for data validation start point
int validValue = 0;
//Use a while loop instead of a goto
while (validValue == 0)
{
Console.WriteLine("Please Enter Value " + i, "Between 1 - 10");
//variable assigned to user input
var value = Console.ReadLine();
//Convert string to integer
//If the first question in the predicate fails, it won't go on to ask the other questions
if (int.TryParse(value, out validValue) && validValue > 0 && validValue < 10)
{
values.Add(validValue);
}
else
{
Console.WriteLine("~Incorrect Data Input~");
}
//If the code reaches this point and validValue hasn't been changed, it'll be 0 and the loop will run again
}
}
for (int i = 0; i < 8; i++)
{
Console.WriteLine();
for (int id = 0; id < values[i]; id++)
Console.Write("*");
}
Console.ReadKey();
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
I built a C# program in which the user is prompted to enter 10 different values and then the program is supposed to sum and average these values and print the sum and average. The problem I am having is that my program is only capturing the last value entered...please help!
namespace ConsoleApplication4
{
class Program
{
const int count = 10;
static void Input(double[] numbers, int num)
{
for (int i = 0; i <= 9; i++)
{
Console.Write("Enter integer {0}: ", i + 1);
numbers[num] = Convert.ToDouble(Console.ReadLine());
}
}
static void Average(double[] numbers, int num)
{
double sum = 0;
double avg = 0;
for (int i = 0; i < numbers.Length; i++)
{
sum += numbers[i];
}
avg = sum / numbers.Length;
Console.WriteLine("The sum of the inputs is {0} and the average is {1}", sum, avg);
}
static void Main(string[] args)
{
double[] numbers = new double[count];
for (int num = 0; num < 1; num++)
{
Input(numbers, num);
Average(numbers, num);
Console.WriteLine("Press the Enter Key");
Console.ReadLine();
}
}
}
}
}
In you Input method, you're assigning user input to numbers[num] instead of numbers[i].
If you look at your Input() method, you're always storing in the num index in your for loop, but num doesn't change in the loop. You should be using numbers[i] instead of numbers[num].
for (int i = 0; i <= 9; i++)
{
Console.Write("Enter integer {0}: ", i + 1);
numbers[num] = Convert.ToDouble(Console.ReadLine());
That should be
numbers[i] = Convert.ToDouble(Console.ReadLine());
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I am having trouble with this. How can I count a length I want input to.
Lets say I ask a user an input. He enters 10. It counts 1,2,3,4,etc... 10.
Or if users enter 5, 1,2,3,4,5 is output
Thanks.
EDIT:
I am sorry. This isn't homework. School doesn't start till next week and I am practicing.
Sorry, I should have given code.
This is what I had that does work
Console.WriteLine("Enter Length");
int length = int.Parse(Console.ReadLine());
for (int i = 0; i < length; i++)
{
Console.WriteLine(i);
}
I am just assuming since I am new that I did some sloppy code and am looking for maybe something cleaner. Or another point of view for it.
update your code with <=
Console.WriteLine("Enter Length");
int length = int.Parse(Console.ReadLine());
for (int i = 0; i <= length; i++)
{
Console.WriteLine(i);
}
You just need to change the '<' operator to '<=':
for (int i = 0; i <= length; i++)
{
Console.WriteLine(i);
}
string length;
Console.Write("Enter Length: ");
length= Console.ReadLine();
for (int i = 1; i <= Int32.Parse(length); i++)
{
Console.WriteLine(i);
}
Console.ReadKey();
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
My program has no compile errors but the output is incorrect. Example input:
size of array: 5
input numbers: 5 4 3 2 1
//sorted: 1 2 3 4 5
search: 1
output: number 1 found at index 4
the output should be number 1 found at index 0 since the numbers were sorted already. How will I change it to this.
int[] nums = new int[100];
int SizeNum;
bool isNum = false;
private void ExeButton_Click(object sender, EventArgs e)
{
int i, loc, key;
Boolean found = false;
string SizeString = SizeTextBox.Text;
isNum = Int32.TryParse(SizeString, out SizeNum);
string[] numsInString = EntNum.Text.Split(' '); //split values in textbox
for (int j = 0; j < numsInString.Length; j++)
{
nums[j] = int.Parse(numsInString[j]);
}
if (SizeNum == numsInString.Length)
{
Array.Sort(numsInString);
key = int.Parse(SearchTextBox.Text);
ResultText.AppendText("Sorted: ");
for (i = 0; i < SizeNum; i++)
ResultText.AppendText(" " + numsInString[i]);
ResultText.AppendText("\n\n");
{
for (loc = 0; loc < SizeNum; loc++)
{
if (nums[loc] == key)
{
found = true;
break;
}
}
if (found == true)
ResultText.AppendText("Number " + key + " Found At Index [" + loc + "]\n\n");
else
ResultText.AppendText("Number " + key + " Not Found!\n\n");
}
}
}
You're sorting numsInString but then searching nums. nums is being populated before the search, so you're seeing the results of searching the unsorted numbers.
Once you've parsed numsInStrings into nums, you should be working with the latter array only. Make sure that's the one you're sorting and searching through.
In other words, once you replace the current sort call with
Array.Sort(nums);
your code will be fine.
Updated:
You actually need another fix. Right now, you're initializing nums to be an array of size 100. By default, each element will be 0. So even though you put numbers in the first five elements, when you sort the array, you end up with 95 0's, followed by 1 2 3 4 5.
You should delay initializing nums until you've seen how big numsInString is:
string[] numsInString = EntNum.Text.Split(' '); //split values in textbox
nums = new int[numsInString.Length];
for (int j = 0; j < numsInString.Length; j++)
{
nums[j] = int.Parse(numsInString[j]);
}
Now when you sort nums, you'll see only the numbers you entered.
you are sorting the numsInString array, but still searching into the nums array.
for (loc = 0; loc < SizeNum; loc++)
{
if (numsInString[loc] == key)
{
found = true;
break;
}
}
You're parsing numsInString then you're sorting it. (I suspect the sort won't do what you want, either.)
I think you really want to be sorting nums instead:
Array.Sort(nums);
Having said that, there are simpler ways of achieving the end result - such as using IndexOf to find the index of a value in an array.
It's also rather unclear why you've got braces here:
for (i = 0; i < SizeNum; i++)
ResultText.AppendText(" " + numsInString[i]);
ResultText.AppendText("\n\n");
{
...
}
That makes it look like you've got a loop with a body, but it's actually equivalent to:
for (i = 0; i < SizeNum; i++)
{
ResultText.AppendText(" " + numsInString[i]);
}
ResultText.AppendText("\n\n");
{
...
}
... the braces serve no purpose here, and merely harm readability.