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;
}
I have debugged your code completely, and it is working fine now.
Errors have been resolved
I will tell you your mistakes
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)
C++ Code error help. I am getting the error: "expression must have a constant value, the...
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...
In the code C++ below it keeps saying expression must have a constant value at line 4, how I can fix it using with the dynamic memory int n; cout << "Enter number of homework grades: "; cin >> n; int hw[n]; float sum = 0; float hwavg, qavg; cout << "Homework Grades: "; for (int i = 0; i < n; i++) { cin >> hw[i]; sum += hw[i]; } hwavg = sum / n;
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; ...
I am receiving an "undefined reference to" build error in my code that i do not know how to fix. Here is my main code in C++ (the line of code that is in question is in bold): #include <iostream> #include<iomanip> #include<stdio.h> #include<stdlib.h> using namespace std; //Prototypes of functions void sortTestScores(int*testScores,int size); double averageTestScore(int*testScores,int size); void printTestScores(int*testScores, int size); int main() { //variable declaration int *testScores; int size; int score; double average; //Prompt to enter size of array cout<<"Enter size...
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);...
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 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 =...
Have Corporate Sales for 6 divisions and their quarterly sales figures. The issue is the error function where I am unable to have user reinput the same quarterly amount without going to the next quarter input. This has affected the total sales for the company. /***************************************** This program gathers sales information for six divisions and displays the total sales for each division and total company. *****************************************/ #include<iostream> using namespace std; // Classs division sales class DivSales { private: // Variables...
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;...