I'm writing a program that returns an array with the indexes of another array as elements when that array's element =1.
So if array[ix]=1 then newarray[newarrcounter]=ix.
However the array that is returned only has 0s as elements.
I'm trying to do this using only pointers and no indexing. Am I using pointers incorrectly here?
int* primearr(int arr[], int size, int& newarrsize){
int* end = arr + size;
int* begin = arr;
while(begin<end){
if(*begin==1)
++newarrsize;
begin++;
}
begin=arr;
int *newarray= new int[newarrsize];
while(begin<end){
if(*begin==1){
*newarray=begin-arr;
newarray++;
}
begin++;
}
return newarray;
}
the rest of the program code...
#include <iostream>
#include <math.h>
using namespace std;
int* arr(int size);
int firstprime(int size, int arr[]);
void crossout(int size, int*arr, int factor);
void findzeros(int arr[], int size);
int* primearr(int arr[], int size,int& newarrsize);
int main()
{
int low, high;
char again='y';
high=low=0;
int firstn;
int newarrsize=0;
cout<<"\tThe Sieve of Eratosthenes"<<endl<<endl;
do{
do{
do{
cout<<"Enter the high boundary: ";
cin>>high;
cout<<endl;
if(high<=0)
cout<<"ERROR: HIGH BOUNDARY MUST BE POSITIVE"<<endl;
}while(high<0);
do{
cout<<"Enter the low boundary: ";
cin>>low;
cout<<endl;
if(low<=0)
cout<<"ERROR: LOW BOUNDARY MUST BE POSITIVE"<<endl;
}while(low<=0);
if(low>high)
cout<<"ERROR: HIGH BOUNDARY MUST BE GREATER THAN LOW BOUNDARY"<<endl;
}while(low>=high);
int* thearr= arr(high);
firstn=firstprime(high,thearr);
do{
crossout(high,thearr,firstn);
firstn=firstprime(high,thearr);
}while(firstn<= sqrt(high));
findzeros(thearr,high);
int* thenewarr= primearr(thearr,high,newarrsize);
cout<<"The prime numbers from "<<low<<" to "<<high<<" are: "<<endl;
int* begin = thenewarr;
int* end = thenewarr+newarrsize;
while(begin<=end){
cout<<*thenewarr<<" ";
++begin;
}
cout<<endl<<endl;
cout<<endl<<endl;
cout<<"Try again with new boundaries? (y/n):"<<endl;
cin>>again;
delete[] thearr;
delete[] thenewarr;
}while(again=='y');
return 0;
}
int* arr(int size){
int* thearray = new int[size]();
thearray[0] = -1;
thearray[1] = -1;
return thearray;
}
int firstprime(int size, int arr[]){
int* end = arr + size;
int* begin = arr;
while(begin<end){
if(*begin==0)
return (begin-arr);
begin++;
}
return -1;
}
void crossout(int size, int*arr, int factor){
int* end = arr + size;
int* begin = arr;
int newfactor=factor+factor;
while(begin<end){
if((begin-arr)==factor)
*begin=1;
else if((begin-arr)==newfactor){
*begin=-1;
newfactor=newfactor+factor;
}
begin++;
}
}
void findzeros(int arr[], int size){
int* end = arr + size;
int* begin = arr;
while(begin<=end){
if(*begin==0)
*begin=1;
begin++;
}
}
int* primearr(int arr[], int size, int& newarrsize){
int* end = arr + size;
int* begin = arr;
while(begin<end){
if(*begin==1)
++newarrsize;
begin++;
}
begin=arr;
int *newarray= new int[newarrsize];
while(begin<end){
if(*begin==1){
*newarray=begin-arr;
newarray++;
}
begin++;
}
return newarray;
}
std::vector. This type of C++ coding has gone out with the dark ages.Cprogrammer teaching C++, and simply doing what they were taught inC.