我不太确定我的程序出了什么问题。它编译没有错误,但它甚至不会正确运行第一个函数。我试图请一些朋友帮助我解决这个问题,但他们没有任何帮助。
我希望有人至少可以通过向我展示我犯的但没有发现的错误来帮助我。
头文件(sieve.h):
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <vector>
using namespace std;
void findPrimeNumbers(int**, int);
void printPrimes(int**, int);
void findPrimeNumbers(int **arr, int n)
{
for (int x = 2; x < n; x++){
cout << " x is " << x;
if (arr[0][x] == 1){
int z = arr[1][x];
cout << " z = " << z;
for (int y = 2 * z; y < 40; y += z){
cout << " y is " << y;
arr[0][y] = 0;
}
}
}
}
void printPrimes(int **arr, int n)
{
for (int x = 0; x < n; x++){
if (arr[0][x] == 1)
cout << arr[1][x] << " is a prime number" << endl;
}
}
主文件(sieve.cpp):
#include "sieve.h"
using namespace std;
int main()
{
int n=1;
cout << "Please enter the maximum value:" << endl;
cin >> n;
vector<int> sir(n);
cout << "You have selected the maximum size as:" << endl;
cout << n << endl;
//allocate the array
int row = n;
int col = n;
int** arr = new int*[row];
for(int i = 0; i < row; i++)
{
arr[i] = new int[col];
}
findPrimeNumbers(arr, n);
printPrimes(arr, n);
for (int j = 0; j < n; j++)
{
cout << " " << arr[0][j];
}
//deallocate the array
for(int i = 0; i < row; i++)
{
delete[] arr[i];
delete[] arr;
}
return 0;
}
即使是最微小的帮助也将不胜感激!