🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: alt.comp.lang.learn.c-c++
Article #108

Re: Problematic Program

#108
From: Thomas Matthews
Date: Mon, 21 Oct 2002 14:08
76 lines
3292 bytes
sleek@crapmail.com (Sleek) wrote in message news:<84654e56.0210201932.671b8a43@posting.google.com>...
> "CM" <mckenzie992@comcast.net> wrote in message news:<ESudnc40b4i4hS6gXTWcow@comcast.com>...
> 
> > for (j=0;j=noOfGrades-1;j++)
> > {
> > cout<<"\t\nGrade #"<<j+1<<": ";
> > cin>>Grades[i][j];
> > if (Grades[i][j] >= 0  && Grades[i][j] <= 59)
> > points+=0;
> > if (Grades[i][j] >= 60 && Grades[i][j] <= 64)
> > points+=1.0;
> > if (Grades[i][j] >= 65 && Grades[i][j] <= 69)
> > points+=1.5;
> > if (Grades[i][j] >= 70 && Grades[i][j] <= 74)
> > points+=2.0;
> > if (Grades[i][j] >= 75 && Grades[i][j] <= 79)
> > points+=2.5;
> > if (Grades[i][j] >= 80 && Grades[i][j] <= 84)
> > points+=3.0;
> > if (Grades[i][j] >= 85 && Grades[i][j] <= 89)
> > points+=3.5;
> > else
> > points+=4.0;
> > }
> 
> You need 1 if statement (the first one) ... one else statement (the
> last one) .. everything inbetween should be "else if"

I believe the OP should eliminate the table and use an array:
const unsigned int LOW_RANGE_INDEX = 0;
const unsigned int HIGH_RANGE_INDEX = 1;
const unsigned int GRADE_POINT_INDEX = 2;
const unsigned int MAX_INDICES = 3;
const unsigned int MAX_RANGES = 7;

const unsigned int grade_scale[MAX_RANGES][MAX_INDICES] =
{
/*   low  high points * 10 */
/*   ---- ---- ----------- */
    {  0,  59,  0},
    { 60,  64, 10},
    { 65,  69, 15},
    { 70,  74, 20},
    { 75,  79, 25},
    { 80,  84, 30},
    { 85,  89, 35},
    { 90, 100, 40} 
};

/* ... */
/* Look up the grade */
for (unsigned int k = 0; i < MAX_RANGES; ++i)
{
    if ((Grades[i][j] >= grade_scale[k][LOW_RANGE_INDEX])
        && (Grades[i][j] <= grade_scale[k][HIGH_RANGE_INDEX]))
    {
        points += (double)(grade_scale[k][GRADE_POINT_INDEX]) / 10.0;
        break;
    }
}

Or for faster service:
const unsigned int MAX_GRADE_VALUES = 100;
const double grade_values[MAX_GRADE_VALUES] =
{ 0.0, 0.0, /* ... */,
 1.0, 1.0, 1.0, 1.0, 1.0, /* indices 60 ... 64 */
 1.5, 1.5, 1.5, 1.5, 1.5, /* indices 65 ... 69 */
/* ... */
};

/* Look up the grade point */
if (Grades[i][j] <= MAX_GRADE_VALUES)
    points += grade_values[Grades[i][j]];


-- Thomas
Technical Details
Message-ID: <20700e8e.0210210608.2c97f534@posting.google.com>
Path: arc1-ca.pugleaf.net!archiver1.google.com!postnews1.google.com!not-for-mail
References: <ESudnc40b4i4hS6gXTWcow@comcast.com> <84654e56.0210201932.671b8a43@posting.google.com>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 109fba,635d58b7486c58ea
X-Google-Attributes: gid109fba,public
X-Google-Thread: 114592,635d58b7486c58ea
X-Google-Attributes: gid114592,public
X-Google-Thread: 10d4ba,635d58b7486c58ea
X-Google-Attributes: gid10d4ba,public
X-Google-ArrivalTime: 2002-10-21 07:08:21 PST
Path: arc1-ca.pugleaf.net!archiver1.google.com!postnews1.google.com!not-for-mail
From: Thomas.Matthews@pb.com (Thomas Matthews)
Newsgroups: alt.comp.lang.learn.c-c++,att.lang.c++,comp.lang.c++
Subject: Re: Problematic Program
Date: 21 Oct 2002 07:08:20 -0700
Organization: http://groups.google.com/
Lines: 76
Message-ID: <20700e8e.0210210608.2c97f534@posting.google.com>
References: <ESudnc40b4i4hS6gXTWcow@comcast.com> <84654e56.0210201932.671b8a43@posting.google.com>
NNTP-Posting-Host: 199.231.49.128
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 8bit
X-Trace: posting.google.com 1035209301 7272 127.0.0.1 (21 Oct 2002 14:08:21 GMT)
X-Complaints-To: groups-abuse@google.com
NNTP-Posting-Date: 21 Oct 2002 14:08:21 GMT