I am not grasping this exercise; Please help with some explanations.

Q: Write a method that accepts an array of numbers as a parameter. The method should multiply each number by five, and display the results. Call this method, passing to it an array of any three integers.

THIS IS WHAT I HAVE ATTEMPTED!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace RandomNumber
{
    public class Program
    {
        int result = 0;
        public Program()
        {
            //Declare the Array
            int[] numArray = new int[3];
            //Using a for loop to display the contents of the array
            for (int loop = 0; loop < 3; loop++)
            {
                Console.WriteLine("Please enter a number");
                numArray[loop] = Int32.Parse(Console.ReadLine());
                Console.WriteLine(numArray[loop] * 5);
            }
        }
        public void receiveNumbers(int[] numbers) //this method accepts the array(the value)
        {
            for (int loop = 0; loop < 3; loop++)
            {
                result = int[loop] * 5; // how do I store each element's new result?
                Console.WriteLine(result);
            }
        }
        public static void Main(string[] args)
        {
            Program p = new Program();
            p.receiveNumbers(numArray); // what do I put inside the parameters?
        }
    }
}

Recommended Answers

All 2 Replies

The only error is that you've used int[loop] rather than your actual array, so your code won't work.

Otherwise it looks like you meet the requirements of the work given. It doesn't specifically say you have to store the new numbers, just display them.

This is what I would have done:

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

namespace ConsoleApplication9
{
    class Program
    {
        static void Main(string[] args)
        {
            // declare and init an int array
            // or use Console readline as you did I'm just quick and dirty here
            int[] mynumbers = new int[] { 1, 2, 4 }; 
            receiveNumbers(mynumbers);
            Console.ReadKey(); // keep console on screen untill key pressed
        }

        static void receiveNumbers(int[] numbers) //this method accepts the array(the value)
        {
            //instead of 3, I used the Length property of the array passed in
            //IMHO you don't have to store a thing, it was not in your question
            for (int loop = 0; loop < numbers.Length; loop++)
            {
                // how do I store each element's new result?
                Console.WriteLine(numbers[loop] * 5);
            }
        }
    }
}

The effort you have done is highly appreciated. Any more questions, please ask. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.