CSCI 101 Algorithms II Elsayed Hemayed
Overview • Multiplication table • Matrix multiplication • Sorting • Practice Problems
Displaying the multiplication table for i=1:5 for j=1:5 fprintf(‘%8d’,i*j); end fprintf(‘n’); end 1 2 3 4 5 2 4 6 8 10 3 6 9 12 15 4 8 12 16 20 5 10 15 20 25
Displaying a modified multiplication table for i=1:5 for j=i:5 fprintf(‘%8d’,i*j); end fprintf(‘n’); end 1 2 3 4 5 4 6 8 10 9 12 15 16 20 25
Matrices Multiplication • Given matrix A [n][k] and matrix B[k][m] find their multiplication matrix C[n][m]. i j i a a j =x A [n][k] B[k][m] C[n][m]
Matrices Multiplication for i =1 to n for j=1 to m C[i][j]=0 for a=1 to k C[i][j]=C[i][j]+A[i][a]*B[a][j] i j i a a j =x
Sorting • Given a list of N integer values, sort them in an ascending order. for i=1 to N for j=i+1 to N If A[i]>A[j] swap A[i] and A[j] 5 4 2 1 6
Array Sorting X=input(‘enter an array:’); for i=1:length(X) for j=i+1:length(X) if X(i)>X(j) t=X(i); X(i)=X(j); X(j)=t; end end end disp(X); 5 4 2 1 6 4 5 2 1 6 2 5 4 1 6 1 5 4 2 6 i=1 j= 2 3 4 5 1 5 4 2 6 enter an array: [5 4 2 1 6] Swapping values
What is the output of this program? N=input(‘enter a number:’); for i=1:N for j=1:i fprintf(‘*’); end fprintf(‘n’); end Enter a number: 5 * ** *** **** *****
Practice Problems
Problem 1 Write a program that reads a number N from the user and prints an array containing the numbers (from 1 to N) that are multiples of 3 or 5 Also print their sum Input/Output Sample: Enter number: 10 The array that has multiples of 3 or 5 = 3 5 6 9 10 The sum of the multiples of 3 or 5 = 33
Problem 2 N children at a party eat scoops of ice cream, write a matlab program to calculate how many scoops of ice cream do they eat altogether and who eats more, if the user enters a negative number, the program asks the user to re-enter the number. Input/Output Sample: Enter the number of children: 4 Enter number of scoops/ child: 1 Enter number of scoops/ child: -2 Error! Enter Positive Number: 4 Enter number of scoops/ child: 5 Enter number of scoops/ child: 3 Number of ice cream scoops all children ate: 13 Child ate more ice scoops: 3
Problem 3 In train station, the price of ticket depends on the destination distance. There is a 20% discount for prices>160, 5% for prices<100 and 10% for any other prices. Write a matlab script to print final price for each station. The program can be executed 3 times. Input/Output Sample: Enter tickets price/station: [150 210 50 75 80 170] Discounted prices are: 135.00 168.00 47.50 71.25 76.00 136.00
Problem 4 Write MATLAB code that reads an array from user and counts the number of elements that have values between 50 and 90 and calculates the max, and average of these numbers. If the array does not contain numbers in the required range the program should display “there are no numbers between 50, 90 in the array” Input/Output Sample: Enter your array of numbers: [2 3 55 88 65 57 99 78] Count of numbers between 50, 90 is: 5 Max is: 88 Avg is: 68.6
Problem 5 A customer want to buy a TV at least price. Write a program to help him to know the average price of the TV and the store at which the least price. The program should ask the customer about the number of stores that have the required TV, and he should then enter the TV price at each store. Then the program should print the average price of TV in all stores, and where the least price. Input/Output Sample: Enter the number of stores have the required TV: 4 Enter the price of the TV in this store: 2800 Enter the price of the TV in this store: 2780 Enter the price of the TV in this store: 2930 Enter the price of the TV in this store: 2855 The average price of TV in all stores: 2841.25 The store have the least TV price: 2

Csci101 lect07 algorithms_ii

  • 1.
  • 2.
    Overview • Multiplication table •Matrix multiplication • Sorting • Practice Problems
  • 3.
    Displaying the multiplicationtable for i=1:5 for j=1:5 fprintf(‘%8d’,i*j); end fprintf(‘n’); end 1 2 3 4 5 2 4 6 8 10 3 6 9 12 15 4 8 12 16 20 5 10 15 20 25
  • 4.
    Displaying a modifiedmultiplication table for i=1:5 for j=i:5 fprintf(‘%8d’,i*j); end fprintf(‘n’); end 1 2 3 4 5 4 6 8 10 9 12 15 16 20 25
  • 5.
    Matrices Multiplication • Givenmatrix A [n][k] and matrix B[k][m] find their multiplication matrix C[n][m]. i j i a a j =x A [n][k] B[k][m] C[n][m]
  • 6.
    Matrices Multiplication for i=1 to n for j=1 to m C[i][j]=0 for a=1 to k C[i][j]=C[i][j]+A[i][a]*B[a][j] i j i a a j =x
  • 7.
    Sorting • Given alist of N integer values, sort them in an ascending order. for i=1 to N for j=i+1 to N If A[i]>A[j] swap A[i] and A[j] 5 4 2 1 6
  • 8.
    Array Sorting X=input(‘enter anarray:’); for i=1:length(X) for j=i+1:length(X) if X(i)>X(j) t=X(i); X(i)=X(j); X(j)=t; end end end disp(X); 5 4 2 1 6 4 5 2 1 6 2 5 4 1 6 1 5 4 2 6 i=1 j= 2 3 4 5 1 5 4 2 6 enter an array: [5 4 2 1 6] Swapping values
  • 9.
    What is theoutput of this program? N=input(‘enter a number:’); for i=1:N for j=1:i fprintf(‘*’); end fprintf(‘n’); end Enter a number: 5 * ** *** **** *****
  • 10.
  • 11.
    Problem 1 Write aprogram that reads a number N from the user and prints an array containing the numbers (from 1 to N) that are multiples of 3 or 5 Also print their sum Input/Output Sample: Enter number: 10 The array that has multiples of 3 or 5 = 3 5 6 9 10 The sum of the multiples of 3 or 5 = 33
  • 12.
    Problem 2 N childrenat a party eat scoops of ice cream, write a matlab program to calculate how many scoops of ice cream do they eat altogether and who eats more, if the user enters a negative number, the program asks the user to re-enter the number. Input/Output Sample: Enter the number of children: 4 Enter number of scoops/ child: 1 Enter number of scoops/ child: -2 Error! Enter Positive Number: 4 Enter number of scoops/ child: 5 Enter number of scoops/ child: 3 Number of ice cream scoops all children ate: 13 Child ate more ice scoops: 3
  • 13.
    Problem 3 In trainstation, the price of ticket depends on the destination distance. There is a 20% discount for prices>160, 5% for prices<100 and 10% for any other prices. Write a matlab script to print final price for each station. The program can be executed 3 times. Input/Output Sample: Enter tickets price/station: [150 210 50 75 80 170] Discounted prices are: 135.00 168.00 47.50 71.25 76.00 136.00
  • 14.
    Problem 4 Write MATLABcode that reads an array from user and counts the number of elements that have values between 50 and 90 and calculates the max, and average of these numbers. If the array does not contain numbers in the required range the program should display “there are no numbers between 50, 90 in the array” Input/Output Sample: Enter your array of numbers: [2 3 55 88 65 57 99 78] Count of numbers between 50, 90 is: 5 Max is: 88 Avg is: 68.6
  • 15.
    Problem 5 A customerwant to buy a TV at least price. Write a program to help him to know the average price of the TV and the store at which the least price. The program should ask the customer about the number of stores that have the required TV, and he should then enter the TV price at each store. Then the program should print the average price of TV in all stores, and where the least price. Input/Output Sample: Enter the number of stores have the required TV: 4 Enter the price of the TV in this store: 2800 Enter the price of the TV in this store: 2780 Enter the price of the TV in this store: 2930 Enter the price of the TV in this store: 2855 The average price of TV in all stores: 2841.25 The store have the least TV price: 2