Hey everyone, so today I had a quiz and this program was the quiz question and I could not get it to compile. The lines are 26, 29, and 32 where the errors occurred. Anyone know what the error is? The output is also included but remember that lines are 26, 29, and 32, not the ones stated.
This program has the user enter in an integer and that represents the number of elements of an array. And this code outputs how many numbers are positive and how many numbers are zero.
count.cpp: In function ‘int main()’:
count.cpp:34: error: expected primary-expression before ‘]’ token
count.cpp:37: error: expected primary-expression before ‘]’ token
count.cpp:40: error: expected primary-expression before ‘]’ token
#include <cstdlib>
#include <iostream>
usingnamespace std;
// FUNCTION PROTOTYPES
void get_list(int list[], int size);
int count_pos(constint list[], int size);
int count_zeros(constint list[], int size);
int main()
{
int length;
int num_pos, num_zeros;
int * list;
int *size=newint[length];
// Prompt and read number of elements from the user into the variable length
cout << "Enter number of elements: ";
cin >> length;
// Create a new array (dynamically allocate) of size length and assign it to variable list
list= newint[length];
// Prompt and read the elements from the user. Call the function get_list
get_list(list[],size);
// Count the number of positive numbers in the list. Call the function count_pos. Use variable num_pos
count_pos(list[], size);
// Count the number of zeros in the list. Call the function count_zeros. Use variable num_zeros
count_zeros(list[],size);
// Display number of positive elements
cout << " Number of positive elements: " << num_pos << endl;
// Display number of zeros
cout << "Number of zero elements: " << num_zeros << endl;
// De-allocate the list
delete [] list;
return 0;
}
// FUNCTION DEFINTIONS GO HERE
void get_list(int list[], int size)
{
int i(0);
int x(0);
cout << "Enter list: " << endl;
while(i<size)
{
cin >> x;
list[i]=x;
i++;
}
}
int count_pos(constint list[], int size)
{
int num_pos(0);
for(int i=0; i < size; i++)
{
if(list[i]>0)
{
num_pos++;
}
}
}
int count_zeros(constint list[], int size)
{
int num_zeros(0);
for(int i=0; i < size; i++)
{
if(list[i] == 0)
{
num_zeros++;
}
}
}
@IWishIKnew. I have done that already. I wish I knew what was different that regular passing. The point of this quiz was to use the [new] and [delete] functions but I don't see how I am passing them incorrectly.
@Lumpkin, My apologies, some reason I pasted the code twice. I have edited it
In line 15, you are initializing *size to length, when length has not yet been declared. Also, for your functions, did you want to return anything? I think you may have missed that step for count_pos and count_zeros...
Actually, having now read your code in more detail, why are you dynamically allocating "size" anyway? For the start of your code, just get rid of size, and replace your references to "size" with length for the rest of your code.
@NT3, so you're saying that I should move line 15 to 21? If so, I still get the same errors. I wonder if I messed up the function prototypes because I don't see how i'm passing the arrays any differently.
Secondly, I did not want to return anything. The quiz stated not to return any values, which I thought was weird, too. This quiz was about dynamically allocating arrays and this was the quiz question. I could have done this without allocating but professor wanted us to allocate.
The quiz had lines 1-14 so we had to use them. And all comments are the steps that the professor wanted us to do. I filled in lines 15 (excluding the comments) So I am not able to pass the variables by reference
#include <cstdlib>
#include <iostream>
usingnamespace std;
// FUNCTION PROTOTYPES
void get_list(int list[], int size); //Function prototype that asks the user for the size and number of elements in an array
int count_pos(constint list[], int size); //Function prototype that counts how many positive numbers there are
int count_zeros(constint list[], int size); //Function prototype that counts how many zeros are in the array
int main()
{
int length;
int num_pos, num_zeros;
int * list;
// Prompt and read number of elements from the user into the variable length
cout << "Enter number of elements: ";
cin >> length;
// Create a new array (dynamically allocate) of size length and assign it to variable list
int *size=newint[length];
list= newint[length];
// Prompt and read the elements from the user. Call the function get_list
get_list(list,length);
// Count the number of positive numbers in the list. Call the function count_pos. Use variable num_pos
count_pos(list,length);
// Count the number of zeros in the list. Call the function count_zeros. Use variable num_zeros
count_zeros(list,length);
// Display number of positive elements
cout << " Number of positive elements: " << num_pos << endl;
// Display number of zeros
cout << "Number of zero elements: " << num_zeros << endl;
// De-allocate the list
delete [] list;
return 0;
}
// FUNCTION DEFINTIONS GO HERE
void get_list(int list[], int size) //This function has the user enter in values for the array
{
int i(0); //Used for the while loop
int x(0); //Variable that the user enters
cout << "Enter list: " << endl;
while(i<size) //Fill in the elements with the corresponding locations
{
cin >> x;
list[i]=x; //The spot in the list where the variable i is located is now filled in with x.
i++; //Increase variable i by one to go to the next integer for list
}
}
int count_pos(constint list[], int size) //This function counts how many positives are in the array
{
int num_pos(0);
for(int i=0; i < size; i++)
{
if(list[i]>0)
{
num_pos++;
}
}
}
int count_zeros(constint list[], int size) //This function counts how many zeros are in the array
{
int num_zeros(0);
for(int i=0; i < size; i++)
{
if(list[i] == 0)
{
num_zeros++;
}
}
}
I have fixed the code, but I am getting random numbers for the number of zeros and number of positive integers. How do I get it to display that without returning anything?