954,506 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

help...one-dimensional array averaging program

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!

elcrapo
Newbie Poster
14 posts since Nov 2006
Reputation Points: 10
Solved Threads: 0
 

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.

~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 

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.

elcrapo
Newbie Poster
14 posts since Nov 2006
Reputation Points: 10
Solved Threads: 0
 

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 ?

~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 

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

elcrapo
Newbie Poster
14 posts since Nov 2006
Reputation Points: 10
Solved Threads: 0
 

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.

~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 

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.

[IMG]http://img511.imageshack.us/img511/5734/finalimageip8.th.jpg[/IMG]

[php]#include
#pragma hdrstop
#include
#include
#include
//#include //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]

elcrapo
Newbie Poster
14 posts since Nov 2006
Reputation Points: 10
Solved Threads: 0
 

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 25Without arrays you won't be able to display the records in the given format and finding out the grade would be really difficult.

~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 

i have given up on the array idea

elcrapo
Newbie Poster
14 posts since Nov 2006
Reputation Points: 10
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?image=finalimageip8.jpg

elcrapo
Newbie Poster
14 posts since Nov 2006
Reputation Points: 10
Solved Threads: 0
 
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

John A
Vampirical Lurker
Team Colleague
7,630 posts since Apr 2006
Reputation Points: 2,240
Solved Threads: 339
 

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...

Infarction
Posting Virtuoso
1,580 posts since May 2006
Reputation Points: 683
Solved Threads: 53
 

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

elcrapo
Newbie Poster
14 posts since Nov 2006
Reputation Points: 10
Solved Threads: 0
 

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

JRM
Practically a Master Poster
621 posts since Nov 2006
Reputation Points: 130
Solved Threads: 75
 

yeah, LOL

sladkjflsadkfj;saldfj crap

cout << "shoot me in the face" <

elcrapo
Newbie Poster
14 posts since Nov 2006
Reputation Points: 10
Solved Threads: 0
 

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)...

Infarction
Posting Virtuoso
1,580 posts since May 2006
Reputation Points: 683
Solved Threads: 53
 

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:

John A
Vampirical Lurker
Team Colleague
7,630 posts since Apr 2006
Reputation Points: 2,240
Solved Threads: 339
 

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.

elcrapo
Newbie Poster
14 posts since Nov 2006
Reputation Points: 10
Solved Threads: 0
 

wanna post the code you have so far?

Infarction
Posting Virtuoso
1,580 posts since May 2006
Reputation Points: 683
Solved Threads: 53
 

[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
 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;


         }



  }[/php] 
mostly this part:

[php]   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: "; */


[/php]

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

elcrapo
Newbie Poster
14 posts since Nov 2006
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You