Algorithm, please help

H

HS1

Hello all

I have a vector that store answer objects. Each object has a question number
and answer value that can be presented as following:
-----------------------------------------------

Question number |Answer

3 Y
5 N
8 Y
10 N
.... ..

3 Y
5 N
8 Y
10 N
.. ...

3 N
5 Y
8 Y
10 N
... ..


What I have to do is to determine how many percent answer is "Y" or "N" for
each question.
For example:

question 3: 13% yes and 87% no
question 5: 23% yes and 68% no

It can be seen that those objects are separated into groups and each has
same number of questions. I also have the list of question numbers (e.g: 3,
5, 8, 10...).

What is the best solution for this. Could you please help

Many thanks
SH1

--------------------------------------------------------------
I have this solution but it run many times through the vector of answer
objects

if (i=0; i<vector.size; i++){

int total_answer=0;
int yes =0;
double percent_3;

if (element(i).question_number = 3){
total = total+1;

if (element(i).value = yes) {
yes= yes+1;
}
}
percent = (double) yes/total;
......
}

if (j=0; j<vector.size; j++){
int total_answer=0;
int yes =0;
double percent_5;

if (element(i).question_number = 5){
total = total+1;

if (element(i).value = yes) {
yes= yes+1;
}
}
percent = (double) yes/total;
......
}
......
.....
 
S

Sudsy

HS1 said:
Hello all

I have a vector that store answer objects. Each object has a question number
and answer value that can be presented as following:
<snip>

Part of the joy of programming is discovering how best to approach
a problem. It takes a lot of experience as well as an analytical
mind.
Reaching out to a ng each time you encounter a problem is not going
to help anyone. The op isn't learning when solutions are provided
on a platter and the resident gurus aren't likely to provide a neat
package, wrapped with bows, when the op hasn't even made the slightest
effort to help his/herself.
Either you have the ability to see solutions to obvious problems or
you don't. In the latter case, you're not likely to advance very far
in the industry.

If you (the op) want to test your mettle, I highly recommend the
following site:
<http://www.topcoder.com>
Enter the time-constrained competitions and see how you do. If
nothing else, you'll have the opportunity to see how some "real
smart cookies" approach the problems posed and can learn something
about programming.
Just remember that you'll have others trying to "poke holes" in
your submitted algorithms and the challenges will often succeed
if you don't take into account what we call the "end conditions"!

Heck, it can make you a better programmer! Or perhaps it might
suggest that you find another calling... :-(
 
J

John B. Matthews

"HS1" <[email protected]> said:
Hello all

I have a vector that store answer objects. Each object has a question number
and answer value that can be presented as following:
-----------------------------------------------

Question number |Answer

3 Y
5 N
8 Y
10 N
.... ..

3 Y
5 N
8 Y
10 N
.. ...

3 N
5 Y
8 Y
10 N
... ..

What I have to do is to determine how many percent answer is "Y" or "N" for
each question.
For example:

question 3: 13% yes and 87% no
question 5: 23% yes and 68% no

It can be seen that those objects are separated into groups and each has
same number of questions. I also have the list of question numbers (e.g: 3,
5, 8, 10...).

What is the best solution for this. Could you please help
[...]
I have this solution but it run many times through the vector of answer
objects

Excellent! You have an algorithm, but you have observed that it
requires many passes through the vector to examine each element. You
wonder how to do the work in one pass.

Assume n is the number of distinct question numbers. Instead of a
single variable to total the positive answers, consider an array,
indexed by question number:

int[] yes = new int[n]

Loop through the vector elements, one at a time. When an answer is
yes, increment that question number's entry in the yes array:

yes[questionNumber] += 1;

After a single pass through the vector, yes will have the total
number of yes's for each question number.
 
H

HS1

Thank you for your help
i forget about array

John B. Matthews said:
"HS1" <[email protected]> said:
Hello all

I have a vector that store answer objects. Each object has a question number
and answer value that can be presented as following:
-----------------------------------------------

Question number |Answer

3 Y
5 N
8 Y
10 N
.... ..

3 Y
5 N
8 Y
10 N
.. ...

3 N
5 Y
8 Y
10 N
... ..

What I have to do is to determine how many percent answer is "Y" or "N" for
each question.
For example:

question 3: 13% yes and 87% no
question 5: 23% yes and 68% no

It can be seen that those objects are separated into groups and each has
same number of questions. I also have the list of question numbers (e.g: 3,
5, 8, 10...).

What is the best solution for this. Could you please help
[...]
I have this solution but it run many times through the vector of answer
objects

Excellent! You have an algorithm, but you have observed that it
requires many passes through the vector to examine each element. You
wonder how to do the work in one pass.

Assume n is the number of distinct question numbers. Instead of a
single variable to total the positive answers, consider an array,
indexed by question number:

int[] yes = new int[n]

Loop through the vector elements, one at a time. When an answer is
yes, increment that question number's entry in the yes array:

yes[questionNumber] += 1;

After a single pass through the vector, yes will have the total
number of yes's for each question number.
 
J

John B. Matthews

Sudsy said:
<snip>

Part of the joy of programming is discovering how best to approach
a problem. It takes a lot of experience as well as an analytical
mind.

Reaching out to a ng each time you encounter a problem is not going
to help anyone. The op isn't learning when solutions are provided
on a platter and the resident gurus aren't likely to provide a neat
package, wrapped with bows, when the op hasn't even made the slightest
effort to help his/herself.

Although I offered the OP somewhat more specific guidance, I agree
with you.
Either you have the ability to see solutions to obvious problems or
you don't. In the latter case, you're not likely to advance very far
in the industry.

In particular, one solution called for indexing an array by some
element of the problem, e.g. look-up table, cross-table etc. I find
it a challenge to students in many languages.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Algorithm 1
HELP PLEASE 4
Help please 8
Minimum Total Difficulty 0
Code help please 4
C program: memory leak/ segmentation fault/ memory limit exceeded 0
Quick sort algorithm 1
Code was not Working Please Help 1

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top