Welcome! Log In Create A New Profile

Advanced

Assignment 3

Posted by Icebabe 
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
Re: Assignment 3
July 06, 2006 08:34AM
Valkeye

a real thanks to you, for spending your time to help people like us.

my for loop runs the main function no problem for the sentences, and i have created the bool function almost similar to yours from the text book, but i don't know how to call the bool funct to work as well,
should i use a nested for loop for the bool, or will a single for loop work for both and how do i address the bool funct called isVowel?

thanks
avatar Re: Assignment 3
July 06, 2006 08:41AM
Hi,

First use an if statement which determines if the length is a multiple of 3. You'd also have an else part as well.

In the else part you'll need a for single for loop running through the length of the string.

Within the for loop you'll need an if statement eg. if(isVowel(MyString)) and if it is true you'll need to assign an * to the position of the char in the string.

That should be about it.

-Valkeye
Re: Assignment 3
July 06, 2006 10:24AM
Valkeye

thanks, here is the else part, but its not working and i can't figure out why

please tell me what i'm doin wrong

for (int i = 1; i <= 2; i++) {
Position = Sentence.find(isVowel);
char ch = '*';
if (isVowel(Sentence)) {
while (Position != -1) {
Sentence.replace(Position, Sentence.size(), char ch);
Position = Sentence.find(isVowel);
}
cout << "The new sentence is :" << NewSentence << endl;
avatar Re: Assignment 3
July 06, 2006 10:45AM
Firstly, your for loop should run from 0 to Sentence.size().

I can't see your isVowel function, but if this code compiles then your parameter in the isVowel function is a string, you need to compare a single char.

You don't need to use any of the string functions (eg. find/replace) in this question. Remember that Sentence[a] will return the char of the ath+1 position (because pos 0 is the 1st char).

So, if your for loop runs from 0 to Sentence.size(), in each iteration of the loop, Sentence[a] will return a char from that sentence. Then what you need to do is call the isVowel function on that char, and if that char is a vowel assign an * to Sentence[a].

So, in the else part...

else{
  for(int a = 0; a < Sentence.size(); a++){ // notice < instead of <=
    if(isVowel(Sentence[a]))
      Sentence[a] = '*';
  }
  cout << Sentence << endl;
}
Hope this helps.

-Valkeye
Re: Assignment 3
July 06, 2006 11:12AM
thanks again

it makes more sence, but my prog still won't compile, thinking the bool function is wrong
here it is

bool IsVowel(char ch) {
switch (ch) {
case 'a':
case 'A':
case 'e':
case 'E':
case 'i':
case 'I':
case 'o':
case 'O':
case 'u':
case 'U':
return true;
default:
return false;
}
}

Thanks
avatar Re: Assignment 3
July 06, 2006 11:22AM
the bool function looks fine. I cant see anything wrong with it. What error is it giving you?

Paste your prog in here and I'll give it a quick look and see if I I can find the error.. Also tell me what line is causing the error.

-Valkeye
Re: Assignment 3
July 06, 2006 11:29AM
you are a life saver

the error is on line

48 C:\unisa\COS111\Assign3\Q3.cpp
no match for call to `(std::string) (char&winking smiley'


// Assignment 3 2006 question 3
#include <iostream>
#include <string>
using namespace std;

//IsVowel to determine if parameter is a vowel
bool IsVowel(char ch) {
switch (ch) {
case 'a':
case 'A':
case 'e':
case 'E':
case 'i':
case 'I':
case 'o':
case 'O':
case 'u':
case 'U':
return true;
default:
return false;
}
}

int main() {
string Sentence, NewSentence, FirstThird, MiddleThird, LastThird, isVowel;
int OneThird;

// for loop to run program 5 times

for (int i = 1; i <= 2; i++) {
cout << "Please enter a sentence: ";
getline(cin, Sentence, '\n'winking smiley;


//to determine number of char's = to a third and swop thirds
if((Sentence.size() % 3) == 0) {
OneThird = Sentence.size()/3;
FirstThird = Sentence.substr(0, OneThird);
MiddleThird = Sentence.substr(OneThird, OneThird);
LastThird = Sentence.substr(OneThird*2, OneThird);
NewSentence = LastThird + MiddleThird + FirstThird;

cout << "The new sentence is :" << NewSentence << endl;
}
else {
for (int j = 0; j < Sentence.size(); j++) {
if (isVowel(Sentence[j]))
Sentence[j] = '*';
}
cout << "The new sentence is :" << Sentence << endl;
}


}
return 0;
}
avatar Re: Assignment 3
July 06, 2006 11:47AM
Aha.. You've got a string called isVowel and a function called IsVowel(...). In your if statement you are trying to pass a parameter into a string and the compiler is saying "What the??".

You can delete the reference for the string isVowel and change the if statement to call IsVowel(Sentence[j])

Quite a hidden error if I ever did see one. smiling smiley

-Valkeye
Re: Assignment 3
July 06, 2006 11:56AM
jeeeee haaaaa !!!

You are the "man" or "Woman"

Thanks, thanks,thanks

What i don't get is that the bool function is ,isvowel and in the if statement you said to change it to uppercase I, IsVowel.
this has got me fooled???
avatar Re: Assignment 3
July 06, 2006 12:02PM
In the code you submitted, your function was IsVowel(...), you had a string isVowel, and you made a call to isVowel(Sentence[j])

you needed to delete the isVowel string and make the right call to IsVowel(...), which means in your if statement going to an uppercase I.

-Valkeye
Re: Assignment 3
July 06, 2006 12:14PM
How Stupid is that, i racked my brains out and did'nt see it

Re: Assignment 3
July 06, 2006 03:30PM
Hi Valkeye

on q4 my if statement doesn't seem to put the string in either StartingWithOn or StartingWithOther

can help again please

for (int i = 0; i < 2; i++) {
cout << "Please enter the Sentence: " << endl;
getline(cin, GivenString, '\n'winking smiley;


if(startingWithOn(GivenString))
StartWithOn = GivenString;
else
StartWithOther = GivenString;
avatar Re: Assignment 3
July 06, 2006 03:41PM
I can see a logic problem in your if statement.

If your first iteration starts with on it will go into StartsWithOn[0], if your second iteration doesn't start with on then the string will be stored in StartsWithOther[1].

You now have a situation where the first element in StartsWithOn is at position 0, whilst the first element in StartsWithOther is located at position 1. You need to introduce at least one other variable to keep track of where the program needs to put the string.

Also, I notice that your main for loop only runs twice. Is that just for testing purposes? Remember also that you should never hard code the number of iterations in a for loop, rather use a variable (so say UNISA) even though the extra variable (constant) will make your program more resource hungry, it's probably best to follow their conventions and use a for loop with a const in it instead of just saying i < 2;.

-Valkeye
Re: Assignment 3
July 06, 2006 04:03PM
Thanks Valkeye

you are talking about the for both StartWithOn and StartWithOther right?
If i change StartWithOther to a [j], how will that affect my for loop

The loop only runs twice now for testing, you are saying that I should declare a const say Num = 2 and then the for loop as i < Num?

thanks!
avatar Re: Assignment 3
July 06, 2006 04:14PM
Yup, use a const int NUM like you've got there.

if(startingWithOn(GivenString)) 
  StartWithOn = GivenString; 
else 
  StartWithOther = GivenString;

You could introduce 1 other variable, I wouldn't call it j though. Personal preference, but I reserve single letter variable names for loops, wheras in this case you could use i to iterate through the loop, and another variable, say, Without, initialised to 0; When a sentence starts with on, you can store it at StartsWithOn[i-Without], if it doesn't you can store at StartsWithOther[Without], and then increment Without.

eg.


int Without = 0;

if(startingWithOn(GivenString)) 
  StartWithOn[i-Without] = GivenString; 
else{ 
  StartWithOther[Without] = GivenString;
  Without++;
}

This way takes a bit more effort to understand, so you could just introduce 2 extra variable, With and Without, initialise them both to 0 and use and increment them each iteration. That way you wont use i at all in your assignments.

-Valkeye
avatar Re: Assignment 3
July 06, 2006 05:30PM
Hey Valkeye

It's not UNISA's idea to use constants instead of literals, it's good programming practice. You can have many loops logically having the same number of loops (e.g. for (int i = 0; i < MAX_CHARACTERS; ++i)). If at some point in the future, you need to change the value, you only have to change it in one place. If you had hard-coded the value, you could possibly miss one and cause a bug. Another reason for using constants is that you increase the readability of your programme. Consider the following:
float profit[[color=red]12[/color]];

for (int i = 0; i < 12; ++i)
  {
  profit = income - expenses;
  }
Compared with:
enum Months {Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec, MonthsInYear = Dec};

float profit[MonthsInYear];

for (int month = Jan; month < MonthsInYear; ++month)
  {
  profit[month] = income[month] - expenses[month];
  }
Note also in the second version that there isn't a single literal.

I try and minimise literals in my programmes as much as possible, I'm only human so every now and again a literal sneaks through the cracks. That's why I set my IDE to show literals in red.

Another thing, there is no extra burden to using constants. The two different methods are compiled in exactly the same way. All constants are substituted with their respective values so it's as if you never had constants at all.
avatar Re: Assignment 3
July 06, 2006 06:56PM
That I never knew. But the way I see it is you declare a constant.. All fine and good. Your computer assigns it some RAM and we're all good to go....

But.. you could be in a position where you've got 20 or so different classes active at the same time, for extended periods of time, each with four constants, now you've got 80 references to variables which are being held in RAM.

I definitaly agree that using constants makes your code a lot more readable and easier to change. There is no doubt about that. And when using constants you are less likely to miss out something causing a bug.

But 80 integer constants in RAM is 80*32bit = 320bytes on its own.... Although thinking about it now.. That's not that much. I've just got an oversion to keeping things in RAM that don't need to be there. Makes programs a lot more resource hungry.

When using a constant it is active and held in RAM while the class or file is active. When using a literal it only comes into being for the duration of the loop and is dereferenced straight afterwards... Or am I smoking something strong?? smiling smiley

But finally to look at your example. In the case that you've provided you could get away without using a literal or constant in your for loop simply by using the Array.size() feature....

-Valkeye
avatar Re: Assignment 3
July 06, 2006 08:11PM
You've made the assumption that a constant is a variable. This is wrong. Constants are only around for the code. As I said before, the compiler substitutes them out so the executable code only has literals in it. If you reverse-compiled your executable, it would be full of literals where your constants used to be. That's what I meant when I said there's no difference. 80 integer constants will take up as much space in RAM as 80 integer literals. A similar principle is used when inlining functions.

If you view the disassembled versions of the above two for loops you will see that they are identical (actually I made an error in the values of the enumeration but that's beside the point). Here are the two assembly versions of the headers of the two for loops

The one with the literal 12:
	for (int i = 0; i < 12; ++i)
		{
0042734E  mov         dword ptr ,0 
00427355  jmp         main+30h (427360h) 
00427357  mov         eax,dword ptr  
0042735A  add         eax,1 
0042735D  mov         dword ptr ,eax 
00427360  cmp         dword ptr ,0Ch 
00427364  jge         main+53h (427383h) 

and the one with the enumeration constant:
	for (int month = Jan; month < MonthsInYear; ++month)
		{
0042734E  mov         dword ptr [month],0 
00427355  jmp         main+30h (427360h) 
00427357  mov         eax,dword ptr [month] 
0042735A  add         eax,1 
0042735D  mov         dword ptr [month],eax 
00427360  cmp         dword ptr [month],0Bh 
00427364  jge         main+53h (427383h)

Note that in the first line of each version, that 0 is being moved into the respective variable (Jan no longer exists). and in the second last line of each, the respective variable is being compared with a literal; the first with 12 (C in hex) and the second with 11 (B in hex - this is where my error is). Once again MonthsInYear no longer exists.

This is what happens with ALL constants, they are turned into literals during compilation.

Another thing, don't use functions in your tests (Array.size()). This will add unnecessary processing(jumping to another function in each iteration) to your programme unless you expect the value to change during the loop. Rather create a temporary variable to hold the value and then test that temporary variable. e.g.
int size = Array.size();
for (int i = 0; i < size; ++i)
  {
  // some code
  }
avatar Re: Assignment 3
July 06, 2006 09:49PM
OK.. I see. Thanks for the pointers... Always cool to learn from someone who's clearly had more experience with it than I have.

I've actually never thought of the extra CPU time required for using an Array.size() in a loop, but it makes so much sence!!

Thanks again. smiling smiley

-Valkeye
Re: Assignment 3
July 06, 2006 10:42PM
Hi all,,,,,,,,does anyone perhaps have some full solutions to this assignmeent for me........Im not goingto submitt it but I'd just like to learn from the correct answers. If you do then please would you e-mail it to tnurick@mailbox.co.za

id really appreciate it thanks..its just i have really battled with this assignment but im keen to pass the exam
Sorry, only registered users may post in this forum.

Click here to login