0

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()
        {

        }


    }
}
3
  • 3
    You will not learn anything, if StackOverflow does your Homework. Commented Apr 21, 2015 at 21:39
  • Look up msdn.microsoft.com/en-us/library/… Commented Apr 21, 2015 at 21:42
  • @joe - if you want to learn more see if you can come up with at least 3 other ways that GetNums could communicate the data it got to the rest of the program (and have the others consume it). See if you could avoid repeating the console.readline/int.parse Commented Apr 21, 2015 at 22:52

5 Answers 5

2

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 :)

Sign up to request clarification or add additional context in comments.

Comments

1

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.

2 Comments

Thank you so much. It was just an understanding issue for me. I now understand it and my cod is coming along quite nice. It was within the order of operations for me, understanding how methods called each other and posted values. I just wanted to provide as much info as I could for good clarification for everyone.
@JoeVanHorn very happy to hear that. We are all here to help. Good luck.
0

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.

1 Comment

Yes this is what I needed to understand. Thank you very much. I was getting stuck on how to pass the arguments through my methods.
0

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 :)

Comments

-1

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));
    }

8 Comments

Great so now, instead of letting him learn a little bit, you just hurt him by just giving him the entire answer.
boooo - u did his homework for him. @joe, make sure you tell yr teacher that SO did yr homework
You should not be doing people's homework for them, especially when they explicitly stated it was for school :)
Even though you gave the answer. Downvoting you is a bit harsh.
Whether it helps or hurts all depends on how he decides to use the information. Reading code and understanding it is helpful. Copying and pasting and submitting it as his own is hurtful.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.