12 Compiler errors found in my code :(

I am not sure why my program is generating these errors. Line 24 especially since I worked with a tutor I did not expect these errors. What am I doing wrong. I know the program is not done and I am only on question 2 out of 5 for my homework, but am getting compiler errors already :( I will post the compiler errors below the code. Anything helps, really. 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
  #include <iostream>
using namespace std; 

//Write a C++ function rotate(int arr[], int d, int n) 
//that rotates arr[] of size n to the left by d elements

//Tutor explanation:
//1.find index
//2. in new indiex ** store in temp. **store index value in temp
//3. Assign current value to new index 
//int d = 3 
//int n = 7
// int arry[] = 0
void rotate(int arr[], int d, int n){
		int array2[n]; //temporary array
		int j = 0;
		int i;

	for(i=d;i<n;i++){
		array2[j]=arr[i];
		j++;
	}	
	for(i=0;i<d;i++){
		array[j] = arr[i];
		j++;
	}   
	//Swap happens here
	for(i=0;i<n;i++){
		arr[i] = array2[i];
	}
}


//Given an array of random numbers, write a C++ function pushZerosToEnd(int arr[],int n) 
//to push all the zero’s of a given array to the end of the array. 
//Do not change the order of the non-zero elements

void pushZerosToEnd(int arr, int n){
int counter1; 
int counter2;
int temp[8];
int j=8;
int k=0;

for(int i=0;i<8;i++){
	if(arr[i] == 0){
		temp[j] = arr[i]; //Starting from right side
		j--;
	}
	else
	{
		temp[k]=arr[i];
		k++;
	}
		 

	}
}


//Given a linked list with at least two nodes...
//remove the last element from the linked list.
//Function prototype: Node* removeLastElement(Node* head)

Node* removeLastElement(Node* head){

	//Code from another in class assignment
	
}

//Reverse a singly linked list.
//Function prototype: Node* reverseList(Node* head)

Node* reverseList(Node* head){

}

//Merge two sorted linked lists and return it as a new list.
// the new list should be made by splicing together 
// the nodes of the first two lists.
//Function prototype: Node* mergeTwoLists(Node* n1, Node* n2)

Node* mergeTwoLists(Node* n1, Node* n2){

}

//In your main function...
//please give at least one test case for each of these five questions. 
int main()
{
	 



}

return 0;




Homework1.cpp:24:3: error: C++ requires a type specifier for all declarations
array[j] = arr[i];
^
Homework1.cpp:45:8: error: subscripted value is not an array, pointer, or vector
if(arr[i] == 0){
~~~^~
Homework1.cpp:46:16: error: subscripted value is not an array, pointer, or
vector
temp[j] = arr[i]; //Starting from right side
~~~^~
Homework1.cpp:51:14: error: subscripted value is not an array, pointer, or
vector
temp[k]=arr[i];
~~~^~
Homework1.cpp:63:1: error: unknown type name 'Node'
Node* removeLastElement(Node* head){
^
Homework1.cpp:63:25: error: unknown type name 'Node'
Node* removeLastElement(Node* head){
^
Homework1.cpp:71:1: error: unknown type name 'Node'
Node* reverseList(Node* head){
^
Homework1.cpp:71:19: error: unknown type name 'Node'
Node* reverseList(Node* head){
^
Homework1.cpp:79:1: error: unknown type name 'Node'
Node* mergeTwoLists(Node* n1, Node* n2){
^
Homework1.cpp:79:21: error: unknown type name 'Node'
Node* mergeTwoLists(Node* n1, Node* n2){
^
Homework1.cpp:79:31: error: unknown type name 'Node'
Node* mergeTwoLists(Node* n1, Node* n2){
^
Homework1.cpp:94:1: error: expected unqualified-id
return 0;
^
12 errors generated.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void rotate(int arr[], int d, int n){
		int array2[n]; // this is not legal C++
		int j = 0;
		int i;

	for(i=d;i<n;i++){
		array2[j]=arr[i];
		j++;
	}	
	for(i=0;i<d;i++){
		array[j] = arr[i]; // error: C++ requires a type specifier for all declarations
		j++;
	}   
	for(i=0;i<n;i++){
		arr[i] = array2[i];
	}
}

What is the array[j]?
You have declared only:
1
2
3
4
5
6
int arr[]
int d
int n
int array2[n]
int j
int i



1
2
3
4
void pushZerosToEnd(int arr, int n){

for(int i=0;i<8;i++){
	if( arr[i] == 0) // error: 'arr' is an int 



error: unknown type name 'Node'
What is the Node?
Hello ryan157,

In addition to what keskiverto has said.

Given these errors:

Homework1.cpp:45:8: error: subscripted value is not an array, pointer, or vector
if(arr[i] == 0){
~~~^~
Homework1.cpp:46:16: error: subscripted value is not an array, pointer, or
vector
temp[j] = arr[i]; //Starting from right side
~~~^~
Homework1.cpp:51:14: error: subscripted value is not an array, pointer, or
vector
temp[k]=arr[i];
~~~^~

For the first two the error did not start on those lines. You have to look at line 38 where you defined "arr" as an int not an array. And for the last error yo will come to learn that it means that "temp" is not defined.

The error messages are good, but sometimes you have to look elsewhere to find where the error first starts.

To put keskiverto's question a different way; Where does "Node" come from? Are you missing a header file or the code for the class?

In the function Node* reverseList(Node* head) where did you define the variable "head" that is passed to the function?

When you have answers to these questions the errors will go away unless something new comes up.

Hope that helps,

Andy

Edit:
Last edited on
Thank you very much for the reply. Line 38 was an int because that is how the function was written in question #2 of my homework:

" Given an array of random numbers, write a C++ function pushZerosToEnd(int arr[],
int n) to push all the zero’s of a given array to the end of the array. Do not change the
order of the non-zero elements. "

Moreover, how do I define the temp like you said?

The Node comes from my homework question as well, I will just post them all below this sentence in order to see if it makes things clearer.

Q #1: Write a C++ function rotate(int arr[], int d, int n) that rotates arr[] of size n to the left
by d elements.

Q #2: Given an array of random numbers, write a C++ function pushZerosToEnd(int arr[],
int n) to push all the zero’s of a given array to the end of the array. Do not change the
order of the non-zero elements.

Q #3: Given a linked list with at least two nodes, remove the last element from the linked list.
Function prototype: Node* removeLastElement(Node* head)

Q #4: Reverse a singly linked list.
Function prototype: Node* reverseList(Node* head)

Q #5: Merge two sorted linked lists and return it as a new list. The new list should be made by
splicing together the nodes of the first two lists.
Function prototype: Node* mergeTwoLists(Node* n1, Node* n2)
Line 38 was an int because that is how the function was written in question #2 of my homework:

" Given an array of random numbers, write a C++ function
pushZerosToEnd( int arr[], int n )
to push all the zero’s of a given array to the end of the array. Do not change the
order of the non-zero elements. "

Is that really "an int"?

Put other way, question requires
void pushZerosToEnd(int arr[], int n)
and you wrote
void pushZerosToEnd(int arr , int n)


Q3-5: "Given a linked list"
Where is the code of linked list? You must have written it in previous excercises. You need it here.
Hello ryan157,

Moreover, how do I define the temp like you said?

Looking at your original code line 41 properly defines the array "temp". What I would add is:
1
2
3
4
5
6
7
8
9
void pushZerosToEnd(int arr[], int useablePart)
{
	constexpr size_t MAXSIZE{ 8 };

	int counter1;
	int counter2;
	int temp[MAXSIZE]{};
	int j = 8;
	int k = 0;

in line 7 the empty {}s will initialize the entire array to zero. The use of "MAXSIZE" means that you only have one place to change the size if needed. Making it a constant means that the program can not change its value. Being defined as a constant the capital letters are most often used and the caps help to remind you that it is defined as a constant.

Another advantage to "MAXSIZE" is the you could use it in your for loop in place of the "8", but in this case "useablepart" is the better choice because you may not use the entire size of the array, so your for loop will only need to use the part that has been used.

By initializing "temp" to all zeros when it is defined the for loop can loop through both "temp" and "arr" starting at zero until you reach "usablePart", BTW you can always change this name to something shorter that describes what it is used for, This way "temp" will start with numbers greater than zero and anything left will already be zero.

After the for loop you will either need to set "arr" equal to "temp" or return "temp" to where it was called from. As it is when the function ends so does the variable 'temp" never to be seen again until the function is called again and a new variable is defined.

Since the function "pushZerosToEnd" has a good start I would comment out everything else you have for now and just work on this function.

It is always better to work on one small piece at a time then trying to work on the whole program at once.

I would also start by defining the array in main along with the variable "n", that should have a better name, then use these variables to call the function for testing. By giving the array some values to start with it makes it easier to test the function knowing that you are using the same values for each test run giving you the opportunity to know what the outcome should be.

Once the function "pushZerosToEnd" is working I would concentrate on the function "rotate" then the others one at a time.

Hope that helps,

Andy
Thank you, keskiverto. I understand now that I needed the []'s !
For question 5 she gave us an example, I guess, that looks like:
Input: 1→2→4, 1→3→4
Output: 1→1→2→3→4→4

Actually, allow me to add all the examples for the homework questions:
Question 1:
Input: arr[] = {1, 2, 3, 4, 5, 6, 7}, d = 3, n = 7
Output: arr[] = {4, 5, 6, 7, 1, 2, 3}

Question 2:
Input: arr[] = {1, 2, 0, 4, 3, 0, 5, 0}
Output: arr[] = {1, 2, 4, 3, 5, 0, 0, 0}

Question 3:
Input: 1→2→6→3→4→5
Output: 1→2→6→3→4

Question 4:
Input: 1→2→3→4→5→NULL
Output: 5→4→3→2→1→NULL

Question 5:
Input: 1→2→4, 1→3→4
Output: 1→1→2→3→4→4

And my teacher requires test cases for each in the main part of the program which I am not sure how to create
For question 5 she gave us an example, I guess, that looks like:

Irrelevant for now.

Have you written class Node or struct Node earlier?
I haven't but my teacher discussed that in class but still confused about how it all works. :/
You cannot write functions that use Node unless you know how the Node has to be used.
The compiler cannot compile your code that uses Node unless it has/sees the code of Node.

If teacher gives function prototypes that contain Node, then she assumes that you already have access to the code of Node.
Topic archived. No new replies allowed.