Dear all,

I have one problem,..I want to count how many examcode and fill in the blank in the matrix below that I have done.

Example is ,, for examcode 1 how many it appear. and for examcode 2 and examcode 1, how many student take both of them??

/*---------------------------
Filename : ConflictMatrix.cpp
---------------------------*/

#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <vector>
using namespace std;

   struct student
{
   string studentid;
   vector <int> examcode;   
};


   int main()
{
   ifstream stream1 ("STA83STU.txt");

   if ( !stream1 )
{
   cout << "While opening a file an error is encountered" << endl;
}
   else
{
   cout << "File is successfully opened" << endl;
}  
   vector <student> students;
   student aStudent;
   string tempStudentID;
   bool readEntireFile = false;     // set to true when reach end of file

   stream1 >> tempStudentID;    //  read in student id of first student
   while ( !readEntireFile )
{
   aStudent.studentid = tempStudentID;  //  new student
   int tempExamCode;
   aStudent.examcode.clear ();
   stream1 >> tempExamCode;    // read in first exam code for this student
   aStudent.examcode.push_back (tempExamCode);  // add this exam code to current student's vector of exam codes
   bool newStudent = false;   // true when a new student id is encountered
   while ( !newStudent && !readEntireFile )
{
   if ( stream1 >> tempStudentID )   // successfully read in student id
{
   if ( tempStudentID.compare (aStudent.studentid) == 0 )  // student id is same as before
{
   stream1 >> tempExamCode;   // read in exam code
   aStudent.examcode.push_back (tempExamCode); // add this exam code to this student;s vector of exam codes
}
   else
   newStudent = true;   // student id is different from before.  Therefore new student.
}
   else
   readEntireFile = true;  // end of file reached.  Want to exit inner and outer while loops
}  // if new student, do not repeat this while loop
   students.push_back (aStudent);   // no more exam codes for this student.  Add aStudent to students vector
}
   stream1.close ();  // We have read the entire file, so time to close it.

   for ( int i = 0; i < students.size (); i++ )
{
   cout << students.at (i).studentid << endl;   // output student id
   for ( int j = 0; j < students.at (i).examcode.size (); j++ )
   cout << students.at (i).examcode.at (j) << "\t";   // output list of exam codes for this student
   cout <<"\n"<<endl;       
} 
    

/*----------
Count ExamID
----------*/

cout<<"i,j|";
for (int colHead = 1; colHead < 11; colHead++)
cout << setw(3.5) << colHead;
cout << endl;
cout <<"__________________________________"<<endl;

for (int rowVal = 1; rowVal < 11; rowVal++)
{
cout << setw(3.5)<< rowVal <<'|';

for (int colVal =1; colVal < 11; colVal++)
cout << setw(3.5)<<"";
cout <<endl;
}
return 0;
}

I want to do like this, but how to open the file to read the examid?

int mycount;


/*-------------------------
 counting elements in array
------------------------*/
  int myints[] = {10,20,30,30,20,10,10,20};   // 8 elements
  mycount = (int) count (myints, myints+8, 10);
  cout << "10 appears " << mycount << " times.\n";

/*-----------------------------
 counting elements in container
-----------------------------*/
  vector<int> myvector (myints, myints+8);
  mycount = (int) count (myvector.begin(), myvector.end(), 20);
  cout << "20 appears " << mycount  << " times.\n";
  return 0;
}

>>I want to do like this, but how to open the file to read the examid?
Not sure what you want? This maybe: ifstream in("filename");

which part to put it?

First you have to know what the file looks like. Post the first 4 or 5 lines.

This the file, which s1 means student 1 take examcode 0003, 0013, 0034....

I want to count, for examcode 0001 and 0002 , how many student take both of the exam. and furthermore.

s1 0003
s1 0013
s1 0034
s1 0054
s1 0071
s1 0081
s1 0097
s1 0105
s1 0116
s1 0135
s1 0138
s2 0003
s2 0015
s2 0036
s2 0056
s2 0071
s2 0083
s2 0097
s2 0106
s2 0118
s2 0135
s2 0138
s3 0003
s3 0010
s3 0031
s3 0051
s3 0071
s3 0078
s3 0097
s3 0105
s3 0113
s3 0135
s3 0138
s4 0003
s4 0011
s4 0032
s4 0052
s4 0071
s4 0079
s4 0097
s4 0106
s4 0114
s4 0135
s4 0138
s5 0003
s5 0011
s5 0032
s5 0052
s5 0071
s5 0079
s5 0097
s5 0106
s5 0114
s5 0135
s5 0138
s6 0003
s6 0016
s6 0037
s6 0057
s6 0071
s6 0084
s6 0097
s6 0105
s6 0119
s6 0135
s6 0138
s7 0003
s7 0009
s7 0030
s7 0050
s7 0071
s7 0077
s7 0097
s7 0105
s7 0112
s7 0135
s7 0138
s8 0003
s8 0016
s8 0037
s8 0057
s8 0071
s8 0084
s8 0097
s8 0106
s8 0119
s8 0135
s8 0138
s9 0003
s9 0009
s9 0030
s9 0050
s9 0071
s9 0077
s9 0097
s9 0106
s9 0112
s9 0135
s9 0138

There are several ways to resolve the problem. Maybe the simplest is to have a vector of structures

struct exams
{
    int id;
    int count;
};

Then have a vector of those structures. When you read a line search the vector to see if there is a structure for the exam id that was just read. If there is, then increment the count. If not, add a new structure

int course;
string student;
..
...
vector<exams> ex;
while( in >> student >> course)
{
     // search ex to see if there is a structure already in it
     // with the same course number.  If there is then just
     // increment the count.
     // if not, then add one and set the count to 1.
}

<map> would be more efficient way to do it, but that depends on if you know how to use it.

So, I must add this structure?

struct exams
{
    int id;
    int count;
}

Don't reinvent the wheel. You already have a struct that works and you already have a successful method of reading the file into the data. See Post # 1 of this thread. You may want to do all sorts of stuff with that data, and in doing so, you may possibly create a new struct to help you do it or add to the old struct, but you've already read in the data and stored it in a vector of struct student. You don't need to read in the data again.

Thank you master Vernon,

I'm very appreciate it. But , I want to count the examcode..and fill in the blank in the matrix.

i,j| 1 2 3 4 5 6 7 8 9 10
__________________________________
1| x x x x x x x x x x
2| x x x x x x x x x x
3| x x x x x x x x x x
4| x x x x x x x x x x
5| x x x x x x x x x x
6| x x x x x x x x x x
7| x x x x x x x x x x
8| x x x x x x x x x x
9| x x x x x x x x x x
10| x x x x x x x x x x

I study from the book how to make a programming count. But how to call the data?

int mycount;
/*----------------------------
 counting elements in array
------------------------------*/
  int myints[] = {10,20,30,30,20,10,10,20};   // 8 elements
  mycount = (int) count (myints, myints+8, 10);
  cout << "10 appears " << mycount << " times.\n";

/*-----------------------------
 counting elements in container
-----------------------------*/
  vector<int> myvector (myints, myints+8);
  mycount = (int) count (myvector.begin(), myvector.end(), 20);
  cout << "20 appears " << mycount  << " times.\n";
  return 0;
}

I'm very appreciate it. But , I want to count the examcode..and fill in the blank in the matrix.

O.K., then why are you asking this?

I want to do like this, but how to open the file to read the examid?

You've already done that.

How to count examid?

How to count examid?

Once again, like I pointed out in a previous post in a previous thread, there is no examid. You continue to ask for advice in non-trivial tasks with four word sentences and you don't even proofread those short sentences to make sure they make any sense. Proofread your posts. This has become a pattern in every single thread you have started.

struct student
{
   string studentid;
   vector <int> examcode;   
};

yes...there is no examid..but i want to count examcode, I wrong.sorry.

1: 3 15 36 56 71 83 97 106 118 135 138

for examcode 003 , I want to fill in the matrix and then count..

i,j| 1 2 3 4 5 6 7 8 9 10..15 36 56 71 83
______________________________________
1| x x x x x x x x x x
2| x x x x x x x x x x
3| x x x x x x x x x x 1 1 1 1 1
4| x x x x x x x x x x
5| x x x x x x x x x x
6| x x x x x x x x x x
7| x x x x x x x x x x
8| x x x x x x x x x x
9| x x x x x x x x x x
10| x x x x x x x x x x

for examcode 1 and examcode 2 , how many take simultaneously.

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.