multidimension array questions

O

osmium

foker said:
for(int r = 0; r < 25; r++)
{
courses_out << course_name[r] << endl;
while(students_file >> student.first_name >> student.last_name >>
student_info[25] >> student.num_courses)


The variable i has no known value in this snippet. I notice that later you
use the same i - did you mean that? If a snippet is incomplete we have no
choice but to start where the snippet starts.

You might want to think about writing a function, purely for testing
purposes, that will print out the entire array. You can insert calls to it
in your mainline program whenever you get suspicious results..

<snip>
 
O

osmium

foker said:
I have this problem where I have 2 text files, one with student name,
id#, # of courses and course #, the second file has course name and
course number. I want to make a multidimensional array that takes the
student id into one portion and the course id into the other. This is
my code:

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

int main()
{
int i = 0;
int student_info[500][25];

I have a better understanding of what you are trying to do now. I would
change that declaration to:

boolean map[500][25]; // true denotes student i is enrolled in course j.

And initialize all the values to false.

Define two structures, one for student input, one for course input. Note
your instructor's comment "You will need additional
arrays to store the student and course information." Extract all the data
from the two input files and put it into two arrays of structures. Then
and only then, the map array above comes into play. If a student is
enrolled in a course, the intersection of student vs. course is set to true.
You were trying to fill the array "on the fly" - as the file is read. To
me, that is more complicated, there are so many things going on at once.
Once map is filled in I think (hope, actually) you can create the two output
files in a non-traumatic fashion. Even if there are problems creating the
output files I think my suggestions will lead to a necessary and usable
plateau.

You have probably been given some misinformation because we misunderstood
the assignment, keep this in mind as you review your position .
 
F

foker

So everythings almost working perfectly. I set my 2d array to type
bool. Read in everything and mapped it instead of trying to do it while
its reading data in still. My last question is, how can I make my
courses_out file output correctly?
#include <iostream>
#include <string>
#include <fstream>
using namespace std;

struct Students
{
string first_name, last_name;
int id, num_courses;
int course_ids;
};
struct Courses
{
string course_name;
int course_id;
};
int main()
{
Students student[500];
Courses course[25];
bool student_info[500][25];
//string course_name[25];
for(int x = 0; x < 500; x++)
{
for(int y = 0; y <25; y++)
student_info[x][y] = false;
}

ifstream courses_file;
courses_file.open("courses.txt");
if (courses_file.fail())
{
cerr << "Could not open courses.txt" << endl;
exit(1);
}
//students file = student name, id, number of classes, class id
ifstream students_file;
students_file.open("students.txt");
if (students_file.fail())
{
cerr << "Could not open students.txt" << endl;
exit(1);
}
//students output file = students name and courses they are in
ofstream students_out;
students_out.open("students_out.txt");
if (students_out.fail())
{
cerr << "Could not open students_out.txt" << endl;
exit(1);
}
//courses output file = courses and students enrolled in them
ofstream courses_out;
courses_out.open("courses_out.txt");
if (courses_out.fail())
{
cerr << "Could not open students_out.txt" << endl;
exit(1);
}

int i = 0, y = 0, x = 0, r = 0;

//Filling courses_name array with the names of the courses and filling
student_info[499] with the course id number
while(courses_file >> course.course_name >> course.course_id)
i++;

while(students_file >> student[r].first_name >> student[r].last_name
student[r].id >> student[r].num_courses)
{
//students_out << student[r].first_name << " " <<
student[r].last_name << endl;
for(i = 0; i < student[r].num_courses; i++)
{
students_file >> student.course_ids;
//cout << student.course_ids;
student_info[student[r].id][student.course_ids] = true;
}
students_out << student[r].first_name << " " << student[r].last_name
<< endl;
for(x = 0; x < 25; x++)
if(student_info[student[r].id][x] == true)
{
students_out << course[x].course_name << endl;
}

if(student_info[student[r].id][r] == true)
{

courses_out << student[r].first_name << " " <<
student[r].last_name << endl;
}
}
students_out.clear();
students_out.close();
courses_out.clear();
courses_out.close();
return 0;
}

That is my current code. The problem here is, first course is cs101 so
it goes through the whole 2d array and prints out everyone who is in
cs101 based on which cells are true. Thats great but I stop right
there. I cant figure out how to print out the next course and then go
through the 2d array again.

I need the final output to look like
course1
student name
however many of them there are
.....
.....
course 2
student name
......
.....
.....
course 3
.......
and so on

can you help me?

foker said:
I have this problem where I have 2 text files, one with student name,
id#, # of courses and course #, the second file has course name and
course number. I want to make a multidimensional array that takes the
student id into one portion and the course id into the other. This is
my code:

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

int main()
{
int i = 0;
int student_info[500][25];

I have a better understanding of what you are trying to do now. I would
change that declaration to:

boolean map[500][25]; // true denotes student i is enrolled in course j.

And initialize all the values to false.

Define two structures, one for student input, one for course input. Note
your instructor's comment "You will need additional
arrays to store the student and course information." Extract all the data
from the two input files and put it into two arrays of structures. Then
and only then, the map array above comes into play. If a student is
enrolled in a course, the intersection of student vs. course is set to true.
You were trying to fill the array "on the fly" - as the file is read. To
me, that is more complicated, there are so many things going on at once.
Once map is filled in I think (hope, actually) you can create the two output
files in a non-traumatic fashion. Even if there are problems creating the
output files I think my suggestions will lead to a necessary and usable
plateau.

You have probably been given some misinformation because we misunderstood
the assignment, keep this in mind as you review your position .
 
F

foker

Please don't top-post. Your replies belong following or interspersed
with properly trimmed quotes. See the majority of other posts in the
newsgroup, or the group FAQ list:
<http://www.parashift.com/c++-faq-lite/how-to-post.html>

Oh sorry is this better?
So everythings almost working perfectly. I set my 2d array to type
bool. Read in everything and mapped it instead of trying to do it while

its reading data in still. My last question is, how can I make my
courses_out file output correctly?
#include <iostream>
#include <string>
#include <fstream>
using namespace std;

struct Students
{
string first_name, last_name;
int id, num_courses;
int course_ids;


};


struct Courses
{
string course_name;
int course_id;

};


int main()
{
Students student[500];
Courses course[25];
bool student_info[500][25];
//string course_name[25];
for(int x = 0; x < 500; x++)
{
for(int y = 0; y <25; y++)
student_info[x][y] = false;
}

ifstream courses_file;
courses_file.open("courses.txt");
if (courses_file.fail())
{
cerr << "Could not open courses.txt" << endl;
exit(1);
}
//students file = student name, id, number of classes, class id

ifstream students_file;
students_file.open("students.txt");
if (students_file.fail())
{
cerr << "Could not open students.txt" << endl;
exit(1);
}
//students output file = students name and courses they are in
ofstream students_out;
students_out.open("students_out.txt");
if (students_out.fail())
{
cerr << "Could not open students_out.txt" << endl;
exit(1);
}
//courses output file = courses and students enrolled in them
ofstream courses_out;
courses_out.open("courses_out.txt");
if (courses_out.fail())
{
cerr << "Could not open students_out.txt" << endl;
exit(1);
}


int i = 0, y = 0, x = 0, r = 0;


//Filling courses_name array with the names of the courses and
filling
student_info[499] with the course id number
while(courses_file >> course.course_name >>
course.course_id)
i++;


while(students_file >> student[r].first_name >>
student[r].last_name

student[r].id >> student[r].num_courses)


{
//students_out << student[r].first_name << " "
<<
student[r].last_name << endl;
for(i = 0; i < student[r].num_courses; i++)
{
students_file >> student.course_ids;

//cout << student.course_ids;

student_info[student[r].id][student.course_ids] = true;
}
students_out << student[r].first_name << " " <<
student[r].last_name
<< endl;
for(x = 0; x < 25; x++)
if(student_info[student[r].id][x] == true)
{
students_out << course[x].course_name
<< endl;
}


if(student_info[student[r].id][r] == true)
{


courses_out <<
student[r].first_name << " " <<
student[r].last_name << endl;
}
}
students_out.clear();
students_out.close();
courses_out.clear();
courses_out.close();
return 0;



}


That is my current code. The problem here is, first course is cs101 so
it goes through the whole 2d array and prints out everyone who is in
cs101 based on which cells are true. Thats great but I stop right
there. I cant figure out how to print out the next course and then go
through the 2d array again.

I need the final output to look like
course1
student name
however many of them there are
.....
.....
course 2
student name
......
.....
.....
course 3
.......
and so on


can you help me?
 
D

Default User

foker said:
Oh sorry is this better?

Better, but not perfect. You left out the attribution line, the "who
said it" part. That makes the discussion easier to follow. Google
should do that for you.





Brian
 
F

foker

Default said:
Better, but not perfect. You left out the attribution line, the "who
said it" part. That makes the discussion easier to follow. Google
should do that for you.
well its not a problem withe syntax or code im having a problem with
the logic part of it.
 
O

osmium

foker said:
So everythings almost working perfectly. I set my 2d array to type
bool. Read in everything and mapped it instead of trying to do it while

its reading data in still. My last question is, how can I make my
courses_out file output correctly?
#include <iostream>
#include <string>
#include <fstream>
using namespace std;

struct Students
{
string first_name, last_name;
int id, num_courses;
int course_ids;


};


struct Courses
{
string course_name;
int course_id;

};


int main()
{
Students student[500];
Courses course[25];
bool student_info[500][25];
//string course_name[25];
for(int x = 0; x < 500; x++)
{
for(int y = 0; y <25; y++)
student_info[x][y] = false;
}

ifstream courses_file;
courses_file.open("courses.txt");
if (courses_file.fail())
{
cerr << "Could not open courses.txt" << endl;
exit(1);
}
//students file = student name, id, number of classes, class id

ifstream students_file;
students_file.open("students.txt");
if (students_file.fail())
{
cerr << "Could not open students.txt" << endl;
exit(1);
}
//students output file = students name and courses they are in
ofstream students_out;
students_out.open("students_out.txt");
if (students_out.fail())
{
cerr << "Could not open students_out.txt" << endl;
exit(1);
}
//courses output file = courses and students enrolled in them
ofstream courses_out;
courses_out.open("courses_out.txt");
if (courses_out.fail())
{
cerr << "Could not open students_out.txt" << endl;
exit(1);
}


int i = 0, y = 0, x = 0, r = 0;


//Filling courses_name array with the names of the courses and
filling
student_info[499] with the course id number
while(courses_file >> course.course_name >>
course.course_id)
i++;


while(students_file >> student[r].first_name >>
student[r].last_name

student[r].id >> student[r].num_courses)


{
//students_out << student[r].first_name << " "
<<
student[r].last_name << endl;
for(i = 0; i < student[r].num_courses; i++)
{
students_file >> student.course_ids;

//cout << student.course_ids;

student_info[student[r].id][student.course_ids] = true;
}
students_out << student[r].first_name << " " <<
student[r].last_name
<< endl;
for(x = 0; x < 25; x++)
if(student_info[student[r].id][x] == true)
{
students_out << course[x].course_name
<< endl;
}


if(student_info[student[r].id][r] == true)
{


courses_out <<
student[r].first_name << " " <<
student[r].last_name << endl;
}
}
students_out.clear();
students_out.close();
courses_out.clear();
courses_out.close();
return 0;



}


That is my current code. The problem here is, first course is cs101 so
it goes through the whole 2d array and prints out everyone who is in
cs101 based on which cells are true. Thats great but I stop right
there. I cant figure out how to print out the next course and then go
through the 2d array again.

I need the final output to look like
course1
student name
however many of them there are
....
....
course 2
student name
.....
....
....
course 3
......
and so on


I would write a function that handles a single course and writes everything
that needs writing. The key parameter (in addition to others pretty
obviously required) would be an index in the range 0..24. Each time the
function is called it goes through all 500 students in student_info finding
the appropriate students to add to its list. Functions are great for
cleansing the mind, nested loops almost seem to disappear. This is surely
not due until Monday so you have time to learn how to pass two-dimensional
arrays as parameters.
 
D

David Harmon

On 8 Sep 2006 13:33:17 -0700 in comp.lang.c++, "foker"
That is my current code. The problem here is, first course is cs101 so
it goes through the whole 2d array and prints out everyone who is in
cs101 based on which cells are true. Thats great but I stop right
there. I cant figure out how to print out the next course and then go
through the 2d array again.

You need two independent output sections with nested loops.

The one for students_out goes
0 .. max_student
0 .. max_course

The one for courses_out goes
0 .. max_course
0 .. max_student

The first one may possibly be combined with the students_in file
reading, but I wouldn't (just for simplicity and clarity.)
 
F

foker

David said:
You need two independent output sections with nested loops.

The one for students_out goes
0 .. max_student
0 .. max_course

The one for courses_out goes
0 .. max_course
0 .. max_student

The first one may possibly be combined with the students_in file
reading, but I wouldn't (just for simplicity and clarity.)

Thanks for your help guys. I finished it and my final program looks
something like this:
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;

struct Students
{
int id, num_courses;
int course_ids;
};
struct Courses
{
string course_name;
int course_id;
};
int main()
{
Students student[500];
string fname[500], lname[500];
Courses course[25];

//2D array which maps student id's and course id's
bool student_info[500][25];

//Initializing my 2d array to false
for(int x = 0; x < 500; x++)
{
for(int y = 0; y <25; y++)
student_info[x][y] = false;
}

//Opening input courses_file and doing error checking
ifstream courses_file;
courses_file.open("courses.txt");
if (courses_file.fail())
{
cerr << "Could not open courses.txt" << endl;
exit(1);
}

//Opening input students_file and doing error checking
ifstream students_file;
students_file.open("students.txt");
if (students_file.fail())
{
cerr << "Could not open students.txt" << endl;
exit(1);
}

//Opening output file students_out and doing error checking
ofstream students_out;
students_out.open("students_out.txt");
if (students_out.fail())
{
cerr << "Could not open students_out.txt" << endl;
exit(1);
}

//Opening output file courses_out and doing error checking
ofstream courses_out;
courses_out.open("courses_out.txt");
if (courses_out.fail())
{
cerr << "Could not open students_out.txt" << endl;
exit(1);
}

int i = 0, y = 0, x = 0, r = 0, p = 0;

//Filling course struct array with course name and course id
while(courses_file >> course.course_name >> course.course_id)
i++;
//Filling arrays first and last name
while(students_file >> fname[p] >> lname[p] >> student[r].id >>
student[r].num_courses)
{
p++;
for(i = 0; i < student[r].num_courses; i++)
{
students_file >> student.course_ids;
//Setting student id and course id's to true in bool array
student_info[student[r].id][student.course_ids] = true;
}
}

//Output student names and courses they are in into students_out.txt
for(y = 0; y < 500; y++)
{
students_out << fname[y] << " " << lname[y] << endl;
for(x = 0; x < 25; x++)
{
if(student_info[y][x] == true)
{
students_out << course[x].course_name << endl;
}
}
}

//Output courses and students that are in them into courses_out.txt
for(x = 0; x < 25; x++)
{
courses_out << course[x].course_name << endl;
for(y = 0; y < 500; y++)
{
if(student_info[y][x] == true)
{
courses_out << setw(15) << fname[y] << " " << lname[y] << endl;
}
}
}
//Closing all files
students_file.close();
courses_file.close();
students_out.close();
courses_out.close();
return 0;
}

Everything works. Wow, a lot of time put into this. I'm happy its done.
Thanks a lot guys.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,277
Latest member
VytoKetoReview

Latest Threads

Top