user4816679
user4816679

Reputation:

C# Console Application - Using Methods to Pass Arguments

I have an application assignment from school that I have been working on and have gotten stuck on. I am not understanding some of the concepts to complete my program. It is simple and I have the basic structure down. Could someone assist me in understanding and completing my program? Below is the listed information:

The following is the overall application assignment:

Write a program to display the average of some numbers. Your program will consist of three user defined methods named GetNums(), CalcAvg(), and DspAvg(). In GetNums() prompt for and read in 3 real numbers from the keyboard. After reading in the numbers, call CalcAvg() passing the three numbers as input. CalcAvg() must RETURN (use the return statement) the average of the 3 numbers. After calling CalcAvg(), call DspAvg() to display the average and the three numbers entered. Your program must not contain any variables with class-wide scope (all variables must be declared inside a method). All method calls (GetNums(), CalcAvg(), and DspAvg() must be called from main()). Using proper passing is important.

Your output should closely resemble the following.

The average of 10.20, 89.50, and 17.60 is 39.10.

Round the average to two decimal places. Display all values with two decimal places.

GetNums() will have three arguments, all pass by out. CalcAvg() will have three arguments, all pass by copy. Do not use four! DspAvg() will have four arguments, all pass by copy.

Below is the following code I have written, but have gotten stuck, based on the requirements above.

static void Main(string[] args)
        {
            int nu1, nu2, nu3, cavg;

            GetNums();
            CalcAvg();
            DspAvg();
        }

        static void GetNums()
        {
            Console.Write("Please enter nu1: ");
                nu1 = int.Parse(Console.ReadLine());

            Console.Write("Please enter nu2: ");
                nu2 = int.Parse(Console.ReadLine());

            Console.Write("Please enter nu3: ");
                nu3 = int.Parse(Console.ReadLine());

                CalcAvg(DspAvg);
        }

        static void CalcAvg()
        {

        }

        static void DspAvg()
        {

        }


    }
}

Upvotes: 0

Views: 3259

Answers (5)

Preston Martin
Preston Martin

Reputation: 2963

I will provide some suggestions, but this a very good introductory programming problem. I would very much recommend trying to solve this problem on your own as it uses basic programming concepts that you will use in the future.

  1. Your variables nu1, nu2, nu3, and cavg should be double values. An int can not be used to store decimal values. (Note: to get a value with 2 decimal places, you should use the Math.Round method)

    double nu1, nu2, nu3, cavg;
    
  2. You seem to be stuck on how to pass paramaters to a method. For example, when you call the CalcAvg method in your main method, you should be passing the 3 values you read from the console into the method.

    static void Main(string[] args){
        CalcAvg(nu1,nu2,nu3);
    }
    
    static void CalcAvg(double nu1, double nu2, double nu3){
    }
    

    By doing this, you can now manipulate the values within your CalcAvg method.

  3. Finally, once you have passed values into a method and manipulated them, you will want to return these values from the method. This can be done with the return statement. You can then call this method by passing in your paramaters and storing the returned value in another variable.

    static void Main(string[] args){
        double cavg;
        cavg = CalcAvg(nu1,nu2,nu3);
    }
    
    static double CalcAvg(double nu1, double nu2, double nu3){
        double cavg;
        return cavg;
    }
    

These three principles should help you complete this assignment. I STRONGLY recommend you do this assignment on your own and not copy the complete answer off of this or any other website. Learning these principles correctly will help you further down the road in your academic career. Good luck :)

Upvotes: 0

Guffa
Guffa

Reputation: 700152

You are not using parameters as described in the assignment. Here is an example of using an out parameter with one method and sending it into another method and returning a value:

double parameter, result;
methodOne(out parameter);
result = methodTwo(parameter);

The methods:

static void methodOne(out double x) {
  x = 42;
}

static double methodTwo(double x) {
  return x;
}

That should get you started on how you need to modify your methods.

Upvotes: 0

AntLaC
AntLaC

Reputation: 1225

Well, we won't do your homework for you, but I can give you a few pointers. Your Main should only call GetNums(). GetNums() should call CalcAvg() passing in the numbers read from the console not the DspAvg() function. Finally pass the returned value from CalcAvg() to DspAvg() to display the result to the console.

Start writing some code and if you are getting errors, then we will be able to help you more.

Upvotes: 1

Padraic
Padraic

Reputation: 667

I've made a few assumptions here. But I've gone ahead to give you a "working" app for you to consider

static void Main(string[] args)
    {
        int nu1, nu2, nu3, cavg;

        GetNums(out nu1, out  nu2, out nu3);
        double average = CalcAvg(nu1, nu2, nu3);
        DspAvg(average);

    }

    static void GetNums(out int nu1, out int nu2, out int nu3)
    {
        Console.Write("Please enter nu1: ");
        nu1 = int.Parse(Console.ReadLine());

        Console.Write("Please enter nu2: ");
        nu2 = int.Parse(Console.ReadLine());

        Console.Write("Please enter nu3: ");
        nu3 = int.Parse(Console.ReadLine());


    }

    static double CalcAvg(int nu1, int nu2, int nu3)
    {
        return (nu1 + nu2 + nu3) / 3;
    }

    static void DspAvg(double average)
    {
        Console.WriteLine(Math.Round(average, 2));
    }

Upvotes: -1

atlanteh
atlanteh

Reputation: 5835

You can't declare variables in one method and use them in another method. the second method doesn't know the variables in the first method. As the specification said you need your GetNums() to have 3 parameters passed by out

static void GetNums(out decimal num1, out decimal num2, out decimal num3)

Start from here. If you need more help please let me know :)

Upvotes: 2

Related Questions