C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

problem finding the average

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
ashmangat@yahoo.com
Guest





PostPosted: Tue Nov 22, 2005 11:27 am    Post subject: problem finding the average Reply with quote



I'm trying to claculate the average number of characters per words but
having some problems
Every time i run this program, i get 0 as the average
#include <iostream>
#include <string>
#include <iomanip>
#include <ctype.h>
#include <stdlib.h>
using namespace std;
void avgg(char[]);
int wordCount(char []);
int main()
{
char cstring[81];
int index;
cout << "Written by Arshdeep Kaur, Cs102 onlinen";
cout << "nEnter a string, 80 or fewer characters:n";
cin.getline(cstring, 81);
cout << "nThe number of words in that string: ";
index = wordCount(cstring);
cout << index << endl;
avgg(cstring);


return 0;


}


int wordCount(char cstring[])
{
int index = 0;
int word = 0;
while (cstring[index] != '')
{
if (isspace(cstring[index]))
{
while (isspace(cstring[index]))
{
index++;
}
}
if (isalnum(cstring[index]))
{
word++;
while(islower(cstring[index]))
{
index++;
}
while((isalnum(cstring[index])) || (
ispunct(cstring[index])))
{
index++;
}
}
index++;
}
return word;

}


void avgg( char cstring[])
{
int index = 0, avg = 0;
int word = 0;
if(cstring[word] != '')
{
while (cstring[word] != '')
{
if (isspace(cstring[word]))
{
while (isspace(cstring[word]))
{
word++;
}
}
if (isalnum(cstring[word]))
{
index++;
while(islower(cstring[word]))
{
word++;
}
while((isalnum(cstring[word])) || (
ispunct(cstring[word])))
{
word++;
}
}
word++;
}
float Total = 0.0;
Total += index; // Keep a running total
avg = Total / word;
cout << "Average: " << avg << endl;
}

}


****Can anyone find the error


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Paul Floyd
Guest





PostPosted: Wed Nov 23, 2005 1:48 pm    Post subject: Re: problem finding the average Reply with quote



On 22 Nov 2005 06:27:41 -0500, [email]ashmangat (AT) yahoo (DOT) com[/email] <ashmangat (AT) yahoo (DOT) com> wrote:


Quote:
cout << "Written by Arshdeep Kaur, Cs102 onlinen";

If you must post your assignments, you could try a little harder to
disguise them.

A bientot
Paul
--
Paul Floyd http://paulf.free.fr (for what it's worth)
Surgery: ennobled Gerald.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
ashmangat@yahoo.com
Guest





PostPosted: Thu Nov 24, 2005 8:11 am    Post subject: Re: problem finding the average Reply with quote



Runs fine until I input puntuation.
#include <iostream>
#include <string>
#include <iomanip>
#include <cctype>
#include <stdlib.h>
using namespace std;
void avgg(char[]);
int wordCount(char []);
int main()
{
char cstring[81];
cout << "nEnter a string, 80 or fewer characters:n";
cin.getline(cstring, 81);
cout << "nThe number of words in that string: ";
cout < avgg(cstring);


return 0;


}


int wordCount(char cstring[])
{
int index = 0;
int word = 0;
while (cstring[index] != '')
{
if (isspace(cstring[index]))
{
while (isspace(cstring[index]))
{
index++;
}
}
if (isalnum(cstring[index]))
{
word++;
while(islower(cstring[index]))
{
index++;
}
while((isalnum(cstring[index])) || (
ispunct(cstring[index])))
{
index++;
}
}
index++;
}
return word;

}


void avgg( char cstring[])
{
int index = 0;
double avg = 0;
int spaces;
int wordd = 0;
double total = 0.0;

while (cstring[index] != '')
{
if (isalnum(cstring[index]))
{ index++;
while(islower(cstring[index]))
{
index++;
}
while((ispunct(cstring[index])))
{
index++;
}
}
if (isspace(cstring[index]))
{
while (isspace(cstring[index]))
{
index++;
}
}
if ((ispunct(cstring[index])))
{
index++;
}


wordd++;
spaces = wordd - 1 ;
total = index-spaces;
avg = total/wordd;
}
cout << "Spaces: " << spaces <<"n"<< "Index: " <<
index << " n"
<<"Total: " << total << endl;
cout << "Average: " << fixed << showpoint <<
setprecision(2) << avg
<< endl;



}


I know my avg function is long, i trying to make it in a way so I can
delete the wordcount function and have only one funtion(avg).


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Vince Morgan
Guest





PostPosted: Thu Nov 24, 2005 8:11 am    Post subject: Re: problem finding the average Reply with quote

<ashmangat (AT) yahoo (DOT) com> wrote

Quote:
I'm trying to claculate the average number of characters per words but
having some problems
Every time i run this program, i get 0 as the average
#include #include #include #include #include using namespace std;
void avgg(char[]);
int wordCount(char []);
int main()
{
char cstring[81];
int index;
cout << "Written by Arshdeep Kaur, Cs102 onlinen";
cout << "nEnter a string, 80 or fewer characters:n";
cin.getline(cstring, 81);
cout << "nThe number of words in that string: ";
index = wordCount(cstring);
cout << index << endl;
avgg(cstring);


return 0;


}


int wordCount(char cstring[])
{
int index = 0;
int word = 0;
while (cstring[index] != '')
{
if (isspace(cstring[index]))
{
while (isspace(cstring[index]))
{
index++;
}
}
if (isalnum(cstring[index]))
{
word++;
while(islower(cstring[index]))
{
index++;
}
while((isalnum(cstring[index])) || (
ispunct(cstring[index])))
{
index++;
}
}
index++;
}
return word;

}


void avgg( char cstring[])
{
int index = 0, avg = 0;
int word = 0;
if(cstring[word] != '')
{
while (cstring[word] != '')
{
if (isspace(cstring[word]))
{
while (isspace(cstring[word]))
{
word++;
}
}
if (isalnum(cstring[word]))
{
index++;
while(islower(cstring[word]))
{
word++;
}
while((isalnum(cstring[word])) || (
ispunct(cstring[word])))
{
word++;
}
}
word++;
}
float Total = 0.0;
Total += index; // Keep a running total

'index' is never touched from it's declaration to this point.
Total therefore will always be 0.

Quote:
avg = Total / word;


Quote:
cout << "Average: " << avg << endl;
}

}


****Can anyone find the error

I think you need to now step back a little and consider restructuring your
code.
Ask yourself a couple of questions, such as "Wouldn't it be better if all
printing cout's were handled in main()?" I believe it should.
"What values do I actualy need to determine the avg number of chars in each
word" I beleive that would be, the total number of alphabetic charsnumber
of words.
"How do I determine the number of words?" The easy way is to count the
spaces, but what if there are other (numbers or punct) chars sepperated by
spaces?

So, what is the definition of a word? (I think someone else offered this,
perhaps all of this, in a previous post) Personaly, I would think it were a
string of alphabetical chars deliminated by non alphabetical chars. This
would exclude hyphenated words, as they would be determined by that method
as two distinct words. But, does that matter in this case? Only you know.

Once you answer these questions of yourself you are ready to begin
restructuring your code.

Your 'cout's' are fine, though I would put _all_ of them in main() to keep
it more rational.
As it is avgg() looks like the twin of wordCount() with a little extra
fucntionality tacked on.
I suspect that you put a 'cout' into avgg() as this is where you have the
values you need at hand. However, I think it would be better to pass other
vars by reference to such a fuct and then use those vals in main() That way
you don't have to determine them all again in avgg().

I beleive avgg() should be very simple, and that the vals it requires should
have been returned previously to main() via a referenced var (or vars)
passed previously to wordCount().
Something like;

int avgg(int &words, int &totalAlphabeticalChars)
{
int val = totalAlphabeticalCharswords;
return val;
}

Perhaps a referenced int that is given it's value in wordCount() would be
enough. In fact, this function (avgg()) becomes so simple that you could
just as easily discard it altogether if you wished and simply do the
operations in main().
--
HTH
Vince Morgan
Remove UNSPAM
[email]vinhar (AT) UNSPAMoptusnet (DOT) com.au[/email]



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
ashmangat@yahoo.com
Guest





PostPosted: Sat Nov 26, 2005 2:28 pm    Post subject: Re: problem finding the average Reply with quote

This program runs absolutely fine, I was thinking If could have all the
cout statements in the main, but I don't know how I can return more
that one variable to main.
#include <iostream>
#include <iomanip>
#include <cctype>
#include <stdlib.h>
using namespace std;
int wordCount(char []);
int main()
{
int word=0, avg=0;
char cstring[81];

cout << "nEnter a string, 80 or fewer characters:n";
cin.getline(cstring, 81);
word = wordCount(cstring);
cout << "nThe number of words in that string: ";
cout << word << endl;
cout << "average again" << avg << endl;
return 0;
}
int wordCount(char cstring[])
{
int index = 0;
double avg;
double characters;
int word = 0;
int ff = 0;
int fff = 0;
int add = 0;
while (cstring[index] != '')
{
if (isspace(cstring[index]))
{
ff++;
while (isspace(cstring[index]))
{
index++;
}
}
if( ispunct(cstring[index]))
{
fff++;
while(ispunct(cstring[index]))
{
index++;
}
}
if ((isalnum(cstring[index])))
{
word++;
while(isalnum(cstring[index]))
{
index++;
}
}
add++;
}
if( index > 0)
{
cout << "add: " << add << endl;
characters = index-(fff + ff);
avg = (characters / word);
cout << "characters: " << characters << "n" << "Index: " << index
<< "n" << "fff: " << fff << "n" << "ff: " << ff << endl;
cout << "Average: " << fixed << showpoint << setprecision(2) <<
avg << endl;
}
return word; //***** want to return 'fff' and 'index' also, so
I can do all the clacultation in the main or a new funtion
}


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
ashmangat@yahoo.com
Guest





PostPosted: Sat Nov 26, 2005 3:00 pm    Post subject: Re: problem finding the average Reply with quote

Thanks for the help My program finally runs perfectly, I shrunk it as
much as possible and simpyfied it as much as possible
Here is My finished code below:
#include <iostream>
#include <iomanip>
#include <cctype>
#include <cstdlib>
using namespace std;
void wordCount(char [], int&, double&, int&);
double get_avg(int, int, double);
int main()
{
int word = 0, Punct = 0;
double AllChar = 0.0;
double avg = 0.0;
char cstring[81];
cout << "Enter a string of 80 or fewer characters" << endl;
cout << "=> ";
cin.getline(cstring, 81);
wordCount(cstring, word, AllChar, Punct);
cout <<"nThe number of words in that string: " << word << endl;
avg = get_avg(word, Punct, AllChar);
cout <<"nAverage number of characters per word: "<< fixed <<
showpoint << setprecision(2) << avg << "n" << endl;
return 0;
}
// wordCount() counts total character, words, and Puctuation
void wordCount(char cstring[],int &word, double &AllChar, int &Punct)
{
int index = 0;

while (cstring[index] != '')
{
if ((isspace(cstring[index])) || (
ispunct(cstring[index])))
{
while((isspace(cstring[index])) || (
ispunct(cstring[index])))
{
index++;
}
}

if ((isalnum(cstring[index])) || (ispunct(cstring[index])))
{
word++;
while
((isalnum(cstring[index]))||(ispunct(cstring[index])))
{
index++;
AllChar++; //Counting total printable character(including digits
and Punctuation
if((ispunct(cstring[index])))
{
Punct++; // Counting Punctuation
}

}
}
index++;
}
}
// get_avg() calculates the average number of characters per words
double get_avg(int word,int Punct, double AllChar )
{
double avg = 0.0;
AllChar = AllChar - Punct; // Subtracting Punctuatoin from All the
characters in the string(not including spaces)
avg = (AllChar / word);
return avg;
}

Thanks
----
Ash


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Ron Natalie
Guest





PostPosted: Sun Nov 27, 2005 10:11 pm    Post subject: Re: problem finding the average Reply with quote

[email]ashmangat (AT) yahoo (DOT) com[/email] wrote:
Quote:
This program runs absolutely fine, I was thinking If could have all the
cout statements in the main, but I don't know how I can return more
that one variable to main.

You can only return one thing from a C++ function. Your choices are
to either put all the values in a class and return that or pass in
pointers or references to all the values.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
int2str@gmail.com
Guest





PostPosted: Mon Nov 28, 2005 3:52 pm    Post subject: Re: problem finding the average Reply with quote


[email]ashmangat (AT) yahoo (DOT) com[/email] wrote:
Quote:
Thanks for the help My program finally runs perfectly, I shrunk it as
much as possible and simpyfied it as much as possible
Here is My finished code below:

Perfectly is always relative Very Happy
See comments below.

Quote:
#include <iostream
#include #include #include

You fixed your header usage, good!

Quote:
using namespace std;

You are allowed to have empty lines to break your code up a little, you
know...

Quote:
void wordCount(char [], int&, double&, int&);
double get_avg(int, int, double);

"get_avg" is misleading. This is not likely a function that you cen
reuse anywhere, so there's no real benefit in making this a function at
all. Naming it get_avg() is additionally misleading.

Quote:
int main()
{
int word = 0, Punct = 0;
double AllChar = 0.0;
double avg = 0.0;

Interesting (read: random) mix of capitalization here. Pick one and
stick to it.

Quote:
char cstring[81];
cout << "Enter a string of 80 or fewer characters" << endl;
cout << "=> ";
cin.getline(cstring, 81);

Magic numbers everywhere.... Declare a buffer size somewhere and use
that everywhere instead of the hard coded values.

Quote:
wordCount(cstring, word, AllChar, Punct);
cout <<"nThe number of words in that string: " << word << endl;
avg = get_avg(word, Punct, AllChar);
cout <<"nAverage number of characters per word: "<< fixed
showpoint << setprecision(2) << avg << "n" << endl;
return 0;
}
// wordCount() counts total character, words, and Puctuation

This is not a very complete comment. Which parameters are input, which
output, what are the pre and post conditions for this function? You may
want to mention for example that cstring must be null terminated....

Quote:
void wordCount(char cstring[],int &word, double &AllChar, int &Punct)
{
int index = 0;

while (cstring[index] != '')

wordCount( NULL,...) would cause a segmentation fault here....

Quote:
{

At this point it would be good idea to alias the current character to
make the code more readable. Something like this comes to mind:

char *ch = cstring + index;

In fact, you can just use pointer arithmetic from there on out then
instead of using the index variable.

Quote:
if ((isspace(cstring[index])) || (
ispunct(cstring[index])))
{
while((isspace(cstring[index])) || (
ispunct(cstring[index])))
{
index++;
}
}

I'm not quite sure why you do the if check here before you do the
while. But then again, I'm not even sure why you make it so complicated
anyway. You're really interested in alpha numeric characters anyway, so
you might as well explicitly state it this way.

Also, a comment would really help in this part of the code to say what
the heck you're trying to do (skip leading whitespace and punctuation).

Quote:

if ((isalnum(cstring[index])) || (ispunct(cstring[index])))
{
word++;
while
((isalnum(cstring[index]))||(ispunct(cstring[index])))
{
index++;
AllChar++; //Counting total printable character(including digits
and Punctuation
if((ispunct(cstring[index])))
{
Punct++; // Counting Punctuation
}

}
}
index++;
}
}

Again, the rest of the function is needlessly complicated and not self
explanatory. You dont need to count punctuation for example.

Quote:
// get_avg() calculates the average number of characters per words

This comment explains why this function should not be called get_avg()
:D

Quote:
double get_avg(int word,int Punct, double AllChar )
{
double avg = 0.0;
AllChar = AllChar - Punct; // Subtracting Punctuatoin from All the
characters in the string(not including spaces)
avg = (AllChar / word);

Unchecked division by zero....

Quote:
return avg;
}

Compacting your code should not mean removing empty lines. Instead you
should try to simplify the logic, not for the sake of less lines of
code only, but more to make it easier to understand.

Cheers,
Andre


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Carl Barron
Guest





PostPosted: Tue Nov 29, 2005 10:18 am    Post subject: Re: problem finding the average Reply with quote

In article <438a110a$0$28464$9a6e19ea (AT) news (DOT) newshosting.com>, Ron
Natalie <ron (AT) spamcop (DOT) net> wrote:

Quote:
ashmangat (AT) yahoo (DOT) com wrote:
This program runs absolutely fine, I was thinking If could have all the
cout statements in the main, but I don't know how I can return more
that one variable to main.

You can only return one thing from a C++ function. Your choices are
to either put all the values in a class and return that or pass in
pointers or references to all the values.

or use boost tuple library then the code only needs to change the

return to return a tuple and the calling routine use a tie to store the
results in its variables such as
boost::tuple<double,int> count_important(/* current args */)
{
double chars;
int words;
/* count them as you please */
return boost::tuple<double,int>(chars,words);
}

void foo()
{
double chars;
int words;
// ...
boost::tie(chars,words) = count_important(/* args ommitted */);
};

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.