Stuart Cowen

Stuart Cowen

  • NA
  • 1
  • 1.6k

Need feedback on some code

Nov 20 2013 4:11 PM
So, I've made a nice little program to do a bunch of different things, and i was wondering if anyone could give me some feedback on the efficiency and quality of my code.

Uploaded source code.

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

namespace FlowchartExcersisesNo1
{
    class Program
    {
        static void Main(string[] args)
        {
            //My own addition to promt the user to start one of the 7 program, divided into 7 methods, rather than 7 projects.
            int input;
            Console.WriteLine("Please select one of the followng programs to run by typing their number:");
            Console.WriteLine(" Flowchart excersise 1\n Flowchart excersise 2\n Flowchart excersise 3\n Flowchart excersise 4\n Flowchart excersise 5\n Flowchart excersise 6\n Flowchart excersise 7\n");
            do
            {
                bool isValid = int.TryParse(Console.ReadLine(), out input);
                switch (input)
                {
                    case 1:
                        Program.numberOne();
                        break;
                    case 2:
                        Program.numberTwo();
                        break;
                    case 3:
                        Program.numberThree();
                        break;
                    case 4:
                        Program.numberFour();
                        break;
                    case 5:
                        Program.numberFive();
                        break;
                    case 6:
                        Program.numberSix();
                        break;
                    case 7:
                        Program.numberSeven();
                        break;
                    case 0:
                        Console.WriteLine("Please enter a positive integer.");
                        break;
                }
                Console.WriteLine("Enter a new number or type 99 to exit.");
            }
            while(input!=99);
        }
        public static void numberOne()
        {
            //Design an algorithm and the corresponding flowchart for finding the sum of the numbers
            //3,4,5,6,... n
            int sum = 0;
            int lim;
            int n;


            Console.WriteLine("Please enter a multiple of 2 to increment to:");
            lim = int.Parse(Console.ReadLine());

            for (n = 0; n <= lim; n+=2)
            {
                sum = sum + n;
            }

            Console.WriteLine(sum);
        }
        public static void numberTwo()
        {
            //Design an algorithm to add the sum of 100 numbers and display it.
            int[] array = new int[] {89, 76, 82, 3, 4, 52, 35, 22, 38, 49, 26, 39, 64, 79, 43, 6, 38, 45, 100, 30, 92, 86, 18, 59, 27, 67,
                7, 8, 20, 76, 94, 71, 7, 63, 52, 76, 14, 29, 2, 20, 44, 72, 29, 17, 61, 61, 4, 46, 27, 35, 72, 83, 55, 49, 67, 57, 60, 26,
                24, 1, 7, 51, 12, 37, 79, 21, 88, 24, 41, 7, 87, 52, 88, 97, 92, 62, 79, 72, 95, 25, 68, 71, 2, 80, 11, 69, 3, 72, 9, 73,
                48, 44, 49, 25, 84, 37, 23, 14, 41, 92};
            int sum = 0;


            for (int i = 1; i < array.Length; i++)
            {
                sum = sum + array[i];
            }

            Console.WriteLine(sum);
        }
        public static void numberThree()
        {
            //Design an algorithm to take two numbers and display the largest of the two
            Console.WriteLine("Please enter the first number you wish to compare");
            int num1 = int.Parse(Console.ReadLine());
            Console.WriteLine("Now the second number");
            int num2 = int.Parse(Console.ReadLine());


            if (num1 > num2)
            {
                Console.WriteLine("{0} is bigger", num1);
            }
            else
            {
                Console.WriteLine("{0} is bigger", num2);
            }
        }
        public static void numberFour()
        {
            //Design an algorithm to take two numbers and display the smallest of the two
            Console.WriteLine("Please enter the first number you wish to compare");
            int num1 = int.Parse(Console.ReadLine());
            Console.WriteLine("Now the second number");
            int num2 = int.Parse(Console.ReadLine());


            if (num1 < num2)
            {
                Console.WriteLine("{0} is smaller", num1);
            }
            else
            {
                Console.WriteLine("{0} is smaller", num2);
            }
        }
        public static void numberFive()
        {
            //Design an algorithm to take three numbers and display the largest of the three
            Console.WriteLine("Please enter the first number you wish to compare");
            int num1 = int.Parse(Console.ReadLine());
            Console.WriteLine("Now the second number");
            int num2 = int.Parse(Console.ReadLine());
            Console.WriteLine("Now the third number");
            int num3 = int.Parse(Console.ReadLine());


            if (num1>num2 && num1>num3)
            {
                Console.WriteLine("{0} was the larger of the 3", num1);
            }
            else if (num2 > num1 && num2 > num3)
            {
                Console.WriteLine("{0} was the larger of the 3", num2);
            }
            else
            {
                Console.WriteLine("{0} was the larger of the 3", num3);
            }
        }
        public static void numberSix()
        {
            //Design an algorithm to find the largest of 100 numbers
            int n = 0;
            int[] array = new int[] {89, 76, 82, 3, 4, 52, 35, 22, 38, 49, 26, 39, 64, 79, 43, 6, 38, 45, 100, 30, 92, 86, 18, 59, 27, 67,
                7, 8, 20, 76, 94, 71, 7, 63, 52, 76, 14, 29, 2, 20, 44, 72, 29, 17, 61, 61, 4, 46, 27, 35, 72, 83, 55, 49, 67, 57, 60, 26,
                24, 1, 7, 51, 12, 37, 79, 21, 88, 24, 41, 7, 87, 52, 88, 97, 92, 62, 79, 72, 95, 25, 68, 71, 2, 80, 11, 69, 3, 72, 9, 73,
                48, 44, 49, 25, 84, 37, 23, 14, 41, 92};


            for (int i = 0; i < array.Length; i++)
            {
                if (array[i]>n)
                {
                    n = array[i];
                }
            }

            Console.WriteLine("{0} was the largest of the 100 numbers", n);
        }
        public static void numberSeven()
        {
            //Design an algorithm to display the largest score of 100 tests and count the number of tests which scored above 50.
            int n = 0;
            int TAB = 0;
            int[] tests = new int[] {89, 76, 82, 3, 4, 52, 35, 22, 38, 49, 26, 39, 64, 79, 43, 6, 38, 45, 100, 30, 92, 86, 18, 59, 27, 67,
                7, 8, 20, 76, 94, 71, 7, 63, 52, 76, 14, 29, 2, 20, 44, 72, 29, 17, 61, 61, 4, 46, 27, 35, 72, 83, 55, 49, 67, 57, 60, 26,
                24, 1, 7, 51, 12, 37, 79, 21, 88, 24, 41, 7, 87, 52, 88, 97, 92, 62, 79, 72, 95, 25, 68, 71, 2, 80, 11, 69, 3, 72, 9, 73,
                48, 44, 49, 25, 84, 37, 23, 14, 41, 92};


            for (int i = 0; i < tests.Length; i++)
            {
                if (tests[i] > n)
                {
                    n = tests[i];
                }
                if (tests[i]>50)
                {
                    TAB++;
                }
            }

            Console.WriteLine("The greatest score was: {0}\nThe amount of students who scored above 50 was: {1}", n, TAB);
        }
    }
}


Answers (1)