I am new to C++. i want to write a program that finds factors of a no. and puts them in array and prints it. i have written a program but it is wrong. PLease correct it.Thank you.

#include<iostream>
using namespace std;
int main()
{
int b,c,i;
int myarray= [10];
cout<<"Please enter  nos"<<endl;
cin>>b;
for(c=2;c<b;c++)
{
if (b%c==0)
{
for(i=0;i<10,i++)
{
myarray[i]=c;
}
}
}
cout<<"The factors of the no. are"<<myarray[i]<<endl;
return 0;
}

Edited 6 Years Ago by Nick Evan: Added [CODE] .. [/CODE] tags

A few things you should try to do is make it so that the array is sorted and also try to use a dynamic array for if there are more factors than your static array can hold.

#include <iostream>

using namespace std;

int main()
{
    int num, index = 0;
    int numArray[25];
    cout << "enter a number: ";
    cin >> num;
    if( num < 1 )
    {
        cout << "Number must be greater than or equal to 1" << endl;
        system("PAUSE");
        return 0;
    }
    for( int i = 1; i < num; i++ )
    {
        if( num % i == 0 )
        {
            numArray[index] = i;
            index++;
            if( i != num )
            {
                numArray[index] = num/i;
                index++;
            }
        }
        if( i*i > num )
            break;
    }
    for( int i = 0; i < index; i++ )
        cout << numArray[i] << endl;
    system("PAUSE");
    return 0;
}

Ok I will make some very general comments about the structure of code:

First off: Always program to the smallest unit of work that you can do. i.e. don't try to factor your number / add to an array and print in one go. Start with getting one part of that correct. That could be the factorization, it could just as well be putting some numbers in an array, it can equally be printing an array.

Let me illustrate :

// TEST Print:
int main()
{
   int NArray[]={1,2,4,5,6};
   // Print part here:
}

or to test the placing numbers in an array

int main()
{
  int* A=new int[10];
  for(int i=0;i<10;i++)
    {
       int number=i*3+4;   // just make a new number anyway.
       // Code to fill A with the number here...
       //...
    }
  delete [] A;
}

So Jack. you are going to need to look up basics about how to allocate arrays, how to write loops [Which you seem to have go right], and please please space your code out with an indent for each level of a loop as sfuo has. That will really help you see the logic. After your have written some code , get it compiled. The smaller the quantity of code you write the easier it is to fix. If the hole code has been compiled, the with pen and paper, figure out what you expect your code to do. Then run it and put an output statement e.g. std::cout<<"Variable i == "<<i<<std::endl; as soon as something changes. That way you have an idea of what is going on.

<b>Algorithm</b>

All factors of a number: In the instance of small numbers, it is perfectly acceptable to loop from 1 to N and all numbers that have no remainder are an acceptable factor. However, please consider that if you find a factor, say X. Then
N/X is also a factor. e.g. if N==20 and X=2 then 20/2 which is 10 is a factor.

So you can improve the speed at which you factor by dividing out the N by the larger factor which sfou decided to do, partially, because then you can also set N to be N/factor, and restart your loop from factor.

However, the code given has an error, let us consider N=16. What happens. [This is why pen+paper REALLY help]

On line 23 there is a test (i!=num) but that can ever be false since the loop condition on line 17 does not let i get to num.

Now when i==4. num/i == 4 and TWO copies of 4 are added to the loop [Not good].

Finally, the if on line 29 should be moved into the loop condition.

p.s. Please please don't use system("PAUSE"). If you want to know why just look it up...[multiple threads here, and the internet] if not just don't use it!!!

Yeah sorry I meant to put

if( i != num/i )

to get rid of doubles.

Also sorry for using system("PAUSE"); I know this is bad because it only works on windows but since I only program on windows I forget and on bigger projects I never use a pause like this instead I use loops.

Edited 6 Years Ago by sfuo: typo

Jack 1 I think this is what you wanted

#include<iostream>
using namespace std;
int main()
{
	int b,c,i;
	int myarray[10];
	cout<<"Please enter  nos"<<endl;
	cin>>b;
	for(c=2, i=0;c<b;c++)
	{
		if (b%c==0)
		{
			myarray[i]=c;
			++i;
		}
	}

	cout<<"The factors of the no. are ";
	for(int j=0;j<i;j++)
	{
		cout<<myarray[j] << " ";
	}
	cout <<endl;
	return 0;
}
This article has been dead for over six months. Start a new discussion instead.