Question
C++ program: How to fix the following message: The value of parameter n cannot be used as a constant and the value of variable count
C++ program: How to fix the following message: The value of parameter n cannot be used as a constant and the value of variable count cannot be used as a constant in this program?
#include
#include
#include
#include
using namespace std;
//function for average
double average(int arr[], int n)
{
double sum = 0; //initialize sum to 0
int i; //to iterate
//get sum
for (i = 0; i sum += arr[i]; return sum / i; //return average } //median function double median(int arr[], int n) { THE VALUE OF PARAMETER n CANNOT BE USED AS A CONSTANT int arr_dup[n]; //duplicate array int i = 0; //to iterate //copy arr to arr_dup while (i { arr_dup[i] = arr[i]; i++; } //sort dup_arr sort(arr_dup, arr_dup + n); //return median if (n % 2 != 0) return arr_dup[n / 2]; else return (arr_dup[n / 2] + arr_dup[n / 2 - 1]) / 2; } //function for mode int mode(int arr[], int n) { THE VALUE OF PARAMETER n CANNOT BE USED AS A CONSTANT int freq[n] = { 0 }; //frequency int i, j; //to iterate //calculate frequency for (i = 0; i { for (j = 0; j { if (arr[i] == arr[j]) freq[i]++; } } //assume first element as mode int mode_index = 0; //find mode for (i = 1; i { if (freq[i]>freq[mode_index]) { mode_index = i; } } return arr[mode_index]; //return mode } //display results void display(double avg, double med, int mod, string least, string most) { cout << "Average: " << avg << endl; cout << "Median: " << med << endl; cout << "Mode: " << mod << endl; cout << "Student who watched least no. of movies: " << least << endl; cout << "Student who watched most no. of movies: " << most << endl; } //execution begins here int main() { ifstream inFile("movies.txt"); //open file string line; //to read line int count = 0; //to count students //count students while (getline(inFile, line)) count++; inFile.close(); //close file THE VALUE OF VARIABLE COUNT CANNOT BE USED AS A CONSTANT int movies[count]; //no. of movies array string students[count]; //students array string fName, lName; //to read first name and last name int i = 0; //to iterate inFile.open("movies.txt"); //open file again //read data while (getline(inFile, line)) { istringstream iss(line); //string stream iss >> fName >> lName >> movies[i]; //read values students[i] = fName + " " + lName; //put student name in array i++; //increment index } //call functions double avg = average(movies, count); double med = median(movies, count); int mod = mode(movies, count); //find students with least and most number of movies string least, most; int min, max; least = most = students[0]; min = max = movies[0]; for (i = 1; i { if (movies[i]>max) { max = movies[i]; most = students[i]; } else if (movies[i] { min = movies[i]; least = students[i]; } } //call function to display results display(avg, med, mod, least, most); inFile.close(); system("pause"); return 0; } Movies.txt Murray Brandl 3 Christal Delamater 4 Zetta Kinlaw 7 Elia Roy 3 Delmer Bibb 4 Joannie Nevers 4 Roselle Gose 10 Jonathan Basnett 0 Marcel Earwood 12 Marina Newton 2 Magdalen Stephan 3 Deane Leach 5 Mariana Crosley 6 Darby Froman 5 Shonda Kyzer 4 Ilana Netto 4 Candida Magnani 1 Laurena Stiverson 2 Elouise Muir 4 Rene Holiday 2
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started