jeudi 23 avril 2015

Prime numbers in an dynamic array

I've a program which inserts all prime numbers up to a specific number in an array. The calculation is correct. My problems are the function parameters and the transfer of my dynamic array to the function. My function doesn't modify my array.

Please take a look at the code:

#include <iostream>

using namespace std;

int primeinlesen(int *i);
int primarrayspeicherung (int *primarray,int *bis);

int main()
{
    int reload=1;
    while(reload==1)
    {
        int bis=0,*primarray,valcounter;
        primeinlesen(&bis);
        valcounter=primarrayspeicherung(primarray,&bis);
        for(int i=0;i<valcounter;i++)
        {
            cout<<i<<". Primzahl: "<<primarray[i]<<"\n";
        }
        delete [] primarray;
        cout<<"Anzahl Primzahlen: "<<valcounter<<endl;
        cout<<"Erneute Berechnung?(Ja(1) oder Nein(0))";
        cin>>reload;
    }
    return 0;
}

int primeinlesen(int *i)
{
    cout<<"Bis zu welchem Wert moechten SiePrimzahlen ausgegeben,haben(max.500)";
    cin>>*i;
    if(*i>500)
    {
        cout<<"Wert zu hoch...";

    }
    return 0;
}
int primarrayspeicherung (int *primarray,int *bis)
{
    int x,y,counter,e,valcounter=0,xcounter=0,xvalcounter=0,xx,xy,xe;
    for(x=2;x<*bis;x++)
    {
        counter=0;
        for(y=2;y<x;y++)
        {
            e=x%y;
            if(e==0)
            {
                counter++;
            }
        }
        if(counter==0)
        {
            valcounter++;
        }
    }
    //ZWEITER DURCHGANG
    primarray=new int[valcounter];
    for(xx=2;xx<*bis;xx++)
    {
        xcounter=0;
        for(xy=2;xy<xx;xy++)
        {
            xe=xx%xy;
            if(xe==0)
            {
                xcounter++;
            }
        }
        if(xcounter==0)
        {
            primarray[xvalcounter]=xx;
            xvalcounter++;
        }

    }
    return valcounter;
}

Best regards

Aucun commentaire:

Enregistrer un commentaire