hello someone out there...help me with algorithm to determine range of marks!!

here is my code so far

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

struct stumarks
{
    int studentid;                             
    int progassn;                              
    int lab;
    int test;
    int exam;                                  
    char seperator;
    double finalmark;
   
       
 }
stumarks[9];                                   

using namespace std;                           

int main()                                     
{                                              
      

    
    ifstream inFile;                                   
    ofstream outFile;                                  

        
    inFile.open("datafile.txt");               

    if (!inFile)                               
    {                                          
        cout << "Cannot open input file. "
             << "Program terminates!" << endl; 
        return 1;                               
    }                                          
       
           outFile.open("datafile.out");              

    int i;
    
    

    inFile >>stumarks[i].studentid >>stumarks[i].seperator>>stumarks[i].progassn
           >>stumarks[i].seperator>>stumarks[i].lab
           >>stumarks[i].seperator
           >>stumarks[i].test>>stumarks[i].seperator
           >>stumarks[i].exam;  
  
  /* while (inFile)                                     
    {             
        cout <<stumarks[i].studentid<<' '
             <<stumarks[i].progassn<<' '
             <<stumarks[i].lab<<' '
             <<stumarks[i].test<<' '
             <<stumarks[i].exam<<endl;
        

        inFile >>stumarks[i].studentid >>stumarks[i].seperator>>stumarks[i].progassn
           >>stumarks[i].seperator>>stumarks[i].lab
           >>stumarks[i].seperator
           >>stumarks[i].test>>stumarks[i].seperator
           >>stumarks[i].exam;    
    } */                                 
cout<<endl;
cout<<"\n\n\n\n";
    outFile << endl; 
    
  
            
            cout<<"\t"<<setw(10)<<"ProgaAssn"<<"\t"<<"Lab"<<setw(3)<<"\t"<<"Test"<<"\t"<<"Exam"<<endl;
            const int arraysize=11;
            int grade[arraysize]={stumarks[i].studentid,stumarks[i].progassn,stumarks[i].lab,stumarks[i].exam};
            for (int j=0; j<arraysize; j++)
            {
                inFile >>stumarks[i].studentid >>stumarks[i].seperator>>stumarks[i].progassn
           >>stumarks[i].seperator>>stumarks[i].lab
           >>stumarks[i].seperator
           >>stumarks[i].test>>stumarks[i].seperator
           >>stumarks[i].exam;    
           
                if(j>0||j<19){
                cout<<"0-19";
                
                break;}
                
                if (j>19||j<39){
                cout<<"20-39";
                
                break;}
                
                if (j>39 || j<59){
                cout<<"40-59";
                break;}
                
                if (j>59 || j<79){
                cout<<"60-79";
                break;}
                
                if (j>79 || j<=100){
                cout<<"80-100";
                break;}
           
                }
                   
    cout<<endl;
    cout<<"\n\n\n"; 
                          
               cout<<setw(4)<<"ID"
                   <<"   "<<"ProgAssn"
                   <<"   "<<"LabExer"
                   <<"   "<<"Test"
                   <<"   "<<"Exam"
                   <<"   "<<"Final Mark"<<endl;                          
 while(inFile)  
      {  stumarks[i].finalmark=(stumarks[i].progassn*0.1+stumarks[i].lab*0.1+stumarks[i].test*0.2+stumarks[i].exam*0.6);
             
        cout <<stumarks[i].studentid<<' '
             <<"   "<<stumarks[i].progassn
             <<"         "<<stumarks[i].lab
             <<"      "<<stumarks[i].test
             <<"     "<<stumarks[i].exam
             <<"      "<<setprecision(2)<<stumarks[i].finalmark<<endl; 
        

        inFile >>stumarks[i].studentid >>stumarks[i].seperator>>stumarks[i].progassn
           >>stumarks[i].seperator>>stumarks[i].lab
           >>stumarks[i].seperator
           >>stumarks[i].test>>stumarks[i].seperator
           >>stumarks[i].exam;    
    
    }       
                 

    inFile.close();                                    
    outFile.close();                                   

system("pause");
    return 0;                                          
}

my problem is to ouput the range of marks between 0-19,20-39, xtra...

i can't figure out the algorithm... it bother me since yesterday...i can't help it...teach me...range of marks algotrithm....

oh yeah. the data file is as follows:

123451,66,75,70,80
123452,39,22,35,55
123453,5,5,20,30
123454,45,33,48,70
123455,77,85,60,40
123461,90,86,88,96
123462,80,90,80,85
123463,55,63,58,65

should be 50 students but i just minimize it to 8 so that i could see the way this program works....all explaination and help are welcome...and don't give me da direct code....

When you say you need the "range of marks", are you asking for the the highest and lowest score for each exam?

something like that but...what i meant was

something that would look like this:

Assng     Lab   Test    Exam
0-19   1        1      0       0
20-39  0        1      1       0

and so on...(i want the output to be like this)

notice that only 1 student got marks between 0-19 for the assngment, lab and the test and exam is above that range....

123451,66,75,70,80
123452,39,22,35,55
123453,5,5,20,30 <<----this particular student here....
123454,45,33,48,70
123455,77,85,60,40
123461,90,86,88,96
123462,80,90,80,85
123463,55,63,58,65

so u get what i'm trying to find?? it's confusing me...it's the number of student that i need to be in a particular range..

I will be decisive and assume that you need the number of A's, B's, C's, D's, and F's for the assignment, lab, test, and exam. A's will be 100% to 90%, B's, will be 89% to 80%, C's will be 79% to 70%, D's will be 69% to 60%. F's will be below 60%:

int get_grade(studmarks& s, char& test)
{
     //test: A = assignment, L = lab, T = test, E = exam

     int grade = 0;

     switch(test)
     {
          case 'A':  grade = s.progassn / 10;  break;
          case 'L':  grade = s.lab  / 10;      break;
          case 'T':  grade = s.test / 10;      break;
          case 'E':  grade = s.exam / 10;      break;
     }

     switch(grade)
     {
          case 10:
          case  9:  return 0;     break;
          case  8:  return 1;     break;
          case  7:  return 2;     break;
          case  6:  return 3;     break;
          default:  return 4;     break;
     }
}

int grades[5];  // A's = [0], B's = [1], C's = [2], D's = [3], F's = [4]

 inFile    >>stumarks[i].studentid >>stumarks[i].seperator>>stumarks[i].progassn
           >>stumarks[i].seperator>>stumarks[i].lab
           >>stumarks[i].seperator
           >>stumarks[i].test>>stumarks[i].seperator
           >>stumarks[i].exam;

//Populate grade statistics
grades[get_grade(stumarks[i].progassn, A)]++;
grades[get_grade(stumarks[i].lab,  L)]++;
grades[get_grade(stumarks[i].test, T)]++;
grades[get_grade(stumarks[i].exam, E)]++;

Administrator locked out my edit:

I will be decisive and assume that you need the number of A's, B's, C's, D's, and F's for the assignment, lab, test, and exam. A's will be 100% to 90%, B's, will be 89% to 80%, C's will be 79% to 70%, D's will be 69% to 60%. F's will be below 60%:

int get_grade(int& grade, char& test)
{    
     //test: A = assignment, L = lab, T = test, E = exam

     grade /= 10;

     switch(grade)
     {
          case 10:
          case  9:  return 0;     break;
          case  8:  return 1;     break;
          case  7:  return 2;     break;
          case  6:  return 3;     break;
          default:  return 4;     break;
     }
}

int grades[5];  // A's = [0], B's = [1], C's = [2], D's = [3], F's = [4]

 inFile    >>stumarks[i].studentid >>stumarks[i].seperator>>stumarks[i].progassn
           >>stumarks[i].seperator>>stumarks[i].lab
           >>stumarks[i].seperator
           >>stumarks[i].test>>stumarks[i].seperator
           >>stumarks[i].exam;

//Populate grade statistics
grades[get_grade(stumarks[i].progassn, A)]++;
grades[get_grade(stumarks[i].lab,  L)]++;
grades[get_grade(stumarks[i].test, T)]++;
grades[get_grade(stumarks[i].exam, E)]++;

thanx, but i don't want to categorize the students in range of A or B or C just a in range of 0-19, 20-39 ...and so on..but i don't really understand your method in categorize the students in range A , B or c...can you explain the algorithm...?

and oh yeah...what is the ouput? i can't seem to figure out your function...

This is your program... i'm just trying to figure out your algorithm
......

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

struct stumarks
{
    int studentid;                             
    int progassn;                              
    int lab;
    int test;
    int exam;                                  
    char seperator;
    double finalmark;
   
       
 }
stumarks[9];
int get_grade(int&, char&);                                   

using namespace std;                           

int main()                                     
{                                              
    
    ifstream inFile;                                   
    ofstream outFile;     
    int i;    
    char A,L,T,E;                         

        
    inFile.open("datafile.txt");               

    if (!inFile)                               
    {                                          
        cout << "Cannot open input file. "
             << "Program terminates!" << endl; 
        return 1;                               
    }                                          
       
           outFile.open("datafile.out");  
       

int grades[5];  // A's = [0], B's = [1], C's = [2], D's = [3], F's = [4]

 inFile    >>stumarks[i].studentid >>stumarks[i].seperator>>stumarks[i].progassn
           >>stumarks[i].seperator>>stumarks[i].lab
           >>stumarks[i].seperator
           >>stumarks[i].test>>stumarks[i].seperator
           >>stumarks[i].exam;

//Populate grade statistics
cout<<grades[get_grade(stumarks[i].progassn, A)]++<<endl;
cout<<grades[get_grade(stumarks[i].lab,  L)]++<<endl;
cout<<grades[get_grade(stumarks[i].test, T)]++<<endl;
cout<<grades[get_grade(stumarks[i].exam, E)]++<<endl; 

system("pause");
return 0;
}
int get_grade(int& grade, char& test)
{    
     //test: A = assignment, L = lab, T = test, E = exam

     grade /= 10;

     switch(grade)
     {
          case 10:
          case  9:  return 0;     break;
          case  8:  return 1;     break;
          case  7:  return 2;     break;
          case  6:  return 3;     break;
          default:  return 4;     break;
     }
}

it just give me weird numbers...

I just threw some code your way to get you started.. maybe jog some of them brain cells. I purposely did not finish your assignment.

This was my thought process (although somewhat impaired after just comming back from a fresh concert last night featuring Axe Murder Boyz opening up for ABK):

1. create a counter that will hold student's test performance based on 5 test ranges: 0-29, 30-49, 50-69, 70-89, and 90-100. Each second-dimension element will serve as a 'counter' of how many test results qualified for each range:

//First  Dimension: [0] is the assignment, [1] is lab, [2] is test, and [3] is exam
//Second Dimension: [0] is 0-29, [1] is 30-49, [2] is 50-69, [3] is 70-89 and [4] is 90 to 100

int ranges[4][5];  

//to make our array easier to read, i'll also do the following quick little trick:
//assign = 0, lab = 1, test = 2, and exam = 3

enum{assign, lab, test, exam};

2. Now that I got a counter, I needed a function that I could plug in test information and will return which element of ranges[][] to populate:

int get_range(int& grade)
{
     if(grade < 30)
     {
          //increment ranges[][0]
          return 0;    
     }
     else if(grade >= 30 && grade <= 49)
     {
          //increment ranges[][1]
          return 1;
     }
     else if(grade >= 50 && grade <= 69)
     {
          //increment ranges[][2]
          return 2;
     }
     else if(grade >= 70 && grade <= 89)
     {
          //increment ranges[][3]
          return 3;
     }
     else
     {
           //Anything else must be in the 90-100 range
           //increment the ranges[][4] counter
           return 4;
     }
}

3. Now that we have some useful tools at our disposal, we can keep a counter of ranges (you'd probably do this in the same loop when you read in your file):

//The get_range() function returns the proper element of the ranges[][] counter to increment:
ranges[assign][get_range(stumarks[i].progassn)]++;
ranges[lab][get_range(stumarks[i].lab)]++;
ranges[test][get_range(stumarks[i].test)]++;
ranges[exam][get_grade(stumarks[i].exam)]++;

4. Now you can display ye' data with great ease:

cout << "assignment" << "    lab" << "    test" << "    exam";
cout << "0-29      " << rages[assign][0] << ranges[lab][0] << ranges[test][0] << ranges[exam][0];
cout << "30-49     " << rages[assign][1] << ranges[lab][1] << ranges[test][1] << ranges[exam][1];
cout << "50-69     " << rages[assign][2] << ranges[lab][2] << ranges[test][2] << ranges[exam][2];
cout << "70-89     " << rages[assign][3] << ranges[lab][3] << ranges[test][3] << ranges[exam][3];
cout << "90-100    " << rages[assign][4] << ranges[lab][4] << ranges[test][4] << ranges[exam][4];

In summary, this is probably one of many many ways to get your assignment done. I usually like to throw code your way until you can pick up and run with it instead of someone spoon feeding you the answer. One bad thing I forgot to do is to initialize the ranges[][] array to ensure every element is zero.

while(inFile)
{
  inFile >>stumarks[i].studentid >>stumarks[i].seperator>>stumarks[i].progassn
           >>stumarks[i].seperator>>stumarks[i].lab
           >>stumarks[i].seperator
           >>stumarks[i].test>>stumarks[i].seperator
           >>stumarks[i].exam; 
   
//The get_range() function returns the proper element of the ranges[][] counter to increment:
ranges[assign][get_range(stumarks[i].progassn)]++;
ranges[lab][get_range(stumarks[i].lab)]++;
ranges[test][get_range(stumarks[i].test)]++;
ranges[exam][get_range(stumarks[i].exam)]++;
}

thx..appreciate it...by the way...can u give me some website or references so that i can tackle questions like this...i need to do a lot more programming bout this kind of thing . i need to practice lots more...i don't want to be spoon feed...:(

The Complete Code :

#include <iostream>                            
#include <fstream>                             
#include <iomanip> 

using namespace std;      

class marks
{
public:

       void read_data();//function to check if datafile is available or not       
       void sort_range();//function to sort the marks into a certain range
       void range_display();//function to ouput the range of marks      
       void display_result();//function to display result for the final marks  
       
private:
         
        enum {student_assignment, student_labexercise, student_test, student_exam};
        //The use of enumerator where sets student_assignment=0, student_labexercise=1,
        //student_test=2, and student_exam=3
        //This is to make the array easier to read 
        int range_marks(int);//function to calculate range of marks
        double calculate_finalmark(double&);//function to calculate the final marks
        
};
//global variable            
int student_id[50],assignment_marks[50],lab_marks[50],test_marks[50],exam_marks[50]; //arrays of 50 student                                
char seperator[50];//This is to read the comma that is a character in the data file

int student_count;       //A global variable to count a number of students in an array
             
const int first_dim=4;       //First  Dimension: [0] is the Assignment, [1] is Lab, [2] is Test, and [3] is Exam
const int second_dim=5;      //Second Dimension: [0] is 0-19, [1] is 20-39, [2] is 40-59, [3] is 60-79 and [4] is 80 to 100
int ranges[first_dim][second_dim]={0};           //initialize array to 0


int main()                                     
{                                              
      marks student;//instantiate object "student" of class marks
                   
             student.read_data();        //to check if the datafile is available for the compiler to read or not.
             student.sort_range();       //read from data file and sort the range of marks  
             student.range_display();    //ouput the range of marks for a calculated number of student        
             student.display_result();   //display final marks result
                
    system("pause");
    return 0;                                          
}

//function to check if datafile is available or not
void marks::read_data()
{
         
    ifstream inFile;                                   
    ofstream outFile; 
   
        
    inFile.open("datafile.txt");               

    if (!inFile)                               
    {                                          
        cout << "Cannot open input file. "
             << "Program terminates!" << endl; 
        return ;                               
    }                                          
       
           outFile.open("datafile.out");  
   
   inFile.close();                                    
   outFile.close(); 
    return;
}                                              

//function to calculate the range of marks
int marks::range_marks(int student_marks)
{
    if(student_marks < 19)
     {     
           //increment ranges[][0]                    
          return 0;             
     }
     else if(student_marks  >= 20 && student_marks  <= 39)
     {    
          //increment ranges[][1]                           
          return 1;          
     }
     else if(student_marks  >= 40 && student_marks  <= 59)
     {     
           //increment ranges[][2]                   
          return 2;          
     }
     else if(student_marks  >= 60 && student_marks  <= 79)
     {    
          //increment ranges[][3]                        
          return 3;         
     }
     else
     {
           //Anything else must be in the 90-100 range
           //increment the ranges[][4] counter
           return 4;
     }
}

//function to sort the marks into a certain range
void marks::sort_range()
{
     
    ifstream infile;
         
        infile.open("datafile.txt");                                    
       
     infile>>student_id[student_count]>>seperator[student_count]
           >>assignment_marks[student_count]>>seperator[student_count]
           >>lab_marks[student_count]>>seperator[student_count]
           >>test_marks[student_count]>>seperator[student_count]
           >>exam_marks[student_count];     
           
do
     {       //The get_range() function returns the proper element of the ranges[][] counter to increment:           
             ranges[student_assignment][range_marks(assignment_marks[student_count])]++;
             ranges[student_labexercise][range_marks(lab_marks[student_count])]++;
             ranges[student_test][range_marks(test_marks[student_count])]++;
             ranges[student_exam][range_marks(exam_marks[student_count])]++;      
           
     infile>>student_id[student_count]>>seperator[student_count]
           >>assignment_marks[student_count]>>seperator[student_count]
           >>lab_marks[student_count]>>seperator[student_count]
           >>test_marks[student_count]>>seperator[student_count]
           >>exam_marks[student_count];     
           
           
}while(!infile.eof());

return;
}

//function to ouput the range of marks
void marks::range_display()
{
  
cout<<"\n\n"<<"\t"<<setw(10)<<"Assignment"<<setw(9)<<"Lab"<<setw(1)<<"\t"<<"Test"<<"\t"<<"Exam"<<endl; 

cout << "\n0-19" <<setw(10)<<ranges[student_assignment][0]
                 <<setw(12)<<ranges[student_labexercise][0]
                 <<setw(8)<<ranges[student_test][0]
                 <<setw(9)<<ranges[student_exam][0];
                 
cout << "\n20-39"<<setw(9)<<ranges[student_assignment][1]
                 <<setw(12)<<ranges[student_labexercise][1]
                 <<setw(8)<<ranges[student_test][1]
                 <<setw(9)<<ranges[student_exam][1];
                 
cout << "\n40-59"<<setw(9)<<ranges[student_assignment][2]
                 <<setw(12)<<ranges[student_labexercise][2]
                 <<setw(8)<<ranges[student_test][2]
                 <<setw(9)<<ranges[student_exam][2];
                 
cout << "\n60-79"<<setw(9)<<ranges[student_assignment][3]
                 <<setw(12)<<ranges[student_labexercise][3]
                 <<setw(8)<<ranges[student_test][3]
                 <<setw(9)<<ranges[student_exam][3];
                 
cout << "\n80-100"<<setw(8)<<ranges[student_assignment][4]
                  <<setw(12)<<ranges[student_labexercise][4]
                  <<setw(8)<<ranges[student_test][4]
                  <<setw(9)<<ranges[student_exam][4];
                  
cout<<"\n\n\n\n"; 
    
return;
}

//function to calculate the final marks
double marks::calculate_finalmark(double& finalmark)
{     
      finalmark=(assignment_marks[student_count]*0.1+lab_marks[student_count]*0.1+test_marks[student_count]*0.2+exam_marks[student_count]*0.6);
                 
 return finalmark;
}

//function to display result for the final marks      
void marks::display_result()
{   
     ifstream inresult;
     inresult.open("datafile.txt");  

     double finalresult;

     cout<<setw(4)<<"Student ID"
         <<setw(13)<<"Assignment"
         <<setw(7)<<"Lab"
         <<setw(9)<<"Test"
         <<setw(7)<<"Exam"
         <<setw(13)<<"Final Mark"<<"\n"<<endl;
             
   inresult>>student_id[student_count]>>seperator[student_count]
           >>assignment_marks[student_count]>>seperator[student_count]
           >>lab_marks[student_count]>>seperator[student_count]
           >>test_marks[student_count]>>seperator[student_count]
           >>exam_marks[student_count];               

//output the result   
do{
         cout<<setw(8)<<student_id[student_count]
             <<setw(11)<<assignment_marks[student_count]
             <<setw(10)<<lab_marks[student_count]
             <<setw(8)<<test_marks[student_count]
             <<setw(7)<<exam_marks[student_count]
             <<setw(11)<<fixed<<setprecision(2)<<calculate_finalmark(finalresult)<<endl;
             
   inresult>>student_id[student_count]>>seperator[student_count]
           >>assignment_marks[student_count]>>seperator[student_count]
           >>lab_marks[student_count]>>seperator[student_count]
           >>test_marks[student_count]>>seperator[student_count]
           >>exam_marks[student_count];  
                
}while(!inresult.eof());

cout<<"\n\n\n\n";

return;
}

good program..

i'll throw in my 2 cents for improvement... take it or leave it:

this will only initialize the [0][0] element:

int ranges[first_dim][second_dim]={0};

instead ye' should try this:
(otherwise you'll have mystery stuff in your array.. and you won't know why it's there)

for(int i=0; i<4; i++)
{
     for(int j=0, j<5; j++)
     {
          ranges[i][j] = 0;
     }
}

Delete lines #126 through 130; move line #119 to line #112.
Delete lines #212 through 216; move line #204 to line #196.

Otherwise, good job on ye' program.

Oh yeah of course, a two dimensional array, to initialize it to 0 must use a loop,thanx man, you helped me a lots... :)

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.