mardi 31 mars 2020

exited, segmentation fault

The input for this problem is a first number that indicates how many cases will be input to be analyzed.

Input:

3
8 12
9 27
259 111

The first number means that there will be 3 cases. The next 3 lines are the cases. The program has to output the GCD (Greatest Common Divisor) of the 3 cases.

4
9
37

The code I wrote looks like the following:

#include <iostream>
#include <vector>

int gcd(int a, int b) {
   if (b == 0)
      return a;
   return gcd(b, a % b);

}

int main() {

   int N;

   std::cin >> N;

   int i = 0;

   std::vector<int> cards;

   while (i <= N) {
      i++;
      int F1, F2;
      std::cin >> F1 >> F2;
      cards[i] = gcd(F1, F2);
   }

   for (int j; j <= N; i++) {
      i++;
      std::cout << cards[i] << "\n";
   }
}

It reads the first integer (number of test cases), runs the loop once (reads one test case) and stops running. The terminal outputs exited, segmentation fault. What is the problem?

Aucun commentaire:

Enregistrer un commentaire