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();
}
Related
I have created a program which allows a user to enter the rainfall over the course of a year. The program is then meant to output a chart as well as summarized data such as average, maximum, minimum and total rainfall.
While all of my outputs based upon data works, the actual chart which is meant to display the numbers inputted with asterisks isn't working.
I can't quite understand where I have gone wrong. I have linked an example output of the "*" table as well as my actual code below. Any help would be much appreciated. I am a beginner to C# by the way.
Example output
int[] monthrainfall = new int[12];
int i;
double average;
int total = 0;
int max = 0;
int min = Int32.MaxValue;
string bar = "";
//title
Console.Write("\n\nRainfall Data:\n");
Console.Write("**************\n");
//asks user to input names
Console.Write("Enter Rainfall for the year:\n");
for (i = 0; i < 12; i++)
{
Console.Write("Enter rainfall for month {0}: ", i + 1);
monthrainfall[i] = Convert.ToInt32(Console.ReadLine());
}
//outputs rainfall results
Console.WriteLine("\nRainfall Chart");
Console.WriteLine("**************");
//outputs results for each month
for (i = 0; i < 12; i++)
{
Console.Write("Month {0}: ", i + 1);
bar = bar + "*";
Console.WriteLine("{0} ", bar[i]);
}
Console.WriteLine("\nSummary data");
Console.WriteLine("**************");
//calculates the maximum rainfall
for (int index2 = 0; index2 < 12; index2++) //inline variable declaration
{
if (monthrainfall[index2] > max) max = monthrainfall[index2];
}
Console.WriteLine("The maximum rainfall was: " + max);
//calculates the minimum rainfall
for (int index3 = 0; index3 < 12; index3++) //inline variable declaration
{
if (monthrainfall[index3] < min) min = monthrainfall[index3];
}
Console.WriteLine("The minimum rainfall was: " + min);
//calculates average rainfall
for (int index = 0; index < 12; index++)
{
total = total + monthrainfall[index];
}
average = (total) / 12;
Console.WriteLine("The average rainfall was: " + average);
Console.WriteLine("The total rainfall was: " + total);
Console.ReadKey();
for (i = 0; i < 12; i++)
{ //loop every month
Console.Write("Month {0}: ", i + 1);
bar = "";
for(int stars = 0; stars < monthrainfall[i]; stars++)
{
bar = bar + "*";
}
Console.WriteLine("{0} ", bar[i]);
}
but as #Flydog57 said, use the string constructor instead of the additional for-loop
All of the explanation is going to take place in this part of code
//outputs results for each month
for (i = 0; i < 12; i++)
{
Console.Write("Month {0}: ", i + 1);
bar = bar + "*";
Console.WriteLine("{0} ", bar[i]);
}
Part 1:
on bar = bar + "*"; you are adding only one * for each iteration of the loop. However, what you want is to add the number based on what have been entered in monthrainfall.
As #Flydog57 suggested, the modification would be
bar = new string('*', monthrainfall[i]);
Part 2:
on Console.WriteLine("{0} ", bar[i]); you are only printing one * as you have selected the element bar[i] of the string bar.
The modification would be Console.WriteLine("{0} ", bar);
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.
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 need to write a console app that asks the user how many number (double) to enter. After accepting that many numbers, display every other number entered. There always seems to be an error when I try to use a double. I also get this error "System.IndexOutOfRangeException was unhandled
HResult=-2146233080
Message=Index was outside the bounds of the array."
after the the for loop has gone through the last loop for this line
myArrai1[i] = int.Parse(Console.ReadLine());
in the first for loop
this all the code I have been able to try to brainstorm with.
class Program
{
static void Main(string[] args)
{
Console.Write("Enter Item Count: ");
int number = int.Parse(Console.ReadLine());
int[] myArrai1 = new int[number];
int i = 0;
for (i = 1; i <= number; i++)
{
Console.WriteLine("Enter Number " + i.ToString() + ": ");
myArrai1[i] = int.Parse(Console.ReadLine());
}
for (i = 0; i < number; i++)
{
Console.WriteLine("Every other number entered: ");
Console.WriteLine(myArrai1[i += 2]);
}
Console.ReadLine();
}
}
}
Not too far off, main changes to fix fix this was loop#1 was set to 0 index to prevent out of range and loop#2 was simplified by intervaling at 2 of ++.
The rest of the changes were aesthetic; I changed the input Console.WriteLine to just Console.Write to keep the input on the same line, and moved the Every Other Line statement to precede the output display loop instead of being written on every iteration.
Console.Write("Enter Item Count: ");
int number = int.Parse(Console.ReadLine());
int[] myArrai1 = new int[number];
for (int i = 0; i < number; i++) {
Console.Write("Enter Number " + (i + 1) + ": ");
myArrai1[i] = int.Parse(Console.ReadLine());
}
Console.WriteLine("Every other number entered: ");
for (int j = 0; j < number; j = j + 2) {
Console.WriteLine(myArrai1[j]);
}
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();