multidimension array questions

F

foker

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];
string first_name, last_name, course_name;

ifstream courses_file;
courses_file.open("courses.txt");
if (courses_file.fail())
{
cerr << "Could not open courses.txt" << endl;
exit(1);
}
ifstream students_file;
students_file.open("students.txt");
if (students_file.fail())
{
cerr << "Could not open students.txt" << endl;
exit(1);
}
int index = 0;
while(students_file >> first_name >> last_name >>
student_info[index][25])
{
index++;
while(courses_file >> course_name >> student_info[500])
{
i++;
}
}
}

When I print out my array I get a bunch of garbage. One of problems is
I am taking in first_name, last_name, student id number in the array
but after that theres a list of course id's the student is taking so
like a random number. I can't get past the student id in the first line
of the file. It prints out junk. Can someone help me understand what my
problem is here and how to fix it?
 
D

David Harmon

On 5 Sep 2006 21:06:49 -0700 in comp.lang.c++, "foker"
int student_info[500][25];

Why are the magic numbers 500 and 25? How can you possibly always
have the same number of students?
string first_name, last_name, course_name;

How can only storing one student name and course name, not all of
them, possibly be satisfactory?
while(students_file >> first_name >> last_name >>
student_info[index][25])

You are writing one past the end of the array.. [25] the last is 24?
Why are you trying to write always in the last location?
You forgot to check for >500 input records.
while(courses_file >> course_name >> student_info[500])


You are writing one past the end of the array.. [500] the last is
499? Why are you trying to write always in the last location?

After this input fails, the outer loop will never read any more from
the failed stream.
When I print out my array I get a bunch of garbage.

No surprise there, you never initialized it.
What are you trying to accomplish?

Avoid using bare naked array for application programming.
Prefer std::vector.
 
O

osmium

:

I can't really tell what you are trying to do; but I will offer some random
comments anyway.
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];

That's an array that holds integers. I doubt if you want that, I think your
variables are more likely to be strings. Perhaps even the course number.
There is no reasonable calculation on a course number, is there? Does it
make sense to multiply two course numbers?
string first_name, last_name, course_name;

ifstream courses_file;
courses_file.open("courses.txt");
if (courses_file.fail())
{
cerr << "Could not open courses.txt" << endl;
exit(1);
}
ifstream students_file;
students_file.open("students.txt");
if (students_file.fail())
{
cerr << "Could not open students.txt" << endl;
exit(1);
}
int index = 0;
while(students_file >> first_name >> last_name >>
student_info[index][25])

You don't want to put *everything* into element [x][25] do you? Even worse,
the array elements are numbered 0..24
{
index++;
while(courses_file >> course_name >> student_info[500])


Do you mean to read the entire file for each student? If you do, you should
"rewind" the file before beginning a new student. Excessive use of
constants again. Do you really want to make a mish-mash of course
information and student information?
{
i++;
}
}
}

When I print out my array I get a bunch of garbage. One of problems is
I am taking in first_name, last_name, student id number in the array
but after that theres a list of course id's the student is taking so
like a random number. I can't get past the student id in the first line
of the file. It prints out junk. Can someone help me understand what my
problem is here and how to fix it?

I think you should fix (if my guesses are right) your logic problems first.
Perhaps write pseudocode first. Use pencil and paper and make sketches of
your main data structures. I *think* you are trying to make array copies of
two external files, then you plan to do something with those two arrays - in
code that hasn't been written yet. The FAQ has information on
two-dimensional arrays that may be helpful. I suspect you actually want
arrays of structures. Structures are what a civilian thinks of as records,
information that is intrinsically bound together. Out of context your first
name and your last name have virtually no relationship to *you* They are
simply parts of names of random people. .

For example:

struct Student
{
string first_name;
string last_name;
string course_name;
};

Student as[500]; // array of students.

The constant is frowned upon by many, the use of arrays are frowned upon by
many. Let them frown.
 
F

foker

Ok apparently I did a really bad job explaining what I'm trying to do
here. I have 2 text files one that will have 500 students in it. It has
their student name, student number, number of courses they are taking,
then the course numbers. The second text file has the name of the
course and the course numbers. So what I want to do is read in the
whole text files for the students. I want to fill the array
student_info[500][25]. I want to have 500 student id's and 25 course
id's. The problem is I cant read in past the first line because my code
is wrong and I'm not sure how to do it. The files are arranged like
this: Students_fil: [Student Name][Student ID][Number of courses they
are taking][Course Id's They are in] Courses_file: [Course Name][Course
ID].
 
F

foker

foker said:
Ok apparently I did a really bad job explaining what I'm trying to do
here. I have 2 text files one that will have 500 students in it. It has
their student name, student number, number of courses they are taking,
then the course numbers. The second text file has the name of the
course and the course numbers. So what I want to do is read in the
whole text files for the students. I want to fill the array
student_info[500][25]. I want to have 500 student id's and 25 course
id's. The problem is I cant read in past the first line because my code
is wrong and I'm not sure how to do it. The files are arranged like
this: Students_fil: [Student Name][Student ID][Number of courses they
are taking][Course Id's They are in] Courses_file: [Course Name][Course
ID].

So with this while loop how do I edit this to store the student id
number and course id number for both text files?
while(students_file >> first_name >> last_name >>
student_info[index][24])
{
index++;
while(courses_file >> course_name >> student_info[499])
{
i++;
}
}
 
T

Thomas J. Gritzan

foker said:
Ok apparently I did a really bad job explaining what I'm trying to do
here. I have 2 text files one that will have 500 students in it. It has
their student name, student number, number of courses they are taking,
then the course numbers. The second text file has the name of the
course and the course numbers. So what I want to do is read in the
whole text files for the students. I want to fill the array
student_info[500][25]. I want to have 500 student id's and 25 course
id's.

Your array declaration "student_info[500][25]" is inconsistent with your
demant "I want to have 500 student id's and 25 course id's."
You don't want to have 500*25, you want 500+25.

Since your students data carry only the course ids, and the course file
identifies course ids with course names, I assume you want to "join" the
two table and identify course ids with course names.

Create a struct like:

struct Student
{
string name;
string /* or int */ id;
vector<int> courses;
};

and fill in the students info, then fill a map with the data from the
course file:

map<int, string> courseNames;

So you can easily lookup course names from the course ids stored with the
students.

For the actual reading: Don't use two nested loops, use one loop that reads
in students file, and another that reads course file.

For ex.:

/* opening course file here */
string course_name;
int course_id;

while(courses_file >> course_name >> course_id)
{
courseNames[course_id] = course_name;
}

[note: insert "std::" where needed]
 
D

David Harmon

On Wed, 06 Sep 2006 19:49:00 +0200 in comp.lang.c++, "Thomas J.
Gritzan said:
struct Student
{
string name;
string /* or int */ id;
vector<int> courses;
};

and fill in the students info,

Looking something like this (Notice: completely unverified code)

vector<Student> student_info;
string line;
while(getline(students_file, line)) {
istringstream readline(line);
student aStud;
int num_courses;
readline >> aStud.first_name >> aStud.last_name >> aStud.id int courseid;
while(readline >> courseid)
aStud.courses.push_back(courseid);
if(num_courses != aStud.courses.size())
cerr << "Warning: expected " << num_courses
<< " found " << aStud.courses.size()
<< " courses for " << aStud.first_name
<< " " << aStud.last_name << "\n";
student_info.push_back(aStud);
}
if(student_info.size() != 500)
cerr << "Warning: read " << student_info.size()
<< " students, not 500\n";
 
F

foker

I can't use vectors or anything else. I have to use a 2d array. My code
right now is:

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

struct Students
{
string first_name, last_name;
int id, num_courses;
int course_ids;
};
int main()
{
Students student;
int i = 0;
int student_info[500][25];
string course_name[25];

//courses file = course name and course id
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);
}

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

int index = 0;
int j = 0;
//While loop fills up students_out.txt - need to figure out the
courses_out.txt from this while loop too probably
while(students_file >> student.first_name >> student.last_name >>
student.id >> student.num_courses)
{
students_out << student.first_name << " " << student.last_name <<
endl;
//courses_out << course_name[index] << endl;
for(i = 0; i < student.num_courses; i++)
{
students_file >> student.course_ids;
for(index = 0; index < 24; index++)
if(student_info[499][index] == student.course_ids)
{
courses_out << course_name[index] << endl;
courses_out << student.first_name << " " << student.last_name <<
endl;
}

for(index = 0; index < 24; index++)
{ //sort this by course #
if(student.course_ids == student_info[499][index])
{
students_out << course_name[index] << endl;
}
}

}
}

This would be easier if i could figure out how to read the student id
into the student_info[500] part of it, because then i could set it to
true and just search through the 2d array for true and print it. Can
someone help me figure that out? Also, my output file students_out.txt
is perfect but the courses_out.txt is not right. I need it to list the
courses and the names of students in the course. I tried to do that in
this code but its all messed up.
 
O

osmium

foker said:
I can't use vectors or anything else. I have to use a 2d array. My code
right now is:
This would be easier if i could figure out how to read the student id
into the student_info[500] part of it, because then i could set it to
true and just search through the 2d array for true and print it. Can
someone help me figure that out? Also, my output file students_out.txt
is perfect but the courses_out.txt is not right. I need it to list the
courses and the names of students in the course. I tried to do that in
this code but its all messed up.

I am still trying to get my arms around what the assignment *is*.

The input course file contains course id and course description, right? The
program produces a third file which contains course id, and student name for
all the students in that course. The maximum number of students in a course
is 25. Is that right? Or are there 25 courses?

You simply *have* to get the data definitions right before you can
successfully code a solution. As I see it now, there are three file
formats. (Is there a place for student information in the course file you
described earlier? ) I've seen bits and pieces of the assignment but not
one complete description in one place. I suggest posting the actual
assignment, including constraints you know of. Such as "don't use vectors".

How many file formats are there? Two? Three? Four?

That number 25 keeps coming up. Does it have only one meaning? If so what
does it mean? I'm guessing it might possibly have two different meanings.

Subtract 100 to send E-Mail.
 
F

foker

I am still trying to get my arms around what the assignment *is*.

Objective: Use multi-dimensional arrays to help solve a problem.
We can use a 2-dimension array to map students and courses. There is a
list of courses (courses.txt) and a list of students (students.txt).
You need to set up the array to match course ids and student ids. Then,
print out two files: students_out.txt and courses_out.txt that contain
the data organized by either students or courses. There are no more
than 500 students, and no more than 25 courses. Your program MUST work
for those numbers, the sample input files are smaller. Do not use the
size of the input files to size your array! You will need additional
arrays to store the student and course information. The 2-dimension
array is only used to map the student numbers to the course numbers. If
a student is listed as taking a class more than once, count them all as
one class. The courses.txt file is organized as follows:
Course Name Course ID
The students.txt file is organized as follows:
First Name Last Name Student ID Number of courses the
student is taking List of course IDs
The input course file contains course id and course description, right? The
program produces a third file which contains course id, and student name for
all the students in that course. The maximum number of students in a course
is 25. Is that right? Or are there 25 courses?
The input file contains course name and course id #. There are 25
courses with no set number of students per course. That doesn't really
even matter.
You simply *have* to get the data definitions right before you can
successfully code a solution. As I see it now, there are three file
formats. (Is there a place for student information in the course file you
described earlier? ) I've seen bits and pieces of the assignment but not
one complete description in one place. I suggest posting the actual
assignment, including constraints you know of. Such as "don't use vectors".
Not allowed to use vectors, haven't covered that yet. Just got done
covering 2d arrays.
How many file formats are there? Two? Three? Four?

1 file format .txt file. 2 input files, students.txt and courses.txt 2
output files students_out.txt and courses_out.txt
That number 25 keeps coming up. Does it have only one meaning? If so what
does it mean? I'm guessing it might possibly have two different meanings.

25 is the maximum number of courses in the courses.txt file.
 
F

foker

The output files should look like this:
students_out.txt
student name
course names

courses_out.txt
course name
students names
 
O

osmium

foker said:
The output files should look like this:
students_out.txt
student name
course names

courses_out.txt
course name
students names

How is that supposed to work? There are anywhere from 0 to 500 student
names. A course name is a string and a student name is a string. How do
you tell where one ends and the other begins?
 
O

osmium

foker said:
Objective: Use multi-dimensional arrays to help solve a problem.
We can use a 2-dimension array to map students and courses. There is a
list of courses (courses.txt) and a list of students (students.txt).
You need to set up the array to match course ids and student ids. Then,
print out two files: students_out.txt and courses_out.txt that contain
the data organized by either students or courses. There are no more
than 500 students, and no more than 25 courses. Your program MUST work
for those numbers, the sample input files are smaller. Do not use the
size of the input files to size your array! You will need additional
arrays to store the student and course information. The 2-dimension
array is only used to map the student numbers to the course numbers. If
a student is listed as taking a class more than once, count them all as
one class. The courses.txt file is organized as follows:
Course Name Course ID
The students.txt file is organized as follows:
First Name Last Name Student ID Number of courses the
student is taking List of course IDs

The input file contains course name and course id #. There are 25
courses with no set number of students per course. That doesn't really
even matter.

Not allowed to use vectors, haven't covered that yet. Just got done
covering 2d arrays.


1 file format .txt file. 2 input files, students.txt and courses.txt 2
output files students_out.txt and courses_out.txt


25 is the maximum number of courses in the courses.txt file.

This program is sufficiently large that it is surely worth more than one
lonely function. As a WAG, I would start with four functions: read student
file, read course file, write student file, write course file. That list
might be added to. The arrays would be in main, and main runs the whole
show. It still isn't obvious to me what that second dimension on the
array[s} is for. ISTM that the output files would be created dynamically,
and a copy of the output files would never exist in main. I would add a
comment in the source telling what the file formats were, especially the
output files. I find myself wandering around in too many documents. Two
:) (Not really , there is the assignment, your chosen file formats, and
the code.)
 
F

foker

This program is sufficiently large that it is surely worth more than one
lonely function. As a WAG, I would start with four functions: read student
file, read course file, write student file, write course file. That list
might be added to. The arrays would be in main, and main runs the whole
show. It still isn't obvious to me what that second dimension on the
array[s} is for. ISTM that the output files would be created dynamically,
and a copy of the output files would never exist in main. I would add a
comment in the source telling what the file formats were, especially the
output files. I find myself wandering around in too many documents. Two
:) (Not really , there is the assignment, your chosen file formats, and
the code.)

It is kind of large. I only did it all in main to see if I could get it
to run but I guess that was a mistake. The dimensions of the array are
like this. student_info[500] which is student id's the second dimension
is student_info[500][25] 25 being the course ids from the course_txt
file. So then I can say like read in the student id number of courses
he has then read in the course ids and match them with the course ids
in student_info[500][25] and set them to true if he is in them. A lot
of this is new to me so im all over the place. If i create my 2d array
in main how would i pass it to a function? We haven't gone over
pointers yet but I think thats the way I would have to pass it right?
Thanks for you help.
 
O

osmium

It is kind of large. I only did it all in main to see if I could get it
to run but I guess that was a mistake. The dimensions of the array are
like this. student_info[500] which is student id's the second dimension
is student_info[500][25] 25 being the course ids from the course_txt
file. So then I can say like read in the student id number of courses
he has then read in the course ids and match them with the course ids
in student_info[500][25] and set them to true if he is in them. A lot
of this is new to me so im all over the place. If i create my 2d array
in main how would i pass it to a function? We haven't gone over
pointers yet but I think thats the way I would have to pass it right?

The FAQ talks about this. Try it and if you get in trouble someone will
help. Note that the function must be given the value of the second
dimension.
 
T

Thomas J. Gritzan

osmium said:
It still isn't obvious to me what that second dimension on the
array[s} is for.

It's a boolean table:

With s : student id and c : course id,

student_info[c] is true, if student s takes course c.
 
F

foker

This program is sufficiently large that it is surely worth more than one
lonely function. As a WAG, I would start with four functions: read student
file, read course file, write student file, write course file. That list
might be added to. The arrays would be in main, and main runs the whole
show. It still isn't obvious to me what that second dimension on the
array[s} is for. ISTM that the output files would be created dynamically,
and a copy of the output files would never exist in main. I would add a
comment in the source telling what the file formats were, especially the
output files. I find myself wandering around in too many documents. Two
:) (Not really , there is the assignment, your chosen file formats, and
the code.)

It is kind of large. I only did it all in main to see if I could get it
to run but I guess that was a mistake. The dimensions of the array are
like this. student_info[500] which is student id's the second dimension
is student_info[500][25] 25 being the course ids from the course_txt
file. So then I can say like read in the student id number of courses
he has then read in the course ids and match them with the course ids
in student_info[500][25] and set them to true if he is in them. A lot
of this is new to me so im all over the place. If i create my 2d array
in main how would i pass it to a function? We haven't gone over
pointers yet but I think thats the way I would have to pass it right?
Thanks for you help.
 
O

osmium

osmium said:
How is that supposed to work? There are anywhere from 0 to 500 student
names. A course name is a string and a student name is a string. How do
you tell where one ends and the other begins?

I would establish an impossible student name as a sentinel to indicate the
end of the list of student names. Such as end~!@#$

If there are zero students in a course this would immediately follow the
course name. This goes along with my goal of never having a RAM copy of the
course output file. A file is similar to a tape recorder with an infinite
tape, arrays do not have that nice attribute.

For some reason this hideous mail program chose to make that name blue. It
was not something I wanted.
 
T

Thomas J. Gritzan

osmium said:
I would establish an impossible student name as a sentinel to indicate the
end of the list of student names.

Such as an empty line?

<courses_out.txt>
Course 1
Student 1
Student 2

Course 2
....
 
F

foker

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)
{
int temp = 0;
students_out << student.first_name << " " << student.last_name <<
endl;
for(i = 0; i < student.num_courses; i++)
{
students_file >> student.course_ids;
int test = student.course_ids;
for(int x = 0; x < 500; x++)
{
for(y = 0;y < 25; y++)
{
student_info[x][test] = true;
if(student_info[x][y] == true)
cout << student.first_name;
}
//}
}
}
}
}

Why wont it update the cells student_info[x][test] to true?
 

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,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top