So I have created an array that holds all the positive integers between 1 and a user inputed number,'n'. I have stored all of them in my array vector<int> numbers
. The problem is accessing them. My plan is to loop through each element of the array and check if that specific element is divisible or not. The problem is that I cannot get the program to divide and instead, I am prompted with an error message when I run it. The message says the entire program is not responding. Debugging wise, there are not any errors.
I think there is something wrong with the
for (int i=1; i<numbers.size()-1; i++)
{
for (int b=0; b<=num; b++)
{
if (numbers[i] % b == 0)
cout << b << ", " ;
}
}
but I am not sure what. Here is the whole program.
#include <iostream>
#include <vector>
using namespace std;
int store (int num);
vector<int> numbers;
int main()
{
int num;
cout << "Enter a number: " << endl;
cin >> num;
store(num);
}
int store (int num)
{
for (int a=1; a<=num; a++)
{
numbers.push_back(a);
}
for (int i=1; i<numbers.size()-1; i++)
{
for (int b=0; b<=num; b++)
{
if (numbers[i] % b == 0)
cout << b << ", " ;
}
}
}