1

I'm not sure how to title my question, but here goes. I am testing some features, and I have hit a snag.

What I want to know is how can I set up a "for" or "if" statement to only put values in an array that meet a criteria? For example, find every divisor for a number, but only put factors in an array.

Any help would be loved, source code can be provided if needed :). Yes, I am new, so be gentle!

#include <iostream>
using namespace std;

int main(){
    int n;
    int counter = 1;
    cout << "What number would you like to use? ";
    cin >> n;
    int DiviArray[n];
    for (int k=0,j=1;k<n;k++,j++)
    {
        DiviArray[k] = n-k;
    }
    int k = 3;
    int factn[n];
    cout << "Factors of " << n << ": " << endl;

    for (int i=0, j=1;i<n;i++,j++)
    {
        factn[i] = n/DiviArray[i];
        if(factn[i]*DiviArray[i]==n)
        {
            cout << counter << ". " << factn[i] << " x " << DiviArray[i] << endl;
            counter++;
        }
    }
    return 0;
}

EDIT: Decided to go with vectors, not sure if I can get it to work, but thanks for the feedback guys :)

7
  • Yes your source code or your ideas about how to do it. Commented Nov 5, 2011 at 17:38
  • @Als pastebin.com/AHjVLyWF I am unsure how to do it. I can get it to print all the values that match, but every other value gets put into the array as well :/ Commented Nov 5, 2011 at 17:39
  • I've tried adding other parameters to the if statement, but it won't have it :/ Commented Nov 5, 2011 at 17:41
  • Well in that, it takes in 2 numbers, n and k, and finds k-possibilities for n. At the moment I just want it to store every factor in an array, to no avail :/. Commented Nov 5, 2011 at 17:43
  • I'm thinking of having 2 arrays, one that stores everything, and would be deleted after use, and one that has factors passed to it. Would that work or is there something easier? Commented Nov 5, 2011 at 17:46

2 Answers 2

1

Since you don't know in advance how many values will meet the condition, you should use a std::vector.

As a benefit, it keeps track of how many elements you've already added, so push_back will always use the next available index.

This also fixes

cin >> n;
int DiviArray[n];

which isn't legal C++.

Sign up to request clarification or add additional context in comments.

2 Comments

(Deleted comment: "How would I implement vectors then?") With dynamic allocation, and copying contents whenever the vector must grow.
std::vector is provided by the standard library (hence the name). You do not need to "implement" it. You need to use it. It is used very much like an array. The .push_back() method appends to the end, after first ensuring that the storage is big enough. It does all of that for you automatically.
0

If you only want to put the values into the array that match the condition, then you should only put a number into the array when the condition is matched. To do that, the statement that puts a number into the array has to be inside the if-block for the condition. I hope I don't need to explain why :)

This is the only time in your program where you actually do want two indices: one that is incremented every time through the loop (to count how many times to run the process), and one that is incremented only when you put a number in the array (to figure out where the next number goes). Everywhere else, you've created a completely useless j variable (the uselessness should be apparent from the fact that there is no code that actually uses the value, only code to set it).

2 Comments

Yes, the j used to be a line number, but counter replaced it and I forgot to remove it. What you're saying makes sense, but it doesn't really answer what I'm asking. I want the factn array to only have the factors, not numbers that don't multiply back.
It answers the question just fine. Your question is "how do I make sure only the factors go into the array?" The answer is "take the code that puts numbers into the array, and put it inside the 'do this part only if the number is a factor' block".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.