2015-01-07 3 views
0

프로그래밍에 익숙하지 않고 누군가가 다음 질문에 도움을 줄 수 있다면 기뻐할 것입니다. "값이 0에서 9까지의 정수를 무작위로 선택하는 프로그램을 작성하십시오. 프로그램 난수의 평균을 계산하고 난수가 평균보다 얼마나 많은지 인쇄합니다 프로그램의 사용자는 임의로 생성되는 숫자의 수를 지정합니다. " 평균을 구하기 위해 난수의 총계를 어떻게 구합니까? 이것은 내가 지금까지 무엇을 가지고 있습니다 :C에서 난수 합계/평균 합계 #

int ChosenRandom; 

Console.Write("Choose a number between 0-10; "); 

ChosenRandom = int.Parse(Console.ReadLine()); 

Random rnd = new Random(); 

int RandomNumber = rnd.Next(0, 10); 

for (int i = 0; i < ChosenRandom; i++) 

{ 
    Console.WriteLine("Random numbers: " + rnd.Next(0, 10)); 
} 

int TotalRandom; 

TotalRandom = ChosenRandom + (RandomNumber); 

Console.WriteLine("Total is:" + TotalRandom); 

int avr; 

avr = TotalRandom/ChosenRandom;  

Console.WriteLine("Average is: " + avr); 

if (ChosenRandom > avr)  
{ 
    Console.WriteLine("Numbers larger than average" + ChosenRandom); 
} 

else 
{ 
    Console.WriteLine("All numbers under average"); 
} 
+4

동일한 클래스의 다른 사람들은 배열을 사용해야 함을 나타냅니다 ... –

답변

2

간단한 방법은 사용하는 배열입니다,

  1. 저장 배열의 번호를 찾기 위해 배열 요소를 사용하여 그들에게

  2. 를 생성하는 동안 전체 및 평균

  3. 각 요소를 평균의 배열과 비교하는 가로 건너 뛰기 배열

+0

어떻게 배열에 임의의 숫자를 저장합니까? –

+0

@ fre87 - 기본적으로 비 난수를 저장하는 것과 같은 방법입니다 :'array [index] = value;'. 당신이 물어보고 싶은 질문은, 임의의 수의 항목이있는 배열을 어떻게 지정 할 것인가입니다. 글쎄, 사용자가 원하는 수의 항목을 가지고 나면 더 이상 무작위가 아닙니다. 그래서 당신은 다음과 같은 것을 할 수 있습니다 :'int [] array = new int [ChosenRandom];' – Corak

+0

오, 죄송합니다. 터미널에서 떨어져 있었지만, 여기 많은 사람들로부터 좋은 답변을 얻은 것처럼 보입니다. – Codeek

0

이 솔루션이 도움이되는지 확인하십시오.

저는 linq을 사용하여 평균을 만들고 '평균'이상의 모든 숫자를 찾습니다.

using System; 
using System.Linq; 

namespace Test 
{ 
    class Program 
    { 
     static void Main() 
     { 
      int chosenRandom; 
      Console.WriteLine("Choose a number between 0-10"); 
      chosenRandom = int.Parse(Console.ReadLine()); 

      Random rand = new Random(); 
      double[] randomNumbers = new double[chosenRandom]; 
      for(int i = 0; i < chosenRandom; i++) 
      { 
       Console.WriteLine("Random numbers: " + (randomNumbers[i] = rand.Next(0, 10))); 
      } 

      double average = randomNumbers.Average(t => t); 

      var numbersAboveAverage = randomNumbers.Where(t => t > average); 
      Console.WriteLine("Average of all random numbers - {0}", average); 

      foreach(var number in numbersAboveAverage) 
       Console.WriteLine(number); 

     } 
    } 
} 
+1

건배 Vinay, 정말 고맙습니다. 당신의 도움! –

+0

@ user4427615는 환영합니다. 돈을 표시하십시오. 그것이 당신의 문제를 해결한다면 대답으로. –

0

프로그램이 좋아 보인다. 그러나, 당신은 틀린 방법으로 질문을 이해했습니다! 문제는 임의의 정수 값이 0-9이어야한다는 것입니다. 임의의 숫자는 아닙니다. 난수의 수는 사용자가 지정한 임의의 값일 수 있습니다.

완전한 구현을 아래에서 찾으십시오.

class Program 
{ 
    static void Main(string[] args) 
    { 
     //Step 1. Get the no of random numbers (n) to be generated from user. 
     Console.WriteLine("Enter the no of Random numbers: "); 
     int n = int.Parse(Console.ReadLine()); 

     //Step 2. Generate 'n' no of random numbers with values rangeing from 0 to 9 and save them in an array. 
     Random rand = new Random(); 
     int[] randCollection = new int[n]; 
     for (int i = 0; i < n; i++) 
     { 
      randCollection[i] = rand.Next(9); 
      Console.WriteLine("Random No {0} = {1}", i + 1, randCollection[i]); 
     } 

     //Step 3. Compute Average 
     double average = randCollection.Average(); 
     Console.WriteLine("Average = {0}", average); 

     //Step 4. Find out how many numbers in the array are greated than the average. 
     int count = 0; 
     foreach(int i in randCollection){ 
      if (i > average) count++; 
     } 
     Console.WriteLine("No of random numbers above their average = {0}", count); 
     Console.ReadLine(); 
    } 
} 

희망이 있습니다. 궁금한 점이 있으면 알려주세요.