#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>
Related
I am trying to get the height of these slashes to be a certain length based on input. So far, I have:
#include <iostream>
using namespace std;
int main() {
int n = 0;
cout << "Enter value: ";
cin >> n;
cout << "You entered: " << n << "\n";
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++)
cout << '/' << '/';
cout << "\n";
}
}
I need it to then reverse and go back.
It prints:
//
////
//////
If the user entered 3.
It should print:
//
////
//////
////
//
Can anyone lead me in the right direction? I am new to cpp.
You can use a different kind of loop and add a bool variable to track when the program have reached "n". Then, after the program reaches "n", it sets the bool variable to true and starts to substract until i equals 0
Code below, read comments and ask if you have any further questions:
#include <iostream>
using namespace std;
int main()
{
int n = 0;
cout << "Enter value: ";
cin >> n;
cout << "You have entered: " << n << "\n";
int i = 1;
bool reachedN = false; // tells if [i] has reached [n]
while (i != 0)
{
// Print required slashes
for (int j = 1; j <= i; j++)
{
cout << "//";
}
cout << '\n'; // new line
// Add until i == n, then substract
if (i == n)
{
reachedN = true;
}
if (reachedN)
{
--i;
}
else
{
++i;
}
}
}
If you enter 3, the output is the following:
This is one way to achieve that:
#include <iostream>
using namespace std;
int main() {
int n = 0;
cout << "Enter value: ";
cin >> n;
cout << "You entered: " << n << "\n";
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++)
cout << '/' << '/';
cout << "\n";
}
for (int i = n - 1; i > 0; i--) {
for (int j = 1; j <= i; j++)
cout << '/' << '/';
cout << "\n";
}
}
This is a shorter solution with only two for-loops.
#include <iostream>
using namespace std;
int main()
{
int n = 0;
cout << "Enter value: ";
cin >> n;
cout << "You entered: " << n << "\n";
n = n * 2 - 1;
int r = 0;
for (int j = 0; j < n; j++)
{
if (j > n / 2) r--;
else r++;
for (int i = 0; i < r; i++)
{
cout << '/' << '/';
}
cout << "\n";
}
return 0;
}
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");
}
}
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;
}
I'm programming Djikstra's algorithm in C++ and I'm getting the correct distances from the source node to the end node but i'm having trouble backtracking the previous nodes visited. It's giving me sort of the correct answer but not the correct answer. Also I noticed with different input data of 1 as the start node and 16 as the finish node that my algorithm is using path's that aren't allowed (it goes from 1 -> 10 -> 8 when 8 isn't allowed) but that could just be me getting path backtracking wrong.
http://pastebin.ca/3188762 - Input data (1st = max nodes and then nodes(node num, x,y) then max edges then all edges with the last line being the start and finish node)
http://textuploader.com/awp89 - Output in console
Code:
#include<iostream>
#include<fstream>
using namespace std;
struct Node
{
int nodeNum;
double x, y;
};
void dji(double map[50][50],int startNode,int endNode,int maxNodes);
int main()
{
int tempA, tempB, maxNodes, maxEdges, startNode, endNode;
double tempD;
double map[50][50];
ifstream fin;
fin.open("ass03.txt");
if(fin.good())
{
fin >> maxNodes;
Node allNodes[maxNodes];
for(int i = 0; i < maxNodes; i++)
{
for(int k = 0; k < maxNodes; k++)
{
map[i][k] = -1;
}
map[i][i] = 0;
}
for(int i = 0; i < maxNodes; i++)
{
fin >> allNodes[i].nodeNum >> allNodes[i].x >> allNodes[i].y;
}
fin >> maxEdges;
for(int i = 0; i < maxEdges; i++)
{
fin >> tempA >> tempB >> tempD;
map[tempA-1][tempB-1] = tempD;
map[tempB-1][tempA-1] = tempD;
}
fin >> startNode >> endNode;
cout << "\t";
for(int i = 0; i < maxNodes; i++)
{
cout << i+1 << "\t";
}
cout << endl;
for(int i = 0; i < maxNodes; i++)
{
cout << i+1 << "\t";
for(int k = 0; k < maxNodes; k++)
{
cout << map[i][k] << "\t";
}
cout << endl;
}
dji(map, startNode-1, endNode-1, maxNodes);
}
else
{
cout << "Incorrect filename" << endl;
}
return 0;
}
void dji(double map[50][50], int startNode,int endNode,int maxNodes)
{
int Intersections[maxNodes], path[maxNodes], temp; // equate for actual endNode
double Distances[maxNodes];
for(int a = 0; a < maxNodes; a++)
{
Intersections[a] = a;
Distances[a] = map[startNode][a];
if(map[startNode][a] != -1)
{
path[a] = startNode;
}
else
{
path[a] = -1;
}
}
Intersections[startNode] = -1;
Distances[startNode] = 0;
double minValue = 99999;
int minNode = 0;
for(int l = 0; l < maxNodes; l++)//loop max amount of times to avoid having to function loop (disconsider l = startNode)?
{
for (int i = 0; i < maxNodes; i++)
{
if(Intersections[i] == -1)
{
continue;
}
if(Distances[i] > 0 && Distances[i] < minValue)
{
minValue = Distances[i];
minNode = i;
}
}
if(Intersections[minNode] == endNode)
{
temp = l;
}
Intersections[minNode] = -1;
cout << " --- Used Node - " << minNode+1 << endl;
for(int i = 0; i < maxNodes; i++)
{
cout << Intersections[i] << " ";
}
cout << endl;
for(int i = 0; i < maxNodes; i++)
{
if(map[minNode][i] < 0)
{
continue;
}
if(Distances[i] < 0)
{
Distances[i] = minValue + map[minNode][i];
path[i] = minNode;
continue;
}
if((Distances[minNode] + map[minNode][i]) < Distances[i])
{
Distances[i] = minValue + map[minNode][i];
path[i] = minNode;
}
}
minValue = 99999;
}
for(int i = 0; i < maxNodes; i++)
{
cout << "Node:" << i+1 << " - PATH= " << path[i] << " distance = " << Distances[i] << endl;
}
cout << "Additional nodes used: " << temp << endl;
temp = path[endNode];
for(int i = 0; i < 4; i++)
{
cout << temp << " ";
temp = path[temp];
}
/*temp = path[endNode];
int temp2 = path[endNode];
for(int i = 0; i < maxNodes; i++)
{
if(i == 0)
{
cout << endNode << " ";
cout << temp << " ";
}
if(i%2 == 0)
{
if(temp != endNode)
{
temp = path[temp2];
cout << temp << " ";
}
else
{
cout << temp << " ";
i = maxNodes;
}
}
else
{
if(temp2 != endNode)
{
temp2 = path[temp]-1;
cout << temp2 << " ";
}
else
{
cout << temp2 << " ";
i = maxNodes;
}
}
}*/
//cout << "PATH = " << endNode << " < - " << path[endNode] << " < - " << path[path[endNode]-1] << " < - " << path[path[path[endNode]-1]-1] << endl;
//cout << "TEST" << path[4] << " " << path[8] << " " << path[16] << " " << endl;
}
Thank you for any help
The problem is you're mixing zero-based and one-based indexing. Your vertices are numbered 1-20, and those are the numbers that end up in your path array with valid indices 0-19. You then use the vertex number as the index into the array.
Change your code to either consistently use the vertex number, or consistently use the array index.
I have searched many examples and have yet to be able to find where exactly my problem lies. I am trying to implement the merge sort algorithm from the Cormen intro to algorithms book-- here is where I am at so far-- I have tried throwing in print statements to follow how the arrays are getting rebuilt but I am not seeing it... can anyone help?
Code:
#include <iostream>
#include <cmath>
#include <ctime>
#include <cstdlib>
using namespace std;
int p = 0;
int q = 0;
int r = 0;
int getRandom()
{
int randNum = 0;
randNum = 1 + rand() % 100;
return randNum;
}
void populateArray(int * array, int size)
{
for (int i = 0; i < size; i++) {
array[i]=getRandom();
}
}
void merge (int * array, int p, int q, int r) // p = start, q = mid, r = end
{
int i = 0; // left array iterator
int j = 0; // right array iterator
int n1 = q - p + 1;
int n2 = r - q;
// int arrayL[n1 + 1];
//int arrayR[n2 + 1];
int arrayL[n1];
int arrayR[n2];
for (i = 0; i < n1; i++) {
arrayL[i] = array[p + i];
}
cout << "TEST ARRAY MS A: ";
for (int count = 0; count < n1; count++) {
cout << arrayL[count] << " ";
}
cout << endl << endl;
for (j = 0; j < n2; j++) {
arrayR[j] = array[q + j + 1];
}
cout << "TEST ARRAY MS B: ";
for (int count = 0; count < n2; count++) {
cout << arrayR[count] << " ";
}
cout << endl << endl;
//arrayL[n1 + 1] = 1000;
//arrayR[n2 + 1] = 1000;
//i = 0;
//j = 0;
for (int k = p, i = j = 0; k <= r; k++) {
if (j >= n2 || (i <= n1 && arrayL[i] <= arrayR[j])) {
array[k] = arrayL[i];
i++;
cout << "TEST ARRAY in loop A: ";
for (int tempIt = 0; tempIt < r; tempIt++) {
cout << array[tempIt] << " ";
}
cout << endl << endl;
}
else {
array[k] = arrayR[j];
j++;
cout << "TEST ARRAY in loop B: ";
for (int tempIt = 0; tempIt < r; tempIt++) {
cout << array[tempIt] << " ";
}
cout << endl << endl;
}
cout << "TEST ARRAY in loop: ";
for (int tempIt = 0; tempIt < r; tempIt++) {
cout << array[tempIt] << " ";
}
cout << endl << endl;
}
}
void mergeSort (int * array, int p, int r)
{
if (p < r) {
q = floor((p + r) / 2);
mergeSort(array, p, q);
mergeSort(array, q + 1, r);
merge(array, p, q, r);
}
}
int main(int argc, const char * argv[])
{
unsigned seed = time(0);
srand(seed);
int testArray[5];
populateArray(testArray, 5);
cout << "TEST ARRAY: ";
for (int count = 0; count < 5; count++) {
cout << testArray[count] << " ";
}
cout << endl << endl;
mergeSort(testArray, 0, 4);
cout << "TEST ARRAY after mergeSort: ";
for (int count = 0; count < 5; count++) {
cout << testArray[count] << " ";
}
cout << endl << endl;
return 0;
}