Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Be sure to test your function for possible inputs. Implement the following funct

ID: 3666208 • Letter: B

Question

Be sure to test your function for possible inputs. Implement the following function* in the 'Primes cpp' file The function prototypes are provided in the 'PrlrrmJ*" header file. The first function "pomeFactors' compute* all prime factors of the .nput number and returns them in an array of Integers For example, pr ixeFactors (6 ) will return the array |}.?.l) and pr ireFactor a (12) will return the array |3.2,2,1) Note that the prime factors appear in decreasing order m the output arrays We include 1 as the last element to allow us to determine the end of the array (1 is not actually part of the pnme factorization Since 1 is not a prime number .) Notice also that the facton:ation of 12 conta-ns 2 twice since 12 = 3*2*2. The second function will return an array of nteger j-rjys (mf) The arrjy consists of the prime factomatiomof all numbers (tn increasing order) from 2 to the input argument n. Each pnme factorisation will be the output from a call to prime Factors". For examplewill assign to pomes an array of sue 5 where You can always make helper functions These functions should exist solely in the "Primes.cpp" file.

Explanation / Answer

main.cpp

// header file
#include <iostream>

#include "Primes.h"


using namespace std;

// use this main function to test your functions
int main()
{
   // declare variables
    int j = 0;
  
   // call function
    int *primes = primeFactors(62);
    cout << primes[0] << " " << primes[1] << " " << primes[1] << std::endl;

    int num = 12;
    int **arrays = allPrimeFactors(num);
  
   // checking the condition using for loop
    for (int i =0;i<num-2;i++)
   {
        int j = 0;
       // display output
        std::cout << "primes[" << i << "] is the array [";
        do
       {
            std::cout << arrays[i][j] << " ";
        } while (arrays[i][j++] != 1);
        std::cout << "] ";
    }

   //call function and passing arguments
    std::string s = displayPrimeFactors(allPrimeFactors(11), 10);
    std::cout << s;
    return 0;
}

Primes.cpp


#include "Primes.h"

//Helper Function
void addToArray(int* &arr, int &arr_size, int element);

/*
    Purpose: Computes all prime factors of the input number
    Input: n: input number
    Output: int*: array of prime factors of the input number
*/
int* primeFactors(int n) {
    int arrLength = 0;
    int *primeNums = 0;

    //Loop through looking for prime factors
    for (int i=2; i<=n;i++)
        //Divide by a prime factor as many times as possible
        while (n % i == 0) {
            //Add prime factor to the array
            addToArray(primeNums, arrLength, i);
            n /= i;
        }

    //Add prime factor to the array
    addToArray(primeNums, arrLength, 1);

    int i, j, sortBuffer;

    //Sort the array of data high to low
    for (i=0;i<(arrLength-1);i++)
        for(j=(i+1);j<arrLength; j++)
            if (primeNums[i] < primeNums[j]) {
                sortBuffer = primeNums[i];
                primeNums[i] = primeNums[j];
                primeNums[j] = sortBuffer;
            }

    return primeNums;
}

/*
    Purpose: Create an array of integer arrays of prime factors
    Input: n: input number
    Output: int**: return the array of arrays
*/
int** allPrimeFactors(int n) {
    //Allocate memory to the array containing arrays
    int **arrays = new int*[n-1];
    for (int i=2; i<=n;i++)
        //Point to each array
        arrays[i-2] = primeFactors(i);

    return arrays;
}

/*
    Purpose: Add an element to the array by expanding the array and
             copying the last array to the new one
    Input: int* &arr: Reference the array to be able to manipulate it
                      inside this function
           int &arr_size: Reference the size of the array to be able to
                          change the value
           int element: Element to add to the array
    Output: None
*/
void addToArray(int* &arr, int &arr_size, int element) {
    //Allocate memory to the new array
    int *primeTemp = new int[++arr_size];
    //Add the element to the end of the array
    primeTemp[arr_size-1] = element;
    //Check for valid pointers and if to copy the old array to the new
    if (arr_size > 1 && arr && primeTemp)
       for(int i=0; i<arr_size-1;i++)
           primeTemp[i] = arr[i];

    //Deallocate the memory from the old array
    delete[] arr;
    //Point the array to the new
    arr = primeTemp;
}

/*
    Purpose: Displays each array of prime factors in a formatted fashion
    Input: int** factorization: Array of integer arrays of prime factors
           int length: length of the array
    Output: std::string: return a formatted string
*/
std::string displayPrimeFactors(int** factorization, int length) {
    //Use string stream for formatting
    std::stringstream ss;

    for (int i =0;i<length;i++) {
        int j = 0;
        //Set the width to 2 and fill single digit numbers with a 0 infront
        //Add element to the string stream
        ss << std::setw(2) << std::setfill('0') << (i+2) << " = ";
        //Determine number of elements in the array
        while (factorization[i][++j] != 1);
        //Add each element to the string stream with an 'x' in between
        for (int k=j-1; k>=0;k--) {
            if (!(k == (j-1)))
                ss << "x";
            ss << factorization[i][k];
        }
        //Add a newline character to the end of each array
        ss << " ";
        //Deallocate memory in each array
        delete[] factorization[i];
    }
    //Deallocate memory to clean up
    delete[] factorization;
    //Return the formatted string
    return ss.str();
}

Primes.h

#ifndef PRIMES_H_INCLUDED
#define PRIMES_H_INCLUDED

#include <iostream>
#include <iomanip>
#include <ios>
#include <sstream>
#include <string>

// Problem 2
int*   primeFactors(int n);
int** allPrimeFactors(int n);


// Problem 3
std::string displayPrimeFactors(int** factorization, int length);


#endif // PRIMES_H_INCLUDED

Sample output

                                                                                                                                                
31 2 2                                                                                                                                                      
primes[0] is the array [2 1 ]                                                                                                                               
primes[1] is the array [3 1 ]                                                                                                                               
primes[2] is the array [2 2 1 ]                                                                                                                             
primes[3] is the array [5 1 ]                                                                                                                               
primes[4] is the array [3 2 1 ]                                                                                                                             
primes[5] is the array [7 1 ]                                                                                                                               
primes[6] is the array [2 2 2 1 ]                                                                                                                           
primes[7] is the array [3 3 1 ]                                                                                                                             
primes[8] is the array [5 2 1 ]                                                                                                                             
primes[9] is the array [11 1 ]                                                                                                                              
02 = 2                                                                                                                                                      
03 = 3                                                                                                                                                      
04 = 2x2                                                                                                                                                    
05 = 5                                                                                                                                                      
06 = 2x3                                                                                                                                                    
07 = 7                                                                                                                                                      
08 = 2x2x2                                                                                                                                                  
09 = 3x3                                                                                                                                                    
10 = 2x5                                                                                                                                                    
11 = 11                          
                                                                                                                                                     

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote