473,486 Members | 1,958 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

multidimension array questions

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])
{
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?

Sep 6 '06 #1
29 3273
On 5 Sep 2006 21:06:49 -0700 in comp.lang.c++, "foker"
<br************@gmail.comwrote,
> 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][i])
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.

Sep 6 '06 #2
"foker" writes:

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][i])
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.
Sep 6 '06 #3
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].

Sep 6 '06 #4
foker wrote:
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])
{
i++;
}
}

Sep 6 '06 #5
foker schrieb:
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<intcourses;
};

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

map<int, stringcourseNames;

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]

--
Thomas
http://www.netmeister.org/news/learn2quote.html
Sep 6 '06 #6
On Wed, 06 Sep 2006 19:49:00 +0200 in comp.lang.c++, "Thomas J.
Gritzan" <Ph*************@gmx.dewrote,
>struct Student
{
string name;
string /* or int */ id;
vector<intcourses;
};

and fill in the students info,
Looking something like this (Notice: completely unverified code)

vector<Studentstudent_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
>num_courses;
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";
Sep 6 '06 #7
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][i] with the course id number
while(courses_file >course_name[i] >student_info[499][i])
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.

Sep 7 '06 #8
"foker" writes:
>I can't use vectors or anything else. I have to use a 2d array. My code
right now is:
<snip>
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.
Sep 7 '06 #9
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.

Sep 7 '06 #10
The output files should look like this:
students_out.txt
student name
course names

courses_out.txt
course name
students names

Sep 7 '06 #11
"foker" writes:
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?
Sep 7 '06 #12
"foker" wrote:
>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.
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.)
Sep 7 '06 #13
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.

Sep 7 '06 #14
"foker" <br************@gmail.comwrote:

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.
Sep 7 '06 #15
osmium schrieb:
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[s][c] is true, if student s takes course c.

--
Thomas
http://www.netmeister.org/news/learn2quote.html
Sep 7 '06 #16
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.

Sep 7 '06 #17
"osmium" wrote:
"foker" writes:
>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?
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.
Sep 7 '06 #18
osmium schrieb:
"osmium" wrote:
>"foker" writes:
>>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?

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
....
</>
Such as end~!@#$
--
Thomas
http://www.netmeister.org/news/learn2quote.html
Sep 7 '06 #19
for(int r = 0; r < 25; r++)
{
courses_out << course_name[r] << endl;
while(students_file >student.first_name >student.last_name >>
student_info[i][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?

Sep 8 '06 #20
"foker" writes:
for(int r = 0; r < 25; r++)
{
courses_out << course_name[r] << endl;
while(students_file >student.first_name >student.last_name >>
student_info[i][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>
Sep 8 '06 #21
"foker" writes:
>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 .
Sep 8 '06 #22
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][i] with the course id number
while(courses_file >course[i].course_name >course[i].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[i].course_ids;
//cout << student[i].course_ids;
student_info[student[r].id][student[i].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?
osmium wrote:
"foker" writes:
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 .
Sep 8 '06 #23
foker wrote:
So everythings almost working perfectly.

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>


Brian
Sep 8 '06 #24
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][i] with the course id number
while(courses_file >course[i].course_name >>
course[i].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[i].course_ids;

//cout << student[i].course_ids;

student_info[student[r].id][student[i].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?

Sep 8 '06 #25
foker wrote:
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?
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
Sep 8 '06 #26

Default User wrote:
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.

Sep 8 '06 #27
"foker" writes:
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][i] with the course id number
while(courses_file >course[i].course_name >>
course[i].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[i].course_ids;

//cout << student[i].course_ids;

student_info[student[r].id][student[i].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.
Sep 8 '06 #28
On 8 Sep 2006 13:33:17 -0700 in comp.lang.c++, "foker"
<br************@gmail.comwrote,
>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.)

Sep 9 '06 #29

David Harmon wrote:
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[i].course_name >course[i].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[i].course_ids;
//Setting student id and course id's to true in bool array
student_info[student[r].id][student[i].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.

Sep 9 '06 #30

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
3106
by: Joe Six-Pack | last post by:
Hi, Im having problems in randomly selecting an element in an array that has not been selected before.. in other words, I have an array of answers to questions, then I want to select 5, with one...
8
3660
by: Peter B. Steiger | last post by:
The latest project in my ongoing quest to evolve my brain from Pascal to C is a simple word game that involves stringing together random lists of words. In the Pascal version the whole array was...
5
1898
by: Robert | last post by:
Hi, This might be a strange question but i would like to know how to return an array from a function. Do you have to use pointers for this? Thanks in advance, Robert
1
1292
by: Manny Chohan | last post by:
Can someone help me with the code to bind 2 dimensional array to datgrid? I just dont know how this can be done. Theres no code on the internet. Manny
28
2412
by: anonymous | last post by:
I have couple of questions related to array addresses. As they belong to the same block, I am putting them here in one single post. I hope nobody minds: char array; int address; Questions...
5
4510
by: Jason Huang | last post by:
Hi, I have a 2 dimension string array MyArray, how do I know the Count of the Rows and Columns in the MyArray? Thanks for help. Jason
0
1290
by: srinivasreddypn | last post by:
Hello everybody, I know its a very simple problem to you, As a learner of flash action script m confused with the syntax of actionscript. Please let me know how to do this. Code: open close...
0
7099
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7123
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6842
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7319
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
4559
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3070
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1378
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
598
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
262
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.