%% CS 310 Team Lab 7: Nesting and Debugging % SOLUTION - Spring 2013 %% Problem 1: Create a multiplication table clear matrix = zeros(25, 25); for row = 1 : 25 for col = 1 : 25 matrix(row, col) = row * col; end end matrix %% Problem 2: Calculate bonuses for division 1 employees clear data = load('division1.txt'); [rows, cols] = size(data); % find the number of rows and cols bonus = 0; % initialize total bonus amount to 0 for r = 1 : rows % for each employee % if the employee gets a bonus, calculate it and it to the total if data(r, 3) % or could do: if data(r, 3) == 1 bonus = bonus + 0.05*data(r, 2); end end disp(['Total bonus for Division 1 = \$', num2str(bonus)]) %% Problem 3: Calculate bonuses for all four divisions clear N = 4; total_bonus = 0; % initialize total bonus amount to 0 div_bonus = zeros(1, N); % stores bonus amount for each division for d = 1:N % for each division filename = ['division' num2str(d) '.txt']; % load division's info data = load(filename); div_bonus(d) = 0; % initialize this division's bonus to 0 [rows, cols] = size(data); for r = 1 : rows % for each employee % if the employee is getting a bonus if data(r, 3) % or could do: if data(r, 3) == 1 bonus = 0.05*data(r, 2); % compute bonus div_bonus(d) = div_bonus(d) + bonus; % add to division total total_bonus = total_bonus + bonus; % add to overall total end end end % display the bonuses for each division as well as the total bonus amount for d = 1:N disp(['Total bonus for division ', num2str(d), ' = \$', ... num2str(div_bonus(d))]); end disp(['Total bonus for all divisions = \$', num2str(total_bonus)]); %% Problem 4: Find the prime factors of a positive integer clear % prompt the user for a value value = input('Enter a positive integer or 0 to quit: '); % NOTE: publishing does not work if there is user input, so when % publishing, we comment out the line above and replace it with % value = 495; while value ~= 0 % while the user has not chosen the "quit" option % if the value is a positive integer if value > 0 && floor(value) == value factors = []; % initialize factors list to empty list V = value; % copy value into V for p = 2 : V/2 % for each number p between 1 and V/2 if isprime(p) % if p is prime while mod(V, p) == 0 % while V is evenly divisible by p factors = [factors p]; % add p as a factor V = V/p; % divide V by p to "remove" p end end end % display all the factors disp(['The factors of ', num2str(value), ' are: ']) disp(factors) else % display an error message indicating what are valid values disp('Invalid input - value must be 0 or a positive integer') end % get the next value from the user value = input('Enter a positive integer or 0 to quit: '); % NOTE: publishing does not work if there is user input, so when % publishing, we comment out the line above and replace it with % value = 0; end %% Problem 5: Count the number of primes between 1 and 100 clear count = 0; for n = 1 : 100 if isprime(n) count = count + 1; end end disp(['There are ', num2str(count), ' primes between 1 and 100']) %% Problem 6: Find and display the 25th prime number clear count = 0; n = 1; % start checking numbers at 1 while count < 25 if isprime(n) count = count + 1; end n = n + 1; end % At the end of this loop, n will be 1 more than the 25th prime, so we'll % need to take this into account. disp(['The 25th prime is ', num2str(n-1)])