pair

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Nov 2007
Posts: 219
Reputation: nurulshidanoni is an unknown quantity at this point 
Solved Threads: 0
nurulshidanoni's Avatar
nurulshidanoni nurulshidanoni is offline Offline
Posting Whiz in Training

pair

 
0
  #1
Mar 4th, 2008
How to make a pair programming..

like I have 1 2 3 4 5 6 7
First, I want to make a pair of 91,20
(1,3)
(1,4)
(1,5)

using i=1, i<9,i++
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 219
Reputation: nurulshidanoni is an unknown quantity at this point 
Solved Threads: 0
nurulshidanoni's Avatar
nurulshidanoni nurulshidanoni is offline Offline
Posting Whiz in Training

Re: pair

 
0
  #2
Mar 4th, 2008
I want to do like this, but how to call from i=0, i<11; i++, and how o insert in this program.



  1.  
  2. #include <iostream.h>
  3.  
  4. template<class C>
  5. class Pair
  6. {
  7. public:
  8. Pair(C, C);
  9. Pair(Pair&);
  10. void Display();
  11.  
  12. private:
  13. C first;
  14. C second;
  15. };
  16.  
  17. template<class C>
  18. Pair<C>::Pair(C left, C right)
  19. {
  20. first = left;
  21. second = right;
  22. }
  23.  
  24.  
  25. template<class C>
  26. Pair<C>::Pair(Pair<C>& p)
  27. {
  28. first = p.first;
  29. second = p.second;
  30. }
  31.  
  32. template<class C>
  33. void Pair<C>::Display()
  34. {
  35. cout<<"(" << first <<","<< second <<")"<<"\n";
  36. }
  37.  
  38. int main()
  39. {
  40. Pair<int> pi(2,3);
  41.  
  42. pi.Display();
  43.  
  44. return 0;
  45. }
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 62
Reputation: Joatmon is an unknown quantity at this point 
Solved Threads: 7
Joatmon Joatmon is offline Offline
Junior Poster in Training

Re: pair

 
0
  #3
Mar 4th, 2008
Well since you want to do it in a for loop, you must realize that this is probably a job for pointers since you can't make a for loop generate names for your variables to hold the class. What I would do is this:

  1. int main()
  2. {
  3. const int SIZE = 8;
  4. Pair<int> * pairs[SIZE]; // Declare an array of pointers to Pair<int>
  5. for (int i = 0; i < SIZE; i++)
  6. pairs[i] = new Pair<int>(1,i); // Construct each new pair and give its address to an index in the pointer array
  7.  
  8. pairs[3]->Display(); //since Display is a member function of an object, and pairs[num] is a pointer to that object, you use the arrow operator to dereference and call the member function
  9. return 0;
  10. }
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 219
Reputation: nurulshidanoni is an unknown quantity at this point 
Solved Threads: 0
nurulshidanoni's Avatar
nurulshidanoni nurulshidanoni is offline Offline
Posting Whiz in Training

Re: pair

 
0
  #4
Mar 4th, 2008
I dont understand
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 62
Reputation: Joatmon is an unknown quantity at this point 
Solved Threads: 7
Joatmon Joatmon is offline Offline
Junior Poster in Training

Re: pair

 
0
  #5
Mar 4th, 2008
If you do not understand what pointers are or how they work, it would not be fitting to describe every aspect of them in this thread.

I will however give you a good tutorial on pointers, and when you practice with their examples, you should have a decent grasp of them, then you may come back to the example I gave previously and it should seem more clear.

http://www.cplusplus.com/doc/tutorial/pointers.html
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 219
Reputation: nurulshidanoni is an unknown quantity at this point 
Solved Threads: 0
nurulshidanoni's Avatar
nurulshidanoni nurulshidanoni is offline Offline
Posting Whiz in Training

Re: pair

 
0
  #6
Mar 4th, 2008
I understand now, but a lot of error.


error C2065: 'pairs' : undeclared identifier
error C2109: subscript requires array or pointer type
error C2061: syntax error : identifier 'Pair'
error C2109: subscript requires array or pointer type
error C2227: left of '->Display' must point to class/struct/union
error C2143: syntax error : missing ';' before '}'
error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : fatal error C1003: error count exceeds 100; stopping compilation
Error executing cl.exe.

78.exe - 102 error(s), 0 warning(s)
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 62
Reputation: Joatmon is an unknown quantity at this point 
Solved Threads: 7
Joatmon Joatmon is offline Offline
Junior Poster in Training

Re: pair

 
0
  #7
Mar 4th, 2008
Can you post your source now please? (and edit out the big error list, it is mostly the same error over and over again and is just a big waste of space)

I will see what you have and try to help you out.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 219
Reputation: nurulshidanoni is an unknown quantity at this point 
Solved Threads: 0
nurulshidanoni's Avatar
nurulshidanoni nurulshidanoni is offline Offline
Posting Whiz in Training

Re: pair

 
0
  #8
Mar 4th, 2008
Here it is..

  1. #include <iostream>
  2. usingnamespace std;
  3. {
  4. const int students.size = 12;
  5. Pair<int> * pairs[students.size]; // Declare an array of pointers to Pair<int>
  6. for (int i = 0; i < students.size; i++)
  7. pairs[i] = new Pair<int>(1,i); // Construct each new pair and give its address to an index in the pointer array
  8.  
  9. pairs[3]->Display(); //since Display is a member function of an object, and pairs[num] is a pointer to that object, you use the arrow operator to dereference and call the member function
  10.  
  11. return 0;
  12. }
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 62
Reputation: Joatmon is an unknown quantity at this point 
Solved Threads: 7
Joatmon Joatmon is offline Offline
Junior Poster in Training

Re: pair

 
0
  #9
Mar 4th, 2008
1.
Did you make sure you kept what you had in front of main? I remove that from the code i posted as it is the same as you had, only the things in main were what i changed.

2.
students.size is referring to a data member of an object that doesn't exist. Moreover, since it would be a data member inside an object, it couldn't be declared again. Use a plain variable name instead of students.size. maybe students_size
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 219
Reputation: nurulshidanoni is an unknown quantity at this point 
Solved Threads: 0
nurulshidanoni's Avatar
nurulshidanoni nurulshidanoni is offline Offline
Posting Whiz in Training

Re: pair

 
0
  #10
Mar 4th, 2008
This is a full code. but error.fatal error C1003: error count exceeds 100; stopping compilation
Error executing cl.exe.

is it cannot exceed 100?
  1. /*------------------------------------------------------------------------------------------+
  2. | Filename : ConflictMatrix.cpp |
  3. +------------------------------------------------------------------------------------------*/
  4.  
  5. #include <iostream> // std::cout
  6. #include <fstream>
  7. #include <iomanip>
  8. #include <string> // std::string
  9. #include <vector> // std::vector<>
  10. #include <algorithm> //std::for each()
  11. using namespace std; // import "std" namespace into global namespace
  12.  
  13.  
  14. struct student
  15. {
  16. string studentid;
  17. vector <int> examcode;
  18. };
  19. int main()
  20. {
  21. ifstream stream1 ("STA83STU.txt");
  22. if ( !stream1 )
  23. {
  24. cout << "While opening a file an error is encountered" << endl;
  25. }
  26. else
  27. {
  28. cout << "File is successfully opened" << endl;
  29. }
  30.  
  31. vector <student> students;
  32. student aStudent;
  33. string tempStudentID;
  34. bool readEntireFile = false; // set to true when reach end of file
  35.  
  36. stream1 >> tempStudentID; // read in student id of first student
  37. while ( !readEntireFile )
  38.  
  39. {
  40.  
  41. aStudent.studentid = tempStudentID; // new student
  42. int tempExamCode;
  43. aStudent.examcode.clear ();
  44. stream1 >> tempExamCode; // read in first exam code for this student
  45. aStudent.examcode.push_back (tempExamCode); // add this exam code to current student's vector of exam codes
  46. bool newStudent = false; // true when a new student id is encountered
  47. while ( !newStudent && !readEntireFile )
  48.  
  49. {
  50.  
  51. if ( stream1 >> tempStudentID ) // successfully read in student id
  52.  
  53. {
  54. if ( tempStudentID.compare (aStudent.studentid) == 0 ) // student id is same as before
  55.  
  56. {
  57. stream1 >> tempExamCode; // read in exam code
  58. aStudent.examcode.push_back (tempExamCode); // add this exam code to this student;s vector of exam codes
  59. }
  60.  
  61. else
  62. newStudent = true; // student id is different from before. Therefore new student.
  63. }
  64.  
  65. else
  66. readEntireFile = true; // end of file reached. Want to exit inner and outer while loops
  67. } // if new student, do not repeat this while loop
  68.  
  69. students.push_back (aStudent); // no more exam codes for this student. Add aStudent to students vector
  70. }
  71.  
  72. stream1.close (); // We have read the entire file, so time to close it.
  73.  
  74. for ( int i = 0 ; i < students.size (); i++ )
  75. {
  76. cout <<i<<":\t" ; // output student id
  77. for ( int j = 0;j<students.at(i).examcode.size(); j++ )
  78. cout << students.at (i).examcode.at(j) <<"\t"; // output list of exam codes for this student
  79. cout<<""<<"\n";
  80. }
  81. /*--------------------------------------------------------------------------------------------+
  82. | counting elements in StudentID |
  83. +--------------------------------------------------------------------------------------------*/
  84.  
  85. {
  86. const int student_size = 12;
  87. Pair<int> * pairs[student_size]; // Declare an array of pointers to Pair<int>
  88. for (int i = 0; i < student_size; i++)
  89. pairs[i] = new Pair<int>(1,i); // Construct each new pair and give its address to an index in the pointer array
  90.  
  91. pairs[3]->Display(); //since Display is a member function of an object, and pairs[num] is a pointer to that object, you use the arrow operator to dereference and call the member function
  92.  
  93. return 0;
  94. }
  95. /*-------------------------------------------------------------------------------------------+
  96. | Matrix ExamID
  97. +--------------------------------------------------------------------------------------------*/
  98. {
  99. cout<<"i,j|";
  100. for (int colHead = 1; colHead < 11; colHead++)
  101. cout << setw(3) << colHead;
  102. cout << endl;
  103. cout <<"__________________________________"<<endl;
  104.  
  105. for (int rowVal = 1; rowVal < 11; rowVal++)
  106. {
  107. cout << setw(3)<< rowVal <<'|';
  108.  
  109. for (int colVal =1; colVal < 11; colVal++)
  110. cout << setw(3)<<"x";
  111. cout <<endl;
  112. }
  113. }
  114. }
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC