PLEASE SERIUS PROGRAMER ONLY. Here is my code. i have to make som slight change
ID: 3786603 • Letter: P
Question
PLEASE SERIUS PROGRAMER ONLY. Here is my code. i have to make som slight change to it. If you scroll down the 2nd party is the assignment of the problem
and down below where i numbere 3 is the change i need to make in that program. THANKS
def addVectors(v1, v2):
"""
Function that add vectors
"""
res = []
# Iterating over vectors
for i in range(0, len(v1)):
# Adding elements and adding to vector
res.append( v1[i] + v2[i] );
return res;
def scalarMult(s, v):
"""
Function that performs scalar multiplication
"""
res = [];
# Iterating over vectors
for i in range(0, len(v)):
# Performing scalar multiplication
res.append(v[i] * s[0]);
return res;
def dotProduct(v1, v2):
"""
Function that performs dot product
"""
res = 0
# Iterating over vector
for i in range(0, len(v1)):
# Performing dot product
res += (v1[i] * v2[i])
return res;
def cross_product(v1, v2):
"""
Function that performs cross product
"""
res = [0,0,0];
# Calculating cross product
res[0] = ( (v1[1] * v2[2]) - (v1[2] * v2[1]) )
res[1] = ( (v1[2] * v2[0]) - (v1[0] * v2[2]) )
res[2] = ( (v1[0] * v2[1]) - (v1[1] * v2[0]) )
return res;
def menu():
"""
Function that displays menu
"""
# Displaying menu
print(" 1 - Add Vectors 2 - Scalar Multiplication 3 - Dot Product 4 - Cross Product 5 - Exit ");
# Accepting user choice
ch = int(input(" Your Choice: "))
return ch;
def fun_input():
vectors=[]
inpNumber= input("How many numbers do you wish to enter:")
for i in range(int(inpNumber)):
inp= input("enter one value and press enter:")
vectors.append(int(inp))
return vectors
def main():
"""
Main function
"""
#p= fun_input()
#print(p)
while(True):
# Displaying menu
option = menu();
# Calling appropriate function based on user choice
if option == 1:
v1 = fun_input()
v2 = fun_input()
result = addVectors(v1,v2);
print(" v1 = " + str(v1) + " v2 = " + str(v2) + " Result = " + str(result));
elif option == 2:
s = fun_input()
v = fun_input()
result = scalarMult(s, v);
print(" s = " + str(s) + " v = " + str(v) + " Result = " + str(result));
elif option == 3:
v1 =fun_input() ;
v2 = fun_input();
result = dotProduct(v1, v2);
print(" v1 = " + str(v1) + " v2 = " + str(v2) + " Result = " + str(result));
elif option == 4:
v1 = fun_input()
v2 = fun_input()
result = cross_product(v1, v2);
print(" v1 = " + str(v1) + " v2 = " + str(v2) + " Result = " + str(result));
else:
return
# Calling main function
main();
====================================================================
2)Write a function addVectors(v1, v2) that takes two lists of numbers of the same length, and returns a new list containing the sums of the corresponding elements of each.
Example: newVector = addVectors( [1,2], [3,4]); print(newVector) # output is [4,6]
Example: newVector = addVectors([1,2,3], [4,4,4]) print(newVector) # output is [5,6,7]
Write a function scalarMult(s, v) that takes a number, s, and a list, v and returns the scalar multiple of v by s.
Example: newVector = scalarMult(10, [2,3]) ; print(newVector) #output is [20,30]
Example: newVector = scalarMult(10, [-2,3]) ; print(newVector) #output is [-20,30]
Write a function dotProduct(v1, v2) that takes two lists of numbers of the same length, and returns the sum of the products of the corresponding elements of each (the dot product)
Example: ans = dotProduct([10,3,-6], [4,-2,-5]
print(ans) #output is 64
Explanation of dot product: (10*4) + (3 * -2) + (-6 * -5) = 40 + -6 + 30 = 64
BONUS: 2 pts Write a function cross_product(v1, v2) that takes two lists of numbers of length 3 and returns their cross product. Calculate it this way
(source: http://www.mathsisfun.com/algebra/vectors-cross-product.html)
When a and b start at the origin point (0,0,0), the Cross Product will end at:
cx = aybz - azby
cy = azbx - axbz
cz = axby - aybx
Create a main program to test these functions. It should be menu-driven.
Keep printing the menu and allowing the user to enter their choice until the user chooses the option to quit:
1. Add 2. Scalar Multiplication 3. Dot Product 4. Quit
NOTE -- if you do the BONUS, you’ll have five options in your menu:
1. Add 2. Scalar Multiplication 3. Dot Product 4. Cross Product 5. Quit
Use another function to gather input from the user. It should allow the user to enter the values for the vector. It should have an argument that indicates the number of values that will be entered into the vector and should return a list of those values.
Next, call the appropriate function (addVectors, scalarMult, dotProduct, or crossProduct with 2 parameters (the 2 lists).
The main program should print the results in a format similar to the examples below:
If the user chooses option 1 (add vectors), and the user entered [1,2,3,4,5] and [6,7,8,9,10] :
Output should look like this: [1,2,3,4,5] + [6,7,8,9,10] = [7,9,11,13,15]
If the user chooses option 2 (scalar multiplication) and the user entered 10 and [1,2,3]:
Output should look like this: 10 * [1,2,3] = [10,20,30]
Etc.
Scoring Guide:
No Doc, no credit. Top of program must have description, author, date. Each function needs to list a description and pre-conditions and post-conditions
4 pts addVector function
2pt scalarMult function
4 pts dotProduct function
4 pt function to gather input from user
2 pts menu continually printed on screen, prints message if invalid option provided,
correctly calls appropriate function
2 pts output from each function call is neatly displayed and formatted appropriately
BONUS: 2pts if crossProduct function is implemented and tested and works properly
3 TO DO
Please make the following revisions in your program and then demonstrate it for me:
1. Add Vectors: allows vectors of different lengths -- this should not happen. The program crashes if the user enters 3 numbers for the first vector and only 2 for the second vector.
2. Scalar Multiplication: It should only ask for ONE vector. The other value that is needed is an integer (multiplier).
3. Dot Product: program allows user to enter 3 values for one vector and 2 for another, and this causes your program to crash.
4. Cross product -- no bonus credit. This function can ONLY handle vectors of length 3. The user should NOT be asked how many values will be in the list here!
5. Please alter your fun_input( ) function so that it takes a parameter (numEntries). That way, if the user selects the Add Vectors choice, in the main program, you'll ask how many in the list only ONCE, and then you can call the fun_input(numEntries) TWICE, once for each vector, and you will ensure that the vectors will have the same length.
2)Write a function addVectors(v1, v2) that takes two lists of numbers of the same length, and returns a new list containing the sums of the corresponding elements of each.
Example: newVector = addVectors( [1,2], [3,4]); print(newVector) # output is [4,6]
Example: newVector = addVectors([1,2,3], [4,4,4]) print(newVector) # output is [5,6,7]
Write a function scalarMult(s, v) that takes a number, s, and a list, v and returns the scalar multiple of v by s.
Example: newVector = scalarMult(10, [2,3]) ; print(newVector) #output is [20,30]
Example: newVector = scalarMult(10, [-2,3]) ; print(newVector) #output is [-20,30]
Write a function dotProduct(v1, v2) that takes two lists of numbers of the same length, and returns the sum of the products of the corresponding elements of each (the dot product)
Example: ans = dotProduct([10,3,-6], [4,-2,-5]
print(ans) #output is 64
Explanation of dot product: (10*4) + (3 * -2) + (-6 * -5) = 40 + -6 + 30 = 64
BONUS: 2 pts Write a function cross_product(v1, v2) that takes two lists of numbers of length 3 and returns their cross product. Calculate it this way
(source: http://www.mathsisfun.com/algebra/vectors-cross-product.html)
When a and b start at the origin point (0,0,0), the Cross Product will end at:
cx = aybz - azby
cy = azbx - axbz
cz = axby - aybx
Explanation / Answer
def addVectors(v1, v2):
"""
Function that add vectors
"""
res = []
# Iterating over vectors
for i in range(0, len(v1)):
# Adding elements and adding to vector
res.append( v1[i] + v2[i] );
return res;
def scalarMult(s, v):
"""
Function that performs scalar multiplication
"""
res = [];
# Iterating over vectors
for i in range(0, len(v)):
# Performing scalar multiplication
res.append(v[i] * s);
return res;
def dotProduct(v1, v2):
"""
Function that performs dot product
"""
res = 0
# Iterating over vector
for i in range(0, len(v1)):
# Performing dot product
res += (v1[i] * v2[i])
return res;
def cross_product(v1, v2):
"""
Function that performs cross product
"""
res = [0,0,0];
# Calculating cross product
res[0] = ( (v1[1] * v2[2]) - (v1[2] * v2[1]) )
res[1] = ( (v1[2] * v2[0]) - (v1[0] * v2[2]) )
res[2] = ( (v1[0] * v2[1]) - (v1[1] * v2[0]) )
return res;
def menu():
"""
Function that displays menu
"""
# Displaying menu
print(" 1 - Add Vectors 2 - Scalar Multiplication 3 - Dot Product 4 - Cross Product 5 - Exit ");
# Accepting user choice
ch = raw_input(" Your Choice: ")
try:
ch = int(ch)
except:
pass
return ch;
def fun_input(s):
"""
Function that take input vectors
"""
vectors=[]
print("Vector: ")
for i in range(int(s)):
inp= input("enter one value and press enter:")
vectors.append(int(inp))
return vectors
def main():
"""
Main function
"""
while(True):
# Displaying menu
option = menu();
# Calling appropriate function based on user choice
if option == 1:
size = input("Enter number of elements in vector: ")
print("")
v1 = fun_input(size)
print("")
v2 = fun_input(size)
result = addVectors(v1,v2);
print(" " + str(v1) + " + " + str(v2) + " = " + str(result));
elif option == 2:
s = input("Enter scalar for scalar product:" )
print("")
size = input("Enter number of elements in vector: ")
print("")
v = fun_input(size)
result = scalarMult(s, v);
print(" " + str(s) + " * " + str(v) + " = " + str(result));
elif option == 3:
size = input("Enter number of elements in vector: ")
print("")
v1 = fun_input(size)
print("")
v2 = fun_input(size)
result = dotProduct(v1, v2);
print(" " + str(v1) + " . " + str(v2) + " = " + str(result));
elif option == 4:
v1 = fun_input(3)
print("")
v2 = fun_input(3)
print("")
result = cross_product(v1, v2);
print(" " + str(v1) + "x" + str(v2) + " = " + str(result));
elif option == 5:
return
else:
print("Please choose from given menu ");
# Calling main function
main();
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.