Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I am having trouble with creating a user defined character array. The question a

ID: 3546349 • Letter: I

Question

I am having trouble with creating a user defined character array.  The question asks to prompt the user to input elements form the periodioc table, and to store them in an array.  The input function will be inside a while loop. I am using an index as well to sort the input that the user gives the program. Below is what my code looks like so far. I keep on getting an error message though.  Any suggestions?

element = []

cont = 'y'

k = 1

disp('Enter the atomic symbol in single qoutes.')
disp('When the symbol is a single letter, precede the letter with a space.')

while cont == 'y' | cont == 'Y'
   B = input('Enter the atomic symbol: ');
   B = char(B)
   element(k) = B
   k = k + 1;
   cont = input('Are there more elements? y/n in single quotes:');
   
end

Explanation / Answer

% you can do it using cell rather than array.

% cell works better for string data type.
% only difference in usage is use of '{ }' rather than '[ ]'


clear all

clc


element = {};

cont = 'y';

k = 1;


disp('Enter the atomic symbol in single qoutes.');

disp('When the symbol is a single letter, precede the letter with a space.');


while cont == 'y' | cont == 'Y'

B = input('Enter the atomic symbol: ');

element{k} = B

k = k + 1;

cont = input('Are there more elements? y/n in single quotes:');

end

% for any further queries mail me at mail2neyaz@yahoo.com