Help with finding length of an array

Hey guys/gals,

I'm trying to find the length of an array. The code that I have written up thus far does not compile. There is an error in the int arrayLength = array1.length();

Can someone take a look at it and see if you can point out why? I need to find out how many values are stored in the array, so I can sort it in descending order. I am also having trouble figuring out how to make the same change to array2 that is made to array1 during sorting.

Any help would be appreciated! Thank you!
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <cctype>
using namespace std;

enum level {FRESHMAN, SOPHOMORE, JUNIOR, SENIOR};
const string STUD_REC = "CREDITS.TXT";
const int MAX_NUM = 999;

void ifUnder(int hrs, string idNum, string idArray1[MAX_NUM], int classLevel1[MAX_NUM]); //if undergraduate, this will fill the arrays with the data
// and apply the enumerated value to it.

void sortArrays(string array1[MAX_NUM], int array2[MAX_NUM]); //sorts the arrays
																						

void sortData(string idArray[MAX_NUM], int classLevel[MAX_NUM]);//goes through data and calls the ifUnder function if necessary

int main()
{	
	string id[MAX_NUM];
	int claLevel[MAX_NUM];
    
	cout << "This program will convert hours taken to class level for all"
	"Undergraduate students.  You will be able to search for each student by"
	"the student ID number." << endl << endl;
	
	sortData(id, claLevel);
	
	cout << endl;
    return 0;
}

void sortData(string idArray[MAX_NUM], int classLevel[MAX_NUM])
{
	string id;
	char stuType;
	double hours;
	ifstream studData;
	
    studData.open (STUD_REC.c_str());      // open the file for reading
    
    if(studData) //If the file opens successfully
	{
		studData >> id >> stuType >> hours;
		
		if((stuType == 'U') || (stuType == 'u'))
		{
			ifUnder(hours, id, idArray, classLevel);
			studData >> id >> stuType >> hours;			
		}
	}
}

void ifUnder(int hrs, string idNum, string idArray1[MAX_NUM], int classLevel1[MAX_NUM])
{
	int array1Count = 0;
	int array2Count = 0;
	level idLevel;
	
	if(0 <= hrs < 32)
	{
		idLevel = FRESHMAN;
	}
	else if(32 <= hrs <= 63)
	{
		idLevel = SOPHOMORE;
	}
	else if(64 <= hrs <= 95)
	{
		idLevel = JUNIOR;
	}
	else if(96 <= hrs)
	{
		idLevel = SENIOR;
	}
			
	idArray1[array1Count] = idNum;
	array1Count++;
	
	classLevel1[array2Count] = idLevel;
	array2Count++;
	
}

void sortArrays(string array1[MAX_NUM], int array2[MAX_NUM])
{
		
	    int i, j, first, temp;
		int arrayLength = array1.length();
		
      	for (i = arrayLength - 1; i > 0; i--)
     	{
           first = 0;                 // initialize to subscript of first element
           for (j=1; j<=i; j++)   // locate smallest between positions 1 and i.
          {
                 if (array1[j] < array1[first])
                 first = j;
          }
         temp = array1[first];   // Swap smallest found with element in position i.
         array1[first] = array1[i];
         array1[i] = temp;
		}
}
There is an error in the int arrayLength = array1.length();array1 is a C-style array. They are not classes and do not have any member functions. And actually, C-style arrays do not know their length at all, so you cannot just ask them (it is more complicated than that, but for almost all cases information about array size is lost as soon as you pass it to function)
So would the solution be to put a counter in the function that reads in the file?

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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <cctype>
using namespace std;

enum level {FRESHMAN, SOPHOMORE, JUNIOR, SENIOR};
const string STUD_REC = "CREDITS.TXT";
const int MAX_NUM = 999;

void ifUnder(int hrs, string idNum, string idArray1[MAX_NUM], int classLevel1[MAX_NUM]); //if undergraduate, this will fill the arrays with the data
// and apply the enumerated value to it.

void sortArrays(string array1[MAX_NUM], int array2[MAX_NUM], int count1);
																						

int sortData(string idArray[MAX_NUM], int classLevel[MAX_NUM]);//goes through data and calls the ifUnder function if necessary

int main()
{	
	string id[MAX_NUM];
	int claLevel[MAX_NUM];
	int counter;
    
	cout << "This program will convert hours taken to class level for all"
	"Undergraduate students.  You will be able to search for each student by"
	"the student ID number." << endl << endl;
	
	counter = sortData(id, claLevel);
	
	cout << counter;
	
	cout << endl;
    return 0;
}

int sortData(string idArray[MAX_NUM], int classLevel[MAX_NUM])
{
	string id;
	char stuType;
	double hours;
	ifstream studData;
	int count = 0;
	
    studData.open (STUD_REC.c_str());      // open the file for reading
    
    if(studData) //If the file opens successfully
	{
		studData >> id >> stuType >> hours; //priming read
		
		while(studData) // while an id was successfully read
		{	
			if((stuType == 'U') || (stuType == 'u'))
			{
				ifUnder(hours, id, idArray, classLevel);
				count++;			
			}
			studData >> id >> stuType >> hours; //attempt to read next line
			
		}
	}
	return count;
}

void ifUnder(int hrs, string idNum, string idArray1[MAX_NUM], int classLevel1[MAX_NUM])
{
	int array1Count = 0;
	int array2Count = 0;
	level idLevel;
	
	if(0 <= hrs < 32)
	{
		idLevel = FRESHMAN;
	}
	else if(32 <= hrs <= 63)
	{
		idLevel = SOPHOMORE;
	}
	else if(64 <= hrs <= 95)
	{
		idLevel = JUNIOR;
	}
	else if(96 <= hrs)
	{
		idLevel = SENIOR;
	}
			
	idArray1[array1Count] = idNum;
	array1Count++;
	
	classLevel1[array2Count] = idLevel;
	array2Count++;
	
}

void sortArrays(string array1[MAX_NUM], int array2[MAX_NUM], int count1)
{
		
	    int i, j, first;
		string temp;
		
      	for (i = count1 - 1; i > 0; i--)
     	{
           first = 0;                 // initialize to subscript of first element
           for (j=1; j<=i; j++)   // locate smallest between positions 1 and i.
          {
                 if (array1[j] < array1[first])
                 first = j;
          }
         temp = array1[first];   // Swap smallest found with element in position i.
         array1[first] = array1[i];
         array1[i] = temp;
		}
}
Last edited on
closed account (48T7M4Gy)
http://stackoverflow.com/questions/4108313/how-do-i-find-the-length-of-an-array
Yes, it would be a good idea to keep counter denoting how much elements ar actually in array: C arrays are not resizeable and if you do not always have all elements containing useful information, you will have to use some way to distiguish physical size (capacity of array) and logical (how much elements are really here)
Topic archived. No new replies allowed.