Search code examples
c++arraysfunctiondynamic-arrays

Dynamic Array and function


I am trying to do my project and I am stuck. My professor wants me to use a dynamic array if I understood, and a function to compare integer numbers and get their GCD. I cant make the function work. Any thoughts? Here is the prom:

Write a a program to calculate the greatest common divisor of any finite set of integers. Use a function to calculate the GCD. The number of elements in the set should not be predetermined.You need to write the code that will count, as you enter the data,how many numbers are in the set. Base in the Euclid's algorithm or so.

I input 10, 100 and 40 and GCD should be 10;however,I get this results:

The GCD of:  is: 
   10         0    
  100         0
   40         0

#include <iostream>
#include<iomanip>

using namespace std;

int greatestdivisor(int b[], int size); /*Write prototype for gcd */

int main()
{
    int greatest;
    int max=1;
    int* a= new int[max]; //allocated on heap
    int n=0;

    cout<<"Input numbers: "<<endl;
    cout<<"Hit Enter key after each input and type any letter to finish"<<endl;

    while(cin>>a[n]){     //read into array
        n++;
        if(n>=max){
            max=n;   //increase size of array
            int* temp = new int[max];    //creates new bigger array

            for(int i=0;i<n;i++){
                temp[i] = a[i];   //copy values to new array
            } //end for
            delete [] a;     //free old array memory
            a = temp;     //a points to new array
        } //end if

    } // end while
    cout<<endl;
    greatest = greatestdivisor(a, max);
    cout<<"The GCD of: "<<" is: "<<endl;
    for(int j=0;j<max;j++)
        cout<<setw(5)<<a[j]<<setw(10)<<greatest<<endl;
    n++;// prints elements of array and call function
} // end main

// gcd finds greatest common divisor of array
int greatestdivisor(int b[], int size)
{
    int greatest =1;// current greatest common divisor, 1 is minimum

    for (int x=0; x<=size; x++) {
        int m=b[x];
        int r=2;
        if(m%r==0){
            greatest =m; // update greatest common divisor
        } //end if
    } // end for
    return greatest; //return gcd
} // end fuction gcd

Solution

  • There are many issues in your code, try this and figure out what you are doing wrong:

    #include <iostream>
    #include<iomanip>
    
    using namespace std;
    
    int greatestdivisor(int b[], int size); /*Write prototype for gcd */
    
    int main()
    {
        int greatest;
        int max=1;
        int* a= new int[max]; //allocated on heap
        int n=0;
    
        cout<<"Input numbers: "<<endl;
        cout<<"Hit Enter key after each input and type any letter to finish"<<endl;
    
        while(cin>>a[n]){     //read into array
            n++;
            if(n>=max){
                max=n+1;   //increase size of array
                int* temp = new int[max];    //creates new bigger array
    
                for(int i=0;i<n;i++){
                    temp[i] = a[i];   //copy values to new array
                } //end for
                delete [] a;     //free old array memory
                a = temp;     //a points to new array
            } //end if
    
        } // end while
        cout<<endl;
        greatest = greatestdivisor(a, n);
        cout<<"The GCD of: "<<" is: "<<endl;
        for(int j=0;j<n;j++)
            cout<<setw(5)<<a[j]<<setw(10)<<greatest<<endl;
    } // end main
    
    
    int gcd(int a,int b)
    {
        int t;
        while(a)
        {
            t = a;
            a = b%a;
            b = t;
        }
        return b;
    }
    
    // gcd finds greatest common divisor of array
    int greatestdivisor(int b[], int size)
    {
        int greatest =b[0];// current greatest common divisor, 1 is minimum
    
        for (int x=1; x<size; x++) {
            greatest = gcd(greatest, b[x]); // update greatest common divisor
        } // end for
        return greatest; //return gcd
    } // end fuction gcd