How to count how many (3, 10 appears in data?
I have done a little bit programming but abnormal program.

(3 , 10)
(3 , 31)
(3 , 51)
(3 , 71)
(3 , 78)
(3 , 97)
(3 , 105)
(3 , 113)
(3 , 135)
(3 , 138)

(3 , 9)
(3 , 30)
(3 , 50)
(3 , 71)
(3 , 77)
(3 , 97)
(3 , 106)
(3 , 112)
(3 , 135)
(3 , 138)


(3 , 10)
(3 , 31)
(3 , 51)
(3 , 71)
(3 , 78)
(3 , 97)
(3 , 106)
(3 , 113)
(3 , 135)
(3 , 138)


(3 , 15)
(3 , 36)
(3 , 56)
(3 , 71)
(3 , 83)
(3 , 97)
(3 , 105)
(3 , 118)
(3 , 135)
(3 , 138)


(3 , 15)
(3 , 36)
(3 , 56)
(3 , 71)
(3 , 83)
(3 , 97)
(3 , 106)
(3 , 118)
(3 , 135)
(3 , 138)


(3 , 10)
(3 , 31)
(3 , 51)
(3 , 71)
(3 , 78)
(3 , 97)
(3 , 106)
(3 , 113)
(3 , 135)
(3 , 138)

int mycount;
            int k; 
			int myints[] = {students.at (i).examcode.at(0),students.at(i).examcode.at(k)};
 			mycount = (int) count (myints, myints+12, 1);
  
			for ( int i = 0 ; i < students.size (); i++ )
			for ( int k = 0+1;k<students.at(i).examcode.size(); k++ )
			cout << "1" << mycount <<"  \n";

Dani AI

Generated

The goal here is to count occurrences of an ordered pair (for example (3,10)) across all records. has the right idea of iterating student records, and correctly pointed out string matching as one way, but treating pairs as numeric keys and counting them with a map is safer, faster, and less fragile than comparing text. 's point about clean formatting and using a good IDE still stands — it makes logic errors easier to spot.

A robust pattern: iterate each record, build a key from the two integers you want to count, and increment a counter stored in an associative container. For modern C++ use an unordered_map keyed by std::pair with a small hash. This does one pass over the data and gives O(1) average cost per increment. If you must support older compilers, use std::map<std::pair<int,int>,int> or pack the two ints into a single 64-bit integer as a key.

Example (keeps structure separate from file I/O and from whatever Student type you already have):

#include <unordered_map>
#include <vector>
#include <utility>
#include <cstdint>

struct PairHash {
  std::size_t operator()(const std::pair<int,int>& p) const noexcept {
    uint64_t key = (uint64_t)(uint32_t)p.first << 32 | (uint32_t)p.second;
    return std::hash<uint64_t>()(key);
  }
};

// rows is a vector of integer lists; row[0] is the first value and row[k] are the partners
std::unordered_map<std::pair<int,int>,int,PairHash> count_pairs(const std::vector<std::vector<int>>& rows) {
  std::unordered_map<std::pair<int,int>,int,PairHash> counts;
  for (auto const &r : rows) {
    if (r.size() < 2) continue;
    int a = r[0];
    for (size_t k = 1; k < r.size(); ++k) ++counts[{a, r[k]}];
  }
  return counts;
}

Troubleshooting tips: always check bounds (use size_t for indices), avoid = when you mean ==, prefer operator[] or structured access for speed once correctness is proven, and compile with warnings enabled to catch common mistakes. Query the map for the specific pair (3,10) after building counts.

Recommended Answers

All 4 Replies

If this data appears in a .txt file you could read the .txt file line by line and pass (3, 10) to a string. When having these contents in the string you could make another string:

std::string Value = "(3, 10)";

From here you could compare Value with the lines you are reading and do a
count = (count + 1).

How to count how many (3, 10 appears in data?
I have done a little bit programming but abnormal program.

int mycount;
            int k; 
			int myints[] = {students.at (i).examcode.at(0),students.at(i).examcode.at(k)};
 			mycount = (int) count (myints, myints+12, 1);
  
			for ( int i = 0 ; i < students.size (); i++ )
			for ( int k = 0+1;k<students.at(i).examcode.size(); k++ )
			cout << "1" << mycount <<"  \n";

nurulshidanoni, start formatting your code better. I have lost count of how many times you have been asked to do this, but your code has such bizarre spacing that code tags, which are there in part to preserve formatting, are little help. I know you program in Windows. Get Visual Studio Express. It's free and it has an easy to use formatting feature that lines everything up so it's easy to read. I don't know what IDE you use, but your tabs are like 15 spaces, which is overkill and makes everything hard to read since it consistently overflows to the next line. A tab of 5 spaces is plenty.

Is it like this? I dont know how to make count statement.

{
ifstream stream1 ("STA83STU.txt");
if ( !stream1 )
stream1.close (); 
std::string Value = "(3, 10)";
//From here you could compare Value with the lines you are reading and do a count = (count + 1). 
using std::string;// Now we can just say "string".

int count;

count = 0;
count= count+1;
std::cout <<  count << "\n"; // Prints 
    
return 0;
 }

I dont know how to do , because I want to declare students.at (i).(0),students.at (i).(k)";
not only (3,10) only.

std::string Value1 = "students.at (i).examcode.at(0),students.at (i).examcode.at(k)";
 using std::string;// Now we can just say "string".
     for ( int m= 1; m<students.at i).examcode.size(); m++)

 if(students.at (i).examcode.at(0)=m,students.at (i).examcode.at(k));
  {
 cout <<count++<<" ";
  else 

 }
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.