Please Help, I have our first assignment here on functions. It is pretty difficult, at least the one part that I keep getting stuck on. I seemed to manuver through the other parts I was having trouble with so far. But to start our assignment is to write a program that will read in an unknown number (not more than 60) of test grades from a text file. the program is to calculate and output the following:

1.Number of test grades
2.Average of the Test Grades
3.The Highest and Lowest Test Grades
4.A list of the Grades and whether the grades is above or below average. The list should appear as follows:
Grade Status
94 Above
65 Below
85 Above
And then output all your results.

I have the first two done and keep getting compile errors on my third, when trying to bring down the number I read in from the file. Also not real sure how to begin 4th one so if you could just point me in the right direction there.

So far I have:

void function1(int ttotal, int ccount);
void function2(int nnumber);

int main()
{
    int number[60];
    int count = 0;
    int total = 0;
    int i, j;


            
    if(!infile) 
    {
        cout << "An error has occurred while opening the file." << endl; 

        frz;
        exit(1);
    }  
    
            for(i=0; i<60; i++)
                 {
                 number[i]=0;
                 } 
    
     while (!infile.eof())   
    {
        infile >> number[i];
        count++;
        total = total + number[i];
        cout << number[i] << endl;
    } 
    cout << endl;
    cout << count << endl;
    cout << total << endl;
    
    function1(total, count);
    function2(number[i]);
        
    infile.close(); 
    return 0;
}   //end main
//******************************************************************************
void function1(int ttotal, int ccount)  
{    
     double average;
     average = ((double)ttotal / (double)ccount); 

     cout << average << endl;

     frz;
}
// end function1
//******************************************************************************
void function2(int nnumber)
{
     int max = -100;
     int min = 100;

     if (number[i] > max) 
         {
         max = number[i]; 
         }
     if (number[i] < min) 
         {
         min = number[i]; 
         }
     cout << min << " " << max << endl;
}

// end function2

Every time I try to bring down the numbers in says in Function2 that number is not declared and i is not declared but I need to bring those down from the top to find max and min

Any suggestions for Function2 and part 4 of the directiosn to list the grades and tell whether they are above or below average.

Thanks,

You need to read your notes a bit more. The way you are passing array to function is not correct. You must do something like this;

int myArray[BUFSIZ] = { 0 };
myFunction(myArray, BUFSIZ);  // function call

void myFunction(int array[], int size)
{
   // do your processing here
}

Also your logic of finding the minimum and maximum is incorrect. Assume that you don't find any grade which is less than -100 and any grade which is greater than 100, what would your algorithm print out?

First off "Salem", yes I have 25 posts and they were all me in my class requesting help on different topics. Actually I had good luck b/c people that were nice and helpful not so rude like yourself commented back I actually learned how to do that particular topic. Also I have not had one post on the topic of functions. This is my first post, first time we covered it so I am requesting help just like I did before. I am not asking you to do my homeork Im just asking for some assistance. You can see I tried and am still trying, but not giving up. Also I do have it layed out almost identical to how it is setup in my notes and I have changed it and tried different things, but stil no luck.

S.O.S. I set it up the same way its in the notes he gives in class I have read them but am still unsuccessful. I also set up the max, and min part the same way as in my notes. I will give what you gave me a try, but will post back and let you know if it works.

try naming your functions relevant to what they do. i will go through this now and see what i can do to help you though :)

First off "Salem", yes I have 25 posts and they were all me in my class requesting help on different topics. Actually I had good luck b/c people that were nice and helpful not so rude like yourself commented back I actually learned how to do that particular topic.

Salem's point is that you keep posting code that is hard to read because you never use CODE tags (the link he posted) and never read any of the rules for posting which also included this information. Salem was far from rude. It's those that continually post hard to read code when posting easy to read code can be done without effort that are rude. And CODE tags have been mentioned a few times in your 25 posts, too, which you ignored.

So read his link, and comply with this site rule, please.

> Actually I had good luck b/c people that were nice and helpful not so rude like yourself commented back
Actually, it is YOU that is the rude, arrogant and inconsiderate little twat that has no regard for the forum rules. Instead of actually reading them, and paying attention, you just post your dribble without any regard for the people who have to read that stuff.

Then you bitch about it whenever anyone tries to point out your mistakes.

I can't find a single post of yours which contains correctly formatted code by your own hand. Most of it is the unreadable mess you always post. What remains has been edited by mods to make it look presentable.

And it's not just me that's been nagging you for the past couple of weeks. A range of moderators and other users have been spending valuable time in an apparent futile attempt to get you into line.
Eg.
http://www.daniweb.com/forums/post395624-16.html
Last edited by WaltP : 6 Days Ago at 6:48 am. Reason: Hasn't anyone mentioned CODE tags yet?!?? And have you failed to see the WORDS on the background of the input box you type in?

http://www.daniweb.com/forums/thread81552.html
Last edited by ~s.o.s~ : 12 Days Ago at 4:50 pm. Reason: Added code tags, learn to use them.

http://www.daniweb.com/forums/thread79489.html
Last edited by ~s.o.s~ : May 28th, 2007 at 6:32 pm. Reason: Added code tags, learn to use them.

http://www.daniweb.com/forums/thread79268.html
ft3ssgeek says "ok, first, please use code tags when posting code..."

http://www.daniweb.com/forums/thread79113.html
Last edited by ~s.o.s~ : May 24th, 2007 at 2:30 am. Reason: Added code tags, learn to use them.
And repeating myself
Read this for posting code in future.
http://www.daniweb.com/techtalkforum...cement8-3.html

Gee, I wonder if there is a pattern emerging there.

I'm done wasting time on you, and if you keep this up, a range of mods will pretty soon have the same frame of mind in that you're just too much "high maintenance" to bother with.

im not tired of you yet (but dont think i cant get there lol) so i will try to help you.

first of all you need to comment your code up when you expect help. Without comments we really dont have a direction in which to begin helping you because then we have to spend valuable time learning your code!

But i digress this is a simple snippet i believe i may be able to help you with it :-D

Question 1: number of test scores (easy i believe you already have that) BUt i also noticed that you are assigning the array size to a const 60 but what does your application do if there are 40 grades or even 100 grades, this can drastically throw off the calculation.

One option is to use vectors

#include <vector>
 
int main() {
    vector<int> testScores;
    while(!infile.eof()) {
        int temp;
        infile >> temp;
        testScores.push_back(temp);
    }

using vectors you can now have a container for any ammount of grades

and to get the size or number of elements (grades)

int testScoresSize = testScores.size();

Question 2: average of the test scores!

very simple with vectors

int sumofScores = 0,
    totalScores = testScores.size();
for (int i = 0; i < totalScores; i++)
{
    sumofScores += testScores.at(i);
}
int averageScore = sumofScores / totalScores;
//i am using an int but if you wish to have more percision you may want to use a different data type.

Question 3: get the highest and lowest test grades

for this you will need to run a comparison method to return the highest/lowest.

we can try something like this:

int max = 0,
        min = testScores.at(1);  // this must be initialized to a value from within the vector if it is not then a min size of 0 will always be found even if it does not exist
    for ( int i = 0; i < int (testScores.size()); i++ )
    {
        for ( int n = 0; n < int (testScores.size()); n++ )
        {
            int ival = testScores.at(i),   //store the first value
                nval = testScores.at(n);  //store the values to check against
            if (ival >= nval &&
                ival >= max)
            {
                max = testScores.at(i); //if our value is greater than the checked against value and greater than the current max
            }
            if (ival < nval &&
                ival < min)
            {
                min = testScores.at(i);  //if the value is less than the checked against value and less than the current min
//this is where it is important to note that min must be set to something other than 0 on first run or the loop will always return zero for min
            }
        }
    }
    cout << "AND THE MAX IS:  " << max << endl;
    cout << "AND THE MIN IS:  " << min << endl;

Question 4: list out all of the grades and if they are above/below average

easy cheezy

//assume we already have the average from before stored as testScoreAverage
 
for each (int testscore in testScores) {
  if (testscore < testScoreAverage) {
     cout << testscore << " Below" << endl;
  }
  if (testscore > testScoreAverage) {
     cout << testscore << " Above" << endl;
  if (testscore == testScoreAverage) {
     cout << testscore << " Average" << endl;
  }
}

simple as that friend!

but please note if you dont wrap your code in the code tags i wont be able to help you ever again :(

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.