Question

What I need: Write a program named Averages that includes a method named Average that accepts...

What I need:

Write a program named Averages that includes a method named Average that accepts any number of numeric parameters, displays them, and displays their average.

For example, if 7 and 4 were passed to the method, the ouput would be:

7 4  -- Average is 5.5

Test your function in your Main(). Tests will be run against Average() to determine that it works correctly when passed one, two, or three numbers, or an array of numbers.

What I have:

using System;
using System.Collections.Generic;
class Program
{
static void Average(List<float> list){
double sum = 0;
Console.Write("For the list (");
for(int i = 0; i < list.Count - 1; i++){
sum += list[i];
Console.Write(list[i] + ",");
}
sum += list[list.Count - 1];
Console.Write(list[list.Count - 1] + ") the average of its elements is : " + Math.Round((sum / list.Count),2));
  
}
static void Main()
{
List<float> list = new List<float>();
Console.WriteLine("Enter 0 to exit or Enter number to continue: ");
float number = float.Parse(Console.ReadLine());
list.Add(number);
while(number != 0){
Console.WriteLine("Enter 0 to exit or Enter number to continue: ");
number = float.Parse(Console.ReadLine());
if(number != 0)
list.Add(number);
}
Average(list);
}
}

Errors I get:

Compilation failed: 1 error(s), 0 warnings

NtTest2cd32bed.cs(15,9): error CS0103: The name `Averages' does not exist in the current context

Test Contents

[TestFixture]
public class AverageTwoNumbersTest
{
   [Test]
   public void AverageTest()
   {
      using (StringWriter sw = new StringWriter())
      {
        Console.SetOut(sw);
        Averages.Average(8, 10);
        string expected = "8 10  -- Average is 9";
        Assert.AreEqual(expected, sw.ToString().Trim());
      }
   }
}
0 0
Add a comment Improve this question Transcribed image text
Answer #1

If you have any doubts, please give me comment...

using System;

using System.Collections.Generic;

using System.Linq;

class Averages {

public static void Average (params int[] list) {

double sum = 0;

for (int i = 0; i < list.Length; i++) {

sum += list[i];

Console.Write (list[i] + " ");

}

Console.WriteLine ("-- Average is "+ Math.Round ((sum / list.Length), 2));

}

public static void Main () {

Average(3,4);

Average(5,2,6,3);

Average(8, 10);

}

}

nagarajuanagaraju-Vostro-3550:30012020$ mcs Averages.cs nagarajuanagaraju-Vostro-3550:30012020$ ./Averages.exe 3 4 -- Average

Add a comment
Know the answer?
Add Answer to:
What I need: Write a program named Averages that includes a method named Average that accepts...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I need help with the following assignment: Write an application named DailyTemps that continuously prompts a...

    I need help with the following assignment: Write an application named DailyTemps that continuously prompts a user for a series of daily high temperatures until the user enters a sentinel value of 999. Valid temperatures range from -20 through 130 Fahrenheit. When the user enters a valid temperature, add it to a total; when the user enters an invalid temperature, display the error message: Valid temperatures range from -20 to 130. Please reenter temperature. Before the program ends, display the...

  • Hello in C#. I need help understanding and knwoing what i missed in my program. The...

    Hello in C#. I need help understanding and knwoing what i missed in my program. The issue is, the program is supposed to have user input for 12 family members and at the end of the 12 it asks for user to input a name to search for. When i put a correct name, it always gives me a relative not found message. WHat did i miss. And can you adjust the new code so im able to see where...

  • Could anybody give me comment here to help me understand this code? please, I want details...

    Could anybody give me comment here to help me understand this code? please, I want details comment. thank you in advance using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Framing { class Program { static void Main(string[] args) { Dictionary charEncoding = new Dictionary(); charEncoding.Add("A", "01000111"); charEncoding.Add("B", "11100011"); charEncoding.Add("C", "11000001"); charEncoding.Add("ESC", "11100000"); charEncoding.Add("FLAG", "01111110"); Console.WriteLine("Enter Sequence: "); string input = Console.ReadLine().Trim().ToUpper(); Console.Write("Byte count: " + Convert.ToString((input.Split(' ').Length + 1), 2).PadLeft(8, '0')); foreach (string str in input.Split(' '))...

  • For the following C# program: Let’s add one more user defined method to the program. This...

    For the following C# program: Let’s add one more user defined method to the program. This method, called ReverseDump is to output the values in the array in revere order (please note that you are NOT to use the Array.Reverse method from the Array class). The approach is quite simple: your ReverseDump method will look very similar to the Dump method with the exception that the for loop will count backwards from num 1 to 0 . The method header...

  • i need help fixing my code. here is the assignment: Make a class that contains the...

    i need help fixing my code. here is the assignment: Make a class that contains the following functions: isLong, isDouble, stringToLong, and stringToDouble. Each receives a string and returns the appropriate type (bool, bool, long, and double).During the rest of the semester you will paste this class into your programs and will no longer use any of the "standard" c# functions to convert strings to numbers. here is my code. it works for the long method but not the double:...

  • C# coding error with Cengage: MoveEstimator.cs(15,58): error CS1525: Unexpected symbol `total' Compilation failed: 1 error(s), 0...

    C# coding error with Cengage: MoveEstimator.cs(15,58): error CS1525: Unexpected symbol `total' Compilation failed: 1 error(s), 0 warnings Cannot open assembly 'MoveEstimator.exe': No such file or directory. Here is my code and the prompt as well Malcolm Movers charges a base rate of $200 per move plus $150 per hour and $2 per mile. Write a program named MoveEstimator that prompts a user for and accepts estimates for the number of hours for a job and the number of miles involved...

  • Write a C# program (Integer Math Calculator, the programming requirements are as follows: When typing in...

    Write a C# program (Integer Math Calculator, the programming requirements are as follows: When typing in 3+4, 10-5, 12*12, 15/5 from console, the program should give the right answers on screen like 7, 5, 144, 3. This is what I have so far: namespace ConsoleApplication3 { class Program { static void Main(string[] args) { String input; do { Console.Write("Type int values to calulate or stop to exit: "); input = Console.ReadLine(); if (input.ToLower() != "stop") { char[] delimiters = {...

  • What I need: Create a new program named Reverse4 and declare four integer variables with the...

    What I need: Create a new program named Reverse4 and declare four integer variables with the values 23, 45, 55, and 67. Add a method named Reverse that reverses the positions of four integer variables. The Reverse method should accept the variables as references. Write a Main() method that displays the four variables both before and after reversing them to ensure the method works correctly. What I have: using System; class Reverse4 { public static void reverse(ref int num1, ref...

  • You must first compile both .cs file into .exe. Then, run the server in one command...

    You must first compile both .cs file into .exe. Then, run the server in one command prompt window and run two cilents, each in a separate command prompt window. To quit a client, you may enter no book title but just hit the enter or enter Ctrl-C. The server always displays a message whenever a client is connected and disconnected. <?xml version="1.0" encoding="utf-8" ?> <configuration> <appSettings> <add key = "U101" value="PHYSICS"/> <add key = "U102" value="EDU"/> <add key = "U103"...

  • Write a program named FahrenheitToCelsius that accepts a temperature in Fahrenheit from a user and converts...

    Write a program named FahrenheitToCelsius that accepts a temperature in Fahrenheit from a user and converts it to Celsius by subtracting 32 from the Fahrenheit value and multiplying the result by 5/9. Display both values to one decimal place. For example, if 88.5 degrees is input, the output would be: 88.5 F is 31.4 C Answer in C# (Posting incomplete code i have so far that wont run correctly.) using System; using static System.Console; class FahrenheitToCelsius { static void Main()...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT