Week 13
Below is a copy and paste of my code in python and java. Interestingly enough it made the background and colors the same as my IDE. Hopefully, this is better than my copy pastes in the past so you can see the code much better, I think. This python code is for my minilab 2. I came into this thinking this would be easy enough and it sorta was until I got to coding random integers to fill an array. Everything else was more or less review from what I did in the first one but I needed to look up how to initialize random class. I did that and came across a bit of a hold up when my code worked but it printed the same number( when I used the randint that I learned in an article). EX 3,3,3,3 (if array size was 4 and seed was 3). From there I tried to look up how to fill an array with random unique numbers and I came across a you tube video that was helpful until I ran the code and it was working because my tempRand was making my boolArray go out of bounds with the index. After many fixes and reruns I found out that the video wasn't using randint, but randrange. Unfortunately, my code still didnt work, so I went back to the way of doing it but with randrange and it worked and thats what you see below. You also see the comments of the algorithm I used from the video. One thing I'm learning is python is so very picky. Things cant be tabbed to much or have too many spaces. Indention are so very important and frustrating because they dont have curly braces so that's how they keep track of what goes with what I believe. Finding out how to do the random things was quite frustrating because I thought I was doing it right and I thought randint was like java and gave you a new randInt for each slot in the array. It was also annoying how the comments were messing with my code. It was making it so some of my code couldn't reach some variables, which was also very frustrating. What the code accomplishes is we get a seed and size of an array. we then use the seed and populate an array with random integers within a range of 0 to MAX. Once that's done we print a menu that you can choose from, you can print the array, find the avg, find the largest element, how many times the COUNT(3) has occurred, how many are half the first element, how many are repeat consecutively, swap the first and last element and if you want to exit. Unfortunately, if you give java and python the same seed they wont print the same numbers. There is a screenshot below of the codes running
import random
def main():
MAX = 8
COUNT = 3
print("\nPlease enter a seed: ")
Seed = int(input())
print("\nPlease enter the size of the array: ")
size = int(input())
while size < 2:
print("\nArray must be greater than 1, Please Reenter: ")
size = int(input())
# boolArray = [False] * size
myArray = [0] * size
random.seed(Seed)
i = 0
for i in range(len(myArray)):
myArray[i] = random.randrange(0, (MAX+1))
#isIndexFree = True
# while isIndexFree:
#tempRand = random.randrange(0, (MAX+1))
#isIndexFree = boolArray[tempRand]
# if isIndexFree:
#continue
#else:
# myArray[i] = tempRand
#boolArray[tempRand] = True
menu = 0
while True:
print("\nPlease choose an option:")
print("\t1\tPrint the array")
print("\t2\tFind the average")
print("\t3\tFind the largest element")
print("\t4\tCount how many times " + str(COUNT) + " occured")
print("\t5\tCount how many elements are less than half of the first element")
print("\t6\tFind how many times numbers repeat consecutively")
print("\t7\tSwap the first and last elements")
print("\t8\tExit")
menu = int(input())
if menu == 1:
print("\nArray ", end="")
i = 0
while i < len(myArray):
print(str(myArray[i]) + " ", end="")
i += 1
print("\n", end="")
if menu == 2:
total = 0
i = 0
while i < len(myArray):
total = total + myArray[i]
i += 1
print("\naverage: ", end="")
print(str(float(total) / len(myArray)) + " " + "\n", end="")
if menu == 3:
large = myArray[0]
i = 0
while i < len(myArray):
if myArray[i] > large:
large = myArray[i]
i += 1
print("\nlargest: ", end="")
print(str(large) + "\n", end="")
if menu == 4:
count = 0
i = 0
while i < len(myArray):
if myArray[i] == COUNT:
count += 1
i += 1
print("\n3 count: ", end="")
print(str(count) + "\n", end="")
if menu == 5:
half = int(myArray[0] / 2)
count2 = 0
i = 0
while i < len(myArray):
if myArray[i] < half:
count2 += 1
i += 1
print("\nLess than half of first: ", end="")
print(str(count2) + "\n", end="")
if menu == 6:
count3 = 0
b = len(myArray)
i = 0
while i < b - 1:
if myArray[i] == myArray[i + 1]:
count3 += 1
i += 1
print("\nRepeat: ", end="")
print(str(count3) + "\n", end="")
if menu == 7:
swap = 0
a = myArray[0]
b = myArray[size - 1]
swap = b
b = a
a = swap
if menu == 8: pass
if menu > 8:
print("\nIllegal option, try again\n", end="")
if not (menu != 8):
break
class Minilab_2:
pass
if __name__ == "__main__":
main()
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner kb = new Scanner(System.in);
final int MAX = 8;
final int COUNT = 3;
System.out.println("\nPlease enter a seed: ");
int seed = kb.nextInt();
System.out.println("\nPlease enter the size of the array: ");
int size = kb.nextInt();
while(size<2) {
System.out.println("\nArray must be greater than 1, Please Reenter: ");
size = kb.nextInt();
}
Random myran = new Random(seed);
int[] myArray = new int[size];
for(int i=0; i<myArray.length;i++) {
myArray[i]=myran.nextInt(MAX+1);
}
int menu;
do
{
System.out.println("\nPlease choose an option:");
System.out.println("\t1\tPrint the array");
System.out.println("\t2\tFind the average");
System.out.println("\t3\tFind the largest element");
System.out.println("\t4\tCount how many times "+COUNT+ " occured");
System.out.println("\t5\tCount how many elements are less than half of the first element");
System.out.println("\t6\tFind how many times numbers repeat consecutively");
System.out.println("\t7\tSwap the first and last elements");
System.out.println("\t8\tExit");
menu = kb.nextInt();
if(menu==1) {
System.out.print("\nArray ");
for (int i=0; i<myArray.length;i++) {
System.out.print(myArray[i]+" ");
}
System.out.print("\n");
}
if(menu==2) {
int total=0;
for(int i=0; i<myArray.length; i++) {
total= total + myArray[i];
}
System.out.print("\naverage: ");
System.out.print((double) total/myArray.length + " "+"\n");
}
if(menu==3) {
int large=myArray[0];
for(int i =0; i <myArray.length; i++) {
if(myArray[i]>large) {
large=myArray[i];
}
}
System.out.print("\nlargest: ");
System.out.print(large+"\n");
}
if(menu==4) {
int count=0;
for(int i=0; i<myArray.length; i++) {
if(myArray[i]==COUNT) {
count++;
}
}
System.out.print("\n3count: ");
System.out.print(count+"\n");
}
if(menu==5) {
double half= myArray[0]/2;
int count2=0;
for(int i=0; i<myArray.length; i++) {
if(myArray[i]<half) {
count2++;
}
}
System.out.print("\nLess than half of first: ");
System.out.print(count2+"\n");
}
if(menu==6) {
int count3=0;
int b= myArray.length;
for(int i=0; i<b-1; i++) {
if(myArray[i]==myArray[i+1]) {
count3++;
}
}
System.out.print("\nRepeat: ");
System.out.print(count3+"\n");
}
if(menu==7) {
int swap;
int a= myArray[0];
int b=myArray[size-1];
swap=b;
b=a;
a=swap;
}
if(menu==8) {
}
if(menu>8) {
System.out.print("\nIllegal option, try again"+"\n");
}
}while(menu !=8);
}
}

Comments
Post a Comment