Question

In: Computer Science

C# Arrays & methods I'm having trouble implementing the GetIntArrayFromUser method in the following problem: Create...

C# Arrays & methods

I'm having trouble implementing the GetIntArrayFromUser method in the following problem:

Create a method AverageIntArray that takes an array of integers and calculates and returns the average of all values in the array as a double Write a program that uses GetIntArrayFromUser method to get an array of 7 numbers, then calls the AverageIntArray method to get the average then prints all values in the array that are greater than the average.
                Sample run:
                                Enter next whole number: 3
                                Enter next whole number: 8
                                Enter next whole number: 4
                                Enter next whole number: 12    
                                Enter next whole number: 13
                                Enter next whole number: 6
                                Enter next whole number: 9
                                Average: 7.85714285714286
                                8
                                12
                                13
                                9

here is GetIntArrayFromUser:

public void GetIntArrayFromUser()
{
Console.WriteLine("Enter number of list items");
int size = Convert.ToInt32(Console.ReadLine());
int[] arr = new int[size];
for (int i = 0; i < size; i++)
{
Console.WriteLine("Enter next whole number");
arr[i] = Convert.ToInt32(Console.ReadLine());
}
Console.WriteLine("Items in the list: ");
foreach (int item in arr)
{
Console.WriteLine(item);
}
}

Solutions

Expert Solution

Below is your complete code: -

using System;
                  
public class Program
{
   public static void Main()
{
int[] arr = GetIntArrayFromUser();
double average = AverageIntArray(arr);
Console.WriteLine("Average: {0}",average);
for(int i = 0; i < arr.Length; i++) {
if(arr[i] > average) {
Console.WriteLine(arr[i]);
}   
}

}
  
public static int[] GetIntArrayFromUser()
{
int count = 0;
int[] arr = new int[7];
while ( count < 7) {
Console.Write("Enter next whole number: ");
arr[count++] = Convert.ToInt32(Console.ReadLine());
}
return arr;
}
  
public static double AverageIntArray(int[] arr) {
double sum = 0;
for(int i = 0; i < arr.Length; i++) {
sum = sum + arr[i];
}
return sum/(double) arr.Length;
}
}

Output: -

Enter next whole number: 3
Enter next whole number: 8
Enter next whole number: 4
Enter next whole number: 12
Enter next whole number: 13
Enter next whole number: 6
Enter next whole number: 9
Average: 7.85714285714286
8
12
13
9


Related Solutions

I'm having trouble trying to create the italian, polish, and netherlands flag in C Here's my...
I'm having trouble trying to create the italian, polish, and netherlands flag in C Here's my code so far: #include<stdio.h> int main(){    int country_choice;    fprintf(stderr, "Enter the country_code of the chosen flag.(1 for Poland, 2 for Netherlands, 3 for Italy)");    fscanf(stdin, "%d", &country_choice);    switch (country_choice) { case 1: printf("Poland Flag\n"); printf("%c%c%c", 255,255,255); printf("%c%c%c", 220,20,60);    break;       case 2: printf("Italian Flag\n"); printf("%c%c%c", 0,146,70); printf("%c%c%c", 225,255,255); printf("%c%c%c", 206,43,55); break;    case 3: printf("Netherlands Flag\n"); printf("%c%c%c", 174,28,40);...
I'm having trouble implementing the merge function on this recursive merge sort. I am not allowed...
I'm having trouble implementing the merge function on this recursive merge sort. I am not allowed to change the parameter list in the functions. This is what I have so far, written in C. So far I've been getting segfaults, and I feel like it's because I'm off by 1, somewhere... void merge_the_array(int *list, int l1, int h1, int l2, int h2){ // create a temp array for l1-h1 int arr_a[(h1-l1)+1]; // create a temp array for l2-h2, adding 1...
I am having trouble with a C++ code that I'm working on. It is a spell...
I am having trouble with a C++ code that I'm working on. It is a spell checker program. It needs to compare two arrays, a dictionary, and an array with misspelled strings that are compared to the strings in the dictionary. the strings that are in the second array that is not in the Dictionary are assumed to be misspelled. All of the strings in the dictionary are lowercase without any extra characters so the strings that are passed into...
I'm having trouble figuring out what the standard deviation is for the following problem. Common stock...
I'm having trouble figuring out what the standard deviation is for the following problem. Common stock of Escapist Films sells for $30 a share and offers the following payoffs next year: Boom 0 $21 Normal $1 $32 Recession $3 $36 The expected return of Escapist is: Boom -30% Normal 10% Recession 30% Calculate the standard deviation of Escapist.
I'm having trouble figuring out the constraints to this problem. I know that I am maximizing...
I'm having trouble figuring out the constraints to this problem. I know that I am maximizing 55x + 45y, however the variables are throwing me off. I only need help on question #1 as it would be a great help to understanding the rest of what the questions are asking. The problem is as follows: NorCal Outfitters manufactures a variety of specialty gear for outdoor enthusiasts. NorCal has decided to begin production on two new models of crampons: the Denali...
Using dev c++ I'm having trouble with classes. I think the part that I am not...
Using dev c++ I'm having trouble with classes. I think the part that I am not understanding is sending data between files and also using bool data. I've been working on this program for a long time with many errors but now I've thrown in my hat to ask for outside help. Here is the homework that has given me so many issues: The [REDACTED] Phone Store needs a program to compute phone charges for some phones sold in the...
I'm having trouble thinking of a way that I can delete duplicates from a c string...
I'm having trouble thinking of a way that I can delete duplicates from a c string of alphabets. So what I'm supposed to do is I'm supposed to delete the repeated letters in the c string using pointers and also dynamically allocating space. I'm guessing that I will have to dynamically allocate space for an array to put in the letters that only appear once. To do that, can I create 2 pointers in a function and have 1 pointer...
I'm having trouble with my do while loop. I'm trying to get it where if the...
I'm having trouble with my do while loop. I'm trying to get it where if the user enter's 3 after whatever amount of caffeinated beverages they've entered before then the loop will close and the rest of my code would proceed to execute and calculate the average price of all the caffeinated beverages entered. Can someone please help me with this? Here's my Code: import java.util.Scanner; public class Main { public static void main(String[] args) { CaffeinatedBeverage[] inventory = new...
I'm having trouble understanding the following code (a snippet of a code). What does it do?...
I'm having trouble understanding the following code (a snippet of a code). What does it do? The whole code is about comparing efficiencies of different algorithms. def partition(list,first,last): piv = list[first] lmark = first+1 rmark = last done = False while not done: while lmark <= rmark and list[lmark]<=piv: lmark=lmark+1 while list[rmark]>=piv and rmark>=lmark: rmark=rmark-1 if rmark<lmark: done = True else: temp = list[lmark] list[lmark]=list[rmark] list[rmark]=temp temp = list[first] list[first]=list[rmark] list[rmark]=temp return rmark
I'm having trouble creating a histogram using openCV (color segmentation)
I'm having trouble creating a histogram using openCV (color segmentation)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT