I'm having some trouble with this averaging program. This program is supposed to determine the final average by taking the sum of the products of the scores, multiplied by the weights and dividing by the sum of the weights. The output of the program includes each score-weight combination retrieved, the number of score-weight combinations returned, the sum of the products, the sum of the weights, the average, and the letter grade that would be issued given the average.

here is what i have so far...:?:

``````#include<iomanip>
#include<iostream>
//#include<conio>
using namespace std;

bool getdata(int &,int &);

int main(int argc, char* argv[])
{
int score, weight;
double sumProduct
sumWeight,   // The sum of the weights
average;     // the Semester average= sumProduct/sumWeight
char grade;
randomize();

//           getch();
return 0;
}

bool getdata(int &score,int &weight)
{
static int final =rand()%30+20;
score=rand()%100+1;
weight=rand()%100+1;
final--;
if(final >0)
{
if (weight < 25)
weight=10;
else
weight=5;

return true;
}
else
{
weight=25;
return false;
}
}
//---------------------------------------------------------------------------``````

thanks!

Hmm...when you say you are having problem what does it actually mean ?

Some more things, if your program wants to calculate the average, where is the array which holds the values or are you accepting input from a file ?

In your get_data( ) function you have :

final = static int which has value between 29 and 49.
What is the significance of this in the algorithm ?

Also you are not seeding the random number so your random numbers would be repetitive and not quite random in nature as you would expect them to be.

Thanks for replying and I uh dont know what im doing. This is my first semester in c++. I'm not even sure what an array is.

Paste your entire question as it is word to word and then maybe I would be able to help you out because your description is rather vague.

And btw if you don't know what you are doing, how come did you write his code ?

output should look like this:

Scores:
43 58 100 79 100 100 93 100 82 100 100 46 100 100 75 97 82 85 89 100 61 100 100 86 100

Weights:
5 5 5 10 5 5 5 5 5 10 10 10 5 10 5 5 5 5 10 25

the number of scores 25
sum of the product 15920.00
Sum of hte Weights 180.00
The semester Average 88.44
The Final Grade B

But the above problem can't be solved without arrays if you want to store the scores for later display.

Maybe you should brush up a bit on arrays from your text book and then try to attempt the problem.

How do i make this loop and look like this? The program is supposed to retrieve values for 'score and 'weight' from a funtion, getdata.. The function returns a bool data type. If the value returned is true values for score and weight are updated and there are more values to retrieve. If the function returns a false, the 'final exam' score and weight are returned and the function should not be called again. Note: Each time the program runs the data retrieved will be different as will the answers.

i dont know how to loop the score and weight values... thanks for helping.

``````#include <vcl.h>
#pragma hdrstop
#include<iomanip>
#include<iostream>
#include<stdlib>
//#include<conio>    //Leave this comment line in
using namespace std;

bool getdata(int &,int &);

int main(int argc, char* argv[])
{
int score, weight;
double sumProduct,   // The sum of the products of the weight and the score;
sumWeight,   // The sum of the weights
average;     // the Semester average= sumProduct/sumWeight
char grade;          //letter grade based on the above average
randomize();

bool next=getdata(score,weight);
//           getch(); //leave this comment line in
while (next != false)
{
cout << score << " " << weight << endl;

cin.ignore();
//}
//else
//{

}
return 0;
cout << "the number of scores: " << score << endl;
}

// beginning of getdata function
//Do not modify this function
bool getdata(int &score,int &weight)
{
static int final =rand()%30+20;
score=rand()%100+1;
weight=rand()%100+1;
final--;
if(final >0)
{
if (weight < 25)
weight=10;
else
weight=5;

return true;
}
else
{
weight=25;
return false;

//cout << "Sum of the Product: " <<
}

}
//---------------------------------------------------------------------------``````

I again repeat myself:

In order to get the output of the form given by you, arrays are a must.

Scores:
43 58 100 79 100 100 93 100 82 100 100 46 100 100 75 97 82 85 89 100 61 100 100 86 100

Weights:
5 5 5 10 5 5 5 5 5 10 10 10 5 10 5 5 5 5 10 25

Without arrays you won't be able to display the records in the given format and finding out the grade would be really difficult.

i have given up on the array idea

Yeah, I have given up on the array idea now. Because i don't know how to do an array. This program is supposed to just randomly output scores, but i can only get it to do that once. Do you know how i can loop the weights and scores? is this correct?

``while ( next != false)``

it is supposed to look like this:

i have given up on the array idea

Listen to what ~s.o.s~ said. Use arrays! If you can't figure out how to use arrays, avoiding it isn't going to help. Even if you do manage to avoid it now, what is going to save you when you are forced to use them?

Quite simply, here is a (random) example of entering data (haven't compiled the code or anything, so there could be errors):

``````int main() {

int array[40]; // this array holds 40 elements

for (int i=0; i<40; i++) { // loop 40 times; 'i' iterates

cout << "\nEnter int: ";
cin >> array[i]; // store data in current spot
cout << endl;
}

// data is now stored

// print out data
for (int i=0; i<40; i++) {

// print "Data for [0] is 50"
//       "Data for [1] is 43", etc
cout << "Data for [" << i << "] is " << array[i] << endl;
}
}``````

Once you've figured out why arrays are useful, and how to use/access them, then it should be easy to find weighted data and print out scores.

Hope this helps

Working with these lists of number is most easily done with an array. I think (if I understand this assignment correctly) that it could be done without them, but in a very cumbersome, roundabout manner. Here's 2 ways to do this:

- Use arrays. Take your input, put all of the scores into one array and all of the weights into another. Although you've demonstrated a disinterest in learning new things, you might want to look into using the `std::vector` class, which is essentially a dynamically sized array. After you've gotten all the input, you can calculate the sum of each list, and do the math you need to do with that. You can also print the lists and results from the array's you have and the math you've just done. That's about it.

- If you truly refuse to use arrays, you can input the data in pairs (e.g. "Enter a score: " followed by "Now enter a weight: "). For each one, add it to a string for that row, that is, one for scores and one for weights. Also add the value to a sum for each row. At the end you'll still have the sums and the strings you need to output, and you can proceed to do whatever math you need to do. This is IMHO a less elegant solution, but should still get the job done...

this is too hard. Can i pay someone to do this? Through paypal or something.

naw, save your money!
You have all the qualifications for a long career in the fast food industry. LOL

yeah, LOL

sladkjflsadkfj;saldfj crap

cout << "shoot me in the face" <<

I hate it when people thing that money can get them through whatever problems are in front of them... that said, I'll do it for \$200, 1/2 of it up front (after I go sign up for paypal)...

yeah, LOL

sladkjflsadkfj;saldfj crap

cout << "shoot me in the face" <<

If you understand what you're doing, you're not learning anything. Programming is like that. We all struggle to learn new concepts, and I think that it's the satisfaction in overcoming these concepts that makes programming worthwhile. It's a challenge.

I hate it when people thing that money can get them through whatever problems are in front of them... that said, I'll do it for \$200, 1/2 of it up front (after I go sign up for paypal)...

LOL. I wouldn't mind doing it except I'd have this guilty feeling that I shouldn't be doing it :o...

:cheesy:

hahaha, 200?

i will just take a zero. I think i can figure it out, its just that it's due tomorrow.

I have got the thing to loop but i cant get it to stop looping and run the rest of the program. im stuck.

wanna post the code you have so far?

``````#include <vcl.h>
#pragma hdrstop
#include<iomanip>
#include<iostream>
#include<stdlib>
//#include<conio>    //Leave this comment line in
using namespace std;

bool getdata(int &,int &);

int main(int argc, char* argv[])
{
int score, weight;
double sumProduct,   // The sum of the products of the weight and the score;
sumWeight,   // The sum of the weights
average;     // the Semester average= sumProduct/sumWeight
char grade;          //letter grade based on the above average
randomize();

bool next=getdata(score,weight);
//getch(); //leave this comment line in
do
{
cout << score << " " << weight << endl;

//cin.ignore();

}   while(next != false);
next = true;
cout << endl;

/*cout << "Sum of the Product: ";
cout << "Sum of the Weights: ";
cout << "The Semester Average: ";
cout << "The Final Grades: "; */

return 0;

}

// beginning of getdata function
//Do not modify this function
bool getdata(int &score,int &weight)
{
static int final =rand()%30+20;
score=rand()%100+1;
weight=rand()%100+1;
final--;
if(final >0)
{
if (weight < 25)
weight=10;
else
weight=5;

return true;
}
else
{
weight=25;
return false;

}

}
``````

mostly this part:

``````  bool next=getdata(score,weight);
//getch(); //leave this comment line in
do
{
cout << score << " " << weight << endl;

//cin.ignore();

}   while(next != false);
next = true;
cout << endl;

/*cout << "Sum of the Product: ";
cout << "Sum of the Weights: ";
cout << "The Semester Average: ";
cout << "The Final Grades: "; */
``````

its supposed to look like this in the end
http://img511.imageshack.us/my.php?image=finalimageip8.jpg

So, I was busy studying for my own finals, but now that I look at this, you never change the value of next in the loop, so it'll never exit the loop if next is true.

So, I was busy studying for my own finals, but now that I look at this, you never change the value of next in the loop, so it'll never exit the loop if next is true.

thanks for helping!

what should i change the value to? I'm not sure what to do.

i have not change much, but this is what it looks like now:

``````bool next=getdata(score,weight);
{
if (next != false)
{
cout << score << " " << weight << endl;
cin.ignore();
}
else
{
cout << "Sum of the Product: ";
cout << "Sum of the Weights: ";
cout << "The Semester Average: ";
cout << "The Final Grades: ";

return 0;

}
``````
``````bool next=getdata(score,weight);
{
if (next != false)``````

Remove the bracket marked in red.

just looking through it quickly (sorry I'm not putting much time into it), I think you want something like this:

``````bool next = getdata(score, weight);
while(next)
{
cout << score << " " << weight << endl;
next = getdata(score, weight);
}``````

Just a guess though

hahah, that did it. THANKS ALOT MAN!!

now how would i find the sum of the weights and the scores?

now how would i find the sum of the weights and the scores?

Easy. I'm not going to use the same variables you're using, partly because I didn't really look at your code, and partly because want you to solve it. But here is the idea:

``````for (int i=0; i < Number_of_scores; i++ ) {

total_weight += weights[i];
total_scores += scores[i];
}``````

Hope you get my idea. Remember to initalize total_weight and total_scores to 0 before attempting a loop like this, and you should be fine.

Hope this helps

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.