My C# array only sums and averages the last user input [closed] - c#

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());

Related

No output for loop program [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 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.

For loop not iterating for each input C# - beginner question

I'm new to C# and I'm trying to calculate the average of 4 numbers from the user I've gotten this far but i'm having issues in the input stage. I wanted to use a for loop to iterate for every number printing "Enter number one:" > user puts 2, its accumulates... Then is asks for the next one "Enter number two" adds to sum and so on until all for numbers have been added. However instead of taking each number at a time. The output for this program is:
number 1number 2number 3number 4:
Question: Why isn't it stopping at each iteration to allow the user to input a number?
namespace Hello
{
class Program
{
static void Main(string[] args)
{
double sum;
for (int i = 1; i <= 4; i++)
Console.Write("Enter number {0}:", i);
sum = +Convert.ToDouble(Console.ReadLine());
}
}
}
Try this:
double sum;
for (int i = 1; i <= 4; i++) {
Console.Write("Enter number {0}:", i);
sum += Convert.ToDouble(Console.ReadLine());
}
You forgot to enclose your for loop in curly braces.

Hello, my program jumps over the for loop, and I don't know why [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
using System;
using System.Collections.Generic;
namespace c3
{
class Program
{
static int sumaTotala;
static void Main(string[] args)
{
int teza;
int numberOfNotes;
Console.WriteLine("Numar de note: ");
numberOfNotes = Convert.ToInt32(Console.ReadLine());
List<int> numarDeNote = new List<int>(numberOfNotes);
for (int i = 1; i < numarDeNote.Count + 1; i++)
{
Console.WriteLine("Introdu " + i + " nota: ");
int x = Convert.ToInt32(Console.ReadLine());
numarDeNote.Add(x);
sumaTotala += x;
}
Console.WriteLine("Teza : ");
teza = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Media a rezultat " + NumberInput(numarDeNote, teza, numberOfNotes));
Console.ReadKey();
}
static float NumberInput(List<int> numarNote, int notaTeza, int numarxd)
{
float part1 = sumaTotala / numarxd;
float part2 = part1 * 3;
float part3 = part2 + notaTeza;
float part4 = part3 / 4;
return part4;
}
}
}
The value of numarDeNote.Count starts at 0, and your i starts at 1, so the condition i < numarDeNote.Count + 1 in the for-loop never becomes true. So the loop body is never executed.
Specifying a value when creating the list means that the initial list has room to grow for that many entries, but still initially there are no entries.
That constructor for List just sets the capacity
It still has a Count of zero until you add items
List Constructor (Int32)
Use
for (int i = 1; i < numberOfNotes + 1; i++)
{
Constructor parameter in
List<int> numarDeNote = new List<int>(numberOfNotes);
is Capacity, not Count of the created list.
for (int i = 1; i < numberOfNotes + 1; i++)
{
...

c# home works can't solve [closed]

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

Having trouble counting in c# [closed]

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();

Categories