Using 2D arrays to solve Sieve of Eratosthenes with C++ -


i'm not sure what's going wrong program here. compiles without error won't run first function correctly. have tried ask friends me out of no help.

i hoping me @ least showing me error have made have not caught.

header file (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) {     (int x = 2; x < n; x++){        cout << " x " << x;         if (arr[0][x] == 1){         int z = arr[1][x];         cout << " z = " << z;         (int y = 2 * z; y < 40; y += z){                 cout << " y " << y;                 arr[0][y] = 0;             }         }     } }  void printprimes(int **arr, int n) {     (int x = 0; x < n; x++){         if (arr[0][x] == 1)              cout << arr[1][x] << " prime number" << endl;     } } 

main file (sieve.cpp):

#include "sieve.h"  using namespace std;  int main() {     int n=1;     cout << "please enter maximum value:" << endl;     cin >> n;     vector<int> sir(n);     cout << "you have selected maximum size as:" << endl;     cout << n << endl;     //allocate array     int row = n;     int col = n;     int** arr = new int*[row];     for(int = 0; < row; i++)     {         arr[i] = new int[col];     }     findprimenumbers(arr, n);     printprimes(arr, n);     (int j = 0; j < n; j++)     {         cout << " " << arr[0][j];     }     //deallocate array     for(int = 0; < row; i++)     {         delete[] arr[i];         delete[] arr;     }     return 0; } 

even tiniest amount of appreciated!

you have 1 problem here:

//deallocate array for(int = 0; < row; i++) {delete[] arr[i]; delete[] arr;} 

you should not deleting arr in loop. formatting code have alerted mistake - should be:

//deallocate array for(int = 0; < row; i++) {     delete[] arr[i]; } delete[] arr; 

ideally though should not using raw arrays - use std::vector instead.


Comments

Popular posts from this blog

c# - How Configure Devart dotConnect for SQLite Code First? -

c++ - Clear the memory after returning a vector in a function -

erlang - Saving a digraph to mnesia is hindered because of its side-effects -