Welcome! Log In Create A New Profile

Advanced

smileys with beer Help with Assignment 3

Posted by Rendie 
Announcements Last Post
Announcement SoC Curricula 09/30/2017 01:08PM
Announcement Demarcation or scoping of examinations and assessment 02/13/2017 07:59AM
Announcement School of Computing Short Learning Programmes 11/24/2014 08:37AM
Announcement Unisa contact information 07/28/2011 01:28PM
smileys with beer Help with Assignment 3
May 13, 2008 08:19AM
Please could you assist with generating a functions starting with Question 2

// the required function symbol should be inserted here
int main( )
{
int mark;
char symb;
cout << "Enter mark out of 100: ";
cin >> mark;
symb = symbol(mark);
cout << "Symbol corresponding to " << mark << " is " << symb << endl;
return 0;
}
avatar smileys with beer Re: Help with Assignment 3
May 13, 2008 01:31PM
You've virtually answered your own question. You've specified the interface details already.

You've set the requirements that you must feed it a single value(type int) and it must return a char.

Therefore your function should look something like:
char symbol(int mark)
  {
  char resultSymbol;

  // code to determine the correct symbol

  return resultSymbol;
  }
Now all you have to do is figure out how you're going to determine how to convert a mark to a symbol.
smileys with beer Re: Help with Assignment 3
May 13, 2008 02:05PM
Please look at the following function: but it does not return the symbol that is required

char symbol(int mark)
{
char A, B, C, D, E, F, GG;
if(mark >= 0 && mark <= 39)
return F;
else if(mark >= 40 && mark <= 49)
return E;
else if(mark >= 50 && mark <= 59)
return D;
else if(mark >= 60 && mark <= 69)
return C;
else if(mark >= 70 && mark <= 79)
return B;
else if (mark >= 80 && mark <= 100)
return A;
else
return GG;
avatar smileys with beer Re: Help with Assignment 3
May 13, 2008 05:54PM
It seems you are not sure what you had to do here. I'm not going into detail here but I will just give you an idea.

The solution: Well most of it as you need to solve this yourself as the assignment is still due:


Your function must return a string ( const char* )

const char* symbol (int mark)
{

 if(mark >= 0 && mark <= 39)
    return "F";

  else if(mark >= 40 && mark <= 49)
    return "E";

  ...
  ... // D - A comes here.
  ...

  else
    return "GG";

}

You had to return a "symbol", this is a character, actually a string because at the end you are returning two characters.

Remember: 1 character is char, 2 or more is a string.

You where trying to return a variable instead of the character.

--------------------------------------------------------------
Don't Assume Everything is a Nail Just Because You Have a Really Big Hammer
avatar smileys with beer Re: Help with Assignment 3
May 13, 2008 06:42PM
Ok, you seem to not have the concept of a variable here. You have defined 7 variables, not 7 characters. Since you have not given them initial values, they have undefined values.

Also, I've just downloaded the tut letter and checked the actual question. There is no symbol GG and I see you are using it merely as an indication of an error state. That's good. It's good programming practice to anticipate erroneous input but you don't need it to be a double letter, a single G would suffice.

That said, you can fuse your idea with Chris'. You can throw away the variables and just use hard-coded symbols but you don't need to use strings since they're not necessary.

A tip on increasing readability: Most people who become programmers have done highschool maths so they're used to seeing a numberline representation of a range of values. e.g. if I say that x is a value between 90 and 100 (inclusive) then mathematically, you'd write it like

90 <= x <= 100

conversely, if you say that x is not in the range of 90 to 100, then you usually write it as

x < 90 or 100 < x

Most people are used to this way of writing numbers from school days so why not use this in programming to increase readability?

When the variable is inside a range, keep the variables between the constants

if (90 <= x && x <= 100)

or if it's outside a range, keep the variables on the outside of the constants

if (x < 90 && 100 < x)

It's just a tip, not a rule.

===================
Anyway, back to the question above, you could rewrite your cascading if statement like so:
char symbol(int mark)
  {
  if      (0 <= mark && mark <= 39)
    return 'F';
  else if (40 <= mark && mark <= 49)
    return 'E';

  // the other symbols

  else
    return 'G'; // invalid mark has been entered
  }

Then don't forget to check for an invalid value by checking for 'G'. If one has been entered, handle it by displaying a message and allowing the user to enter an acceptable value.
Re: Help with Assignment 3
May 13, 2008 10:08PM
What about:

if (mark < 0)
  return 'G';
else if(mark <= 39) 
  ... 
else if(mark <= 49) 
  ... 
  ...
  ... 
else if(mark <= 100) 
  ... 
else 
  return 'G';

Also those && seems a bit unnecessary. It's all about looking at it logically. If mark is less than (or equal to) 39, only that part of the if statement will be executed. If it got to the else if(mark <= 49) it means that mark as to be > 39, so to put that in the if statements seems a bit unnecessary.
avatar smileys with beer Re: Help with Assignment 3
May 14, 2008 10:52AM
Good point. I didn't see that one.
smileys with beer Re: Help with Assignment 3
May 14, 2008 01:14PM
Guys thanks

i cannot believe that I was just missing something between the lines
smileys with beer Re: Help with Assignment 3
May 14, 2008 07:45PM
Before and After the characters you mean... or should I say '''
smile
smileys with beer Re: Help with Assignment 3
May 18, 2008 11:14AM
My question 2 of assignment 3 looks a little bit different but it still works.

I am struggling with question 4a,b.c where they ak you to draw a frame I can only get the top , left and bottom frame can any one maby help me wtih this question?eye rolling smiley
smileys with beer Re: Help with Assignment 3
May 18, 2008 05:48PM
Try and do the left and right frame at the same time. It is a bit tricky cause the number of spaces between them isn't always the same, but if you got the spacing between the top and bottom frames right you can figure out the spacing between the left and right sides.

Good Luck.
smileys with beer Re: Re: Help with Assignment 3
May 22, 2008 07:51PM
Hi there

i see you guys was oin the topic of assignment 3 question 2 i have a issue with the 2b bit of the question to write a function of boolean type to verify the symbol for pass or not but mine does not seem to work could someone help please

here is wat i have it just seems to come back with pass the entire time what did i miss


/vraag2 taak 3
#include <iostream>
using namespace std;

//function for symbol return
char symbol(int perc)
{
if (perc >= 80)
return 'A';
if (perc >= 70)
return 'B';
if (perc >= 60)
return 'C';
if (perc >= 50)
return 'D';
if (perc >= 40)
return 'E';
else
return 'F';
}
//passornot function
int passornot(char x)

{
if (x != 'E' || x != 'F'winking smiley
return true;
else
return false;
}
//main program
int main( )
{
int mark;
char symb;

cout << "Enter mark out of 100: ";
cin >> mark;
symb = symbol(mark);
cout << "Symbol corresponding to " << mark << " is " << symb << endl;

if (passornot(symb))

cout << ". You Pass - congratulations!" << endl;
else
cout << "Unfortunately tou Fail." << endl;

return 0;
}
smileys with beer Re: Help with Assignment 3
May 22, 2008 10:18PM
Hi Andre

have a look at the if statement slowly...

For the "or" operator, only one condition need to be true for the statement to be true.
For the "and" operator, both conditions need to be true for the statement to be true.
so your if statement will only be false if x = 'E' and 'F', which is impossible.

My Tip... Try and stay away from not with and/or. It really makes it difficult to read and follow easily.

What I found also to work is to put the not around the complete statement e.g.

IF not(person eq woman or person eq alien)
person is a man
else
person is from venus

easiest still is to through the not away and swap the statements around

if (person eq woman or person eq alien)
person is from venus
else
person is a man
smileys with beer Re: Re: Help with Assignment 3
May 23, 2008 08:02AM
I have tried to swap the statements by taking away the not

but i get a non-lvalue in assignment error

what does that mean
smileys with beer Re: Help with Assignment 3
May 23, 2008 09:34AM
Hi Andres,

It would help if you posted the code, but I expect that you accidentally used = (assignment) where you meant to use == (comparison).

If you are having trouble understanding your if statements, try drawing a truth table:

x | x != 'E' | x != 'F' | (x != 'E' || x != 'F'winking smiley | (x != 'E' && x != 'F'winking smiley
A   true       true        true                    true
B   true       true        true                    true
C   true       true        true                    true
D   true       true        true                    true
E   false      true        true                    false
F   true       false       true                    false

By DeMorgan's laws, (x != 'E' && x != 'F'winking smiley is equal to !(x == 'E' || x == 'F'winking smiley.

You guys did boolean logic, right?
smileys with beer Re: Re: Re: Help with Assignment 3
May 23, 2008 10:44AM
Thanks Gustav
we didnt do demorgans law but it makes perfect sense thanks alot
it realy helps

sometimes you just look past something
avatar smileys with beer Re: Help with Assignment 3
May 23, 2008 02:21PM
Cool, never thought of that

--------------------------------------------------------------
Don't Assume Everything is a Nail Just Because You Have a Really Big Hammer
Sorry, only registered users may post in this forum.

Click here to login