6 Replies - 1029 Views - Last Post: 08 June 2015 - 01:26 PM Rate Topic: -----

#1 develauper   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 10-February 15

Asked to convert program to methods?

Posted 07 June 2015 - 05:29 PM

I was given the following program code and asked to convert it to methods. I assume I am supposed to break it down into smaller pieces, but I am unsure how (I've tried multiple times and when I break down the code into smaller pieces with {} I cant get it to run.) How would I break down this code into methods?

 


        static void Main(string[] args)
        {
            int number_of_scores;
            double score, total_score = 0, high_score, low_score, average;

            Console.Write("please enter the number of scores that you wish to process? ");
            number_of_scores = int.Parse(Console.ReadLine());

            Console.Write("Please enter score " + 1 + " ");
            score = double.Parse(Console.ReadLine());

            high_score = score;
            low_score = score;

            total_score = total_score + score;

            for (int i = 2; i <= number_of_scores; i++)
            {
                Console.Write("Please enter score " + i + " ");
                score = double.Parse(Console.ReadLine());

                total_score = total_score + score;

                if (score > high_score)
                    high_score = score;

                if (score < low_score)
                    low_score = score;
}
  average = total_score / number_of_scores;

        Console.WriteLine("you entered " + number_of_scores + "scores"));
    Console.WriteLine("the high score is " + high score);
    Console.WriteLine("the low score is " + low_score);
    Console.WriteLine("the average score is " + average);
}

}

} 
 


Is This A Good Question/Topic? 0
  • +

Replies To: Asked to convert program to methods?

#2 macosxnerd101   User is online

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12360
  • View blog
  • Posts: 45,475
  • Joined: 27-December 08

Re: Asked to convert program to methods?

Posted 07 June 2015 - 05:36 PM

A method is a modular and reusable piece of functionality. Think of it like a little black box. You feed it an input, press a button, then it gives you some output.

One suggestion I have is to store the input values into an array of some sorts. Next, let's identify the important pieces of functionality:
-Find min
-Find max
-Get total score
-Get average score

Could you write methods for each of these individual operations, given input? Do you see how methods for these operations might be more modular and reusable?
Was This Post Helpful? 1
  • +
  • -

#3 develauper   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 10-February 15

Re: Asked to convert program to methods?

Posted 07 June 2015 - 05:44 PM

View Postmacosxnerd101, on 07 June 2015 - 05:36 PM, said:

A method is a modular and reusable piece of functionality. Think of it like a little black box. You feed it an input, press a button, then it gives you some output.

One suggestion I have is to store the input values into an array of some sorts. Next, let's identify the important pieces of functionality:
-Find min
-Find max
-Get total score
-Get average score

Could you write methods for each of these individual operations, given input? Do you see how methods for these operations might be more modular and reusable?



So would it be best to break down my code into smaller pieces like :

 


        static void Main(string[] args)
        {
            int number_of_scores;
            double score, total_score = 0, high_score, low_score, average;

            Console.Write("please enter the number of scores that you wish to process? ");
            number_of_scores = int.Parse(Console.ReadLine());

            Console.Write("Please enter score " + 1 + " ");
            score = double.Parse(Console.ReadLine());

            high_score = score;
            low_score = score;

            total_score = total_score + score;



Method 2
            for (int i = 2; i <= number_of_scores; i++)
            {
                Console.Write("Please enter score " + i + " ");
                score = double.Parse(Console.ReadLine());

                total_score = total_score + score;

                if (score > high_score)
                    high_score = score;

                if (score < low_score)
                    low_score = score;
}



Method 3
  average = total_score / number_of_scores;

        Console.WriteLine("you entered " + number_of_scores + "scores"));
    Console.WriteLine("the high score is " + high score);
    Console.WriteLine("the low score is " + low_score);
    Console.WriteLine("the average score is " + average);





And how exactly do I break it down? What is the best way to create a method/How do I create a new one within a single program and have it be able to run? I broke down the code into 3 methods based on function but I am not sure if there was a better way.

My earlier issue was when I broke down the code I couldnt get it to find the variables I created if it was not in that method.
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101   User is online

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12360
  • View blog
  • Posts: 45,475
  • Joined: 27-December 08

Re: Asked to convert program to methods?

Posted 07 June 2015 - 05:51 PM

I would suggest familiarizing yourself with the syntax of methods. This tutorial is a good starting point.

Quote

My earlier issue was when I broke down the code I couldnt get it to find the variables I created if it was not in that method.

This is why methods accept parameters. A parameter is a piece of input the method expects and you need to pass. Go through the above tutorial and take some time to work through it. I think it will do a good job of clearing some things up. Then try to restructure your code and we can work through it some more. :)
Was This Post Helpful? 0
  • +
  • -

#5 develauper   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 10-February 15

Re: Asked to convert program to methods?

Posted 07 June 2015 - 09:54 PM

So I looked at what you sent me, it was very helpful but still a little lost. This is what I have
 
 class Program
    {

        static void Main(string[] args)
        {
            int number_of_scores;
            double score, total_score = 0, high_score, low_score, average;

            Console.Write("please enter the number of scores that you wish to process? ");
            number_of_scores = int.Parse(Console.ReadLine());

            Console.Write("Please enter score " + 1 + " ");
            score = double.Parse(Console.ReadLine());

            high_score = score;
            low_score = score;

            total(score, ref total_score);

            decide(ref int number_of_scores, double high_score, double low_score, double score, double total_score);


        Console.WriteLine("you entered " + number_of_scores + "scores");
    Console.WriteLine("the high score is " + high_score);
    Console.WriteLine("the low score is " + low_score);
    Console.WriteLine("the average score is " + average);


            }

        }

        static void total(double score, ref double total_score)
        {
            total_score = total_score + score;
        }

        static void decide (ref int number_of_scores, double high_score, double low_score, double score)
        {
             for (int i = 2; i <= number_of_scores; i++)
            {
                Console.Write("Please enter score " + i + " ");
                score = double.Parse(Console.ReadLine());

                total(score, ref total_score);

                if (score > high_score)
                    high_score = score;

                if (score < low_score)
                    low_score = score;

                 average = total_score / number_of_scores;
        }
    }
}




I have big issues around
    Console.WriteLine("you entered " + number_of_scores + "scores");
    Console.WriteLine("the high score is " + high_score);
    Console.WriteLine("the low score is " + low_score);
    Console.WriteLine("the average score is " + average);
 
where I have red squiggles over writeline and my variables
Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101   User is online

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12360
  • View blog
  • Posts: 45,475
  • Joined: 27-December 08

Re: Asked to convert program to methods?

Posted 07 June 2015 - 10:03 PM

So let's refactor the logic some. First, your Main() method will be where you get input. Store that input into an array. Then you'll pass that array to the different methods. Outside of your Main() method, don't prompt for input or output anything. Use the parameters as starting points and return values instead.

The logic is more like this.
class Program
{
    Static void Main(string[] args)
    {
        //construct an int[] and populate it with the user input
        int[] input = new int[/*some value you get from user input*/];
        int max = findMax(input);
    }

    Static int findMax(int[] elements)
    {
        //loop through elements and find the max
        //then return the max value
    }
}



Notice how when I invoke findMax(input), I don't include a type in there. The only time I declare a type is in the method, such as in the header: Static int findMax(int[] elements).

So here in your Main() method, this is illegal syntax.
 decide(ref int number_of_scores, double high_score, double low_score, double score, double total_score);


Was This Post Helpful? 0
  • +
  • -

#7 Curtis Rutland   User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5104
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: Asked to convert program to methods?

Posted 08 June 2015 - 01:26 PM

I've just written a new Methods tutorial that corrects some errors in the previous version, and has some additional information. If you're interested, check it out here:

http://www.dreaminco...4-c%23-methods/
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1