Worst Fit Algorithm using an Array C++ - c++

I've created a code which is supposed to demonstrate the Worst Fit Algorithm using an array of size 256 (memory[256]), the code displays a map showing how the memory is being stored, the code allows the user to delete a memory location too , I want to have another array of any other size but larger than the first array , let's say memory2[456] , according to the Worst Fit Algorithm the elements should be added to the bigger memory first until it becomes smaller than the other and so on , the problem is that I can't think of a way of how to use the second array that I want to add to my code and add the elements to it first and then switch to the other array when the larger array becomes smaller than the other , could someone please help me !!
P.S I can't use a node,vector,linked list or any other thing it has to be an array
#include <iostream>
#include <cmath>
using namespace std;
int PutInMemory(int memory[], int size)
{
if (size < 1)
cout << "Error!" << endl;
int firstSize = 0;
int j;
for (int i = 0; i < 256; i++)
{
if (memory[i] < 0 && abs(memory[i]) >= size)
{
j = i;
firstSize += abs(memory[j]);
break;
}
}
if (firstSize < size)
{
cout << "Out of Memory";
return 0;
}
if (j + size <= 256)
{
memory[j] = size;
for (int i = j + 1; i < j + size; i++)
memory[i] = 0;
int i = j + size;
int count = 0;
while (memory[i] <= -1 && i < 256)
{
count++;
i++;
}
if (count != 0)
{
memory[i - 1] = -count;
memory[j + size] = -count;
}
return j;
}
else
{
cout << "Out of memory";
}
}
void DelSeg(int memory[], int n)
{
int count = memory[n];
int prev = 0;
int next = count - 1;
int i = n + 1;
int pos = n;
if (memory[n - 1] < -1 && n != 0)
{
i += memory[n - 1];
prev = -memory[n - 1];
count -= memory[n - 1];
pos = i;
}
while (true)
{
for (i; i < pos + count - 1; i++)
{
memory[i] = -1;
}
if (memory[i + 1] < -1)
{
count += -memory[i + 1] + 1;
next = -memory[i + 1];
}
else
{
break;
}
}
memory[n - prev] = 0 - count;
memory[n + next] = 0 - count;
}
void checkMemory(int memory[])
{
int countFreeSeg = 0;
int countFullSeg = 0;
int countFullMem = 0;
int countFreeMem = 0;
for (int i = 0; i < 256; i++)
{
if (memory[i] < 0)
{
if (memory[i] < 0)
cout << "Beginning of the adress:" << i << ", ";
int count = 0;
while (memory[i] < 0 && i < 256)
{
count++;
i++;
}
countFreeSeg++;
cout << "Size = " << count << endl;
countFreeMem += count;
i--;
}
}
cout << "Number of free processes = " << countFreeSeg << endl << endl;
cout << "Number of free memory = " << countFreeMem << endl << endl;
for (int i = 0; i < 256; i++)
{
if (memory[i] > 0)
{
cout << "Beginning adress: " << i << ", size - " << memory[i]
<< endl;
countFullMem += memory[i];
i += memory[i] - 1;
countFullSeg++;
}
}
cout << "Number of occupied processes = " << countFullSeg << endl;
cout << "Number of occupied memory = " << countFullMem << endl;
}
void print(int memory[])
{
for (int i = 0; i < 256; i++)
{
cout << memory[i] << " ";
}
}
int main()
{
int memory[256];
memory[0] = -256;
for (int i = 1; i < 256; i++)
{
memory[i] = -1;
}
while (true)
{
system("cls");
cout << "1.Allocate Memory \n2.Free memory segment\n3.Get information about the memory\n4.Exit" << endl;
int choice;
cin >> choice;
int m = 0;
switch (choice)
{
case 1:
system("cls");
cout << "Enter the amount of memory to be entered:" << endl;
cin >> m;
cout << PutInMemory(memory, m) << endl;
break;
case 2:
system("cls");
cout << "Enter the starting address of the memory location:"
<< endl;
cin >> m;
DelSeg(memory, m);
break;
case 3:
checkMemory(memory);
print(memory);
break;
case 4:
system("cls");
exit(0);
break;
default:
cout << "Incorrect entry" << endl;
break;
}
system("pause");
}
}

Related

Display first even and then odd elements in a C++array

I'm a C++ newb. I need to insert numbers to an array and then display first the odd numbers and then the even numbers in a single array. I've managed to create two separate arrays with the odd and even numbers but now I don't know how to sort them and put them back in a single array. I need your help to understand how to do this with basic C++ knowledge, so no advanced functions. Here's my code:
#include <iostream>
using namespace std;
int main()
{
int N{ 0 }, vector[100], even[100], odd[100], unify[100], i{ 0 }, j{ 0 }, k{ 0 };
cout << "Add the dimension: " << endl;
cin >> N;
cout << "Add the elements: " << endl;
for (int i = 0; i < N; i++) {
cout << "v[" << i << "]=" << endl;
cin >> vector[i];
}
for (i = 0; i < N; i++) {
if (vector[i] % 2 == 0) {
even[j] = vector[i];
j++;
}
else if (vector[i] % 2 != 0) {
odd[k] = vector[i];
k++;
}
}
cout << "even elements are :" << endl;
for (i = 0; i < j; i++) {
cout << " " << even[i] << " ";
cout << endl;
}
cout << "Odd elements are :" << endl;
for (i = 0; i < k; i++) {
cout << " " << odd[i] << " ";
cout << endl;
}
return 0;
}
If you don't need to store the values then you can simply run through the elements and print the odd and the even values to different stringstreams, then print the streams at the end:
#include <sstream>
#include <stddef.h>
#include <iostream>
int main () {
std::stringstream oddStr;
std::stringstream evenStr;
static constexpr size_t vecSize{100};
int vec[vecSize] = {10, 5, 7, /*other elements...*/ };
for(size_t vecIndex = 0; vecIndex < vecSize; ++vecIndex) {
if(vec[vecIndex] % 2 == 0) {
evenStr << vec[vecIndex] << " ";
} else {
oddStr << vec[vecIndex] << " ";
}
}
std::cout << "Even elements are:" << evenStr.rdbuf() << std::endl;
std::cout << "Odd elements are:" << oddStr.rdbuf() << std::endl;
}
Storing and sorting the elements are always expensive.
Basically, it would be better to sort them first.
#include <iostream>
using namespace std;
int main()
{
int numbers[5];
int mergedArrays[5];
int evenNumbers[5];
int oddNumbers[5];
for(int i=0;i<5;i++){
cin>>numbers[i];
}
int temp=numbers[0];
//bubble sort
for(int i = 0; i<5; i++)
{
for(int j = i+1; j<5; j++)
{
if(numbers[j] < numbers[i])
{
temp = numbers[i];
numbers[i] = numbers[j];
numbers[j] = temp;
}
}
}
int nEvens=0;
int nOdds=0;
for(int i = 0; i<5; i++)
{
if(numbers[i]%2==0)
{
evenNumbers[nEvens]=numbers[i];
nEvens++;
}
else if(numbers[i]%2!=0)
{
oddNumbers[nOdds]=numbers[i];
nOdds++;
}
}
int lastIndex=0;
//copy evens
for(int i = 0; i<nEvens; i++)
{
mergedArrays[i]=evenNumbers[i];
lastIndex=i;
}
//copy odds
for(int i =lastIndex; i<nOdds; i++)
{
mergedArrays[i]=oddNumbers[i];
}
return 0;
}
If you have to just output the numbers in any order, or the order given in the input then just loop over the array twice and output first the even and then the odd numbers.
If you have to output the numbers in order than there is no way around sorting them. And then you can include the even/odd test in the comparison:
std::ranges::sort(vector, [](const int &lhs, const int &rhs) {
return ((lhs % 2) < (rhs % 2)) || (lhs < rhs); });
or using a projection:
std::ranges::sort(vector, {}, [](const int &x) {
return std::pair<bool, int>{x % 2 == 0, x}; });
If you can't use std::ranges::sort then implementing your own sort is left to the reader.
I managed to find the following solution. Thanks you all for your help.
#include <iostream>
using namespace std;
int main()
{
int N{0}, vector[100], even[100], odd[100], merge[100], i{0}, j{0}, k{0}, l{0};
cout << "Add the dimension: " << endl;
cin >> N;
cout << "Add the elements: " << endl;
for (int i = 0; i < N; i++)
{
cout << "v[" << i << "]=" << endl;
cin >> vector[i];
}
for (i = 0; i < N; i++)
{
if (vector[i] % 2 == 0)
{
even[j] = vector[i];
j++;
}
else if (vector[i] % 2 != 0)
{
odd[k] = vector[i];
k++;
}
}
cout << "even elements are :" << endl;
for (i = 0; i < j; i++)
{
cout << " " << even[i] << " ";
cout << endl;
}
cout << "Odd elements are :" << endl;
for (i = 0; i < k; i++)
{
cout << " " << odd[i] << " ";
cout << endl;
}
for (i = 0; i < k; i++)
{
merge[i] = odd[i];
}
for (int; i < j + k; i++)
{
merge[i] = even[i - k];
}
for (int i = 0; i < N; i++)
{
cout << merge[i] << endl;
}
return 0;
}
You can use Bubble Sort Algorithm to sort whole input. After sorting them using if and put odd or even numbers in start of result array and and others after them. like below:
//Bubble Sort
void bubbleSort(int arr[], int n)
{
int i, j;
for (i = 0; i < n - 1; i++)
// Last i elements are already
// in place
for (j = 0; j < n - i - 1; j++)
if (arr[j] > arr[j + 1])
swap(arr[j], arr[j + 1]);
}
// Insert In array
int result[100];
if(odd[0]<even[0])
{
for (int i = 0; i < k; i++)
{result[i] = odd[i];}
for (int i = 0; i < j; i++)
{result[i+k] = even[i];}
}else
{
for (int i = 0; i < j; i++)
{result[i] = even[i];}
for (int i = 0; i < k; i++)
{result[i+k] = odd[i];}
}

Binary search algorithm in c++

I am new to programming so please help me completing the task
the problem is:
After pressing y the while loop does not run again.
and secondly, how to print or get the array elements in descending order?
thank you!
#include <iostream>
using namespace std;
int main()
{
int item;
int flaging = 0;
int ind_low = 0;
int ind_high = 9;
int ind_mid = (ind_low + ind_high) / 2;
char conti;
//Array declaration and taking user input
int arr[10];
cout << "enter some values here : \n" << endl;
for (int i = 0; i < 10; i++)
{
cin >> arr[i];
}
// for sorthing the array
int temp;
for (int p = 1; p <= 9; p++)
for (int c = 0; c <= 8; c++)
if (arr[c] > arr[c + 1])
{
temp = arr[c];
arr[c] = arr[c + 1];
arr[c + 1] = temp;
}
do {
//asking for searching
cout << "Enter the value you want to search : " << endl;
cin >> item;
while (ind_low <= ind_high)
{
if (item == arr[ind_mid])
{
cout << "At " << ind_mid << " index the value " << item << " is found " << endl;
flaging++;
break;
}
if (item < arr[ind_mid])
{
ind_high = ind_mid - 1;
ind_mid = (ind_low + ind_high) / 2;
}
else
{
ind_low = ind_mid + 1;
ind_mid = (ind_low + ind_high) / 2;
}
}
if (flaging == 0)
{
cout << "Value not found" << endl;
}
cout << "To search again press 'y', to exit press any key" << endl;
cin >> conti;
} while ((conti == 'y') || (conti == 'Y'));
}
when I ran it on my pc after pressing y it did run again, can you provide the input that failed you?
for the second question what do you mean?
you can do a for loop that goes like this:
for(int index = ARR_SIZE -1 ; index >= 0 ; --index){
cout << array[index];
}
edit: I understand what you mean. after each run you should reset your indexes otherwise you will always run on the same once:
before you end the loop the values should be reseted.
ind_low = 0;
ind_high = 9;
ind_mid = (ind_low + ind_high) / 2;
that gonna print the array from end to start.

power function no scope in function

#include<iostream>//Pls note:Only header allowed...
As this is the C++ i dont think so any other header is needed for that math function.
using namespace std;
int comparator(int audience[][2], int index1, int index2) {
int b1, e1;
int b2, e2;
b1 = audience[index1][1];
e1 = audience[index1][2];
b2 = audience[index2][1];
e2 = audience[index2][2];
double re1;
re1 = pow(b1, e1);
cout << re1 << endl;
double re2 = pow(b2, e2);
cout << re2 << endl;
if (re1 == re2)
{
return 0;
}
if (re1 > re2)
{
return -1;
}
if (re1 < re2)
{
return 1;
}
}
//Nothing has to be done with the rest of the two functions.
void sorting(int audience[][2], int N, int &i_index, int &j_index)
{
int i, j, temp;
for (i = 0; i<N - 1; i++)
{
if (audience[i][2] < audience[i + 1][2])
continue;
else
i_index = i;
break;
}
for (i = N; i > 1; i++)
{
if (audience[i][2]>audience[i - 1][2])
continue;
else
j_index = i;
break;
}
for (i = i_index + 1; i < j_index - 1; i++)
{
min = audience[i_index + 1][2];
for (i = )
if (audience[i_index][1] > audience[i_index + 1][1])
{
temp = audience[i_index + 1][1];
audience[i_index + 1][1] = audience[i_index][1];
audience[i_index][1] = temp;
}
}
for (i = i_index + 1; i <= j_index - 1; i++)
{
min = audience[i][2];
for (j = i_index + 2; j <= j_index - 1; j++)
{
if (min > audience[j][2])
{
temp = audience[i_index + 2][2];
audience[i_index + 1][2] = audience[i_index][2];
audience[i_index][2] = temp;
}
}
}
}
void merge(int audience[][2], int mergedarray[][2], int N, int i_index, int j_index)
{
}
int main()
{
int audience[100][2], mergedmarks[100][2];
int i, N;
int index1 = 0, index2 = 0;
int comp_result;
cout << "Enter the value of N : ";
cin >> N; // Enter size of the table
cout << "Enter the base and exponent for " << N << "rows " << endl;
for (i = 0; i < N; i++)
cin >> audience[i][0] >> audience[i][1]; //Enter numbers in the table
cout << endl << "Checking Function 1: Compare ecodes for 5 index pairs" << endl;
for (i = 0; i < 5; i++)
{
cout << "\nEnter indices of row1 and row2 that you want to compare: ";
cin >> index1 >> index2;
if (index1 < 0 || index2 < 0 || index1 >= N || index2 >= N)
continue;
comp_result = comparator(audience, index1, index2);
if (comp_result == -1)
cout << "ecode of index 1 is greater than ecode of index2" << endl;
else if (comp_result == 1)
cout << "ecode of index 1 is less than ecode of index2" << endl;
else if (comp_result == 0)
cout << "ecode of index 1 is equal to ecode of index2" << endl;
}
cout << endl;
int index_i = 0, index_j = N;
sorting(audience, N, index_i, index_j);
cout << "Checking Function 2: Printing sorted array " << endl;
for (i = 0; i < N; i++)
cout << audience[i][0] << " " << audience[i][1] << endl;
cout << endl;
cout << "index i: " << index_i << "\nindex j: " << index_j << endl;
cout << endl << "Checking Function 3: Printing Merged Array " << endl;
merge(audience, mergedmarks, N, index_i, index_j);
int merge_array_size = index_i + (N - (index_j + 1));
for (i = 0; i < N; i++)
cout << mergedmarks[i][0] << " " << mergedmarks[i][1] << endl;
cout << endl;
return 0;
}
This is the whole problem. I have to still edit the merge function. That is the whole problem.This is all.
You need to include the pow header, which is math.h, in order to use it.
add at the beginning of your file:
#include <math.h>
#include <iostream>
using namespace std;
POW is declared in math.h header file so use
#include<math.h>

Having problems with creating a merge sort algorithm (C++)

I'm having issues with the final implementation of merge sort. Below is my code:
#include <iostream>
using namespace std;
void decomposeArray(int* array, int length) {
int arrayOfArrays[length];
cout << endl;
for(int i = 0; i < length; i++) {
cout << "Array: " << array[i] << endl;
}
if (length > 1) {
int midpoint = length/2;
int elemInFirst = midpoint;
int elemInSecond = elemInFirst;
if ((length % 2) == 1) {
elemInSecond += 1;
}
int firstArray[elemInFirst];
int secondArray[elemInSecond];
for(int i = 0; i < elemInFirst; i ++) {
firstArray[i] = array[i];
}
for(int j = elemInFirst; j < length; j++) {
secondArray[j-elemInFirst] = array[j];
}
for(int i = 0; i < elemInFirst; i++) {
cout << "First half: " << firstArray[i] << endl;
}
for(int i = elemInFirst; i < length; i++) {
cout << "Second half: " << secondArray[i-elemInFirst] << endl;
}
decomposeArray(firstArray, elemInFirst);
decomposeArray(secondArray, elemInSecond);
int* superiorArray;
int* inferiorArray;
int dominantIndex;
int inferiorIndex;
if (elemInFirst > elemInSecond) {
dominantIndex = elemInFirst;
superiorArray = firstArray;
inferiorIndex = elemInSecond;
inferiorArray = secondArray;
}
else {
dominantIndex = elemInSecond;
superiorArray = secondArray;
inferiorIndex = elemInFirst;
inferiorArray = firstArray;
}
for(int i = 0; i < dominantIndex; i++) {
for (int j = 0; j < inferiorIndex; j++) {
if (superiorArray[i] > inferiorArray[j]) {
int tempVal = array[i];
array[i] = inferiorArray[j];
array[(dominantIndex+j)] = tempVal;
}
else if (j == (inferiorIndex-1)){
int tempVal = array[i];
cout << "Array at i is " << array[i] << endl;
cout << "i is " << i << endl;
cout << "This temp value is " << array[i] << endl;
array[i] = superiorArray[i];
cout << "superiorArray[i]: " << superiorArray[i] << endl;
array[((dominantIndex)+j)] = tempVal;
}
else {
continue;
}
}
cout << "Current array " << endl;
for(int i = 0; i < length; i++) {
cout << array[i] << ",";
}
cout << endl;
}
/*while ((firstIterator != elemInFirst) && (secondIterator !=elemInSecond) && (origArrayIter != length)) {
if (firstArray[firstIterator] > secondArray[secondIterator]) {
array[origArrayIter] = secondArray[secondIterator];
array[origArrayIter + 1] = firstArray[firstIterator];
firstIterator++;
secondIterator++;
origArrayIter+= 2;
}
else {
array[origArrayIter] = firstArray[firstIterator];
array[origArrayIter+1] = secondArray[secondIterator];
firstIterator++;
secondIterator++;
origArrayIter+= 2;
}
}*/
cout << endl;
for(int i = 0; i < length; i++) {
cout << "Array coming out: " << array[i] << endl;
}
/*int tempArray[length];
for(int i = 0; i < length; i++) {
tempArray[i] = array[i];
}
return tempArray;*/
}
}
Specifically, the snippet that I'm having trouble with is merging the arrays back together:
for(int i = 0; i < dominantIndex; i++) {
for (int j = 0; j < inferiorIndex; j++) {
if (superiorArray[i] > inferiorArray[j]) {
int tempVal = array[i];
array[i] = inferiorArray[j];
array[(dominantIndex+j)] = tempVal;
}
else if (j == (inferiorIndex-1)){
int tempVal = array[i];
cout << "Array at i is " << array[i] << endl;
cout << "i is " << i << endl;
cout << "This temp value is " << array[i] << endl;
array[i] = superiorArray[i];
cout << "superiorArray[i]: " << superiorArray[i] << endl;
array[((dominantIndex)+j)] = tempVal;
}
else {
continue;
}
}
cout << "Current array " << endl;
for(int i = 0; i < length; i++) {
cout << array[i] << ",";
}
cout << endl;
}
When I use an array of values {5,3,8,2}, I am returned an array {2,5,3,3}. The step before the final array produces an array {2,3,8,5}. For some reason, I just don't know why the 5 and 8 aren't swapping. Any help or guidance would be appreciated, thank you.

Printin a X with *

i wanna print a X with * , i have done the left side of the X but i don't know how to print the other side (flip/mirror) .
if you run this codes it will print just the left side of (X) and now i wanna print the right side of (X) ? so what should i do to complete the (X) using stars(*)? thank you guys.
i was wondering is it possible to do this?(i'm a newbie to programming)
#include <iostream>
// Expected output pattern:
//
// * *
// * *
// * *
// *
// * *
// * *
// * *
using namespace std;
int main() {
cout << "Printing X with star(*)" << endl;
cout << endl;
int i;
int p;
for (i = 1; i <= 10; i++) {
for (int j = 1; j <= 10; j++) {
if (j > i) break;
cout << " ";
cout << "\t";
}
cout << "\t\t\t\t";
for (p = 1; p <= 10; p++) {
cout << "*";
}
cout << endl;
}
for (i = 10; i >= 1; i--) {
for (int j = 1; j <= 10; j++) {
if (j > i) break;
cout << " ";
cout << "\t";
}
cout << "\t\t\t\t";
for (p = 1; p <= 10; p++) {
cout << "*";
}
cout << endl;
}
return 0;
}
You're on the right track, to do the right hand side you have to print more **** on each line in addition to what you already have done. It might help to think of printing each line of the X as printing some **** then some spaces then more **** and reduce the number of spaces each time you get closer to the cross-over point. Does that make sense? This might help get you further (. = space):
*......*
.*....*
..*..*
...**
and so on
This is one of many ways you could get there:
int main()
{
int size = 8;
int spacesBefore;
int spacesBetween = size;
int numStars = 1;
// Top half:
int i, j;
for ( i = 0; i < size/2; i++ ) {
spacesBetween = size - ( 2 * ( i + 1 ) );
spacesBefore = i;
for ( j = 0; j < spacesBefore; j++ ) // before
cout << " ";
for ( j = 0; j < numStars; j++ ) // * left
cout << "*";
for ( j = 0; j < spacesBetween; j++ ) // between
cout << " ";
for ( j = 0; j < numStars; j++ ) // * right
cout << "*";
cout << endl;
}
// bottom half, do the same kind of thing but changing the spacings
// ...
}
ok thank you every one that helped me , i found the answer i wanted after almost 6 hours and here is the answer:
#include <iostream>
using namespace std;
int main() {
cout << "Printing X with stars" << endl;
cout << endl;
int i;
int p;
int k;
int s;
int count = 72;
for (i = 1; i <= 10; i++) {
for (int j = 1; j <= 10; j++) {
if (j > i) break;
cout << " ";
cout << "\t";
}
cout << "\t\t\t\t";
for (p = 1; p <= 10; p++) {
cout << "* ";
}
for (k=1; k<=count; k++){
cout << " ";
}
count-=8;
for (s=1; s<=10; s++){
cout << "* ";
}
cout << endl;
}
count = 0;
for (i = 10; i >= 1; i--) {
for (int j = 1; j <= 10; j++) {
if (j > i) break;
cout << " ";
cout << "\t";
}
cout << "\t\t\t\t";
for (p = 1; p <= 10; p++) {
cout << "* ";
}
for (k=1; k<=count; k++) {
cout << " ";
}
count +=8;
for (s=1; s<=10; s++){
cout << "* ";
}
cout << endl;
if (count == 80) break;
}
return 0;
}