New to 2-D array

I am just gathering some knowledge about c++ 2-D arrays for an assignment.

I am given a file with (n) number of records, and each record has a score (0-100), gender(M/F) and group # (1-3) attached to it. How do i read from file and create and 2-D array and count the number of items or tally those entries?

I am working on a pseudocode for this and would appreciate any help in understanding on how to solve the problem. I dont need any code. just simple english sentence or pseudocode will do.

Here is the working pseudocode.



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
  

create File Scanner object, fileScanner
read line, 
create line Scanner with line, 
get score, gender, group #  and store in variables.
dispose of line Scanner

Create record array
for index goes from 0 to < n
  read line with File Scanner
  create line Scanner with line
  get next token
  create 2-D double array
  dispose of line scanner
  
end for loop
dispose of file scanner
Last edited on
closed account (48T7M4Gy)
Each record has 3 attributes. So instead of a 2d array you might like to consider 3 - 1d arrays, one for each attribute all tied togther by the index number. ie the k-th record will consist of score[k], gender[k] and group#[k].
Alternately, you could create a struct with those three fields. Then created an array of those struct objects. Read from the file and fill in the three fields in each i-th position of the array.
Is it possible if can get an pseudo code example for creating struct with those three fields? I have written a pseudo code below. please let me know if i am in the right direction. Thanks for all the help.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
create File Scanner object, fileScanner
create temp variable storage for three fields - temp 1=0499; temp2="M"; temp3="F"; temp4=3;

Struct structname {
  int record;
  char gender;
  int groupNum;
}
  
read line, 
create line Scanner with line, 
get score, gender, group #  and store in variables.
dispose of line Scanner

Create record array
for index goes from 0 to <n
  open file
  read line with File Scanner
  
while end of the file
  read in line
   if record =temp1

     if gender = temp2 || temp3
      
       if group =temp4
 
store in struct. 

 end if
end for loop
dispose of line Scanner
dispose of file scanner



I also wanted to know how can we count the number of records?
Line 1: In C++, the proper object is an ifstream.

Line 2: Purpose of temp1, temp2, temp3, temp4 is unclear since the variable names are meaningless.

Line 10-13: These are out of place.

Line 11: No need for a line scanner. Just user the input operator (>>)

Line 16: n is undefined. Since you don't know how many records there will be, a while loop is more appropriate. This code is redundant since you're reading the file at line 20.

Line 20: Do not loop on !eof(). This does not work the way you expect. The eof bit is set true only after you make a read attempt on the file. This means after you read the last record of the file, eof is still false. Your attempt to read past the last record sets eof, but you're not checking it there. You proceed as if you had read a good record. This will result in reading an extra (bad) record. The correct way to deal with this is to put the >> operation as the condition in the while statement.
1
2
3
  while (cin >> var) 
  {  //  Good cin or istream operation
  }


Line 22, 24, 26: The purpose of your if statements is not clear.

Line 24: C++ does not support implied left hand side in conditionals. You must fully specify the conditions.
Example:
if (ans == 'Y' || 'y') evaluates as if ((ans == 'Y') || ('y'))

('y') always evaluates to 1 (true), therefore the if statement is always true.

Lines 22, 24, 26: You're using the assignment operator (=), not the comparison operator (==).

Line 30: Not clear which if this terminates.

Line 13,32-33: No need to explicitly dispose of objects unless they were created with new.





Last edited on
Thank you fellow users for the comments. Really Appreciate it. And sorry for not being clear. This is my first time doing a c++ programming.

I wanted to understand the basic solution on how to solve the question using pseudo code since its easier to apprehend. From the comments received I have tried to come up with a code from my understanding.

i am still having trouble on how to count the number of entries. Please let me know if this is right. Thank you people.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39

Struct structname {
  int record;
  char gender;
  int groupNum;
}

create File Scanner object, fileScanner //ifstream

// I created temp vars- temp1,2,3,4 because i can have fixed value that i can compare during reading file and creating a 2-D array with it. 

create temp variable storage for three fields - temp 1=0499; temp2="M"; temp3="F"; temp4=3;


Create record array
read line, 
create line Scanner with line, 
  
while not end of the file

// I want to read the file and accordingly assign the records, gender and group # in the struct field. 
  read file
   if record == temp1

     if ((gender == temp2) || (gender == temp3))
      
       if group == temp4
 
store in struct. 

            end if
         end if
      end if

end while

dispose of line Scanner
dispose of file scanner
am still having trouble on how to count the number of entries.

Line 13: Create counter. Initialize to 0.

Line 21: Add 1 to counter.

Line 12: I still hate your use of temp1, temp2, temp3, temp4 variable names. Those names convey no meaningful information.

Line 19: You missed my point about not looping on eof.

Line 22: What if the read fails? If it fails, you proceed as if nothing had happened.

Line 23: Do all records begin with 499?

I realize you're probably coming from java, where file scanners, line scanners and dispose are common idioms. These really don't apply to C++.




Yeah, i have taken java first. its little bit confusing when i do it in C. as for the record they start from 000-0499.
My point regarding line 23 and record number 499 is that your pseudo-code would only store the last record (i.e. the one with record number 499).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38

Struct structname {
  int record;
  char gender;
  int groupNum;
}

create File Scanner object, fileScanner //ifstream

int counter=0;

Create record array
read line, 
create line Scanner with line, 
  
while ....(still not sure what to do).....

counter++;
// I want to read the file and accordingly assign the records, gender and group # in the struct field. 
  read file
   if record >0 && <=0499

     if ((gender == "M') || (gender =='F'))
      
       if group >0 && <=3
 
store in struct. 

else print"file error"

            end if
         end if
      end if

end while


 
Here's actual C++ code.
Hope this helps:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
#include <fstream>
using namespace std;

struct structname 
{   int record;
    char gender;
    int groupNum;
};
const int ARRAY_SIZE = 500;

int main ()
{   ifstream    ifs ("filename.txt");
    int counter=0;
    structname  arr[ARRAY_SIZE];
    //  temporary variables
    int     rec;
    char    gender;    
    int     group;

    while (ifs >> rec >> gender >> group) 
    {   //  Good read
        if (rec <= 0 || rec > 499)
        {   cout << "Bad rec#" << endl;
            break;
        }
        if ((gender != 'M') && (gender !='F'))
        {   cout << "Bad gender" << endl;
            break;
        }
        if (group < 1 || group > 3)
        {   cout << "Bad group" << endl;
            break;
        }
        //  Good values
        arr[counter].record = rec;
        arr[counter].gender = gender;
        arr[counter].groupNum = group;
        counter++;
    }   //  end while
    return 0;
}

thank you very much...this will help me in understanding c more..
Topic archived. No new replies allowed.