I want to do two-dimensional array..

STUDENT1 take 001, 003, 005,006
STUDENT2 take 005, 007, 009, 001

001 002 003 004
like this 001 0 2 1 ....
002
003
004

and I do a programming like below, but how to fill inside the { }?

// --------------------------------------------------------------
// Read a file of Stu83.txt
// Count number of ExamID in following ranges
// --------------------------------------------------------------


#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <cassert>
#include <stdlib.h>
using namespace std;

// =========================
// Global Declarations
// =========================
const int NUM_EXAMID = 140


int var_name[140]; // C++ Array of type int with maximum size = 10000.
int array_matrix[140][140]; //Matrix of 3 * 3 type using c++ array of int data type
// =========================
// Main Function
// =========================
int main()
{
	ifstream stream1 ("STA83STU.txt");
	string filename;
	double examid[NUM_EXAMID];  // declares 10 scores
	// Prompt user for the file name

	cout << "Enter input file name containing scores: ";
	getline(cin, filename);

	infile.open(filename.c_str());

	// Validate that the input file opened successfully
	assert(infile);
	char lgrade;
	int Acount = 0, Bcount = 0, Ccount = 0, Dcount = 0, Fcount = 0, invalidcount = 0;

	for ( int i = 0; i < NUM_EXAMID; ++i)
	{
		infile >> examid[i];
		

		switch (ltotal)
		{
			
		}
		
		cout << examid[i] << " => " << ltotal << endl;  
	}

	// Display counter statistics


	infile.close();
	
	// TODO: Write a function printArray that prints the entire array
	// of scores.  Pass the array in as a parameter.

		cout << "Enter input file name containing examid: ";
	getline(cin, filename);

	infile.open(filename.c_str());

	// Validate that the input file opened successfully
	assert(infile);

	printScores(scores, NUM_EXAMID);



	return 0;
}




void printExamID(const double s[], int len)
{
	

}

Dani AI

Generated

The original posts show the intent: build a 2D table that records which exam IDs each student took (or how many times). The simplest, robust model is: rows = unique ExamIDs, columns = students, cell = 0/1 (or a count). Treat exam IDs as strings if they include leading zeros ("001"), collect all unique IDs first, map each ID to a row index, then build a zero-filled matrix and increment the appropriate cells for each student. 's points about using an integer type only when IDs are numeric and about not relying on assert for file-open checks are correct; file-open should be tested and handled.

A compact workflow (first pass: parse and collect IDs per student; second pass: allocate and fill matrix). Example implementation sketch:

#include <fstream>
#include <sstream>
#include <vector>
#include <string>
#include <map>
#include <set>

std::vector<std::vector<std::string>> student_lists;
std::set<std::string> all_ids;

// parse file: one student per line, tokens are exam IDs
std::ifstream in("STA83STU.txt");
if (!in) return 1; // handle file error

std::string line;
while (std::getline(in, line)) {
  if (line.empty()) continue;
  std::istringstream iss(line);
  std::string id;
  std::vector<std::string> ids;
  while (iss >> id) { ids.push_back(id); all_ids.insert(id); }
  student_lists.push_back(ids);
}

// build ID->row mapping and matrix
std::vector<std::string> ids(all_ids.begin(), all_ids.end());
std::map<std::string,int> id_to_row;
for (int r = 0; r < (int)ids.size(); ++r) id_to_row[ids[r]] = r;

std::vector<std::vector<int>> matrix(ids.size(), std::vector<int>(student_lists.size(), 0));
for (int c = 0; c < (int)student_lists.size(); ++c)
  for (auto &id : student_lists[c])
    ++matrix[id_to_row[id]][c];

Notes and pitfalls: preserve leading zeros by storing IDs as strings; initialize the matrix to zero to avoid garbage; check bounds if switching to fixed-size arrays; prefer std::vector/std::map for dynamic sizes. If the original switch (ltotal) was intended to map totals to letter grades, compute ltotal before the switch and use int for scores. See the C++ I/O and string-stream docs for details (e.g., std::ifstream and std::istringstream).

line 30: why is that a double instead of an int ? Do exam IDs contain decimal places ?

line 39: assert does nothing if the program is not compiled for debug. And it does not stop the program if the file fails to open. In otherwords, its not useful for what you are trying to use it for. Instead you should test for valid opening

if( !infile.is_open())
{
   cout << "File not opened\n";
   return 1; // don't continue the program
}

line 48: I don't know what you want to do in that switch statement.

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.