Advertisement
Guest User

Untitled

a guest
Jun 26th, 2016
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
4CS 3.40 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace ConsoleApplication4
  8. {
  9.     class Program
  10.     {
  11.         public static void differentiate(float[,] input)
  12.         {
  13.             if (input.GetLength(1) == 2)
  14.             {
  15.                 for(int i = 0; i < input.GetLength(0); i++)
  16.                 {
  17.                     if(input[i,1] != 0)
  18.                     {
  19.                         input[i, 0] = input[i, 0] * input[i, 1];
  20.                         input[i, 1] = input[i, 1] - 1;
  21.                     }
  22.                     else
  23.                     {
  24.                         input[i, 0] = 0;
  25.                         input[i, 1] = 0;
  26.                     }
  27.                 }
  28.             }
  29.             else
  30.             {
  31.                 Console.WriteLine("Length 0 of input array != 2");
  32.             }
  33.            
  34.         }
  35.  
  36.         public static void printEq(float[,] eq)
  37.         {
  38.             for (int i = 0; i < eq.GetLength(0); i++)
  39.             {
  40.                 if (eq[i, 1] == 0) //if power is 0
  41.                 {
  42.                     if (eq[i, 0] != 0)
  43.                     {
  44.                         Console.Write(eq[i, 0]);
  45.                     }
  46.                 }
  47.                 else if (eq[i, 1] == 1) //if power is 1
  48.                 {
  49.                     if (eq[i, 0] == 1) //if coefficient is 1
  50.                     {
  51.                         Console.Write("x");
  52.                     }
  53.                     else
  54.                     {
  55.                         Console.Write(eq[i, 0] + "x");
  56.                     }
  57.                 }
  58.                 else //if it is another power
  59.                 {
  60.                     if (eq[i, 0] == 1) //if coefficient is 1
  61.                     {
  62.                         Console.Write("x^" + eq[i, 1]);
  63.                     }
  64.                     else
  65.                     {
  66.                         Console.Write(eq[i, 0] + "x^" + eq[i, 1]);
  67.                     }
  68.                 }
  69.  
  70.                 if (i != (eq.GetLength(0) - 1)) //every term but the last
  71.                 {
  72.                     if(eq[i+1,0] != 0)
  73.                     {
  74.                         Console.Write(" + ");
  75.                     }
  76.                 }
  77.             }
  78.             Console.WriteLine();
  79.         }
  80.  
  81.         static void Main(string[] args)
  82.         {
  83.             String input;
  84.             int num;
  85.  
  86.             do
  87.             {
  88.                 Console.WriteLine("How many different powers of x does the equation have?");
  89.                 input = Console.ReadLine();
  90.             }
  91.             while (!(Int32.TryParse(input, out num)));
  92.  
  93.             float[,] eq = new float[num, 2];
  94.  
  95.             for (int i = 0; i<eq.GetLength(0); i++)
  96.             {
  97.                 do
  98.                 {
  99.                     Console.WriteLine("Enter the power of x");
  100.                     input = Console.ReadLine();
  101.                 }
  102.                 while (!(Single.TryParse(input, out eq[i,1])));
  103.  
  104.                 do
  105.                 {
  106.                     Console.WriteLine("Enter the coefficient of x^" + eq[i, 1]);
  107.                     input = Console.ReadLine();
  108.                 }
  109.                 while (!(Single.TryParse(input, out eq[i, 0])));
  110.             }
  111.  
  112.             printEq(eq);
  113.             differentiate(eq);
  114.             printEq(eq);
  115.            
  116.             Console.ReadLine();
  117.         }
  118.     }
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement