我正在运行Visual Studio C++,我有一个头文件"GameEngine.h",我试图让另一个文件看到.
当我#include"GameEngine.h"时,它给我一个错误,它无法打开源文件.我不知道该怎么做.我已经完成了这一千次,但由于某种原因,这现在无法正常工作.
我有一个任务,要求我创建一个分配和释放内存的"堆"类.我相信我的代码工作,解决方案构建和运行正常,但我想确保我没有任何内存泄漏.我还需要添加一些代码来检查分配给堆的所需数量是否可用...如果有人要分配非常大的数量.如何检查堆上分配的内存是否可用,或者如果内存不足,是否可以为NULL.到目前为止,这是我的代码:
#include <iostream>
using namespace std;
class Heap{
public:
double* allocateMemory(int memorySize)
{
return new double[memorySize];
};
void deallocateMemory(double* dMemorySize)
{
delete[] dMemorySize;
};
};
int main()
{
Heap heap;
cout << "Enter the number of double elements that you want to allocate: " << endl;
int hMemory;
const int doubleByteSize = 8;
cin >> hMemory;
double *chunkNew = heap.allocateMemory(hMemory);
cout << "The amount of space you took up on the heap is: " <<
hMemory*doubleByteSize << " bytes" << …Run Code Online (Sandbox Code Playgroud) 而不是ELEMENTS是25有一种方法可以随机生成大量的元素...... 10000,100000甚至1000000个元素,然后使用我的插入排序算法.我正在尝试使用大量元素并使用插入排序将它们按顺序排列,然后也按相反的顺序排列.接下来,我使用time.h文件中的clock()来计算每个算法的运行时间.我试图用大量数字进行测试.
#define ELEMENTS 25
void insertion_sort(int x[],int length);
void insertion_sort_reverse(int x[],int length);
int main()
{
clock_t tStart = clock();
int B[ELEMENTS]={4,2,5,6,1,3,17,14,67,45,32,66,88,
78,69,92,93,21,25,23,71,61,59,60,30};
int x;
cout<<"Not Sorted: "<<endl;
for(x=0;x<ELEMENTS;x++)
cout<<B[x]<<endl;
insertion_sort(B,ELEMENTS);
cout <<"Sorted Normal: "<<endl;
for(x=0;x<ELEMENTS;x++)
cout<< B[x] <<endl;
insertion_sort_reverse(B,ELEMENTS);
cout <<"Sorted Reverse: "<<endl;
for(x=0;x<ELEMENTS;x++)
cout<< B[x] <<endl;
double seconds = clock() / double(CLK_TCK);
cout << "This program has been running for " << seconds << " seconds." << endl;
system("pause");
return 0;
}
Run Code Online (Sandbox Code Playgroud) 到目前为止,这是我的代码,我被困住了.就绝对值而言,最接近7的值是5.如何检查数组的每个元素以查看它是否是最接近的元素,然后返回该值.我知道这可能比我想象的要容易,但我是编程的初学者.
#include <iostream>
#include <cmath>
using namespace std;
const int MAX = 25;
int searchArray(int [], int); \\prototype
int main(){
int numArray[MAX] ={1,4,5,10,11,12,13,14,15,16,17,18,19,20,35,26,43,15,48,69,32,45,57,98,100};
searchArray(numArray, 7);
system("pause");
return 0;
}
int searchArray(int a[], int b){
int distance = 0;
for(int i=0;i<MAX;i++){
if(i==b)
return i;
else if(i<b || i > b)
abs(distance) == i-b;
return distance;
}
}
Run Code Online (Sandbox Code Playgroud) 有没有办法创建一个函数,可以将一块内存分配到堆上,调用者可以传递一个他们想要分配的大小并返回一个有效的地址供调用者使用?我知道如何分配特定的大小,但有没有办法让呼叫者通过所需的金额?
我应该编写一个传递两个参数的函数:一维int值数组和一个整数值.该函数查找数组中与第二个参数值最接近的值.我的代码可以工作,但是当我输入数字1-5时我的输出为0.当我输入5以上的数字时,我开始得到准确的结果.我不太清楚为什么会这样,但到目前为止这是我的代码:
#include <iostream>
#include <cmath>
using namespace std;
const int MAX = 5;
int searchNearest(int anArray[],int key)
{
int value = abs(key - anArray[0]);
int num = 0;
for(int x = 0;x < MAX; x++)
{
if(value > abs(key - anArray[x]))
{
value = abs(key - anArray[x]);
num = anArray[x];
}
}
return num;
}
int main()
{
int A[MAX] = {3,7,12,8,10};
int search;
int nearest;
cout << "Enter a number to search: ";
cin >> search;
nearest = searchNearest(A,search); …Run Code Online (Sandbox Code Playgroud)