| | |
help...one-dimensional array averaging program
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Nov 2006
Posts: 14
Reputation:
Solved Threads: 0
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...
thanks!
here is what i have so far...

C++ Syntax (Toggle Plain Text)
#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!
Last edited by ~s.o.s~; Dec 4th, 2006 at 1:07 pm. Reason: Please use [code=cplusplus] [/code] tags while posting code
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.
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.
The romantic image of an über-programmer is someone who fires up Emacs, types like a machine gun, and delivers a flawless final product from scratch. A more accurate image would be someone who stares quietly into space for a few minutes and then says “Hmm. I think I’ve seen something like this before.” - John D
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 ?
And btw if you don't know what you are doing, how come did you write his code ?
The romantic image of an über-programmer is someone who fires up Emacs, types like a machine gun, and delivers a flawless final product from scratch. A more accurate image would be someone who stares quietly into space for a few minutes and then says “Hmm. I think I’ve seen something like this before.” - John D
•
•
Join Date: Nov 2006
Posts: 14
Reputation:
Solved Threads: 0
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
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.
Maybe you should brush up a bit on arrays from your text book and then try to attempt the problem.
The romantic image of an über-programmer is someone who fires up Emacs, types like a machine gun, and delivers a flawless final product from scratch. A more accurate image would be someone who stares quietly into space for a few minutes and then says “Hmm. I think I’ve seen something like this before.” - John D
•
•
Join Date: Nov 2006
Posts: 14
Reputation:
Solved Threads: 0
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]
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.
I again repeat myself:
In order to get the output of the form given by you, arrays are a must.
Without arrays you won't be able to display the records in the given format and finding out the grade would be really difficult.
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
The romantic image of an über-programmer is someone who fires up Emacs, types like a machine gun, and delivers a flawless final product from scratch. A more accurate image would be someone who stares quietly into space for a few minutes and then says “Hmm. I think I’ve seen something like this before.” - John D
•
•
Join Date: Nov 2006
Posts: 14
Reputation:
Solved Threads: 0
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
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.
![]() |
Other Threads in the C++ Forum
- Previous Thread: Recursive Insertion on Linked Lists
- Next Thread: Output problems
Views: 4823 | Replies: 26
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api application array arrays based beginner binary c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete developer display dll dynamiccharacterarray email encryption error file format forms fstream function functions game generator givemetehcodez graph iamthwee ifstream image input int java lib list loop looping loops map math matrix memory multiple newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg search simple sort sorting spoonfeeding string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






