polar
polar

Reputation: 7

How to find factors of each number in an array

I have an array of numbers input by the user, the program then sorts it in ascending order. I just need to find a way to get the factors of each number in the array and have it be printed out

#include "stdafx.h"
#include <iostream>
#include <limits>
#define MAX 200

using namespace std;
int arr[MAX];
int n, i, j, k;
int temp;


int main()
{
    //array declaration
        int arr[MAX];
    int n, i, j;
    int temp;

    //read total number of elements to read
    cout << "Enter total number of numbers to read: ";
    cin >> n;

    //check bound
    if (n<0 || n>MAX)
    {
        cout << "Input valid range!!!" << endl;
        return -1;
    }

    //read n elements
    for (i = 0; i < n; i++)
    {
        cout << "Enter element [" << i + 1 << "] ";
        cin >> arr[i];
        cout << endl;
    }

    //print input elements
    cout << "Unsorted Array elements:" << endl;
    for (i = 0; i < n; i++)
        cout << arr[i] << "\t";
    cout << endl;

    //sorting - ASCENDING ORDER
    for (i = 0; i<n; i++)
    {
        for (j = i + 1; j < n; j++)
        {
            if (arr[i]>arr[j])
            {
                temp = arr[i];
                arr[i] = arr[j];
                arr[j] = temp;
            }
        }
    }

    //print sorted array elements
    cout << endl;
    cout << "Sorted (Ascending Order) Array elements:" << endl;
    for (i = 0; i < n; i++)
        cout << arr[i] << "\t";
    cout << endl <<endl;

    //trying to find factors
    cout << "Factors of " << arr[i] << " are: " << endl;
    for (k = 1; k <= arr[i]; ++i)
    {
        if (arr[i] % k == 0)
            cout << k << endl;
    }
system ("pause")
return 0;
}

I want it to print each number from the array with "The factors of (number) are ...' "The factors of (next number) are ..."

and so on

Upvotes: 1

Views: 3157

Answers (2)

Hiroki
Hiroki

Reputation: 2880

The final for-loop should be loop with k and you forgot to increment k. You should also write i-loop:

//trying to find factors
for (i = 0; i < n; i++)
{
    cout << "Factors of " << arr[i] << " are: " << endl;
    for (k = 1; k <= arr[i]; ++k)
    {
        if (arr[i] % k == 0)
            cout << k << endl;
    }
}

In addition, as pointed out by @LocTran, the upper bound of outer loop should be n-1. Alternatively, you can easily sort arr using std::sort as follows:

std::sort(arr, arr+n);

Then your code would well work for you:

Live Demo

Upvotes: 2

Loc Tran
Loc Tran

Reputation: 1180

There are some issues with your source code.

1> Sorting problem with for outer loop

    //sorting - ASCENDING ORDER
    for (i = 0; i < (n-1); i++)
    {
        for (j = i + 1; j < n; j++)
        {
            if (arr[i] > arr[j])
            {
                temp = arr[i];
                arr[i] = arr[j];
                arr[j] = temp;
            }
        }
    }

The upper bound of outer loop should be (n-1), not n as following but maybe you're lucky you won't see the problem when n < MAX. In case of n == MAX you will see the problem

    //sorting - ASCENDING ORDER
    //for (i = 0; i < n; i++)
    for (i = 0; i < (n-1); i++)
    {
        for (j = i + 1; j < n; j++)
        {
            if (arr[i] > arr[j])
            {
                temp = arr[i];
                arr[i] = arr[j];
                arr[j] = temp;
            }
        }
    } 

2> The print functionality for entire array, you should add the outer loop for index of your array, and change the i++ by k++ in your loop as well

//trying to find factors
cout << "Factors of " << arr[i] << " are: " << endl;
for (k = 1; k <= arr[i]; ++i)
{
    if (arr[i] % k == 0)
        cout << k << endl;
}

should be replaced by

    //trying to find factors
    for (i = 0; i < n; i++)
    {
        cout << "Factors of " << arr[i] << " are: " << endl;
        //for (k = 1; k <= arr[i]; ++i)
        for (k = 1; k <= arr[i]; ++k)
        {
            if (arr[i] % k == 0)
                cout << k << endl;
        }
    }

Here is my solution based on modified source code

#include <iostream>
#include <limits>
#define MAX 200

using namespace std;
int arr[MAX];
int n, i, j, k;
int temp;


int main()
{
    //array declaration
    int arr[MAX];
    int n, i, j;
    int temp;

    //read total number of elements to read
    cout << "Enter total number of numbers to read: ";
    cin >> n;

    //check bound
    //if (n<0 || n>MAX)
    if (n<0 || n>MAX)
    {
        cout << "Input valid range!!!" << endl;
        return -1;
    }

    //read n elements
    for (i = 0; i < n; i++)
    {
        cout << "Enter element [" << i + 1 << "] ";
        cin >> arr[i];
        cout << endl;
    }

    //print input elements
    cout << "Unsorted Array elements:" << endl;
    for (i = 0; i < n; i++)
        cout << arr[i] << "\t";
    cout << endl;

    //sorting - ASCENDING ORDER
    //for (i = 0; i < n; i++)
    for (i = 0; i < (n-1); i++)
    {
        for (j = i + 1; j < n; j++)
        {
            if (arr[i] > arr[j])
            {
                temp = arr[i];
                arr[i] = arr[j];
                arr[j] = temp;
            }
        }
    }

    //print sorted array elements
    cout << endl;
    cout << "Sorted (Ascending Order) Array elements:" << endl;
    for (i = 0; i < n; i++)
        cout << arr[i] << "\t";
    cout << endl << endl;

    //trying to find factors
    for (i = 0; i < n; i++)
    {
        cout << "Factors of " << arr[i] << " are: " << endl;
        //for (k = 1; k <= arr[i]; ++i)
        for (k = 1; k <= arr[i]; ++k)
        {
            if (arr[i] % k == 0)
                cout << k << endl;
        }
    }
    return 0;
}

Upvotes: 1

Related Questions