Can't compile the program

Task:
Write a function that finds the longest decreasing sequence in an integer array. And apply this function to three kinds of arrays in main.
1. An array of the number and values ​​of elements of which are given during initialization (const).
2. An array of item counts must be entered and item values ​​are randomly generated.
3. For several arrays located in dynamic memory, the values ​​of which are read from the file. The file should contain several lines, the first line contains the number of array elements, the next - these elements themselves.

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
#include <iostream>
#include <vector>
#include <cstdlib>
#include <fstream>

const char* ERROR_NOT_INT = "Error: must be integer!";
const char* ERROR_WRONG_ARRAY_SIZE = "Error: size of the array must be greater than 0!";
const char* ERROR_FILE = "Error: file can't be opened!";


int findLongestDecrSeq(const int array[], const int arr_size)
{
	int lds[arr_size]; 
	int i, j, max = 0;

	for (i = 0; i < arr_size; i++)
	 {	std::cout <<  array[i] <<" ";
		lds[i] = 1;} 
		
	for (i = 1; i < arr_size; i++)
		for (j = 0; j < i; j++)
			if (array[i] < array[j] && lds[i] < lds[j] + 1) 
				lds[i] = lds[j] + 1; 
              
	for (i = 0; i < arr_size; i++)
		if (max < lds[i]) 
			max = lds[i]; 
			
	return max;
}
int main()
{
	int array, arr_size, i, fileName, option;
	try
 {
	int choice;
	std::cout << "Choose type of array for finding maximum length of subsequence\n";
	std::cout << "1. Array with constant values\n"  
	          <<  "2. Array with random values\n" 
	          <<  "3. Array with values from file\n";
	std::cin >> choice;
	switch(choice)
	{ 
		case 1:
			{
			int array[] = {6, 5, 4, 8, 1};
			int arr_size = sizeof(array) / sizeof(array[0]);
	        std::cout << "\n Length of longest decreasing sequence is " << findLongestDecrSeq(array, arr_size);
			break;
	      }
			
		case 2:
			{
			std::cout << "Input size of array";
			std::cin >> arr_size;
			std::vector<int> array(arr_size);
			
			if (!std::cin)
		     {
			   throw ERROR_NOT_INT;
		    }

		    if (arr_size < 1)
		     {
			   throw ERROR_WRONG_ARRAY_SIZE;
	  	    }

			
              for (int i = 0; i < arr_size; i++) 
			  {
               array[i] = rand() % 100 + 1;
            }
             std::cout << "\n Length of longest decreasing sequence is "  << findLongestDecrSeq( array[], arr_size); //compiler writes "73 104 [Error] expected primary-expression before ']' token" here
			 break;
		   } 
		
		case 3:
		  {	
		    int var;
		    std::cout << "Enter a name of input file: " << std::endl;
		    std::cin >> fileName;
		    std::ifstream file("input.txt");

	      	    if (!file.is_open()) {
			        throw ERROR_FILE;
		        }
		        file >> arr_size;
		        std::vector<int> array(arr_size);
		        
		       while(!file.eof())
		       {
		       	for(i = 0; i < arr_size; i++)
		       	{
		       		file >> var;
		       		array[i] = var;
		       	}
		       	file.close();
		       	
		       	std::cout << "\n Length of longest decreasing sequence is "  << findLongestDecrSeq(array[], arr_size); //the same is here
		       }
		 }
    }
 }
         catch (const char *error) {
           std::cerr << std::endl << error << std::endl;
           return -1;
          }
    return 0;
}
Last edited on
You "can't", but what is the actual error?

Is it:
error: expected primary-expression before ']' token

on lines 73 and 99?

Similar statement on line 48 is ok? What is the difference?


Note also that line 33 declares int array;. That variable is one integer, not an array.

You do declare on line 46 int array[], which is an array (and hides the integer "array" declared on outer scope) but it exists only on lines 46-49.
findLongestDecrSeq( array[], arr_size); //compiler writes "73 104 [Error] expected primary-expression before ']' token" here

look at line 25 findLongestDecrSeq(array, arr_size);
just write the name of the variable.
And when you have corrected that error, note that array is a vector:
std::vector<int> array(arr_size);

Yet your function expects it to be a standard array:
int findLongestDecrSeq(const int array[], const int arr_size)


You need to choose one type of array and be consistent.
@tapni, you have serious variable scoping problems. You declare variables and arrays (vectors) in local blocks of code that aren't seen outside of those blocks.

https://www.learncpp.com/cpp-tutorial/local-variables/

Passing a vector as a parameter to a function doesn't require passing the vector's size as another parameter. Passing the size is only required when passing a regular array.

Not this:
int findLongestDecrSeq(const int array[], const int arr_size)

This:
int findLongestDecrSeq(const std::vector<int>& array)

What's the & doing there? Passing the vector as a "reference" so the vector isn't copied when passed.

https://stackoverflow.com/questions/15890427/passing-vector-by-reference
@Furry_Guy @lastchance @ne555 @keskiverto
thanks everyone. it works but now the problem is that the program outputs the maximum length of the sequence infinitely.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
int findLongestDecrSeq(const int array[], const int arr_size)
{
	int lds[arr_size]; 
	int i, j, max = 0;

	for (i = 0; i < arr_size; i++)
	 {	std::cout <<  array[i] <<" ";
		lds[i] = 1;} 
		
	for (i = 1; i < arr_size; i++)
		for (j = 0; j < i; j++)
			if (array[i] < array[j] && lds[i] < lds[j] + 1) 
				lds[i] = lds[j] + 1; 
              findLongestDecrSeq(array, arr_size);
	for (i = 0; i < arr_size; i++)
		if (max < lds[i]) 
			max = lds[i]; 

	return max;
}
Last edited on
That's a little sloppy, you're missing a } somewhere.
@tapni

You don't have a base case for the recursion, it needs this to decide where or how to end the looping.

Put the closing brace in the same column as the opening one:

6
7
8
9
for (i = 0; i < arr_size; i++)
	 {	std::cout <<  array[i] <<" ";
		lds[i] = 1;
         }



Ideally put braces for the body of a loop even if it only one line, this will save problems when adding code later:

1
2
3
4
5
6
7
8
9
10
for (i = 1; i < arr_size; i++) 
{
		for (j = 0; j < i; j++)
                {
			if (array[i] < array[j] && lds[i] < lds[j] + 1) 
                        { 
				lds[i] = lds[j] + 1; 
                        }
                }
}


For code like the above, I find braces on their own line a waste, so I do this:

1
2
3
4
5
6
7
for (i = 1; i < arr_size; i++) {
		for (j = 0; j < i; j++){
			if (array[i] < array[j] && lds[i] < lds[j] + 1) { 
				lds[i] = lds[j] + 1; 
                        }
                }
}

Last edited on
Topic archived. No new replies allowed.