Question

Please write a C# program For this part of the lab, you'll be writing methods to...

Please write a C# program

For this part of the lab, you'll be writing methods to work with 1D arrays. We will expect you to know how to create an array, and store and retrieve information from them.

  1. Continue working in the same project and “Program.cs” file.
    • You will be generating random numbers, but the class Random is part of the standard library (don’t need any additional using statements).
    • You should consider developing the methods for this project incrementally.

  1. In Main, use an initializer list create an array of integers called nums holding the following values: 1, 4, 13, 43, -25, 17, 22, -37, 29.

  1. Write a method, called findLargest, that returns the largest value in the array and have the Main print the result.

  1. Create an integer array of size 20, called data and write a method to fill the array with random integers from [-100, 100].
    • Recall that Random’s Next(x, y) method returns a value from x to y-1. The upper bound is exclusive, in other words. Modify the values to fit the required ranges.

  1. Use the findLargest method you previously wrote to return the largest value in data for the Main to print.

  1. Print the sum of the largest values from nums and data.

  1. Print the contents of data.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

CODE FOR THE FOLLOWING PROGRAM:

using System;
class Large {
//Function to find largest element in an array
public static int findLargest(int[] arr,int size) // No Parameter
{
int largest=arr[0];
for (int i = 0; i < size; i++) {
if(arr[i]>largest){
largest=arr[i];
}
}
return largest;
}
public static void Main() {
//myNum array
int[] myNum = {1, 4, 13, 43, -25, 17, 22, -37, 29};
//initalization of data array
int [] data=new int[20];
int sum;// array to store largest element of myNum and data
int large_myNum=findLargest(myNum,myNum.Length); //calling findLargestfor myNum
Console.WriteLine("The largest element in data array is : "+large_myNum);
// Instantiate random number generator
Random rand = new Random();
// filling data with random values from -100 to 100(inclusive)
  
for (int i = 0; i <20; i++) {
data[i]=rand.Next(-100, 101);
}
int large_data=findLargest(data,data.Length);//CAlling findLargest for data array
Console.WriteLine("The largest element in data array is : "+large_data);
sum=large_myNum+large_data; // adding largest elements of myNum and data array
Console.WriteLine("Sum of the largest elements of myNum and data is: "+sum);
Console.WriteLine("Elements in data are: ");
//Printing content of data array
for (int i = 0; i <20; i++) {
Console.Write(data[i]+" ") ;
}
}
}

SCREENSHOT AND OUTPUT OF THE PROGRAM:-

SAMPLE OUTPUT:-HAPPY LEARNING

Add a comment
Know the answer?
Add Answer to:
Please write a C# program For this part of the lab, you'll be writing methods to...
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 java code for the following problem. Lab 7: Methods 1. Write a Java program called Numbers that calls the following methods and displays the returned value: Write a method called cubelt that a...

    I need java code for the following problem. Lab 7: Methods 1. Write a Java program called Numbers that calls the following methods and displays the returned value: Write a method called cubelt that accepts one integer parameter and returns the value raised to the third power as an integer. o Write a method called randominRange that accepts two integer parameters representing a range. The method returns a random integer in the specified range inclusive. 2. o Write a method...

  • C++ Program Int Main First Please Write one program that does the following: 1.       1.   Ask the...

    C++ Program Int Main First Please Write one program that does the following: 1.       1.   Ask the user for ten (10) grades, and store the data in an array.  Compute the average of all the grades.  Print the original ten grades and the average. a.       Declare an integer array with the name of “grades” of size 10 in the main function. b.      Create a function called “getGrades” that prompts the User for the grades and puts them in an integer array.                                                                i.      The function will receive...

  • Lab Objectives Be able to write methods Be able to call methods Be able to declare...

    Lab Objectives Be able to write methods Be able to call methods Be able to declare arrays Be able to fill an array using a loop Be able to access and process data in an array Introduction Methods are commonly used to break a problem down into small manageable pieces. A large task can be broken down into smaller tasks (methods) that contain the details of how to complete that small task. The larger problem is then solved by implementing...

  • daily21. Computing 1. C programming Please provide comments and write pre-condition and post-condition for the function....

    daily21. Computing 1. C programming Please provide comments and write pre-condition and post-condition for the function. Description Create a project called Daily21. Add a source file called daily21.c into the project. In this exercise, you will practice working with an array of integers. In the main function, you first need to create an integer array with 10 elements in t. Use a loop to read 10 integers from the user and store the values to the array (in the order...

  • LAB: How many negative numbers Write a program that generates a list of integers, and outputs...

    LAB: How many negative numbers Write a program that generates a list of integers, and outputs those integers, and then prints the number of negative elements. Create a method (static void fillArray(int [] array)) that uses Random to generate 50 values (Random.nextInt()), put each value in the array (make sure to pass the array in to this method as an argument). Write another method (static int printAndCountNeg(int [] array)) that prints each element’s value and count the number of negative...

  • Create a class named Program10. Your program should have the following 4 methods (other helper methods...

    Create a class named Program10. Your program should have the following 4 methods (other helper methods are allowed, but these four methods must be implemented as specified). You need to define a global scanner object and only use it inside the main method and the getValues method, since other methods do not get any values from the input. getValues: This method does not have any input parameter and returns an array of integers. This method prompts the user with a...

  • *Java* Hi. I need some help with creating generic methods in Java. Write a program GenMethods...

    *Java* Hi. I need some help with creating generic methods in Java. Write a program GenMethods that has the following generic methods: (1) Write the following method that returns a new ArrayList. The new list contains the nonduplicate (i.e., distinct) elements from the original list. public static ArrayList removeDuplicates(ArrayList list) (2) Write the following method that shuffles an ArrayList. It should do this specifically by swapping two indexes determined by the use of the random class (use Random rand =...

  • Write a complete Java program called MethodTest according to the following guidelines. The main method hard-codes...

    Write a complete Java program called MethodTest according to the following guidelines. The main method hard-codes three integer values into the first three positions of an array of integers calls a method you write called doubleEachValue that takes an array of integers (the one whose values you hard-coded in main) as its only argument and returns an ArrayList of integers, with each value in returned ArrayList equal to double the correspondingly indexed value in the array that is passed in...

  • In C only Please! This lab is to write a program that will sort an array...

    In C only Please! This lab is to write a program that will sort an array of structs. Use the functions.h header file with your program. Create a source file named functions.c with the following: A sorting function named sortArray. It takes an array of MyStruct's and the length of that array. It returns nothing. You can use any of the sorting algorithms, you would like though it is recommended that you use bubble sort, insertion sort, or selection sort...

  • Write a program that instantiates an array of integers named scores. Let the size of the...

    Write a program that instantiates an array of integers named scores. Let the size of the array be 10. The program then first invokes randomFill to fill the scores array with random numbers in the range of 0 -100. Once the array is filled with data, methods below are called such that the output resembles the expected output given. The program keeps prompting the user to see if they want to evaluate a new set of random data. Find below...

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