Question

C++ Code error help. I am getting the error: "expression must have a constant value, the...

C++ Code error help. I am getting the error: "expression must have a constant value, the value of parameter 'n' ( declared at line 7) cannot be used as a constant" I am also getting this error at lines 65 and 66 with m and n. I do not know how to fix this. Here is my code and ive marked where the errors were with lines:

#include<iostream>
using namespace std;

// Function to allocate memory to blocks as per worst fit
// algorithm
void worstFit(int blockSize[], int m, int processSize[],
   int n)
{
   // Stores block id of the block allocated to a
   // process
------ int allocation[n]; -------- here

   // Initially no block is assigned to any process
   memset(allocation, -1, sizeof(allocation));

   // pick each process and find suitable blocks
   // according to its size ad assign to it
   for (int i = 0; i<n; i++)
   {
       // Find the best fit block for current process
       int wstIdx = -1;
       for (int j = 0; j<m; j++)
       {
           if (blockSize[j] >= processSize[i])
           {
               if (wstIdx == -1)
                   wstIdx = j;
               else if (blockSize[wstIdx] < blockSize[j])
                   wstIdx = j;
           }
       }

       // If we could find a block for current process
       if (wstIdx != -1)
       {
           // allocate block j to p[i] process
           allocation[i] = wstIdx;

           // Reduce available memory in this block.
           blockSize[wstIdx] -= processSize[i];
       }
   }

   cout << "\nProcess No.\tProcess Size\tBlock no.\n";
   for (int i = 0; i < n; i++)
   {
       cout << " " << i + 1 << "\t\t" << processSize[i] << "\t\t";
       if (allocation[i] != -1)
           cout << allocation[i] + 1;
       else
           cout << "Not Allocated";
       cout << endl;
   }
}

// Driver code
int main()
{
   int m, n;
   cout << "Enter no. of memory partitions: ";
   cin >> m;
   cout << "\nEnter the number of processes:";
   cin >> n;

------ int blockSize[m];--------- here
------- int processSize[n];------- here
   cout << "\nEnter size of each mermory partition: \n";
   for (int i = 1; i <= m; i++) {
       cout << "Partition no." << i << ":";
       cin >> blockSize[i];
   }

   cout << "\nEnter the size of each process :-\n";
   for (int i = 1; i <= n; i++) {
       cout << "Process no. " << i << ":";
       cin >> processSize[i];
   }

   worstFit(blockSize, m, processSize, n);

   return 0;
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

I have debugged your code completely, and it is working fine now.

Errors have been resolved

I will tell you your mistakes

  1. You are creating an array  int blockSize[m]; (size of the block is m, elements will be stored from 0 to m-1, right?)
  2. for (int i = 1; i <= m; i++) {
           cout << "Partition no." << i << ":";
           cin >> blockSize[i];
    } Here you are using memory blockSize[m] which is not defined, and blockSize[0] is getting filled with random value. So i have initialised i from 0 until i<m.
  3. for (int i = 1; i <= n; i++) {
           cout << "Process no. " << i << ":";
           cin >> processSize[i];
       } Same is the scenario with this. So i have initialised i from 0 until i<n

Here is the corrected code.

#include<iostream>
#include<bits/stdc++.h>
using namespace std;

// Function to allocate memory to blocks as per worst fit
// algorithm
void worstFit(int blockSize[], int m, int processSize[],
int n)
{
// Stores block id of the block allocated to a
// process
int allocation[n];

// Initially no block is assigned to any process
memset(allocation, -1, sizeof(allocation));

// pick each process and find suitable blocks
// according to its size ad assign to it
for (int i = 0; i<n; i++)
{
// Find the best fit block for current process
int wstIdx = -1;
for (int j = 0; j<m; j++)
{
if (blockSize[j] >= processSize[i])
{
if (wstIdx == -1)
wstIdx = j;
else if (blockSize[wstIdx] < blockSize[j])
wstIdx = j;
}
}

// If we could find a block for current process
if (wstIdx != -1)
{
// allocate block j to p[i] process
allocation[i] = wstIdx;

// Reduce available memory in this block.
blockSize[wstIdx] -= processSize[i];
}
}

cout << "\nProcess No.\tProcess Size\tBlock no.\n";
for (int i = 0; i < n; i++)
{
cout << " " << i + 1 << "\t\t" << processSize[i] << "\t\t";
if (allocation[i] != -1)
cout << allocation[i] + 1;
else
cout << "Not Allocated";
cout << endl;
}
}

// Driver code
int main()
{
int m, n;
cout << "Enter no. of memory partitions: ";
cin >> m;
cout << "\nEnter the number of processes:";
cin >> n;

int blockSize[m];
int processSize[n];
cout << "\nEnter size of each mermory partition: \n";
for (int i = 0; i <m; i++) {
cout << "Partition no." << i << ":";
cin >> blockSize[i];
}

cout << "\nEnter the size of each process :-\n";
for (int i = 0; i <n; i++) {
cout << "Process no. " << i << ":";
cin >> processSize[i];
}

worstFit(blockSize, m, processSize, n);

return 0;
}

(If you like the answer, Please do give an upvote, Thank you)

Add a comment
Know the answer?
Add Answer to:
C++ Code error help. I am getting the error: "expression must have a constant value, the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • The second picture that I attached is the input and output. I already did the code...

    The second picture that I attached is the input and output. I already did the code but I also have to add the partition number assigned to the job (if the job was allocated). Can you please do that for me? I copied and pasted my code below. #include <iostream> #include <string.h> #include <stdio.h> using std::cout; using std::cin; using std::endl; unsigned int memorySize; // Function prototypes unsigned int FirstFit(struct Partition *, int, struct Job *, int); unsigned int BestFit(struct Partition...

  • Am I getting this error because i declared 'n' as an int, and then asking it...

    Am I getting this error because i declared 'n' as an int, and then asking it to make it a double? This is the coude: #include "stdafx.h" #include <iostream> #include <fstream> #include <string> #include <algorithm> #include <vector> using namespace std; void sort(double grades[], int size); char calGrade(double); int main() {    int n;    double avg, sum = 0;;    string in_file, out_file;    cout << "Please enter the name of the input file: ";    cin >> in_file;   ...

  • IN C++ Help to fix code Library management program. Error is when i type a letter...

    IN C++ Help to fix code Library management program. Error is when i type a letter into options menu the code breaks. after your edits, the code should still run as normal. CODE: #include<iostream> using namespace std; //structure to store library data class BookDetails{ public: struct library { int code; char title[20]; int status; }; library book_details[100]; }; //structure to store user data class UserDetails:public BookDetails{ public: struct users { int id; char name[20]; int booksNumber; }; users user_details[100]; };...

  • Can someone help me solve this problem? Everything works but I keep getting an error "expression...

    Can someone help me solve this problem? Everything works but I keep getting an error "expression must have a constant value". Its the Extended Euclidean Algorithm #include <iostream> using namespace std; #include<vector> void TwoLargest(int a[], int x) {    int largeOne = a[0];    int largeTwo = a[0];    for (int i = 1; i < x; i++)    {        if (a[i] > largeOne)        {            largeTwo = largeOne;            largeOne =...

  • The code will not run and I get the following errors in Visual Studio. Please fix the errors. err...

    The code will not run and I get the following errors in Visual Studio. Please fix the errors. error C2079: 'inputFile' uses undefined class 'std::basic_ifstream<char,std::char_traits<char>>' cpp(32): error C2228: left of '.open' must have class/struct/union (32): note: type is 'int' ): error C2065: 'cout': undeclared identifier error C2065: 'cout': undeclared identifier error C2079: 'girlInputFile' uses undefined class 'std::basic_ifstream<char,std::char_traits<char>>' error C2440: 'initializing': cannot convert from 'const char [68]' to 'int' note: There is no context in which this conversion is possible error...

  • C++ getline errors I am getting getline is undefined error messages. I would like the variables...

    C++ getline errors I am getting getline is undefined error messages. I would like the variables to remain as strings. Below is my code. #include <iostream> #include<string.h> using namespace std; int index = 0; // variable to hold how many customers are entered struct Address //Structure for the address. { int street; int city; int state; int zipcode; }; // Customer structure struct Customer { string firstNm, lastNm; Address busAddr, homeAddr; }; // Functions int displayMenu(); Customer getCustomer(); void showCustomer(Customer);...

  • Need help with a C++ program. I have been getting the error "this function or variable...

    Need help with a C++ program. I have been getting the error "this function or variable may be unsafe" as well as one that says I must "return a value" any help we be greatly appreciated. I have been working on this project for about 2 hours. #include <iostream> #include <string> using namespace std; int average(int a[]) {    // average function , declaring variable    int i;    char str[40];    float avg = 0;    // iterating in...

  • I am getting the Segmentation fault error on the Ubuntu machine but not on macOS. Any...

    I am getting the Segmentation fault error on the Ubuntu machine but not on macOS. Any help would be appreciated. /**** main.c ****/ #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <stdio.h> #include <stdlib.h> #include <time.h> #include <unistd.h> #include <pthread.h> #include <string.h> #define WORD_LEN 6 #define TOP 10 char * delim = "\"\'.“”‘’?:;-,—*($%)! \t\n\x0A\r"; struct Word { char word[30]; int freq; }; int threadCount; int fileDescriptor; int fileSize; off_t chunk; struct Word* wordArray; int arrIndex = 0; pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;...

  • Hello, I need help with my code. The code needs to display random number from 1...

    Hello, I need help with my code. The code needs to display random number from 1 to 50 every time the program runs but the program displays the same random numbers every time. Thanks #include #include using namespace std; void dynAlloc(int size, int *&arr); void displayArray(int *arr, int n); void insertionSort(int *arr, int n, int *temp); void linear_search(int *arr, int n, int key); void binary_search(int *arr, int n, int key); int main() {   const int n = 50; int *arr,...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT