Question

Please help me with this in C# language. Returning an array from a function The goal...

Please help me with this in C# language.

Returning an array from a function

The goal for this exercise is to make sure that you return an array from a method (as a return value). Also: to give you more practice creating and using methods/functions.

What you need to do for this exercise:

  1. In the starter project, add code to the Returning_An_Array class, so that the RunExercise method does the following:
    1. Declare an integer array variable, but DO NOT ALLOCATE THE ARRAY ITSELF.
    2. Call the CreateRandomlyFilledArray method (found in the ArrayReturnMethods class) which will allocate (and then return, via the return value) a brand-new array. This method will take as it's only parameter an integer, which tells the method how long (how many elements) the array should have. The array is then created inside the method, filled with values that have been randomly created by the method. The random values should vary between 0 and 100 (inclusive of both 0 and 100).
      1. You may assume that the parameter’s value is a positive number. In other words, you are NOT required to check for zero, or negative numbers.
        1. As always, you’re welcome to do this check, it’s just not required J
    3. The array will then be passed (as a parameter) to the PrintArray method (found in the ArrayReturnMethods class), which will take as it's single parameter an array of integers, and will print out everything in the array.
      1. If you haven’t implemented the PrintArray method yet, you should. It’s pretty simple, good practice, and very useful.
      2. It should take as it’s only parameter an array of integers, and it should print out (using Console.WriteLine) every element of the array.
    4. There is a block of commented-out code for the Returning_An_Array class, including some ‘sample code’ that helps to demonstrate how to use the methods that you’ll be writing. You need to uncomment this, and build your answer on it.
      Some of this code you will have to modify to complete this exercise, and the rest of it you are allowed to modify (but don't modify the code to the point where it avoids the goal of this exercise J )
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ArrayReturnMethods
{
class Program
{
static void Main(string[] args)
{
int[] arr;
arr = CreateRandomlyFilledArray(10);
Console.WriteLine("Array with size 10 is: ");
PrintArray(arr);

}

private static void PrintArray(int[] arr)
{
for (int i = 0; i < arr.Length; i++)
Console.WriteLine(arr[i]);
}
public static int[] CreateRandomlyFilledArray(int size)
{
int[] Array = new int[size];
Random random = new Random();
  
for (int i = 0; i < size; i++)
{
Array[i] = random.Next(1, 101);
}
return Array;
}
}
}

output- x C. C:\WINDOWS\system32\cmd.exe Array with size 10 is: 29 43 19 64 45 Press any key to continue ... Type here to search A

If you have any query regarding the code please ask me in the comment i am here for help you. Please do not direct thumbs down just ask if you have any query. And if you like my work then please appreciates with up vote. Thank You.

Add a comment
Know the answer?
Add Answer to:
Please help me with this in C# language. Returning an array from a function The goal...
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
  • 1.Write code for a Java method, printArray, that takes an int array as parameter and prints...

    1.Write code for a Java method, printArray, that takes an int array as parameter and prints it out, one element per line. public static void printArray (int[] values){ Methods that do NOT return a result have “void” as return type. Notice how an array parameter type is passed, int [] }// end printArray 2.Write code for a Java method that takes in as input parameter an integer number, say num, and returns a double array of size num with all...

  • please help me with this in C# language. Constructors The goal for this exercise is to...

    please help me with this in C# language. Constructors The goal for this exercise is to understand what constructors are, how to define them, and how to call them, including ‘default’ constructors, and including the use of overloading to provide multiple constructors. One of the advantages of having a clear separation between the public interface of an object and private internal implementation of an object is that once you've got the data in the object you can then ask the...

  • I need help in this please, in the c# language. Objects As Parameters The goal for...

    I need help in this please, in the c# language. Objects As Parameters The goal for this exercise is to define the Television object, which you will be using in several, subsequent exercises. For all the methods that you implement (in this course (not just this exercise, but in this course, as you go forwards)), you should remember that since method is public, anyone, anywhere can call the method. Even people whom you never thought would call this method. Therefore,...

  • In this lab, you will create one class named ArrayFun.java which will contain a main method...

    In this lab, you will create one class named ArrayFun.java which will contain a main method and 4 static methods that will be called from the main method. The process for your main method will be as follows: Declare and create a Scanner object to read from the keyboard Declare and create an array that will hold up to 100 integers Declare a variable to keep track of the number of integers currently in the array Call the fillArray method...

  • What to submit: your answers to exercises 1, 3, and 4 and separate the codes to...

    What to submit: your answers to exercises 1, 3, and 4 and separate the codes to each question. Using your solution to question 3 of Lab Practice 2, modify it to create a class that contains a static method; the method takes a string as a parameter and returns a boolean value indicating whether the parameter string has repeated characters in it or not. That is, return true if there is at least one character which appears more than once...

  • Java question Q1) Use the following code snippet which generates a random sized array with random...

    Java question Q1) Use the following code snippet which generates a random sized array with random contents to complete the following problems: public int [] createRandomArray() {         int size = (int) (Math.random() * 10) + 1;         int[] array = new int [size];         for (int i = 0; i < array.length; i++) {             array[i] = (int) (Math.random() * 10 ) + 1;         }         return array;     } Assignment...

  • Write a JAVA program with methods that initializes an array with 20 random integers between 1...

    Write a JAVA program with methods that initializes an array with 20 random integers between 1 and 50 and then prints four lines of output, containing 1)The initialized array. 2)Every element at an even index. 3)Every even element. 4)All elements in reverse order. Requirements (and hints): 1. Build a method that takes any integer array as input and prints the elements of the array. ALL printing in this lab must be done using a call to the printArray method and...

  • JAVA PROG HW Problem 1 1. In the src −→ edu.neiu.p2 directory, create a package named...

    JAVA PROG HW Problem 1 1. In the src −→ edu.neiu.p2 directory, create a package named problem1. 2. Create a Java class named StringParser with the following: ApublicstaticmethodnamedfindIntegerthattakesaStringandtwocharvariables as parameters (in that order) and does not return anything. The method should find and print the integer value that is located in between the two characters. You can assume that the second char parameter will always follow the firstchar parameter. However, you cannot assume that the parameters will be different from...

  • 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...

  • Part 1- Method practice Create a main method. In there, make an array of 100 random...

    Part 1- Method practice Create a main method. In there, make an array of 100 random integers(numbers between 0 and 1000). Create a method to find average of the array. Pass the array and return the number(double) that is the average In the main method, call the method created in Step 2. Print out the average Create a method to find the min value of the array. Pass the array and return the int that is the smallest.(pass the value...

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