5 Replies - 1016 Views - Last Post: 23 October 2008 - 02:19 AM Rate Topic: -----

#1 ucmath786   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 12-October 08

need DESPERATE help! ---arrays in a function

Posted 22 October 2008 - 07:23 PM

im making a grade book code using functions and SINGLE arrays (multi arrays would be easier, but my teacher wants this in our code)

outline:
from main() ask the user for info about 4 students
---> use functions getStudentInfo() 4x (once per student; im assuming this will have the students names)
----> call ComputerAverage() 4x from main (average of students grades. they will have 5 grades, where the lowest will be dropped and then averaged together)
----> call PrintGradeInfo() 4x as well to print grade, pausing in between each student so user can view all info (pause is a function as well)

example of output:

Enter Name of Student: Billy Bob
Score 1: 100
Score 2: 100
Score 3: 100
Score 4: 100
Score 5: 100

here is grade info:

Billy Bob
Exam Scores: 100 100 100 100 100
Average: 100
Grade: A

Hit enter key to continue...
etc...
(it should show this once the data for all 4 students entered)

for the names i will be using the getline function
and using a for loop to drop the lowest score and each student has its own 1 dimensional array

my code so far:
#include <iostream>
#include <string> // for student names
using namespace std;

//function prototypes
void GetStudentInfo(string& name, int scores[]);
int LowestScore (const int scores[]);
void ComputeAveAndGrade (const int scores[], double& average, char& grade);
void PrintGradeInfo (const string& name, const int scores[], double average, char grade);
void Pause(); 


int main()
{

// function calls
[color=#009900]GetStudentInfo (name[0], scoreS0);
DetermineGrade (scoreS0. average[0], grade[0]);
PrintStudentInfo(name[0], scoreS0, average[0], grade[0]);
// there are more to call, this is just the beginning since i have 4 students
Pause();[/color]

return 0;
}

// functions here



this is how the layout should be according to my professor, but she didnt elaborate much as to WHY it should be like this
all the code that is in green is the ones that i am confused on (like why are passed by references being used? and what will the array do??)
im also confused on how to write the functions and strings itself (my book does a pathetic job by taking only 2 pages to cover strings)

if you could explain to me what is going on in the code-it would really help me understand!

thanks guys! :-)

This post has been edited by ucmath786: 22 October 2008 - 09:43 PM


Is This A Good Question/Topic? 0
  • +

Replies To: need DESPERATE help! ---arrays in a function

#2 salindor   User is offline

  • D.I.C Regular
  • member icon

Reputation: 46
  • View blog
  • Posts: 304
  • Joined: 10-November 06

Re: need DESPERATE help! ---arrays in a function

Posted 22 October 2008 - 07:43 PM

There are two different ways to pass variables in most langages. Pass by value and pass by reference.

So imagine your in ancient times and your royalty and need a castle built. So you write a letter and on your letter you ask how much is the castle going to cost.

Now when you hand it to the courier to go deliver the letter; he has two choices. Now your courier has two choices depending on what you want todo. First your courier could make a copy of the letter, put the original in a vault to make sure it is preserved and deliver the copy. Sense the letter is just a copy, there is no need to return the letter back, so the builder writes a new letter with the price and sends it back with the courier. Once back, your courier fetches the original letter from the safe and hands both back to you.

Your second choice, is the courier takes the original letter. Once there, the builder decides he wants to itemize the cost and places the price of each item you requested next to the item. Your courier; having the original letter has to return it back to you (but now it is all marked up).

The first method is like passed by value. No matter what the builder does to the first letter, it doesn't matter. The second method is like pass by refernce. If the function modifies the letter; when it returns the letter is modified.

Sometimes however, passing by reference is used a timesaver. For example, back to the courier example above. Suppose the letter you were sending was actually a 50 page book. You could get an answer alot faster by just sending the book rather than making a copy. If you do this; and the function isn't suppose to modify the function; always always make it const. This protects you in helping you make sure the code really doesn't modify the variable; and it lets other developers who might use your code know that their variable is safe from maniplation.



I am afraid I can't see green; so I am not sure what else you might be confused on.

Salindor
Was This Post Helpful? 0
  • +
  • -

#3 ucmath786   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 12-October 08

Re: need DESPERATE help! ---arrays in a function

Posted 22 October 2008 - 07:49 PM

wow that was a really good analogy!!! thanks!!
but what do i do with the arrays??

how do i start the actual code?
*the one above was just the layout of how my teacher wanted it*

how should the user input be?
like this?
//function 
UserInput
{
   cout << "Enter student name"
   //doesnt a string go here?? or cin?? 
   //my teacher said to use getline
   
   // scores
   cout << "Score 1: " << endl;
   etc..
}



will this function return a value or be void??
the part that troubles me is using the string and arrays to get all the info and print it out

This post has been edited by ucmath786: 22 October 2008 - 07:50 PM

Was This Post Helpful? 0
  • +
  • -

#4 salindor   User is offline

  • D.I.C Regular
  • member icon

Reputation: 46
  • View blog
  • Posts: 304
  • Joined: 10-November 06

Re: need DESPERATE help! ---arrays in a function

Posted 22 October 2008 - 08:07 PM

Did you know an array is just a pointer?

Not sure if you know what pointers are so here we go. Your traveling down the road on a long trip and suddenly you realize your hungry. So you look up and see a giant sign with a big cheese burger on it advertising Burger King that way 5 mi. So you think that is good and your go that way 5 mi to burger king. The sign is a pointer, it takes up physical space in the world; but you can't actually order food there. The same is with a pointer; it tells the program where to go get object that it is pointing to; but the pointer itself is just a sign.

In my previous example, there really is a third method your courier could do. Lets pretend that you didn't want to move the book from its place. So instead of having your courier deliver the book; you have the courier deliver a letter saying; come read the book in my vault and tell me what it will cost. The builder then travels to your vault, looks at the book and decides to mark up the book with prices; then leaves.

The difference between by reference and by pointer is the passing by reference; the builder is garenteed to get an object (otherwise you will get a compiler error); however when passing by pointer; you can point the builder to somewhere that doesn't exist.

In your case, when passing arrays, your only choice is to pass a pointer. Again if the function isn't suppose to modify the array, make sure it is const. So lets look at your input function. It contains two parameters. The first is passed by reference and the second is passed by pointer. This means the function is capable of modifying both parameters. Also you know when the function is complete; because it will return. This means the functions parameters are set up so all you need todo is use the parameters inside just like you would if it was all one function.

Here is a reference I found to getline. Please read it then ask your question you have about it.
Was This Post Helpful? 0
  • +
  • -

#5 ucmath786   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 12-October 08

Re: need DESPERATE help! ---arrays in a function

Posted 22 October 2008 - 09:41 PM

that helps a bit-but can someone show me code wise how this should look like?
how do i start off the code?
the one above was just a layout. but how do i actually start writng the functions?

like i understand what your saying-but i cant put it into code.

could u show me like an example (one line or something like that?)

your doing great on the explaining!!

This post has been edited by ucmath786: 23 October 2008 - 01:29 AM

Was This Post Helpful? 0
  • +
  • -

#6 David W   User is offline

  • DIC supporter
  • member icon

Reputation: 298
  • View blog
  • Posts: 1,839
  • Joined: 20-September 08

Re: need DESPERATE help! ---arrays in a function

Posted 23 October 2008 - 02:19 AM

... a little example, then ... :D

For some beginning programming helps, see ...

http://developers-he...index.php/topic,46.0.html

 // student records using a C++ 'struct' //

#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

// Globals ...

const int numStuds  = 2; // Just change these here ... to whatever you need.
const int numScores = 3; // Small numbers were used to make debugging easier.

struct student
{
        string  name;
        int     score[numScores];
        int     lowest;
        float   average;
        string  grade;
        
}; // *** NOTE: semi-colon needed here. *** //

void getInfo( student & s );
void getGrade( student & s );
void showInfo( student & s );
void pause()
{
    cout << "\nPress 'Enter' key to continue ... ";
    cin.clear();// Clear any error flags so program won't 'crash out'
    cin.sync(); // 'flush' cin  
    cin.get();  // Wait for 'Enter' key to be pressed 
}

int main()
{
    // create an array to hold your 'student' objects
    student fall2008[numStuds];
    
    // fill array with name and score info from keyboard
    for (int i=0; i<numStuds; ++i)
    {
        cout << "\nFor student " << i+1 << " of " << numStuds;
        getInfo( fall2008[i] );
    }
        
    for (int i=0; i<numStuds; ++i)
        getGrade( fall2008[i] );
        
    for (int i=0; i<numStuds; ++i)
        showInfo( fall2008[i] );
       
    pause();
    
    return 0;
}

void getInfo( student & s )
{
    cout << ", please enter the student's name: ";
    cin >> s.name;
    for (int i=0; i<numScores; ++i)
    {
        for(;;)/> // do forever ...until break
        {
            cout << "Score " << i+1 << ": ";
            cin.sync();
            cin >> s.score[i];
            if ( !cin.good() )
            {
                cout << "\nEnter an integer only please.\n";
                cin.clear();
                continue;
            }
            // if we reach here then HAVE A GOOD number ... so
            break; // out of 'inner' for loop
        }
    }
}

void getGrade( student & s )
{
    s.lowest =  s.score[0]; // use this  to get staretd
    int total = s.score[0]; // so get this too, since loop below begins with '1'
    
    for (int i=1; i<numScores; ++i)
    {
        total += s.score[i];
        if ( s.score[i] < s.lowest ) s.lowest = s.score[i];
    }
    
    // now don't count 'lowest' in average
    s.average = float(total-s.lowest)/(numScores-1);
    
    if ( s.average  >= 80 ) s.grade = "A";
    else if ( s.average  >= 70 ) s.grade = "B";
    else if ( s.average  >= 60 ) s.grade = "C";
    else if ( s.average  >= 50 ) s.grade = "D"; 
    else s.grade = "F";  
}

void showInfo( student & s )
{
    cout << endl << s.name
         << "\nExam scores: ";
    for (int i=0; i<numScores; ++i)
        cout << s.score[i] << " ";
    cout << "\nAverage: " << setprecision(4) << s.average
         << "\nGrade:   " << s.grade
         << "\nThe lowest score, " << s.lowest 
         << ", wasn't counted in the average of the top " << numScores-1 
         << " scores." << endl;
}

This post has been edited by David W: 23 October 2008 - 02:34 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1