I have problem with my homework in C++
There is number n and numbers a1, a2, ... , an ( n<100 ) And I need to get thous number pairs, that are mutual prime.
Also I need to use function, that calculates two number mutual primes. :S

Recommended Answers

All 6 Replies

Yes I mean coprimes! thx for links ;) Well program must be like if I enter 4 ; 9 ; 3 ; 16 it will print 4 pairs like 4 and 9 ; 4 and 3 ; 9 and 16 ; 3 and 16

#include <iostream>
using namespace std;
int main ()
{ 
      int coprime( int a, int b);
      int c[100]; int n; int d;
      int there, i, a, b, y;
      do
    {
        if (a<=b) {}  // if a>b change a and b places, so a would be <=b
        else { there=a; a=b; b=there; }
        if (a==b) { return 0; } // if equal, then aren't coprime
        else { for (i=2; i<a; i++)  // if there is number from 2 till a, which % = 0, then aren't primes
                 {
                    if ( (a%i==0) && (b%i==0) ) return 0;
                 }
             return  1;
             }
      cout << "enter numbers " << endl;
      cin >> n;
      for (int i=0; i<n; i++)
          { cin >> c[i]; }
      for (int i=0; i<n; i++)
          {
               for ( int j=i; j<n; j++)
               { d=coprime (c[i], c[j]);
                 if (d) { cout << c[i] << ' ' << c[j] << endl; }
               }
          }
          cout << "to do again press 1, lto finish - other key" << endl;
       cin >> y;
    } while (y==1);
    system ("pause"); 
      return 0;
}

Pls, help - don't know why it isn't working :S

Neither do we. You didn't tell us why you think it's wrong. Did you miss the post titled Read Me: Read This Before Posting?

Well step 1 is learn how to indent code so that you can see more clearly what is going on.

#include <iostream>
using namespace std;
int main()
{
    int coprime(int a, int b);
    int c[100];
    int n;
    int d;
    int there, i, a, b, y;

    do {
        if (a <= b) {
        }                       // if a>b change a and b places, so a would be <=b
        else {
            there = a;
            a = b;
            b = there;
        }

        if (a == b) {
            return 0;
        }                       // if equal, then aren't coprime
        else {
            for (i = 2; i < a; i++) // if there is number from 2 till a, which % = 0, then aren't primes
            {
                if ((a % i == 0) && (b % i == 0))
                    return 0;
            }
            return 1;
        }

        cout << "enter numbers " << endl;
        cin >> n;
        for (int i = 0; i < n; i++) {
            cin >> c[i];
        }

        for (int i = 0; i < n; i++) {
            for (int j = i; j < n; j++) {
                d = coprime(c[i], c[j]);
                if (d) {
                    cout << c[i] << ' ' << c[j] << endl;
                }
            }
        }
        cout << "to do again press 1, lto finish - other key" << endl;
        cin >> y;
    } while (y == 1);

    system("pause");
    return 0;
}

Look at lines 20 to 30.
See how many return statements you have? How is it ever going to get to line 31?

Step 2 is compile with as many warnings as possible, like

$ g++ -W -Wall -ansi -pedantic -O2 bar.cpp
bar.cpp: In function `int main()':
bar.cpp:9: warning: 'a' might be used uninitialized in this function
bar.cpp:9: warning: 'b' might be used uninitialized in this function

The first thing you do with the junk values in a and b is compare them. This is not good.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.