944,124 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 5579
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Dec 4th, 2006
0

help...one-dimensional array averaging program

Expand Post »
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...


C++ Syntax (Toggle Plain Text)
  1. #include<iomanip>
  2. #include<iostream>
  3. //#include<conio>
  4. using namespace std;
  5.  
  6. bool getdata(int &,int &);
  7.  
  8. int main(int argc, char* argv[])
  9. {
  10. int score, weight;
  11. double sumProduct
  12. sumWeight, // The sum of the weights
  13. average; // the Semester average= sumProduct/sumWeight
  14. char grade;
  15. randomize();
  16.  
  17.  
  18. // getch();
  19. return 0;
  20. }
  21.  
  22.  
  23. bool getdata(int &score,int &weight)
  24. {
  25. static int final =rand()%30+20;
  26. score=rand()%100+1;
  27. weight=rand()%100+1;
  28. final--;
  29. if(final >0)
  30. {
  31. if (weight < 25)
  32. weight=10;
  33. else
  34. weight=5;
  35.  
  36. return true;
  37. }
  38. else
  39. {
  40. weight=25;
  41. return false;
  42. }
  43. }
  44. //---------------------------------------------------------------------------

thanks!
Last edited by ~s.o.s~; Dec 4th, 2006 at 1:07 pm. Reason: Please use [code=cplusplus] [/code] tags while posting code
Reputation Points: 10
Solved Threads: 0
Newbie Poster
elcrapo is offline Offline
14 posts
since Nov 2006
Dec 4th, 2006
0

Re: help...one-dimensional array averaging program

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.
Super Moderator
Featured Poster
Reputation Points: 3241
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,873 posts
since Jun 2006
Dec 4th, 2006
0

Re: help...one-dimensional array averaging program

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.
Last edited by elcrapo; Dec 4th, 2006 at 1:33 pm.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
elcrapo is offline Offline
14 posts
since Nov 2006
Dec 4th, 2006
0

Re: help...one-dimensional array averaging program

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 ?
Super Moderator
Featured Poster
Reputation Points: 3241
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,873 posts
since Jun 2006
Dec 4th, 2006
0

Re: help...one-dimensional array averaging program

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
elcrapo is offline Offline
14 posts
since Nov 2006
Dec 4th, 2006
0

Re: help...one-dimensional array averaging program

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.
Super Moderator
Featured Poster
Reputation Points: 3241
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,873 posts
since Jun 2006
Dec 6th, 2006
0

Re: help...one-dimensional array averaging program

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.

http://img511.imageshack.us/img511/5...mageip8.th.jpg

[php]#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: " <<
}


}
//---------------------------------------------------------------------------[/php]
Last edited by elcrapo; Dec 6th, 2006 at 2:28 pm.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
elcrapo is offline Offline
14 posts
since Nov 2006
Dec 6th, 2006
0

Re: help...one-dimensional array averaging program

I again repeat myself:

In order to get the output of the form given by you, arrays are a must.
Quote ...
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.
Super Moderator
Featured Poster
Reputation Points: 3241
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,873 posts
since Jun 2006
Dec 6th, 2006
0

Re: help...one-dimensional array averaging program

i have given up on the array idea
Last edited by elcrapo; Dec 6th, 2006 at 2:54 pm.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
elcrapo is offline Offline
14 posts
since Nov 2006
Dec 6th, 2006
0

Re: help...one-dimensional array averaging program

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? [php]while ( next != false)[/php]

it is supposed to look like this:

http://img511.imageshack.us/my.php?i...alimageip8.jpg
Last edited by elcrapo; Dec 6th, 2006 at 2:58 pm.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
elcrapo is offline Offline
14 posts
since Nov 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Recursive Insertion on Linked Lists
Next Thread in C++ Forum Timeline: Output problems





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC