0

I am really stuck on WHERE ON EARTH to go for this problem.

If someone can push me into the right direction. I would be deeply grateful

The following is my code

Grader.h

#ifndef GRADER_H
#define GRADER_H
#define MAXSIZE 100
#include <iostream>
#include <cstdlib>

class Grader {

public:
Grader( );

void addScore( int score );
void addScores( int scores[], int size );
void clear(); 


int findBiggest( ) const;
int findSmallest( ) const;

private:
int my_Values[ MAXSIZE ];
int my_ValuesSeenSoFar;

};
#endif

Grader.cpp

#include <iostream>
#define MAXSIZE 100
#include "Grader.h"

Grader::Grader( ){
my_Values [MAXSIZE] = 0;
my_ValuesSeenSoFar = 0;
}

void Grader::addScore( int score ){
    my_Values[MAXSIZE] = score;
}
void Grader::addScores( int scores[], int size ){
    my_Values[MAXSIZE] = scores[size];
}
void Grader::clear(){
    my_Values[0];
}

int Grader::findBiggest() const{
    int i;
    for (i = 0; i < MAXSIZE; i++)
    return my_Values[i];
}
int Grader::findSmallest( ) const{
}

Driver.cpp

#include <iostream>
#include "Grader.h"
using namespace std;
int main( )
{
Grader g;
double d[5]= {99,70,85,93,84};
double e[4]= {100,81,60,91};

g.addScore( 75 );
g.addScore( 82);
g.addScores( d, 5 ); 

cout << "Best Score = " << g.findBiggest( ) << endl;
/// should give value 99
cout << "Worst Score = " << g.findSmallest( ) << endl;
/// should give value 70
g.clear( );

g.addScore( 50 );
g.addScore( 74 );
g.addScores( e, 4 ); 

cout << "Best Score = " << g.findBiggest( ) << endl;
/// should give value 100
cout << "Worst Score = " << g.findSmallest( ) << endl;
/// should give value 50 
}

I am having trouble figuring out a) How exactly to implement the addScore/addScores

and b) how to call those scores into the findBiggest and findSmallest.

8
  • Sort the array, the smallest is first and the largest is last (or the other way around, depending on sorting direction). Commented Jul 29, 2013 at 9:20
  • The Driver.cpp has to remain as is Commented Jul 29, 2013 at 9:22
  • Where I get lost is in the Grader.cpp Commented Jul 29, 2013 at 9:22
  • By the way, when you do my_Values [MAXSIZE] = 0; you actually write beyond the limit of your array. Consider using std::vector instead. Commented Jul 29, 2013 at 9:23
  • 1
    Hint - If you only need the min and the max and you can't delete individual values, none of the other values matter (assuming you're free to change the private members of Grader). Commented Jul 29, 2013 at 9:25

7 Answers 7

1

You have many problems with your code:

  • Like I mentioned in a comment, my_Values [MAXSIZE] = 0; in the constructor writes one beyond the max index of the array.
  • You also do this in other places. You can't use MAXSIZE as index, since index goes from 0 to MAXSIZE - 1.
  • You can't add an array to another array like you do in addScores.

Using the facilities provided by the standard library (see here for a good reference site) will really help you now and in the future.


In the future, whenever you think about a dynamic array of any kind, you should be thinking about std::vector.

For example, adding a value to a vector is as simple as

std::vector<int> vector;

vector.push_back(123);

To insert an old-style array, like you want to do in your addScores function, you can use std::copy together with std::back_inserter:

std::copy(array, array + size, std::back_inserter(vector));

To keep the vector sorted, use the std::sort standard function:

std::sort(std::begin(vector), std::end(vector));
Sign up to request clarification or add additional context in comments.

4 Comments

For OP's reference, the old-style array can also use std::copy(scores, scores+size, std::back_inserter(vec)) instead of an explicit loop.
@Useless Ah of course, how could I forget. Updated my answer with that, thanks!
You can also use std::begin and std::end with (statically allocated) C arrays, which is arguably more idiomatic: std::copy(std::begin(array), std::end(array), std::back_inserter(vector));.
@FrerichRaabe not if the array has decayed to a pointer though, as it has here.
0

I feel grader.cpp should look like this. for finding max and min without sorting

#include <iostream>
#define MAXSIZE 100
#include "Grader.h"

Grader::Grader( ){
my_Values [MAXSIZE] = 0;
my_ValuesSeenSoFar = 0;
}

void Grader::addScore( int score ){
if( my_ValuesSeenSoFar < MAX_SIZE)
    my_Values[my_ValuesSeenSoFar++] = score;
}
void Grader::addScores( int scores[], int size ){
    for(int i=0; (i < size) && (my_ValuesSeenSoFar < MAX_SIZE) ; i++)
        my_Values[my_ValuesSeenSoFar++] = scores[i];
}
void Grader::clear(){
    for(int i=0;  i < MAX_SIZE ; i++)
        my_Values[i] = 0;

}

int Grader::findBiggest() const{

   int max = my_Values[0];
   for(int i=1 i < my_ValuesSeenSoFar ; i++)
   {
        if( my_Values[i] > max)
            max = my_Values[i];
   }   
   return max;
}
int Grader::findSmallest( ) const{
    int min = my_Values[0];
    for(int i=1 i < my_ValuesSeenSoFar ; i++)
    {
        if( my_Values[i] < min )
            min = my_Values[i];
    } 
    return min;
}

4 Comments

std::vector would actually be ideal.
Your addScores( int scores[], int size ) could overwrite existing values. Replace i by my_ValuesSeenSoFar.
This is immensely informative. As i'm sure with everyone here, I learn best by example-- and our professor has this class 100% independent study, so this is a huge help in understanding class arrays. Thank you very much
@Jack For some reason on the second pass through the smallest value returned is 0-- when it should be 50 cout << "Worst Score = " << g.findSmallest( ) << endl; /// should give value 50 }
0

Here's the problem:

void Grader::addScore( int score ){
    my_Values[MAXSIZE] = score; <---
}

You're always adding on your MAXSIZE-th element (which isn't wise). You need to do something like:

void Grader::addScore( int score ){
    my_Values[myValuesSoFar++] = score;
}

As for biggest and smallest, you could use qsort on addScore() and addScores(). This way your smallest element will be on my_values[0] and the biggest on my_values[myValuesSoFar-1].

Also, there are a tons of problems with your code (just take a look at ::clear()). I recommend this article on working with arrays, or try std::vector

2 Comments

@losif M. I assume I might do something similar to addScores as well?
yes, for addScores() you'd do something similar (with minor changes, but i'm sure the article will help you in this way too)
0

You need a variable that show how many elements are in my_Values. Suppose

    private:
    int my_Values[ MAXSIZE ];
    int my_ValuesSeenSoFar;
    int my_size;

Grader::Grader( ){
my_Values [MAXSIZE] = 0; //here you put 0 out of array (to array.size+1 place). delete it

my_ValuesSeenSoFar = 0;
my_size = 0; 
}

void Grader::addScore( int score ){
if (my_size < MAXSIZE )
   my_Values[my_size] = score;  //put new element on next place
   ++my_size;
else
   std::cout<<"No more place in array";
}    
void Grader::addScores( int scores[], int size ){
int i =0;
while (my_size < MAXSIZE && i<size)
{
    my_Values[my_size] = scores[i];
    ++my_size;
     ++i;
}
if (my_size == MAXSIZE && i==size)
    std::cout<<"no more place in array";
}

int Grader::findBiggest() const{
    int i;
    int max=my_Values[0];
    for (i = 0; i < my_size; i++)  //finding max element in array
    {
       if(my_Values[i]>max) 
          max = my_Values[i];
    }    
    return max;
}
int Grader::findSmallest( ) const{
    int i;
    int min=my_Values[0];
    for (i = 0; i < my_size; i++)  //finding min element in array
    {
       if(my_Values[i]<min) 
          min = my_Values[i];
    }    
    return min;

}

Something like that if i got question right.

Comments

0

a/ How about having an instance variable which indicates the current index of the array. increment it by n when you add n element, decrement it by n when you remove n elements.

b/ Two options:
i. Each time this is called, iterate through all elements and get the min/max. OK if max array size not too big.
ii. Each time a value is added or removed, iterate over all elements and set global variables min/max which you can then return on calls to findSmallest() and findBiggest()

2 Comments

In a, why use a global instead of an instance variable?
Yes: Instance variable. No idea why I put global. Corrected.
0

To answer your exact question, you can use std::minmax_element (in C++11) to get the largest and smallest value of an array:

#include <algorithm>
#include <iostream>

int main()
{
    double d[] = { 99, 70, 85, 93, 84 };

    auto result = std::minmax_element( std::begin( d ), std::end( d ) );
    std::cout << "minimum: " << *(result.first) << "\n"
              << "maximum: " << *(result.second) << "\n";
}

This prints:

minimum: 70
maximum: 99

Comments

0

For reference, here's a complete implementation:

#include <vector>
#include <iterator>
#include <algorithm>

class Grader {
  std::vector<int> scores_;

public:
  void addScore(int score) {
    scores_.push_back(score);
  }

  void addScores(int *scores, int nscores) {
    std::copy(scores, scores + nscores,
              std::back_inserter(scores_));
  }

  void clear() { scores_.clear(); }

  int findBiggest() const {
    if (scores_.empty()) return -1; // handle this error somehow
    return *std::max_element(scores_.begin(),
                             scores_.end());
  }

  int findSmallest() const {
    if (scores_.empty()) return -1; // handle this error somehow
    return *std::min_element(scores_.begin(),
                             scores_.end());
  }
};

Note that I didn't sort the scores in findBiggest or findBiggest, since they're declared const.

1 Comment

Your findBiggest and findSmallest methods will try to dereference the end iterator f the scores_ vector is empty.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.