need help in function

I'm having problems here with lowPosition[] , i don't know what i'm doing wrong but it's always 0.
i would appreciate some help, thanks in advance.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void lowest(int math[][STUDENTS],int lowestGrade[],int lowPosition[],int i)
{   int position=0;
    int low=math[i][0];
    for (int z=0;z<STUDENTS;z++)
    {
        if (low>math[i][z])
        {
           low=math[i][z];
           lowestGrade[i]=low;
           position=z;
           lowPosition[i]=position; // having problem here, i don't know why is always 0.
        }else

         lowestGrade[i]=low;
         lowPosition[i]=0;
    }
    cout <<"lowestGrade "<< lowestGrade[i]<< " position "   // this is for testin purpose
          << lowPosition[i]<<" math " <<math[i][position]<<" position " <<position ; // same here
}




this is the whole program:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <iostream>
#include <cstdlib>

using namespace std;

const int ROWS=3;
const int STUDENTS=4;

void highest(int math[][STUDENTS],int highestGrade[],int highLocation[],int i)
{
    int high=0;
    for (int x=0;x<STUDENTS;x++)
    {
        if (math[i][x]>high)
        {
            high=math[i][x];
            highestGrade[i]=high;
            highLocation[i]=x;
        }
    }

}
void lowest(int math[][STUDENTS],int lowestGrade[],int lowPosition[],int i)
{   int position=0;
    int low=math[i][0];
    for (int z=0;z<STUDENTS;z++)
    {
        if (low>math[i][z])
        {
           low=math[i][z];
           lowestGrade[i]=low;
           position=z;
           lowPosition[i]=position; // having problem here, i don't know why is always 0.
        }else

         lowestGrade[i]=low;
         lowPosition[i]=0;
    }
   cout <<"lowestGrade "<< lowestGrade[i]<< " position "   // this is for testin purpose
          << lowPosition[i]<<" math " <<math[i][position]<<" position " <<position ; // same here
}

int main()
{

    int math[ROWS][STUDENTS];
    float averageGrade[ROWS];
    int highestGrade[ROWS];
    int lowestGrade[ROWS];
    int indicator=0;
    int lowPosition[ROWS];
    int highLocation[ROWS];
    int indexLow=0;


    for (int i=0;i<ROWS;i++)
   {
       cout << "\n\n\tDigite la nota del examen de los alumnos de la hilera: " << i+1 <<" :\n";
       for (int x=0;x<STUDENTS;x++)
       {
           cout << "\talumno "<< x+1 <<" : ";
           cin >> math[i][x];
       }

       highest(math,highestGrade,highLocation,i);
       lowest(math,lowestGrade,lowPosition,i);


   }
   return 0;
}
It is the result of statements

1
2
3
4
5
        }else

         lowestGrade[i]=low;
         lowPosition[i]=0;


You always set lowPosition[i] to zero. This statement is outside the else statement. In fact you have

1
2
3
        }else  lowestGrade[i]=low;

         lowPosition[i]=0;


I do not see any sense in the else statement. You could write the following way

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void lowest( int math[][STUDENTS],i nt lowestGrade[], int lowPosition[], int i )
{
    lowestGrade[i] = math[i][0];
    lowPosition[i] = 0;

    for ( int z = 0; z < STUDENTS; z++ )
    {
        if ( math[i][z] < lowestGrade[i] )
        {
           lowestGrade[i] = math[i][z];
           lowPosition[i] = z;
        }
    }

    cout <<"lowestGrade "<< lowestGrade[i]<< " position "   // this is for testin purpose
          << lowPosition[i]<<" math " <<math[i][lowPosition[i]];
}
Last edited on
thanks, that solved my problem :)
There is no such member function declaration as void words() inside the class Date. So the function definition

void Date::words()
...

is invalid.
Last edited on
Topic archived. No new replies allowed.