Computer Hope

Software => Computer programming => Topic started by: MichelleCarrothers on September 10, 2010, 06:33:58 PM

Title: I have an assignment
Post by: MichelleCarrothers on September 10, 2010, 06:33:58 PM
please help me if you can I have this assignment and I really do not understand one bit of it at all.
Consider the following selection statement where X is an integer test score between 0 and 100.

input X

if (0 <= X and X < 49)
   output "you fail"

else if (50 <= X and X < 70)
   output "your grade is" X
   output "you did OK"

else if (70 <= X and X < 85)
output "your grade is" X
   output "you did well"

else if (85 <= X and X < 100)
output "your grade is" X
   output "you did great"

endif
output "how did you do?"

then I have to take this above and answer these questions and I am so lost and I just can't get a grip on this at all.

•   What will be printed if the input is 0?

•   What will be printed if the input is 100?


•   What will be printed if the input is 51?

•   What will be printed if the user enters “Wingding”?


•   Is this design robust? If so, explain why. If not, explain what you can do to make it robust.

•   How many levels of nesting are there in this design?


•   Provide a set of values that will test the normal operation of this program segment. Defend your choices.

•   Provide a set of test values that will cause each of the branches to be executed.


•   Provide a set of test values that test the abnormal operation of this program segment

if anyone can help me that would be wonderful.
Thanks Michelle
Title: Re: I have an assignment
Post by: ghostdog74 on September 10, 2010, 07:20:41 PM
">" , ">=" ,"<"  and "<=" are just maths operators. I am sure you have learnt that in school. If not i don't know what you are doing in school. check your text book what those operators mean. You can then answer qns 1-4
Title: Re: I have an assignment
Post by: Allan on September 11, 2010, 04:08:11 AM
We don't do homework here.
Title: Re: I have an assignment
Post by: ghostdog74 on September 11, 2010, 04:13:00 AM
We don't help with homework here.
can i take it that you are talking to OP and not me?
Title: Re: I have an assignment
Post by: Allan on September 11, 2010, 04:29:03 AM
can i take it that you are talking to OP and not me?
Yes, of course :)
Title: Re: I have an assignment
Post by: Salmon Trout on September 11, 2010, 04:50:56 AM
We don't "do" homework, sure, but I don't see why we should not help a genuinely puzzled person understand what an assignment requires them to do. If that is genuinely what we have here. Having said that, I wonder if I detect the hand of Billrich?

MIchelleCarrothers, surely you understand what this code is doing? (If you don't you should see your teacher, not attempt to cheat by asking on the web) It is a test. If zero is less than or equal to X AND X is less than 49, in other words, if X is between 0 and 49, then do something. (Hint: output a message) If you grasp that you can look at the code and see what it will do for different values of X.

Code: [Select]
if (0 <= X and X < 49)
    output "You fail"
Title: Re: I have an assignment
Post by: Big on September 13, 2010, 07:31:35 AM
  What will be printed if the input is 0?
- "you fail"

   What will be printed if the input is 100?
- Good question, because
      else if (85 <= X and X < 100)

   This is basically saying "if your input is equal or greater than 85 and smaller than 100". So what happens if you  input 100? Then input is greater than 85 but also equal to 100 and there is no equal to 100 in that condition. I would say the output is "you did great" because it seems like it's a sort of an 'else' being indented a few spaces (I don't know what programming language that is).

 What will be printed if the input is 51?
"you did OK" because 51 is greater than 50 and smaller than 70.
  else if (50 <= X and X < 70)

 What will be printed if the user enters “Wingding”?
- I don't think anything will be printed then since it's not in the program logic. Maybe an exception might be thrown, depending on the language.

 Is this design robust? If so, explain why. If not, explain what you can do to make it robust.
- This design is not robust.
      Why? It doesn't deal with all possible situations. This program solely expects you to enter numbers.
      What if the user enters characters from the alphabet, or other strange Unicode/special characters?
      What will the program do? How will it handle those exceptions?
      What if you enter 2965687923 as a number? Maybe it falls out of scope and the program will think it's greater than 0 because it starts all over (crosses 0 and begins to count again)?

- How many levels of nesting are there in this design?
  I think there is 1 level of nesting.
  Nesting is basically the indented stuff you see. e.g.:
  output "your grade is" X
    output "you did well"
  "you did well" is the nested statement.
 
 Provide a set of values that will test the normal operation of this program segment. Defend your choices.
input: 0, 1, 48 (for the first segment)
          50, 51, 69 (second segment)
          70, 71, 84 (third segment)
          85, 86, 99 (last segment)

They will all print out the correct output, respectively because they respect the if conditions' boundaries.

 Provide a set of test values that will cause each of the branches to be executed.
- Same as before, not sure. =)

 Provide a set of test values that test the abnormal operation of this program segment
- Maybe you could enter the values -1, "-1" and 3165165112316549684135 and -6461321236549846531. lol =)

You know, I think it's funny because this program is written in a strange way to me.
If (y < x). Makes it a bit confusing. =) Usually I would write if (x < y).

Operators explanation

In programming, combinations of operators are used. An operator is something like / * - +.
An operand is the value to be calculated, such as 1 2 3 6 -1 0,1 1,55.
So:
1              +              2               =     3
operand   operator    operand         result

Combining operators is a short way of writing an expression; e.g. if (0 == x OR 0 < x) can be written short version as:  if ( 0 <= x) because it combines == and < at the same time, i.e. <=
See?

Another example is ++ and +=. e.g.: x + 1; can be written shorter as x++, because the '++' operator does the same thing as '+1'. x += 1; does also the same thing because the long version of that is:  x = x + 1;


Big











Title: Re: I have an assignment
Post by: ghostdog74 on September 13, 2010, 08:11:17 AM
I would say the output is "you did great" because it seems like it's a sort of an 'else' being indented a few spaces (I don't know what programming language that is).
Nope. It is not "you did great". The control flow ends without testing for X==100. Naturally, control goes back to outside the block after endif.


Quote
What will be printed if the user enters “Wingding”?
- I don't think anything will be printed then since it's not in the program logic.
No. Something will be printed.

Quote
- How many levels of nesting are there in this design?
  I think there is 1 level of nesting.
  Nesting is basically the indented stuff you see. e.g.:
  output "your grade is" X
    output "you did well"
  "you did well" is the nested statement.

don't think it has anything to do with indentation. I  just says to print "your grade is X" and then "you did well" to stdout.
 
Title: Re: I have an assignment
Post by: Big on September 13, 2010, 08:16:24 AM
Ah. C++ huh? Well, I don't know that language.
Title: Re: I have an assignment
Post by: BC_Programmer on September 13, 2010, 08:20:06 AM

Quote
   What will be printed if the input is 100?
- Good question, because
      else if (85 <= X and X < 100)

   This is basically saying "if your input is equal or greater than 85 and smaller than 100". So what happens if you  input 100? Then input is greater than 85 but also equal to 100 and there is no equal to 100 in that condition. I would say the output is "you did great" because it seems like it's a sort of an 'else' being indented a few spaces (I don't know what programming language that is).
I'm fairly certain that nothing would be printed; I imagine the indentation is there to throw off the observer, but everything in that block until the end if (the two outputs) are certainly part of it- otherwise why would there be end if in this language (whatever it is)


Quote
What will be printed if the user enters “Wingding”?
- I don't think anything will be printed then since it's not in the program logic. Maybe an exception might be thrown, depending on the language.
Yep, without knowing what the language is and therefore it's type system it's impossible to say. If it's a dynamic or typeless language nothing will be printed- a strongly typed language will emit a error when X is read (depending of course on the type of X).
 
Quote
      What if you enter 2965687923 as a number? Maybe it falls out of scope and the program will think it's greater than 0 because it starts all over (crosses 0 and begins to count again)?
Probably not; I would think if they put a number outside the range of the numeric variable (assuming X is a numeric variable) it would simply give an overflow. of course this is also completely dependent on the type system the language uses and so knowing the language would have been great. agree with your other sentiments regarding robustness.

Quote
- How many levels of nesting are there in this design?
  I think there is 1 level of nesting.
  Nesting is basically the indented stuff you see. e.g.:
  output "your grade is" X
    output "you did well"
  "you did well" is the nested statement.

This depends on the language as well. in C/C++/C#:

Code: [Select]
if (X < 50)
{
   Console.Writeln("X is smaller than 50");
}
else if (X==75)
{
 Console.Writeln("X is 75");
}
It looks like a single level of nesting (like the original code) but in this case there are actually two levels of nesting in the else; the else contains a nested if statement ,if (x==75)  which itself has the nested Console method call. But this is purely semantics; and that's C#, it's impossible to say wether the language in question here considers else if to be a special token or wether it's treating the statements as an if within and else clause. I'd be inclined to guess the latter simply because the if and else are separated by a space.

Quote
You know, I think it's funny because this program is written in a strange way to me.
If (y < x). Makes it a bit confusing. =) Usually I would write if (x < y).
So, it's not just me then! I think the method of testing, whereby the value goes before the variable being tested arose from C, where a programmer might mistakenly write this:

Code: [Select]
if(value=null)
...
of course, = is the assignment operator, so instead of testing the value, it would assign it to null and then execute the if! quite a source of bugs. So, programmers instead opted to do it this way:
Code: [Select]
if(null==value)
This way if they accidentally forgot a equals sign, it would cause a compiler error, rather then a difficult to find logic error. The actual reason was apparently lost in time to some people, who not only extended this to the relational operators (for which there is no assignment to avoid) but also started using them in other languages. It's a case of Cargo Cult programming.


Operators explanation

Quote
In programming, combinations of operators are used. An operator is something like / * - +.
An operand is the value to be calculated, such as 1 2 3 6 -1 0,1 1,55.

-1 wouldn't be an operand, since the - is a unary minus operator :P

Quote
Combining operators is a short way of writing an expression; e.g. if (0 == x OR 0 < x) can be written short version as:  if ( 0 <= x) because it combines == and < at the same time, i.e. <=
yeah... but that's not really a programming concept (aside from the transliteration of ≥ to >=  and ≤ to <=. most languages preserve the order even... => is used in a lot of languages for something entirely different, usually to indicate a predicate/lambda expression.) It's more a math concept. Not that you are wrong, it just seems like your trying to go a bit too low level; It would sort of be like describing the history of how we came to write the various math symbols and numbers as they are now "well, we started with roman numerals..." type thing)

Quote
Another example is ++ and +=. e.g.: x + 1; can be written shorter as x++, because the '++' operator does the same thing as '+1'. x += 1; does also the same thing because the long version of that is:  x = x + 1;
depends entirely on the language; ++, --, as well as the operator= assignments aren't supported universally in all languages.

in C and it's variants, the ++ operator is both a postfix as well as a prefix operator, you can say:
Code: [Select]
y=12;
x=y++;
z=++y;
printf("%d,%d,%d",x,y,z);
which will print:
Code: [Select]
13,14,13
the first statement of course initializes y to 12, the second increments y and stores that in x (13), and the last stores y in z (13), and then increments it (to 14).


Title: Re: I have an assignment
Post by: BC_Programmer on September 13, 2010, 08:23:48 AM
Ah. C++ huh? Well, I don't know that language.

The language of the original sample isn't C++. (I am rather curious what the language is, actually)
Title: Re: I have an assignment
Post by: ghostdog74 on September 13, 2010, 10:00:00 AM
I'm fairly certain that nothing would be printed;
"how did you do" will be printed, since nothing in the if/else match 100.

Title: Re: I have an assignment
Post by: Salmon Trout on September 13, 2010, 10:19:25 AM
The language of the original sample isn't C++. (I am rather curious what the language is, actually)

It might be a kind of "educational pseudocode".

A Google search showed that very same assignment was posted on justanswer.com's "Ask a Homework Question, Get an Answer ASAP!" service, "790 days and 19 hours ago.". (You have to pay to see the answer - 3 UK pounds)


Title: Re: I have an assignment
Post by: Allan on September 13, 2010, 10:21:24 AM
We don't do homework here.
Well, apparently I was mistaken. Some of us not only do homework here, but actually enjoy it  ;D
Title: Re: I have an assignment
Post by: Salmon Trout on September 13, 2010, 10:25:32 AM
I don't think Big should have posted what he did, but then he seems to do what he likes, even after being told what forum policy is.
Title: Re: I have an assignment
Post by: BC_Programmer on September 13, 2010, 09:01:12 PM
"how did you do" will be printed, since nothing in the if/else match 100.



I appear to have missed that.

It might be a kind of "educational pseudocode".


my first thought was C++, but with macros that change things- macros for and to be &&, endif = }, cin >> = input, cout << = input, etc. Sadly, you see that type of thing all the time, and it doesn't make a whole lot of sense. If you want to teach a language that uses, for example, endif, then use such a language, don't just hodgepodge a bunch of C++ macros to do it that way.

Oh well.

The only problem is that there is no starting brace for the if blocks if that was the case.

I'd definitely have to say it's some sort of psuedocode as well.


A Google search showed that very same assignment was posted on justanswer.com's "Ask a Homework Question, Get an Answer ASAP!" service, "790 days and 19 hours ago.". (You have to pay to see the answer - 3 UK pounds)

I would have suspected as much. It's not like it's particularly difficult- it involves more math concepts then it does programming concepts. Although it's a lot better then the trivia-related questions both in classes as well as as interview question. Stupid, retarded questions that essentially test ones knowledge of a few language corner cases, like "Is it possible for a function to return more often than it is called?" or how does the XXX keyword work, or other nonsense. It's pointless to ask such questions- you're trying to make/find programmers, not people who read and memorize language references. You want to find somebody who won't sit around testing out a certain programming construct or modifier to see what it does and will instead actually defer to the language reference to find out; a question like:

Quote
what is the output from
2["hello"]
and why?
Is a completely stupid question. First off, is it really that important that they know how to deal with such trivial corner cases? Sure, it proves they have a handle on pointers, but it also proves that they are willing to make "clever" code when it's not necessary, and their code is probably strewn with ternary conditionals. "clever" code is bad and difficult to read. Clever interview questions are bad because they encourage clever code.

Title: Re: I have an assignment
Post by: Salmon Trout on September 13, 2010, 11:43:13 PM
Do you think Big is Billrich? And MichelleCarrothers?
Title: Re: I have an assignment
Post by: CBMatt on September 15, 2010, 06:23:58 AM
I don't think Big should have posted what he did, but then he seems to do what he likes, even after being told what forum policy is.

Don't like the competition, eh?
Title: Re: I have an assignment
Post by: Big on September 15, 2010, 06:31:22 AM
Quote
-1 wouldn't be an operand, since the - is a unary minus operator
I guess One's Complement doesn't count here (signed bit)? :P
Title: Re: I have an assignment
Post by: BC_Programmer on September 15, 2010, 07:39:16 AM
I guess One's Complement doesn't count here (signed bit)? :P

No, because even if that was the case, it would be in the form of an operator (such as C's ~) :P

Although I imagine there are languages out there that may actually not represent negative numbers via unary operators. Don't know any off the top of my head though.
Title: Re: I have an assignment
Post by: Salmon Trout on September 15, 2010, 10:17:37 AM
Don't like the competition, eh?

I resent that. What a stupid remark. Idiotic even. I don't do people's homework for them.
Title: Re: I have an assignment
Post by: Allan on September 15, 2010, 11:53:05 AM
Locked