Help with Pointers and functions

So I am dealing with the following errors.
*************
1) Expected constant expression
2)'cont' : unknown size
3) '=': cannot convert from double *' to 'int*'
4) '=': cannot convert from 'int *' to 'int'
5) cannot allocate an array of constant size 0
***************
Any help would be awesome, thanks ahead of time!



#include<iostream>
#include<iomanip>
using namespace std;

// Function Prototypes
void movieAvg(int *, int); // Function used for calculating the //Average
void movieMode(int *, int); // Function used for calculating the //Mode
void movieMed(int *, int); // Function used for calculating the //Median

int main()
{
int *studentsPtr, // Used to dynamically allocate an array
stuSurvey, // Used to hold the cin value presented //by the user
students; // Used to hold the value of the Ptr

// Getting the total amount of students of took part in this survey
cout << " How many students took part in this survey: ";
cin >> stuSurvey;

// Used to dynamically allocate an array that will be large enough for //the
// total number of students involved
studentsPtr = new double[stuSurvey];

// Getting the total amount of movies each student watched.
cout << "Please enter the amount of movies each student watched. \n";
for (int count = 0; count < stuSurvey; count++)
{
cout << "Student #" << (count + 1) << ": ";
cin >> studentsPtr[count];
}
students = studentsPtr;

movieAvg(&students, stuSurvey);
movieMed(&students, stuSurvey);
movieMode(&students, stuSurvey);

return 0;
}

// Used to calculate the total number of movies that all the students watched.
void movieAvg(int *studentsPtr, int stuSurvey)
{
double total = 0.0,
average = 0.0;
for (int count = 0; count < stuSurvey; count++)
{
total += studentsPtr[count];
}
average = total / stuSurvey;
cout << "In total, " << stuSurvey << " college students watched an average of ";
cout << average << " movies a month.\n";
}

// Used to sort and then calculate the median
void movieMed(int *studentsPtr, int stuSurvey)
{
int startCount,
minIndex,
minValue;
for (startCount = 0; startCount < (stuSurvey - 1); startCount++)
{
minIndex = startCount;
minValue = studentsPtr[startCount];
for (int index = (startCount + 1); index < stuSurvey; index++)
{
if (studentsPtr[index] < minValue)
{
minValue = studentsPtr[index];
minIndex = index;
}
}
studentsPtr[minIndex] = studentsPtr[startCount];
studentsPtr[startCount] = minValue;
}
int first = 0,
middle,
last = stuSurvey,
leftMiddle,
rightMiddle;
double middle2;
if (stuSurvey % 2 == 0) // In case the total number of //students entered is even
{
leftMiddle = ((first + last) / 2) - 1;
rightMiddle = (first + last) / 2;
middle2 = (studentsPtr[leftMiddle] + studentsPtr[rightMiddle]) / 2;
cout << fixed << showpoint << setprecision(2);
cout << "The median value of the movies seen is: " << middle2 << "\n";
}
else
{
middle = (first + last) / 2;
cout << "The median value of the movies seen is: " << studentsPtr[middle] << "\n";
}
}

// Function used to calculate the mode (the most common number) of the survey
void movieMode(int *studentsPtr, int stuSurvey)
{
int cont[stuSurvey],
countTotal = 0,
mode,
highest1,
highest2,
temp1,
temp2,
startCount,
minIndex,
minValue;
// Used to sort function
for (startCount = 0; startCount < (stuSurvey - 1); startCount)
{
minIndex = startCount;
minValue = studentsPtr[startCount];
for (int index = (startCount + 1); index < stuSurvey; index++)
{
if (studentsPtr[index] < minValue)
{
minValue = studentsPtr[index];
minIndex = index;
}
}
studentsPtr[minIndex] = studentsPtr[startCount];
studentsPtr[startCount] = minIndex;
}
for (int count = 0; count < stuSurvey; count++)
{
for (int index2 = 0; index2 < stuSurvey; index2++)
{
if (count != index2)
{
if (studentsPtr[count] == studentsPtr[index2])
{
countTotal++;
}
}
}
cont[count] = countTotal;
}
// Now that it's been orginize the mode can be determined
for (int count = 0; count < (stuSurvey - 1); count++)
{
if (cont[count] > cont[count + 1])
{
temp1 = cont[count];
temp2 = studentsPtr[count];
cont[count] = cont[count + 1];
studentsPtr[count] = studentsPtr[count + 1];
highest1 = temp1;
highest2 = temp2;
}
}
mode = temp2;
cout << "The mode for the college student movie survey is: " << mode << "\n";
}
You need to find and consistently use an indentation style, your code as presented is almost impossible to read.

For the first error the size of arrays in C++ must be compile time constants, using variables to set the size is not allowed. To correct this issue I recommend you switch to std::vector instead of the arrays.


Next in future please post the complete error messages, all of them, exactly as they appear in your development environment. The messages you posted are missing important information.



Topic archived. No new replies allowed.