Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   Please I Need Help In This. Anyone Please Help! (http://www.go4expert.com/forums/i-help-help-t19793/)

yousuf_az 17Oct2009 15:46

Please I Need Help In This. Anyone Please Help!
 
Question = Write a program that calculates the gcd’s (greatest common divisors) of
each of the two consecutive elements of an array, whose length should be defined as a
constant, and prints them. Also write a
function GCD(a, b) that would take the two consecutive elements of the array as
arguments and return the GCD.

I have done it without making a function because the function can be made easily the main problem is i can't seem to get the calculations right. please help me in this program.

Code:

#include<stdio.h>
#include<conio.h>

const arr[10];

void GCD (int a, int b);

int main (void)

{

double arr[10], n, temp, remainder, gcd=0;
clrscr();
    for (n=0;n<10;n++)
    {
    printf("Enter A Number = ");
    scanf("%lf",&arr[n]);
    }
    for (n=0;n<10;n++)
    {
    if (arr[n] == 0 || arr[n+1] == 0)
    {
    printf("Division By 0 Is Not Possible\n");
    }
    if (arr[n] == 1 ||  arr[n+1] == 1)
    {
    printf("The GCD is 1\n");
    }
    if (arr[n+1] > arr[n])
    {
    temp = arr[n];
    arr[n] = arr[n+1];
    arr[n+1] = temp;
    }
    }
    for (n=0;n<10;n++)
    {
    if ((arr[n] / arr[n+1]) > 0)
    {
    remainder = (arr[n] * arr[n+1]);
    gcd++;
    printf("The GCD#%0.0f Is = %0.0f\n",gcd, remainder);
    }
    }
    while (remainder != 0)
    {
    remainder = (arr[n] % arr[n+1]);
    arr[n] = arr[n+1];
    arr[n+1] = remainder;
    printf("%d\n",arr[n]);
    }
    printf("The GCD#%d Is %d\n",n,arr[n]);
    }
    getch();

return(0);

}


xpi0t0s 18Oct2009 16:33

Re: Please I Need Help In This. Anyone Please Help!
 
Well, do you know how to calculate GCD's? If I were to give you two numbers, how would you go about determining the GCD? Would you use prime factorisation, Euclid's algorithm, or some other approach?

How exactly are the calculations going wrong? What input did you give, what output did you expect, and what output did you get?

yousuf_az 18Oct2009 20:53

Re: Please I Need Help In This. Anyone Please Help!
 
Quote:

Originally Posted by xpi0t0s (Post 58977)
Well, do you know how to calculate GCD's? If I were to give you two numbers, how would you go about determining the GCD? Would you use prime factorisation, Euclid's algorithm, or some other approach?

How exactly are the calculations going wrong? What input did you give, what output did you expect, and what output did you get?

well i would use prime factorization and i have searched about Euclid's algorith but i dont want to use a function i want to calculate it the detailed way.

well the storing of 10 numbers in the array are working and mostle the checking of 0 and 1 is working fine but the calculation of gcd doesn't work like if i try something it calculates the gcd of 90 and 10 and gives 10 but when i calculate the gcd of 3 and 4 it gives 8 and vice versa if i try somthing else.

xpi0t0s 18Oct2009 22:46

Re: Please I Need Help In This. Anyone Please Help!
 
"i dont want to use a function i want to calculate it the detailed way"

This doesn't make sense; a function is about how you structure the code, not about how detailed the calculation is. Functions make code easier to read and more reusable; once you've written a gcd() function you can then use it anywhere you need it (for example your next assignment might be about LCM's, and you can then lift the gcd() function out of this assignment very easily).

Functions also make code easier to write, so you can start by writing a gcd() function and just focus on that, creating a function that takes two integers and returns a third, and debug that and get it working. Then you can focus on the main function which creates and populates an array according to the rules, then calls gcd() on each pair.

Anyway you don't have to use a function if you don't want to; just stuff all the code in the main function. But be prepared to lose marks, because the assignment states you must do that.

Regardless of the algorithm you use, and prime factorisation is as good as any, the next step is to work out how you would do it on paper, because if you don't know how to do something then you stand no chance of programming a computer to do it. Try a few examples, and when you've done a few, try to think how a computer might do it. For example if a particular step involves adding one number to another, that implies two variables and an "a+=b;" statement. Prime factorisation means you're going to have to decompose each number into a list of primes so that you can pick out the common ones. Will you have prime number tables or will you calculate primes on the fly? Will you store the decomposed number in memory and if so how - will you store them in arrays; will there be a maximum length or will you use variable length arrays? How will you pick out the common primes?

One suggestion I would make based on attempting to work out what your code is doing (as it's uncommented I have no idea what you're trying to do at any given stage), is that you shouldn't modify the array as you go along, so that you don't confuse yourself. So for example you swap array entries "if (arr[n+1] > arr[n])", which means that next time round the loop you're comparing an element that was two elements away, not with its neighbour. So if your numbers are 10, 20, 30, then first time round you will swap 10 and 20, making the array 20 10 30, then next time round you will compare 30 with 10, not 20. Also you seem to be modifying the array entries (within "while (remainder != 0)") so it's anyone's guess what you'll actually trying to be gcd'ing with 30 on the next time round.

Also please indent your code. It makes it a lot easier to work out what's going on. Yes, YOU know what it does, but I don't. And your teacher won't, either, which is arguably more important.


All times are GMT +5.5. The time now is 02:17.