Cristian Ionescu
Cristian Ionescu

Reputation: 1

An array where every element is a divisor of a number and the divisor of the next one

Before anything, I am a newbie in programming. I am actually a high school student, so therefore my question may sound stupid. I want to create an array where every element is a divisor of a number and the divisor of the next one: e.g. for n=12

1 2 6 12

My solution:

#include <iostream>

using namespace std;

int main()
{
    int n, d, i, j, a[100];
    cin>>n;
    for(d=n; d>=1; d--)
    {
        if(n%d==0)
            j++;
    }

    for(i=1; i<=j; i++)
    {
        for(d=n; d>=1; d--)
        {
            if(n%d==0)
                a[i]=n%d;
        }
    }
    for(i=1; i<=j; i++)
    {
        cout<<a[1];
        if(a[i]%a[i+1]==0)
            cout<<a[i+1]<<" ";
    }
    return 0;
}

It gives me the following error:-1073741676 (0xC0000094)

A solution to this problem would be much appreciated. I am new to this community and I have really become passionate about programming since I entered high school. Thank you :)

Upvotes: 0

Views: 73

Answers (1)

bruno
bruno

Reputation: 32586

j is used including to compute an index in a, but it is never initialized in your main

In

if(a[i]%a[i+1]==0)

You initialize a doing if(n%d==0) a[i]=n%d; so a[i+1] is always 0 so the modulo is always invalid.

Furthermore, because you initialize only a up to the index j you access to an uninitialized value doing a[i+1] when i is j


A possibility is :

#include <iostream>
#include <vector>
using namespace std;

int main()
{
  int n;

  if (!(cin >> n))
    return -1;

  if (n == 0) {
    cout << "there are an infinite number of solution for 0" << endl;
    return -1;
  }

  vector<int> v;

  // 1 is a first value
  v.push_back(1);

  int max = (n > 0) ? n : -n;

  for (int d = 2; d <= max; ++d) {
    if ((n % d) == 0) {
      // can be a candidate, check if compatible with last value
      if ((d % v.back()) == 0)
        v.push_back(d);
    }
  }

  for (vector<int>::const_iterator it = v.begin(); it != v.end(); ++it)
    cout << *it << ' ';
  cout << endl;

  return 0;
}

We are in C++, so I use a vector rather than a C array with a max size

I check a number was read, and I bypass the case of 0, I also manages the case where the input number is negative to stop the search, and I decide to use positive values.

Compilation and execution :

pi@raspberrypi:/tmp $ g++ -pedantic -Wextra v.cc
pi@raspberrypi:/tmp $ ./a.out
12
1 2 4 12 
pi@raspberrypi:/tmp $ 

Note that if I search from the number rather than from 1 that gives other values :

#include <iostream>
#include <list>
using namespace std;

int main()
{
  int n;

  if (!(cin >> n))
    return -1;

  if (n == 0) {
    cout << "there are an infinite number of solution for 0" << endl;
    return -1;
  }

  list<int> v;

  // the number itself is a first value
  v.push_back(n);

  for (int d = ((n > 0) ? n : -n) - 1; d != 0; --d) {
    if ((n % d) == 0) {
      // can be a candidate, check if compatible with first value
      if ((v.front() % d) == 0)
        v.push_front(d);
    }
  }

  for (list<int>::const_iterator it = v.begin(); it != v.end(); ++it)
    cout << *it << ' ';
  cout << endl;

  return 0;
}

That time I use a list to push_front, it was possible to still use a vector putting the value in the reverse order compared to the first version

Compilation and execution :

pi@raspberrypi:/tmp $ g++ -pedantic -Wextra vv.cc
pi@raspberrypi:/tmp $ ./a.out
12
1 3 6 12 

Upvotes: 1

Related Questions