Why is my function giving me a string when it should be giving a structure?

We are asked to write a function 'carGenerator' that takes 5 inputs and uses conditional statements to determine if a car is valid based on these parameters. If it is not, it should return the string 'Invalid car parameters.'. My function works for the example problems given, however it won't work for the random test and is giving back the string 'Invalid car parameters.' for what should be a valid car.
Here are the parameters given:
"Your program should check to see if the car is valid. Invalid car properties include:
-Values less than 0 for any numeric property.
-A value greater than 100 for fuelORchargeif isElectricis true. Batteries cannot be more than 100% charged."
*Note: isElectric is a Boolean variable determining whether the car is electric or fuel burning
fuelORcharge represents how much fuel (in volume) or how much charge the car has
Here is my current code:
function [car] = carGenerator (mass, passengers, isElectric, fuelORcharge, workPerUnit)
%mass = mass of car (kg)
%passengers = number of passengers
%isElectric = car is electric or not
%fuelORcharge = volume of fuel or percent charge (if electric)
% workPerUnit = rate at which fuel or battery life is converted into energy
if (mass <= 0) || (passengers <= 0) || (fuelORcharge < 0) || (workPerUnit < 0)|| (isElectric == 1 && fuelORcharge > 100)
car = 'Invalid car parameters';
else
car.mass = mass;
car.passengers = passengers;
car.isElectric = isElectric;
car.fuelORcharge = fuelORcharge;
car.workPerUnit = workPerUnit;
end

8 Comments

Nothing obvious. Could you give an example of parameters that it is failing on?
It's only failing on random tests that I can't see because the program does it without showing the call it's using to the function. It tells you to test your function using this
mass = (randn+3)+1000;
passengers = randi(9)-2;
isElectric = randi(2)-1;
fuelORcharge = rand*125-5;
workPerUnit = rand*260000-10000;
I assume that your function is graded through cody coursework. Unfortunately, without knowing the fail conditions there's not much we can do to help. I'd say you can rightfully complain to whomever created the assignment that the fail/success conditions are not defined clearly enough.
If cody does display messages that you generate in your function, you could always add a line:
fprintf(mass: %f, passengers: %d, isElectric: %d, fuelOrcharge: %d, workPerUnit: %f\n', mass, passengers, isElectric, fuelOrcharge, workPerUnit);
If it does suppress the display then I'm afraid you're left with trials and errors.
edit: Just created a cody coursework assignment. It does suppress these messages so you are left with trying to modify your function until it works and complaining
Yeah it's being graded in something called 'zybooks', I don't know whether it uses what you're talking about. But thanks all for the help I'll just keep trying other things!
@Guillaume: Is it possible to export information over a side-channel. Does sendmail or urlread work? What about using pause() an encoding the information as delay? If I can read the memory of the kernel space, it should be possible to provide some debug information from Cody Coursework.
@Kristen: The provided test data are not useful, because you cannot know, if these inputs are valid or not. So you need your solution to check if your solution works correctly.
@Jan,
I doubt that sendmail would work, it's unlikely that the server running cody coursework (or that zybooks instance) provides an smtp server.
I guess webwrite may work if it's not blocked by the server firewall, as long as you have a server up and running to receive the data. I don't really have the time to set up a test server to test though.
You get very little information from cody coursework, a lot less than the regular Cody. Just a pass/fail. I doubt that you could build a timing attack succesfully.
@Guillaume: I'm very interested in this topic. Matlab is powerful and allows to interact with the OS of the virtual machines aggressively, even without Meltdown and Spectre. But I do not want to encourage anybody to misuse the Cody platform and leave the discussion.

Sign in to comment.

Answers (1)

Jan
Jan on 16 Feb 2018
Edited: Jan on 16 Feb 2018
A guess: "Values less than 0" is x < 0, not x <= 0. This is not meaningful for mass and passengers, but according to the text, only "less than 0" is considered as error.

1 Comment

Yeah I originally had that but it didn't work so I started seeing if I needed to add the equals to the condition, that didn't work either though

Sign in to comment.

Asked:

on 16 Feb 2018

Edited:

Jan
on 19 Feb 2018

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!