General > General Technical Chat
Video: AI that generates code
<< < (13/15) > >>
RoGeorge:

--- Quote from: eti on June 16, 2022, 05:35:02 am ---You'd have more success trying to lick your elbow

--- End quote ---





 :)
tpowell1830:
Here is the GPT-3 free online tool writing C# code as follows. The first part is my description of program:

design a program for C# that will have a form, full screen, with title "Resistor Color Code Decryption". Add 4 text boxes with labels for "Color Code" that is left justified plus 50, centered from top. Write the code needed to examine the contents of the text boxes and define each according to resistor color codes when a push button, centered on form, is pressed. Add necessary prompts for errors and remarks for each code grouping.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ResistorColorCodeDecryption
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string colorCode1, colorCode2, colorCode3, colorCode4;
            int value1, value2, value3;
            double resistorValue;

            colorCode1 = textBox1.Text;
            colorCode2 = textBox2.Text;
            colorCode3 = textBox3.Text;
            colorCode4 = textBox4.Text;

            if (colorCode1 == "black")
            {
                value1 = 0;
            }
            else if (colorCode1 == "brown")
            {
                value1 = 1;
            }
            else if (colorCode1 == "red")
            {
                value1 = 2;
            }
            else if (colorCode1 == "orange")
            {
                value1 = 3;
            }
            else if (colorCode1 == "yellow")
            {
                value1 = 4;
            }
            else if (colorCode1 == "green")
            {
                value1 = 5;
            }
            else if (colorCode1 == "blue")
            {
                value1 = 6;
            }
            else if (colorCode1 == "violet")
            {
                value1 = 7;
            }
            else if (colorCode1 == "gray")
            {
                value1 = 8;
            }
            else if (colorCode1 == "white")
            {
                value1 = 9;
            }
            else
            {
                MessageBox.Show("Invalid color code entered for first band");
                return;
            }

            if (colorCode2 == "black")
            {
                value2 = 0;
            }
            else if (colorCode2 == "brown")
            {
                value2 = 1;
            }
            else if (colorCode2 == "red")
            {
                value2 = 2;
            }
            else if (colorCode2 == "orange")
            {
                value2 = 3;
            }
            else if (colorCode2 == "yellow")
            {
                value2 = 4;
            }
            else if (colorCode2 == "green")
            {
                value2 = 5;
            }
            else if (colorCode2 == "blue")
            {
                value2 = 6;
            }
            else if (colorCode2 == "violet")
            {
                value2 = 7;
            }
            else if (colorCode2 == "gray")
            {
                value2 = 8;
            }
            else if (colorCode2 == "white")
            {
                value2 = 9;
            }
            else
            {
                MessageBox.Show("Invalid color code entered for second band");
                return;
            }

            if (colorCode3 == "black")
            {
                value3 = 0;
            }
            else if (colorCode3 == "brown")
            {
                value3 = 1;
            }
            else if (colorCode3 == "red")
            {
                value3 = 2;
            }
            else if (colorCode3 == "orange")
            {
                value3 = 3;
            }
            else if (colorCode3 == "yellow")
            {
                value3 = 4;
            }
            else if (colorCode3 == "green")
            {
                value3 = 5;
            }
            else if (colorCode3 == "blue")
            {
                value3 = 6;
            }
            else if (colorCode3 == "violet")
            {
                value3 = 7;
            }
            else if (colorCode3 == "gray")
            {
                value3 = 8;
            }
            else if (colorCode3 == "white")
            {
                value3 = 9;
            }
            else
            {
                MessageBox.Show("Invalid color code entered for third band");
                return;
            }

            resistorValue = (value1 * 10 + value2) * Math.Pow(10, value3);

            if (colorCode4 == "black")
            {
                MessageBox.Show("Resistor value is " + resistorValue + " ohms +/- 20%");
            }
            else if (colorCode4 == "brown")
            {
                MessageBox.Show("Resistor value is " + resistorValue + " ohms +/- 1%");
            }
            else if (colorCode4 == "red")
            {
                MessageBox.Show("Resistor value is " + resistorValue + " ohms +/- 2%");
            }
            else if (colorCode4 == "orange")
            {
                MessageBox.Show("Resistor value is " + resistorValue + " ohms +/- 3%");
            }
            else if (colorCode4 == "yellow")
            {
                MessageBox.Show("Resistor value is " + resistorValue + " ohms +/- 4%");
            }
            else if (colorCode4 == "green")
            {
                MessageBox.Show("Resistor value is " + resistorValue + " ohms +/- 0.5%");
            }
            else if (colorCode4 == "blue")
            {
                MessageBox.Show("Resistor value is " + resistorValue + " ohms +/- 0.25%");
            }
            else if (colorCode4 == "violet")
            {
                MessageBox.Show("Resistor value is " + resistorValue + " ohms +/- 0.1%");
            }
            else if (colorCode4 == "gray")
            {
                MessageBox.Show("Resistor value is " + resistorValue + " ohms +/- 0.05%");
            }
            else if (colorCode4 == "white")
            {
                MessageBox.Show("Resistor value is " + resistorValue + " ohms +/- 5%");
            }
            else
            {
                MessageBox.Show("Invalid color code entered for fourth band");
                return;
            }
        }
    }
}

It does not create the form in code, and suppose, that is realistic, since this will be done by user.
AndyBeez:

--- Quote from: tpowell1830 on June 16, 2022, 06:12:16 pm ---Here is the GPT-3 free online tool writing C# code as follows. The first part is my description of program...
《 etc 》
It does not create the form in code, and suppose, that is realistic, since this will be done by user.

--- End quote ---
And here is another problem; code normalization.

An experienced C# developer will be thinking classes, iterators, generics, event binding and anything else in the toolkit to make that forms code work in as few maintainable lines as possible. Not to mention the added/grinding complexity of Windows Presentation Foundation. Seeing this in production code might raise a few 'issues' in review.

My opinion is, regardless if language, the AI code generator is just a starting point for developers. Maybe this will become the finishing point go-to app for students programming in 'Arduino'? No one knows how or even why the code works, it just does.
tpowell1830:

--- Quote from: AndyBeez on June 16, 2022, 08:21:57 pm ---
--- Quote from: tpowell1830 on June 16, 2022, 06:12:16 pm ---Here is the GPT-3 free online tool writing C# code as follows. The first part is my description of program...
《 etc 》
It does not create the form in code, and suppose, that is realistic, since this will be done by user.

--- End quote ---
And here is another problem; code normalization.

An experienced C# developer will be thinking classes, iterators, generics, event binding and anything else in the toolkit to make that forms code work in as few maintainable lines as possible. Not to mention the added/grinding complexity of Windows Presentation Foundation. Seeing this in production code might raise a few 'issues' in review.

My opinion is, regardless if language, the AI code generator is just a starting point for developers. Maybe this will become the finishing point go-to app for students programming in 'Arduino'? No one knows how or even why the code works, it just does.

--- End quote ---

I tried again, same request and the code was shortened considerably by utilizing "switch" instead of all of "else if" statements. The third time it even included remarks as I asked, code below:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Resistor_Color_Code_Decryption
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnDecode_Click(object sender, EventArgs e)
        {
            //define variables
            string colorCode1, colorCode2, colorCode3, colorCode4;
            int numValue1, numValue2;
            double multiplier, tolerance;

            //get user input
            colorCode1 = txtColorCode1.Text;
            colorCode2 = txtColorCode2.Text;
            colorCode3 = txtColorCode3.Text;
            colorCode4 = txtColorCode4.Text;

            //calculate and display results
            numValue1 = ColorCode(colorCode1);
            numValue2 = ColorCode(colorCode2);
            multiplier = Multiplier(colorCode3);
            tolerance = Tolerance(colorCode4);
            lblResult.Text = Result(numValue1, numValue2, multiplier, tolerance);
        }

        static int ColorCode(string colorCode)
        {
            int numValue = 0;

            switch (colorCode.ToLower())
            {
                case "black":
                    numValue = 0;
                    break;
                case "brown":
                    numValue = 1;
                    break;
                case "red":
                    numValue = 2;
                    break;
                case "orange":
                    numValue = 3;
                    break;
                case "yellow":
                    numValue = 4;
                    break;
                case "green":
                    numValue = 5;
                    break;
                case "blue":
                    numValue = 6;
                    break;
                case "violet":
                    numValue = 7;
                    break;
                case "gray":
                    numValue = 8;
                    break;
                case "white":
                    numValue = 9;
                    break;
                default:
                    MessageBox.Show("Invalid Color Code");
                    break;
            }
            return numValue;
        }

        static double Multiplier(string colorCode)
        {
            double multiplier = 1;

            switch (colorCode.ToLower())
            {
                case "black":
                    multiplier = 1;
                    break;
                case "brown":
                    multiplier = 10;
                    break;
                case "red":
                    multiplier = 100;
                    break;
                case "orange":
                    multiplier = 1000;
                    break;
                case "yellow":
                    multiplier = 10000;
                    break;
                case "green":
                    multiplier = 100000;
                    break;
                case "blue":
                    multiplier = 1000000;
                    break;
                case "violet":
                    multiplier = 10000000;
                    break;
                case "gray":
                    multiplier = 100000000;
                    break;
                case "white":
                    multiplier = 1000000000;
                    break;
                default:
                    MessageBox.Show("Invalid Color Code");
                    break;
            }
            return multiplier;
        }

        static double Tolerance(string colorCode)
        {
            double tolerance = 20;

            switch (colorCode.ToLower())
            {
                case "brown":
                    tolerance = 1;
                    break;
                case "red":
                    tolerance = 2;
                    break;
                case "green":
                    tolerance = 0.5;
                    break;
                case "blue":
                    tolerance = 0.25;
                    break;
                case "violet":
                    tolerance = 0.1;
                    break;
                case "gray":
                    tolerance = 0.05;
                    break;
                case "gold":
                    tolerance = 5;
                    break;
                case "silver":
                    tolerance = 10;
                    break;
                default:
                    MessageBox.Show("Invalid Color Code");
                    break;
            }
            return tolerance;
        }

        static string Result(int numValue1, int numValue2, double multiplier, double tolerance)
        {
            double result = (numValue1 * 10 + numValue2) * multiplier;
            string strResult = "";

            strResult = result.ToString() + " ohms, +/- " + tolerance.ToString() + "%";
            return strResult;
        }
    }
}

I don't think this machine learning tool will replace programmers at all, but it does give a quick starting point as you said. I don't consider myself a programmer and this work would have taken me 1.5 to 2 hours to complete, and then I may have rewritten again. I have written some user tools and over the last 30 years and some stuff specific to hardware that I have designed, but i never considered myself a coder (programmer). This would have saved me hours in my work so I can be working on other machine design issues.

Doesn't matter to me anymore, since I am retired.
Circlotron:
Yeah, I think these things are better viewed as something to assist you rather than something to replace you. For now at least.
Navigation
Message Index
Next page
Previous page
There was an error while thanking
Thanking...

Go to full version
Powered by SMFPacks Advanced Attachments Uploader Mod