Trying to make an Array in C# [closed] - c#

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 5 years ago.
Improve this question
Just trying to make a simple array that takes grades (double variables) and assigns them to the array elements. Also the user determines how many grades they want to assign. It seems simple, yet every time I execute the code, it prints out way too many lines and doesn't stop based on the user input. Not sure what I am missing here.
static void Main(string[] args)
{
int num = 0;
Write("How many grades are you wanting to process...");
num = Read();
Double[] grades = new Double[num];
for (int i = 0; i < grades.Length; i++)
{
WriteLine("Enter grade:");
grades[i] = ToDouble(Read());
WriteLine();
}
It has been a while and feel silly, just came back from python.

Just few modification
static void Main(string[] args)
{
int num = 0;
Write("How many grades are you wanting to process...");
num = Convert.ToInt32(Console.ReadLine());
Double[] grades = new Double[num];
for (int i = 0; i < num; i++)
{
Console.Write("Enter grade:");
grades[i] = Convert.ToDouble(Console.ReadLine());
}
}
ReadKey (returns a character): reads only one single character from
the standard input stream. Usually used when you're giving options to
the user in the console to select from, such as select A, B or C.
Another prominent example, Press Y or n to continue.
ReadLine (returns a string): reads only single line from the standard input stream. As an example, it can be used to ask the user
enter their name or age.
Read (returns an int): reads only one single character from the standard input stream. Similar to ReadKey except that it returns an
integer.
This was clearly described with examples in the MSDN documentation
(links are included above).

Related

Is There a Problem With My Code or is Visual Studio Code Broken [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
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.
Closed 1 year ago.
Improve this question
I am new to c#, but have been coding in java before. I downloaded visual studio code and the c# extension, and it seems to be working when I do Console.WriteLine("Example"); yet when I have a for loop it seems to not run it. I have a really simple sum calculator, but it doesn't work:
static void Main(string[] args)
{
int sum = 0;
Console.Write("Started Program");
Console.WriteLine("...");
for (int i = 0; i < 10; i++)
{
Console.Write("Choose numbers you want to add: ");
int num = Console.Read();
sum = sum + num;
}
Console.WriteLine(sum);
}
Its probably passing the .read input as ASCII characters instead of the actual int ... try changing;
int num = Console.Read();
to
int num = Int32.Parse(Console.ReadLine());

Append not adding to list C# [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
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.
Closed 1 year ago.
Improve this question
for(int i = 1; i != amount+1; i++)
{
Console.WriteLine("What is the name of the {0} person?", i);
people.Append(Console.ReadLine());
Console.WriteLine("What was their grade?");
grades.Append(Convert.ToDecimal(Console.ReadLine()));
}
(This section is what is affected)
It gets the inputs from the user (I.E. it gets the names and grades, though it doesn't add to the array. I am new to C#, coming from python. Do you guys have any idea how I could fix this issue? TIA
Are you looking for something like this?
using System.Collections.Generic;
int amount = 5;
List<string> people = new List<string>();
List<decimal> grades = new List<decimal>();
for (int i = 0; i < amount; i++)
{
Console.WriteLine("What is the name of the {0} person?", i);
people.Add(Console.ReadLine());
Console.WriteLine("What was their grade?");
grades.Add(Convert.ToDecimal(Console.ReadLine()));
}

Why is the for-loop choosing the wrong IF statement path? [closed]

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 3 years ago.
Improve this question
So I am doing an online coding challenge and have come across this issue that has me stumped:
This is my code:
static void Main(String[] args)
{
int noOfRows = Convert.ToInt32(Console.ReadLine());
for (int i = 0; i < noOfRows; i++)
{
string odds = "";
string evens = "";
//get the input word from console
string word = Console.ReadLine();
for (int j = 0; j < word.Length; j++)
{
//if the string's current char is even-indexed...
if (word[j] % 2 == 0)
{
evens += word[j];
}
//if the string's current char is odd-indexed...
else if (word[j] % 2 != 0)
{
odds += word[j];
}
}
//print a line with the evens + odds
Console.WriteLine(evens + " " + odds);
}
}
Essentially, the question wants me to get the string from the console line and print the even-indexed characters (starting from index=0) on the left, followed by a space, and then the odd-indexed characters.
So when I try the word 'Hacker', I should see the line printed as "Hce akr". When I debugged it, I saw the code successfully put the letter 'H' on the left (because it is index=0, thus even), and put the letter 'a' on the right (odd index). But then when it got to the letter 'c', instead of going through the first IF path (even index), it skips it and goes to the odd index path, and places it on the right hand side?
The funny thing is when I try the word 'Rank' it works fine and prints the correct statement: "Ra nk", yet other words do not.
Its just bizarre that I'm getting different results.
What am I missing?
word[j] is a character in your string; j is the index you want to check the evenness of.
if (j%2) should provide the correct path. You're using if( word[j] %2) which is doing modular arithmetic on a character, not an index. Most likely using modulo on the ASCII value. Hope this helps.
you want to check if the index is even,yet you compare word[j] % 2 == 0 which is not an index.
what you should do:
if(j % 2 == 0){
}

C# Find the row in a list whose price is closest to 0 [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
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.
Closed 4 years ago.
Improve this question
I have a clump of data pulled from an SQL database and stored inside a list:
as you can see in the image there are a few data columns. What I'm trying to do is out of the 69 items I want to find the one whose Rate_Price is closest to 0. It can be a negative or a positive.
Current Code:
var data = _rateManager.Get30Year("30-Year Fixed Rate");
Here's a console app that adds a load of -ive and +ive integers randomly to a list and then sorts the list producing the one that's closest to 0:
class Program
{
static void Main(string[] args)
{
var random = new Random(DateTime.Now.Millisecond);
var values = new List<int>();
for (int i = 0; i < 100; i++)
{
values.Add(random.Next(-1000, 1000));
}
foreach (var item in values.OrderBy(i => Math.Abs(i)))
{
Console.WriteLine($"{item}, ");
}
Console.WriteLine("The closest to 0 therefore is:");
Console.WriteLine(values.OrderBy(i => Math.Abs(i)).First());
Console.Read();
}
}
The key bit here is:
values.OrderBy(i => Math.Abs(i)).First()
Or in case of your example:
data.OrderBy(i => Math.Abs(i.Rate_Price)).First();
Or
data.First(i => Math.Abs(i.Rate_Price));
That said in your example i am guessing all Rate_Price will be a positive number so to get the first all you need is:
data.OrderBy(i => i.Rate_Price).First();
in sql (if you can)
select * from thingies order by (abs(rate)) asc limit 1
If you can use SQL, try select * from thingies where RATE_PRICE = (select MIN(RATE_PRICE) from thingies where RATE_PRICE > 0

How to give an employee a raise using arrays and loops [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
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.
Closed 7 years ago.
Improve this question
I need some help. I created a constant for a small company initialize to 5. I declared an array to hold unique salaries and declared a variable to hold a raise. However I am struggling with using loops to give each salary in the array the raise the user received and then use another loop to display the new salary amounts with the format specifier. Below is the code I am trying to put together.
public class Program
{
public static void Main(string[] args)
{
const int NumOfSalaries = 5;
int[] UniqueSalary = {30000,40000,50000,55000,60000};
decimal raise = 0.0M;
Console.WriteLine("Please enter a raise amount");
raise = int.Parse(Console.ReadLine());
foreach (int number in UniqueSalary)
raise += number;
foreach (int number in UniqueSalary)
Console.WriteLine(" {0}", UniqueSalary);
} // end Main
} // end class
Run a for loop and assign the raise to each element of the array as shown below.
decimal[] UniqueSalary = { 30000, 40000, 50000, 55000, 60000 };
decimal raise = 0.0M;
Console.WriteLine("Please enter a raise amount");
raise = int.Parse(Console.ReadLine());
for (int i = 0; i < UniqueSalary.Length; i++)
UniqueSalary[i] += raise;
for(int i = 0; i < UniqueSalary.Length; i++)
Console.WriteLine(" {0}", UniqueSalary[i]);
Console.ReadLine();
Note: I have changed the array type from an int to decimal. You could maintain an int array and do a conversion when adding the raise.
The problem is inside your for loop.
foreach (int number in UniqueSalary)
raise += number;
This is adding the variable number to the raise and storing it back in raise. You need to store it in the array instead.

Categories

Resources