Write a method that adds two polynomials. Represent them as arrays of their coefficients as in the example below: x2 + 5 = 1×2 + 0x + 5 -> 501

SHARE:

Write a method that adds two polynomials. Represent them as arrays of their coefficients as in the example below: x2 + 5 = 1×2 + 0x + 5 = 501



using System;

    class PolynomialsAsArraysOfCoefficients
    {
        static int[] AddPolynomals(int[] firstPolynomial, int[] secondPolynomial)
        {
            int[] result;

            if (firstPolynomial.Length < secondPolynomial.Length)
            {
                result = new int[secondPolynomial.Length];
                for (int i = 0; i < secondPolynomial.Length; i++)
                {
                    if (i >= firstPolynomial.Length)
                    {
                        result[i] = secondPolynomial[i];
                        continue;
                    }
                    result[i] = firstPolynomial[i] + secondPolynomial[i];
                }
            }

            else
            {
                result = new int[firstPolynomial.Length];
                for (int i = 0; i < firstPolynomial.Length; i++)
                {
                    if (i >= secondPolynomial.Length)
                    {
                        result[i] = firstPolynomial[i];
                        continue;
                    }
                    result[i] = firstPolynomial[i] + secondPolynomial[i];
                }
            }
            return result;
        }

        static void Main(string[] args)
        {
            Console.Write("Enter length of first polinomial: ");
            int firstPolynomialsLength = int.Parse(Console.ReadLine());

            int[] firstPolynomial = new int[firstPolynomialsLength];
           
            for (int i = 0; i < firstPolynomialsLength; i++)
            {
                Console.Write("Enter number: ");
                firstPolynomial[i] = int.Parse(Console.ReadLine()); 
            }

            //Secon polynomial
            Console.Write("Enter length of second polynomial: ");
            int secondPolynomialsLength = int.Parse(Console.ReadLine());

            int[] secondPolynomial = new int[secondPolynomialsLength];

            for (int i = 0; i < secondPolynomialsLength; i++)
            {
                Console.Write("Enter number: ");
                secondPolynomial[i] = int.Parse(Console.ReadLine());
            }

            //Result of the two polynomials
            int[] result = AddPolynomals(firstPolynomial, secondPolynomial);

            Console.Write("Result: ");
            for (int i = result.Length - 1; i >= 0; i--)
            {
                if (i == 0)
                {
                    Console.WriteLine(result[i]);
                    break;
                }
                if (result[i] != 0) Console.Write(result[i] + "x" + i + " + ");
            }
        }
    }

COMMENTS

Name

ADOBE ILLUSTRATOR,5,Advantages of it,14,Android,3,Android App,1,Annoucements,5,Apple,27,Apple TV 2,1,Apps,3,AutoDraw,1,Basic It,23,best rct game,4,best rct version,2,best roller coaster tycoon game,2,Black Desert Tamer,1,Blogger,1,c# tutorials,25,Cloud Computing,1,Color Schemes,1,console,1,Crowdfunding,2,Devices,1,Dock,2,elcapitan,1,Enterprise,1,Excel,1,FaceTime,1,Far Cry Primal,1,Farmville 2,5,Featured,2,Games,13,games for ios,1,Glossary,2,Google,1,google docs,1,google sheets,1,Html,3,iDevice,5,ios,1,iOS Apps,5,iOS Tweaks,1,iPhone 5,3,iPhone 5S,2,It Tricks,10,jailbreak rct,1,java,1,javascript,1,Just Cause 2,1,leeter hero,1,Mac OS X,8,macos,1,macOS,3,malwares,1,marriage in stardew valley,1,marriage stardew valley,1,Mavericks,1,Microsoft,1,Mount and Warblade,1,New Technology,6,optical character recognition,1,osx,1,php,1,Pillars of Eternity,1,PS4,1,RCT,5,RCT 2,1,RCT 3,3,rct 3 cheat,1,rct 4 hack,1,rct3,1,rct3 cheats,1,rct4,3,rct4 hack,1,RCT4 Mobile,4,rct4 redeem codes 2016,1,rct4 redeem codes 2017,1,roller coaster 3 cheats,1,rollercoaster tycoon 3,1,rollercoaster tycoon 3 money,1,rollercoaster tycoon 3 money cheat,1,Security,1,sierra,1,Sony,1,Sql,1,Stardew Valley,3,Stardew Valley Marriage,1,Store,1,Surface,1,Tamil,2,Tech,1,Ui Design,1,unflod malware,1,unlimited coins rct 4,1,Updates,1,Webdesign,5,Word,9,Wordpress,3,yosemite,1,Youtube,1,
ltr
item
Lankaitschool | Lanka itschool: Write a method that adds two polynomials. Represent them as arrays of their coefficients as in the example below: x2 + 5 = 1×2 + 0x + 5 -> 501
Write a method that adds two polynomials. Represent them as arrays of their coefficients as in the example below: x2 + 5 = 1×2 + 0x + 5 -> 501
https://1.bp.blogspot.com/-xnse2ZVaV_8/Wv2oD5C-XKI/AAAAAAAAE8A/UDC4iLCIgHI40tfVWCFV1xTbwIZlD7CbgCK4BGAYYCw/s640/maxresdefault.jpg
https://1.bp.blogspot.com/-xnse2ZVaV_8/Wv2oD5C-XKI/AAAAAAAAE8A/UDC4iLCIgHI40tfVWCFV1xTbwIZlD7CbgCK4BGAYYCw/s72-c/maxresdefault.jpg
Lankaitschool | Lanka itschool
http://www.lankaitschool.com/2018/05/write-method-that-adds-two-polynomials.html
http://www.lankaitschool.com/
http://www.lankaitschool.com/
http://www.lankaitschool.com/2018/05/write-method-that-adds-two-polynomials.html
true
7411958204546789358
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS CONTENT IS PREMIUM Please share to unlock Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy