×
Samples Blogs Make Payment About Us Reviews 4.9/5 Order Now

C# Program to Create Maze Assignment Solution

July 03, 2024
Emily Davis
Emily Davis
🇦🇺 Australia
C#
Emily Davis is a proficient C# Homework Helper with 9 years of experience. She earned her Master's degree at the University of New South Wales, Australia.
Key Topics
  • Instructions
  • Requirements and Specifications
Tip of the day
Use Python libraries effectively by importing only what you need. For example, if you're working with data, using libraries like pandas and numpy can save time and simplify complex tasks like data manipulation and analysis.
News
In 2024, the Biden-Harris Administration has expanded high-dosage tutoring and extended learning programs to boost academic achievement, helping programming students and others recover from pandemic-related setbacks. These initiatives are funded by federal resources aimed at improving math and literacy skills​

Instructions

Objective
Write a program to solve math problems in C#.

Requirements and Specifications

Description: A C# console program that allows users to select different types of math problems, show the user the appropriate problem, and check whether or not they gave the correct answer. MUST BE DONE IN FUNCTIONAL PROGRAMMING.
Source Code
using System;
using System.Collections.Generic;
namespace WangApp
{
    // Create a class to represent a Math problem
    class MathProblem
    {
        private string description;
        private double answer;
        public MathProblem(string desc, double ans)
        {
            description = desc;
            answer = ans;
        }
        public string getDescription() { return description; }
        public double getAnswer() { return answer; }
        public bool checkAnswer(double ans)
        {
            return answer == ans;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            // Create a list of math problems
            List problems = new List ();
            problems.Add(new MathProblem("Solve for x: x + 5 = 2x-17", 22));
            problems.Add(new MathProblem("Calculate 7^2", 49));
            problems.Add(new MathProblem("Calculate 10/(100-98) + 4^2", 21));
            problems.Add(new MathProblem("Solve for y: (8y - 4)/(4y+12) = 1", 4));
            problems.Add(new MathProblem("Calculate 1567^0", 1));
            display_menu(problems);
            // Get option from user
            int option = getMenuOption(1, 5); // between 1 and 5 because we have 5 problems
            // Display problem again:
            Console.WriteLine(problems[option - 1].getDescription());
            Console.Write("Answer = ");
            // Now, ask for answer
            double answer = Convert.ToDouble(Console.ReadLine());
            // Check if it is correct
            bool correct = problems[option - 1].checkAnswer(answer);
            if(correct)
            {
                Console.WriteLine("That is correct!");
            }
            else
            {
                Console.WriteLine("Sorry, that is not correct.");
            }
            Console.Read();
        }
        // Create a function to display the Math rpblems (Menu)
        static void display_menu(List problems)
        {
            // Loop through problems and prints them
            int i = 1;
            foreach(MathProblem p in problems)
            {
                Console.WriteLine(i.ToString() + ") " + p.getDescription());
                i += 1;
            }
        }
        // Create a function that requests the user an integer that is inside a range
        static int getMenuOption(int lower, int upper)
        {
            int option;
            // Create a while-loop that will keep requesting user for an option until s/he enters a valid option
            while(true)
            {
                Console.Write("Enter option: ");
                option = Convert.ToInt32(Console.ReadLine());
                if(option >= lower && option <= upper)
                {
                    return option;
                }
                else
                {
                    Console.WriteLine("Please enter a valid option between " + lower.ToString() + " and " + upper.ToString() + ".");
                }
            }
        }
    }
}

Related Samples

Discover our C# Assignments Sample Section, featuring expertly crafted solutions. From basic syntax to object-oriented principles and LINQ queries, explore annotated code examples. Whether you're learning GUI development or database connectivity, these samples provide clarity and guidance to excel in C# programming assignments effortlessly.