using System;
using System.Linq;
namespace PleaseHelpMe
{
public class SumAndAverage
{
public static void Main(string[] args)
{
var data = Enumerable.Range(1, 100);
Console.WriteLine("The sum is "+ data.Sum());
Console.WriteLine("The average is " + data.Average());
Console.ReadKey();
}
}
}
I need to change this to a Do-While loop with an expected output sum of 5050 and an average of 50.
A do/while loop is a bit of a strange choice here, I think. A better choice would be a for loop.
Anyway, you'll need to track the count and the current iteration i:
int i = 1;
int count = 100;
You will also need to store the sum. For big values (sum > 2147483647) you should use long:
int sum = 0;
Now you'll need to increment i until it matches count:
do
{
sum += i; // add current value of i to sum.
}
while (i++ < count);
Note that I've used i++ here because it returns the value of i before it is incremented, in contrast to ++i which returns the value of i after it is incremented. If ++i were used, you would have to compare it with <=.
Finally calculate the average and output the sum:
double average = sum / (double)count;
Console.WriteLine("The sum is " + sum);
Console.WriteLine("The average is " + average);
Console.ReadKey();
Try it online
I think that a for loop is better suited to this (although I understand if it's for an assignment you probably have to jump through the hoops of using do/while):
int sum = 0;
int count = 100;
for (int i = 1; i <= count; ++i)
{
sum += i;
}
double average = sum / (double)count;
Console.WriteLine("The sum is " + sum);
Console.WriteLine("The average is " + average);
Console.ReadKey();
int sum = 0;
decimal avg = 0;
do
{
sum += n;
avg = sum/n;
n++;
} while (n <= 100);
This should works.
Related
I'm trying to find the sum from (1 to n) or given number.
using this code:
int n;
int counter = 0;
int sum = 0;
Console.Write("Please enter the sum limit number: ");
n = int.Parse(Console.ReadLine());
//around here is where code freezes and nothing else happens
while(counter <= n)
{
counter = +1;
sum = sum + counter;
}
Console.Write("The sum from 1 - " + n + " =" + sum);
I know I can use:
int n;
int counter = 0;
int sum = 0;
Console.Write("Please enter the sum limit number: ");
n = int.Parse(Console.ReadLine());
var sum = Enumerable.Range(1, n);
Console.Write("The sum from 1 - " + n + " =" + sum.Sum());
but my next challenge is to only add the numbers that are divisible by 3 or 5, so I'm planning on doing:
if (sum % 3 == 0 | sum % 5 == 0)
{
total = total + sum;
}
What is wrong with my method? Also, alternative ways to do this are more than appreciated!
To get out of while loop, condition needs to satisfy.First you need increment counter present in while loop.
To increment counter variable either you can try counter++/++counter i.e. post/pre increment operator or you can do counter += 1/ counter = counter + 1.
Something similar to
//around here is where code freezes and nothing else happens
while(counter <= n)
{
counter += 1; // not counter=+1;
sum = sum + counter;
}
Reference: Increment decrement in C#
if you want to increment the counter you should either use
counter = counter + 1;
or
counter++;
or
counter += 1;
I was assigned to make the below application
The computer inserts 20 random numbers (with values between 0 and 200)
in an array of 20 elements. After the array has been completely
filled, all elements are shown and the average of the numbers in the
array is determined and shown. Lastly, the difference between the
numbers in the array with the average is shown
I managed to make half of the program but now am at the point where I have to make the app shows the difference between each element of the array and the average and I can't manage to think of a formula to get this done !!!
this is my code so far and it Answers half of the question.
static void Main(string[] args)
{
int sum = 0;
int[] numbers = new int[20];
Random numbergenarator = new Random();
for(int i=0; i<numbers.Length;)
{
numbers[i] = numbergenarator.Next(0, 201);
Console.WriteLine("Element "+i +" is: "+ numbers[i]);
sum += numbers[i];
i++;
}
int average = sum / 20;
Console.WriteLine("The average is: " + average);
Console.ReadKey();
}
thanks in Advance
the resolution to my issues was by adding another (for loop) for generating the difference between each element and the average of elements.
for better comprehension read the code down through :D
static void Main(string[] args)
{
int sum = 0;
int[] numbers = new int[20];
Random numbergenarator = new Random();
for(int i = 0; i<numbers.Length;)
{
numbers[i] = numbergenarator.Next(0, 201);
Console.WriteLine("Element "+i +" is: "+ numbers[i]);
sum += numbers[i];
i++;
}
int average = sum / 20;
Console.WriteLine("The average is: " + average);
for (int i = 0; i < numbers.Length;)
{
Console.WriteLine("the difference between the average and the element " + i +
" is " + (average - numbers[i]));
i++;
}
Console.ReadKey();
}
I am beginner in C# coding, and I am trying to write some code for the program where my program will show the average of numbers given by users. I have written below code, but I am thinking this would be more better and efficient. Can anyone please help me. Thanks in advance.
static void Main(string[] args)
{
int a=0;
double total = 0;
double result;
Console.Write("For how many numbers you want to do the average calculation: ");
a = int.Parse(Console.ReadLine());
double[] array = new double[a];
for (int j = 0; j < a; j++)
{
Console.Write("Please enter value for {0}: ", j+1);
array[j]= double.Parse(Console.ReadLine());
}
foreach (var item in array)
{
total += item;
}
result = total / a;
Console.WriteLine($"Your Calculated average value is {result}");
Console.ReadKey();
}
You dont need to take extra array for your purpose, you can do this just by adding each input value and then divide that total with total number of input items.
double total = 0;
double result;
Console.Write("For how many numbers you want to do the average calculation: ");
int a = int.Parse(Console.ReadLine());
for (int j = 0; j < a; j++)
{
Console.Write("Please enter value for {0}: ", j + 1);
total += double.Parse(Console.ReadLine());
}
result = total / a;
Console.WriteLine($"Your Calculated average value is {result}");
Console.ReadKey();
I wrote I method which is suppose to recieve a nubmer from user and then check number from 0 to 1000. Then it should return all number which have digit sum equal to recieved number. So if I enter 6, it should return numbers like 6, 42, 51, 33, 123 etc. I'd really appreciate help since I've been dwelling on this for a while now.
public static double number() {
Console.WriteLine("Enter your number! ");
string enter = Console.ReadLine();
double x = Convert.ToDouble(enter);
for (int i = 0; i < 1000; i++ ) {
double r;
double sum = 0;
while (i != 0) {
r = i % 10;
i = i / 10;
sum = sum + r;
}
if (sum == x) {
Console.WriteLine(i + " ");
}
}
return(0);
}
I am aware of the fact that there is a problem with "return(0)", but I'm not completely sure what exactly it is that this should be returning.
I'd suggest trying to do something like this:
public static IEnumerable<int> number()
{
Console.WriteLine("Enter your number!");
string enter = Console.ReadLine();
int digitSum = int.Parse(enter);
foreach (var n in Enumerable.Range(0, 1000))
{
if (n.ToString().ToCharArray().Sum(c => c - '0') == digitSum)
{
yield return n;
}
}
}
When I run this and enter 6 then I get this result:
You are almost there: the only remaining problem is that you are modifying your loop counter i inside the nested while loop, which changes the workings of the outer loop.
You can fix this problem by saving a copy of i in another variable, say, in ii, and modifying it inside the while loop instead:
double r;
double sum = 0;
int ii = i;
while (ii != 0) {
r = iCopy % 10;
ii /= 10;
sum = sum + r;
}
I need to display the average number of rolls needed to get a six, and the number of sixes that average was based.
The problem I am having I think is with this part of the code? So I want the average number of rolls which I think I have as the variable AVGroll. The number of sixes the average was base on should be the loopcount variable.
AVGroll = AVGroll + loopcount;
average = AVGroll / loopcount;
Tried to comment my code as best as possible to make it readable.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace CE0721a
{
class Tut4_7
{
public void run()
{
// Random number generator
Random rndm = new Random();
//declaring number for Random Number Generator
int number;
// average number of runs
int average;
//declaring loopcount starts at 1
int loopcount = 1;
//Average roll starts at 0
int AVGroll = 0;
//Variable if it continues
int progcontinue;
//Start lable
Start:
do
{
number = rndm.Next(6) + 1;
Console.WriteLine(number);
Console.ReadKey();
if (number < 6)
{
loopcount++;
}
} while (number != 6);
AVGroll = AVGroll + loopcount;
average = AVGroll / loopcount;
Console.WriteLine("The roll count is:");
Console.WriteLine(loopcount);
Console.WriteLine("average");
Console.WriteLine(AVGroll);
Console.WriteLine("press 1 to continue or 0 to exit");
progcontinue = (int.Parse(Console.ReadLine()));
if (progcontinue > 0)
{
loopcount = 1;
goto Start;
}
else
{
}
}
}
}
I am a bit confused by your code but I think you are using the wrong variables to calculate the AvgRoll. the first run AVGroll will always be 1 example:
AVGroll = AVGroll (0) + loopcount (5);
average = AVGroll (5) / loopcount (5);
so it will be 1
I think you need to do something like this:
int timesContinued = 1;
//Start lable
Start:
do
{
number = rndm.Ne
xt(6) + 1;
Console.WriteLine(number);
Console.ReadKey();
if (number < 6)
{
loopcount++;
}
} while (number != 6);
AVGroll = AVGroll + loopcount;
average = AVGroll / timesContinued;
Console.WriteLine("The roll count is:");
Console.WriteLine(loopcount);
Console.WriteLine("average");
Console.WriteLine(AVGroll);
Console.WriteLine("press 1 to continue or 0 to exit");
progcontinue = (int.Parse(Console.ReadLine()));
if (progcontinue > 0)
{
loopcount = 1;
timesContinued++;
goto Start;
}
this way you will devided the total times rolled by the number of times that you pressed continue which I hope is what you wanted.
You divide by the wrong thing:
AVGroll = AVGroll + loopcount;
average = AVGroll / loopcount;
You want to average over the number of trials. One trial is you roll until you get 6.
Then based on progcontinue you do more trials.
Thus have an extra variable that counts trials and divide by that:
int trial = 1;
//...
AVGroll = AVGroll + loopcount;
average = AVGroll / trials;
//...
if (progcontinue > 0)
{
loopcount = 1;
++trials;
goto Start;
}
Also you need to print average, not AVGroll:
Console.WriteLine("average");
Console.WriteLine(AVGroll); //should be average