Create a console-based application whose Main() method
declares an array of eight integers. Call a method to interactively
fi ll the array with any number of values up to
eight. Call a second method that accepts out parameters
for the arithmetic average and the sum of the values in the
array. Display the array values, the number of entered elements,
and their average and sum in the Main() method.
I am honestly not even sure what to do here..
Loading
Datta KharadPosted Jan 10, 2012, 1:53 AM
I did it, I think this will help you......you can write this code..
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
int[] arrsam = new int[8];
int sum=0;
float avg;
Console.WriteLine("Enter numbers:");
for (int i = 0; i < arrsam.Length; i++)
{
arrsam[i] = Convert.ToInt32(Console.ReadLine());
}
CalcOperation(arrsam, out sum, out avg);
Console.WriteLine("You have entered Values :-");
foreach (int k in arrsam)
{
Console.WriteLine(k);
}
Console.WriteLine("Sum of these values:-" + sum);
Console.WriteLine("Average:- " + avg);
Console.ReadKey();
}
public static void CalcOperation(int[] arrsam, out int sum, out float avg)
{
sum = 0;
foreach(var i in arrsam)
{
sum+=i;
}
avg =(float)sum / arrsam.Length;
}
}
}
Prime bPosted Jan 10, 2012, 12:07 PM
//created the array
int[] numbers = new int[8];
//use this method to "interactivly" fill it with values
FillArray(numbers);
int avg;
int sum;
//call ArrayCalc to get the values...
ArrayCalc(numbers, out avg, out sum);
Console.WriteLine("The sum of numbers you entered is {0} and the average {1}", sum, avg);
}
public static void FillArray(int[] array)
{
string inputString;
for (int index = 0; index < array.Length; index++)
{
//console.read from user, and put value into the array at index
Console.WriteLine("Enter your eight numbers: ");
inputString = Console.ReadLine();
array[index] = Convert.ToInt32(inputString);
}
}
public static void ArrayCalc(int[] array, out int avg, out int sum)
{
avg = 0;
//standard summing algorithm
sum = 0;
foreach (int number in array)
{
sum += number;
}
sum = 0;
for(int index = 0; index < array.Length; index++)
{
sum += array[index];
}
avg = sum / array.Length;