TutorMe homepage

SIGN IN

Start Free Trial

Jhonnatan R.

Tutor for 10 years in Computer Science and Databases

Tutor Satisfaction Guarantee

Information Technology

TutorMe

Question:

What exactly is an SQL injection attack? What should a programmer do to prevent such an attack?

Jhonnatan R.

Answer:

An SQL injection attack consists of the insertion or "injection" of an SQL query by means of the input data from the client to the application. A successful SQL injection attack can read sensitive information from the database, modify the information (Insert / Update / Delete), execute management operations on the database. The main problem with these attacks is that if we let the user of the program freely enter characters without any control (using forms, for example) can get to take advantage of the quotes (simple and double with which we declare strings of text or strings). In order to prevent and avoid these attacks, the programmer must avoid that special characters (such as quotes) could be introduced without first transforming them (for example, a double quotation mark: "it should be transformed into \" Character that closes or opens a text in the query, but according to the language can be implemented in different ways and some are automatic and more optimized to avoid the SQL injection.

Computer Science (General)

TutorMe

Question:

You are to consider bubble and selection sort. Both are O(n^2) however it may be possible to classify one algorithm as being more efficient than the other. You are to discuss which algorithm you feel is the most efficient and in what cases it will be more efficient. Provide any relevant test cases and code to support your belief.

Jhonnatan R.

Answer:

I feel that the selection algorithm is more efficient because the bubble sorting algorithm does more work to bring each element to its position. This means that if we have a vector of 10000 elements then in a basic bubble algorithm 10000 comparisons are made to locate each element in its position. The selection algorithm is also more efficient because of the number of exchanges it performs. Ordering by selection will be more efficient in cases where we have few large records, so by analyzing the number of exchanges that the selection method makes, we see that it is of order O (n) versus the order O (n^2) of exchanges of the bubble method. That is why in general we can say that the selection method does fewer iterations, exchanges, and comparisons than the bubble method as we can see in the following case proves: Procedure Bubble (var Vector: tVector; N: tRange); Var I, J: tRange; Aux: tBase; {Auxiliary Element of the same type as the vector} Begin For I: = 1 to N - 1 Do {N-1 Cycle Iterations} For J: = 1 to N-I Do {Bubble of the Highest Value} If Vector [J]> Vector [J + 1] Then Begin {Exchange} Aux: = Vector [J]; Vector [J]: = Vector [J + 1]; Vector [J + 1]: = Aux; End; End; Demonstration: Given the following vector {40,21,4,9,10,35} we will sort it from lowest to highest. First pass: {21,40,04,09,10,35} <- The 21 is changed by 40. {21,04,40,09,10,35} <- 40 is changed by 04. {21,04,09,40,10,35} <- The 09 is changed by 40. {21,04,09,10,40,35} <- 40 is changed by 10. {21,04,09,10,35,40} <- The 35 is changed by 40. Second pass: {04,21,09,10,35,40} <- The 21 is changed by 04. {04,09,21,10,35,40} <- The 09 is changed by the 21. {04,09,10,21,35,40} <- The 21 is changed by 10. Total iterations: 8 They are already ordered, but to verify it would have to finish this second test and make a third. Now we test the selection sort.. Procedure Selecction (var Vector: tVector; N: tRange); Var I, J: tRange; Aux: tBase; {Auxiliary Element of the same type as the vector} Begin For I: = 1 to N - 1 Do {N-1 Cycle Iterations} For J: = I + 1 to N Do If Vector [I]> Vector [J] Then Begin {Exchange} Aux: = Vector [I]; Vector [I]: = Vector [J]; Vector [J]: = Aux; End; End; Demonstration: Given the following vector {40,21,4,9,10,35}, we will sort it from lowest to highest. {04,21,40,09,10,35} <- The 04 is placed, the smallest, in the first position: the 04 is changed by the 40. {04,09,40,21,10,35} <- The 09 is placed, in second position: the 09 is changed by the 21. {04,09,10,21,40,35} <- The 10 is placed, in third position: the 10 is changed by the 40. {04,09,10,21,40,35} <- The 21 is placed, in third position: already placed. {04,09,10,21,35,40} <- The 35 is placed in third position: the 35 is changed by 40. Total iterations: 5 They are already ordered, but to verify it would have to finish this second test and make a third.

C Sharp Programming

TutorMe

Question:

How to create a program in C # to add, subtract and multiply polynomials?

Jhonnatan R.

Answer:

//***THIS CLASS CONTAINS THE DEFINITION OF THE DATA TYPE POLYNOMIAL WITH THE THREE OPERATIONS AND PRINT METHOD***// using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Polynomial { //***This is the definition of the data type polynomial through a class***// class Polynomial { public int degree; //class variable public double[] coefficients; //class variable //Constructor 1 degree public Polynomial(int _degree) { if (_degree >= 0) { degree = _degree; coefficients = new double[_degree + 1]; } } //Constructor 2 coefficients public Polynomial(double[] _coefficients) { if ((_coefficients.Length-1) >= 0) { degree = _coefficients.Length-1; coefficients = new double[_coefficients.Length]; coefficients = _coefficients; } } //Constructor 3 degree and coefficients public Polynomial(double[] _coefficients, int _degree) { if (_degree >= 0) { degree = _degree; coefficients = new double[_degree + 1]; coefficients = _coefficients; } } //This is the method that overloads the + operator to SUM two polynomials, return a Polynomial variable public static Polynomial operator +(Polynomial p1, Polynomial p2) { Polynomial sum; Polynomial t = new Polynomial(p1.degree); Polynomial q = new Polynomial(p2.degree); if (p1.degree >= p2.degree) { for (int i = 0; i <= p1.degree; i++) { if (p2.degree == 0) { double[] arr = new double[p1.coefficients.Length]; for (i = 0; i < p1.coefficients.Length; i++) { arr[i] = p1.coefficients[i]; } arr[0] += p2.coefficients[0]; t.coefficients = arr; } else if (i <= p2.coefficients.Length) { t.coefficients[i] = (p1.coefficients[i] + p2.coefficients[i]); } } sum = new Polynomial(t.coefficients); } else { for (int i = 0; i <= p2.degree; i++) { //////////////// if (p1.degree == 0) { double[] arr = new double[p2.coefficients.Length]; for (i = 0; i < p2.coefficients.Length; i++) { arr[i] = p2.coefficients[i]; } arr[0] += p1.coefficients[0]; q.coefficients = arr; } else //////////////// if (i <= p1.coefficients.Length) { q.coefficients[i] = (p2.coefficients[i] + p1.coefficients[i]); } } sum = new Polynomial(q.coefficients); } return sum; } //This is the method that overloads the - operator to calculate the difference of two polynomials, return a Polynomial variable public static Polynomial operator -(Polynomial pol1, Polynomial pol2) { Polynomial temp = new Polynomial(pol2.degree); for (int i = 0; i <= pol2.degree; i++) { temp.coefficients[i] = pol2.coefficients[i] * -1; } return pol1 + temp; } //This is the method that overloads the * operator to calculate the product of two polynomials, return a Polynomial variable public static Polynomial operator *(Polynomial pol1, Polynomial pol2) { double[] a = new double[(pol1.degree + pol2.degree) + 1]; for (int i = 0; i <= pol1.degree; i++) { for (int j = 0; j <= pol2.degree; j++) { a[i + j] += pol1.coefficients[i] * pol2.coefficients[j]; } } Polynomial product = new Polynomial(a); return product; } //This method allows to Print in a string the polynomial ordered in mathematical notations, return String with the Polynomial public override string ToString() { string Polynomial = ""; int temp_g=this.degree; for (int i = 0; i < this.coefficients.Length; i++) { if (temp_g > 1) { if(coefficients[i]<0) Polynomial = Polynomial + "("+coefficients[i].ToString()+")"+ "x" + "^" + temp_g+" + "; else Polynomial = Polynomial + coefficients[i].ToString() + "x" + "^" + temp_g + " + "; } else if (temp_g == 1) { if (coefficients[i] < 0) Polynomial = Polynomial + "("+ coefficients[i].ToString()+")"+ "x" + " + "; else Polynomial = Polynomial + coefficients[i].ToString()+ "x" + " + "; } else { if (coefficients[i] < 0) Polynomial = Polynomial + "("+coefficients[i].ToString()+")"; else Polynomial = Polynomial + coefficients[i].ToString(); } temp_g--; } return Polynomial; } } } MAIN PROGRAM SOURCE CODE C# using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Polynomial { class Program { static void Main(string[] args) { //variables int degree; int t_d; double[] coefficients_a; double[] coefficients_b; Polynomial result; Console.WriteLine("Welcome!! this Program takes two polynomials and prints (+, -,*)\n\n"); //***WE READ THE GREATER DEGREE OF THE TWO POLYNOMIALS, THE DEGREE OF THE POLYNOMIAL MUST BE >= 0***// do { Console.Write("Please insert the highest degree of the two polynomials: "); degree = int.Parse(Console.ReadLine()); if (degree < 0) Console.Write("Error: Degree of being greater than or equal to 0...\n"); } while (degree < 0); //WE CREATE THE ARRAY FOR THE COEFFICIENTS OF THE TWO POLYNOMIALS TO BE INPUT coefficients_a=new double[degree+1]; coefficients_b=new double[degree+1]; //WE READ THE COEFFICIENTS AND INDEPENDENT TERM FOR THE FIRST POLYNOMIAL Console.WriteLine("\n\n1. Enter coefficients (from left to right) of the FIRST POLYNOMIAL\n"); t_d = degree; for (int i = 0;i <degree; i++) { Console.Write("Please insert the coefficient of degree {0} (x^{0}): ", t_d); coefficients_a[i] = double.Parse(Console.ReadLine()); t_d--; } Console.Write("Please insert the independent term: "); coefficients_a[degree] = double.Parse(Console.ReadLine()); //WE READ THE COEFFICIENTS AND INDEPENDENT TERM FOR THE SECOND POLYNOMIAL Console.WriteLine("\n\n2. Enter coefficients (from left to right) of the SECOND POLYNOMIAL\n"); t_d = degree; for (int i = 0; i < degree; i++) { Console.Write("Please insert the coefficient of degree {0}: ", t_d); coefficients_b[i] = double.Parse(Console.ReadLine()); t_d--; } Console.Write("Please insert the independent term: "); coefficients_b[degree] = double.Parse(Console.ReadLine()); //WE CREATE THE TWO POLYNOMIAL TYPE VARIABLES WITH THEIR COEFFICIENTS Polynomial a = new Polynomial(coefficients_a,degree); Polynomial b = new Polynomial(coefficients_b,degree); //WE PRINT THE TWO POLYNOMIAL Console.WriteLine("\n\nYour two polynomials are:\n"); Console.WriteLine("P(x) = "+a.ToString() + "\n"); Console.WriteLine("Q(x) = "+b.ToString() + "\n"); //WE CALCULATE IN THE VARIABLE RESULT THE SUM OF TWO POLYNOMIAL AND PRINT RESULT result = a + b; Console.WriteLine("\n\nTHE SUM OF THE TWO POLYNOMES IS:"); Console.WriteLine("\nP(x) + Q(x) = " + result.ToString() + "\n"); //WE CALCULATE THE VARIABLE RESULT THE DIFFERENCE BETWEEN TWO POLYNOMIAL AND PRINT RESULT result = a - b; Console.WriteLine("\n\nTHE DIFFERENCE OF THE TWO POLYNOMES IS:"); Console.WriteLine("\nP(x) - Q(x) = " + result.ToString() + "\n"); //WE CALCULATE THE VARIABLE RESULT THE PRODUCT OF TWO POLYNOMIAL AND PRINT RESULT result = a * b; Console.WriteLine("\n\nTHE PRODUCT OF THE TWO POLYNOMES IS:"); Console.WriteLine("\nP(x) * Q(x) = " + result.ToString() + "\n"); Console.ReadLine(); } } }

Send a message explaining your

needs and Jhonnatan will reply soon.

needs and Jhonnatan will reply soon.

ContactÂ Jhonnatan

Ready now? Request a lesson.

Start Session

FAQs

What is a lesson?

A lesson is virtual lesson space on our platform where you and a tutor can communicate.
You'll have the option to communicate using video/audio as well as text chat.
You can also upload documents, edit papers in real time and use our cutting-edge virtual whiteboard.

How do I begin a lesson?

If the tutor is currently online, you can click the "Start Session" button above.
If they are offline, you can always send them a message to schedule a lesson.

Who are TutorMe tutors?

Many of our tutors are current college students or recent graduates of top-tier universities
like MIT, Harvard and USC.
TutorMe has thousands of top-quality tutors available to work with you.

Made in California

Â© 2019 TutorMe.com, Inc.