1
$\begingroup$

I have an assignment to use Module and in which I have to implement:

  1. The input of the program are a list of numbers and a list of strings
  2. Check whether every number in the first list are integer or not
  3. Check whether every string in the second list are truly strings or not
  4. Find the maximum and the mean of the first list (the numbers list)
  5. Plot the data and the mean:

the picture above is how the plot output supposed to look like

That's how the plot supposed to look like. Unfortunately, mine look like this:

enter image description here

So, what do you think is wrong?

$\endgroup$
  • $\begingroup$ string = If[All[...],...] should be string = If[AllTrue[...],...]. In future, please post code (using the backticks) rather than screenshots - it's easier for us to copy it than type out code from a picture. $\endgroup$ – flinty Jun 22 at 15:42
  • $\begingroup$ Also your input {i, am,so,confused} is a list of symbols but you want strings so it should be {"i","am","so","confused"} $\endgroup$ – flinty Jun 22 at 15:45
0
$\begingroup$

Problem 1: The source of the error is the second ListLinePlot. You can see this because if you click on the orange bubble {...} on the error and select Show Stack Trace you get this:

> Message[ListLinePlot::lpn, 3.6]

> ListLinePlot[18/5, PlotStyle -> RGBColor[0, 1, 0]]

You are trying to plot a single number 18/5, the mean. You need to provide at least two points to get a line in ListLinePlot. To fix this use ListLinePlot[{{0,mean},{Length[ang],mean}}].

Problem 2: You need to use AllTrue instead of All when checking the list of strings.

Also you can shorten your If statements. If they fail and the Print's are triggered, then integer and string get assigned the return value of Print which is Null. I assume you really want False.

Problem 3: You want to pass in strings into your function {"i", "am", "so", "confused"}, not the list of symbols {i,am,so,confused}

Putting it all together:

fungsi[ang_, str_] := Module[{integer, string, max, mean, gambar},
  integer = AllTrue[ang, IntegerQ];
  string = AllTrue[str, StringQ];
  If[Not[integer], Print["not integers"]];
  If[Not[string], Print["not strings"]];
  max = Max[ang];
  mean = Mean[ang];
  gambar = 
   Show[
    ListLinePlot[ang, PlotStyle -> Blue], 
    ListLinePlot[{{0,mean},{Length[ang],mean}}, PlotStyle -> {Green,Dashed}],
    ListLinePlot[{{0,max}, {Length[ang],max}}, PlotStyle -> Red]
   ]
 ]
fungsi[{1, 2, 3, 4, 8}, {"i", "am", "so", "confused"}]

You can also add lines and other graphics to plots using Epilog like this:

ListLinePlot[{1, 2, 3, 4}, 
 Epilog -> {Purple, Dashed, Line[{{0, 2}, {4, 2}}]}]
| improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.