Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace ConsoleApplication4
- {
- class Program
- {
- public static void differentiate(float[,] input)
- {
- if (input.GetLength(1) == 2)
- {
- for(int i = 0; i < input.GetLength(0); i++)
- {
- if(input[i,1] != 0)
- {
- input[i, 0] = input[i, 0] * input[i, 1];
- input[i, 1] = input[i, 1] - 1;
- }
- else
- {
- input[i, 0] = 0;
- input[i, 1] = 0;
- }
- }
- }
- else
- {
- Console.WriteLine("Length 0 of input array != 2");
- }
- }
- public static void printEq(float[,] eq)
- {
- for (int i = 0; i < eq.GetLength(0); i++)
- {
- if (eq[i, 1] == 0) //if power is 0
- {
- if (eq[i, 0] != 0)
- {
- Console.Write(eq[i, 0]);
- }
- }
- else if (eq[i, 1] == 1) //if power is 1
- {
- if (eq[i, 0] == 1) //if coefficient is 1
- {
- Console.Write("x");
- }
- else
- {
- Console.Write(eq[i, 0] + "x");
- }
- }
- else //if it is another power
- {
- if (eq[i, 0] == 1) //if coefficient is 1
- {
- Console.Write("x^" + eq[i, 1]);
- }
- else
- {
- Console.Write(eq[i, 0] + "x^" + eq[i, 1]);
- }
- }
- if (i != (eq.GetLength(0) - 1)) //every term but the last
- {
- if(eq[i+1,0] != 0)
- {
- Console.Write(" + ");
- }
- }
- }
- Console.WriteLine();
- }
- static void Main(string[] args)
- {
- String input;
- int num;
- do
- {
- Console.WriteLine("How many different powers of x does the equation have?");
- input = Console.ReadLine();
- }
- while (!(Int32.TryParse(input, out num)));
- float[,] eq = new float[num, 2];
- for (int i = 0; i<eq.GetLength(0); i++)
- {
- do
- {
- Console.WriteLine("Enter the power of x");
- input = Console.ReadLine();
- }
- while (!(Single.TryParse(input, out eq[i,1])));
- do
- {
- Console.WriteLine("Enter the coefficient of x^" + eq[i, 1]);
- input = Console.ReadLine();
- }
- while (!(Single.TryParse(input, out eq[i, 0])));
- }
- printEq(eq);
- differentiate(eq);
- printEq(eq);
- Console.ReadLine();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement