Finding GCD using arrays in C - c

Hi I am trying to write a code in C to find the GCD for more than 2 numbers. However, when I compile and run the code, the GCD is always 0. Would appreciate if anyone can help. Thank you.
#include <stdio.h>
static int gcd(int x, int y)
{
int r;
if (x <= 0 || y <= 0)
return 0;
while ((r = x % y) != 0)
{
x = y;
y = r;
}
return y;
}
int main (void)
{
int A[5];
int g = A[0];
int i;
int n;
printf ("How many elements are there? \n")
scanf ("%d", &n);
printf ("Input the elements. \n");
for (i = 0; i < n; i++)
{
scanf ("%d", &A[i]);
}
for (i = 1; i < n; i++)
g = gcd(g, A[i]);
printf ("GCD is: %d \n");
return 0;
}

You set g equal to A[0] before you set A[0] to any particular value.

You need a ; after the first printf.
You need to declare A with n elements after you read the number of elements n.
You have to write the g after ", in your last printf.
This is how I think your main should look like:
int main (void)
{
int i;
int n;
printf ("How many elements are there? \n");
scanf ("%d", &n);
int A[n];
printf ("Input the elements. \n");
for (i = 0; i < n; i++)
{
scanf ("%d", &A[i]);
}
int g = A[0];
for (i = 1; i < n; i++)
g = gcd(g, A[i]);
printf ("GCD is: %d", g);
return 0;
}

Related

My code is not working. Write a C program to find GCD and LCM of ā€˜nā€™ numbers, with n being the input from user

I thought of declaring the variables first, and then finding the gcd and lcm. But when I tried to run, my code is not working. And the VS code is not even showing the errors. I am posting my code here:
#include <stdio.h>
int gcd(int a, int b)
{
for (int j = 1; j <= a && j <= b; ++j)
{
if (a % j == 0 && b % j==0)
return j;
}
}
int main ()
{
int i, n, pro=1, g, t, lcm;
int num[n];
printf ("Enter the no. of numbers: ");
scanf ("%d", &n);
for (i = 0; i <= n-1; i++)
{
printf ("Enter the number: ");
scanf ("%d", &num[i]);
}
g = gcd (num[0], num[1]);
for (t=2; t <= n; t++)
g = gcd(num[t], g);
for (i=0; i <= n-1; i++)
pro = pro*num[i];
lcm = pro/g;
printf ("GCD is %d\n", g);
printf ("LCM is %d", lcm);
return 0;
}
Well, I tried doing it again. The problem with my code was that the return j was in the loop, and thus, it was returning the value of j instead of giving the value of g.
The corrected code:
#include <stdio.h>
int gcd(int a, int b)
{
int g;
for (int j = 2; j <= a && j <= b; ++j)
{
if (a % j == 0 && b % j==0)
g = j;
}
return g;
}
int main ()
{
int i, n, pro=1, g, t, lcm;
printf ("Enter the no. of numbers: ");
scanf ("%d", &n);
int num[n];
for (i = 0; i <= n-1; i++)
{
printf ("Enter the number: ");
scanf ("%d", &num[i]);
}
g = gcd (num[0], num[1]);
for (t=2; t <= n; t++)
g = gcd(num[t], g);
for (i=0; i <= n-1; i++)
pro = pro*num[i];
lcm = pro/g;
printf ("GCD is %d\n", g);
printf ("LCM is %d", lcm);
return 0;
}

I need help please

This program gives me zero primes in the array after running it. This is a program that takes 2d array [n x m] then calculate how many prime numbers are there in the 2d array.
int isprime(int n)
{
int k;
if (n <= 1)
return 0;
for (k = 2; k <= (n / 2); k++){
if (n % k == 0)
return 0;
}
return 1;
}
}
int primecount(int x, int y, int a[x][y]){
int r, c, count = 0;
for(r = 0; r < x; r++) {
for(c = 0; c < y; c++) {
if(isprime(a[r][c]))
{
count++;
}
}
}
return count;
}
int main()
{
int n, m, i, j, z;
printf("Enter the Number of Rows: ");
scanf("%d", &n);
printf("\n");
printf("Enter the Number of Columns: ");
scanf("%d", &m);
printf("\n");
int a[n][m];
printf("Enter the elements of the array: \n");
for(i = 0; i < n; i++)
{
for(j = 0; j < m; j++)
scanf("%d", &a[i][j]);
}
z = primecount(n, m, a[n][m]);
printf("\n");
printf("The Number of Prime Numbers in the array is: %d", z);
printf("\n");
return 0;
}
For starters instead of this call
z = primecount(n, m, a[n][m]);
you need to write
z = primecount(n, m, a);
In this call of the function isprime as in the call shown above
if(isprime(r, c, a[r][c]))
the expression a[r][c] is a scalar object of the type int. However the function isprime expects a two-dimensional array instead of a scalar object of the type int.
int isprime(int p, int q, int a[p][q])
^^^^^^^^^^^
Just declare the function like
int isprime( int x );
and correspondingly change its definition.
The function will be called like
if( isprime( a[r][c] ) )
Pay attention to that the logic of the function isprime is incorrect. It returns logical true for values equal to 1 and 0 though such values are not prime numbers.
Also you need to deal with an array with elements of the type unsigned int. Otherwise the user can enter negative values.
Here is your updated program.
#include <stdio.h>
int isprime(int n)
{
int k;
if (n <= 1)
return 0;
for (k = 2; k <= (n / 2); k++)
{
if (n % k == 0)
return 0;
}
return 1;
}
int primecount(int x, int y, int a[x][y]) //function to count prime numbers
{
int r, c, count = 0;
for(r = 0; r < x; r++)
{
for(c = 0; c < y; c++)
{
if(isprime(a[r][c]))
{
count++;
}
}
}
return count;
}
int main()
{
int n, m, i, j, z;
printf("Enter the Number of Rows: ");
scanf("%d", &n);
printf("\n");
printf("Enter the Number of Columns: ");
scanf("%d", &m);
printf("\n");
int a[n][m];
printf("Enter the elements of the array: \n");
for(i = 0; i < n; i++)
{
for(j = 0; j < m; j++)
scanf("%d", &a[i][j]);
}
z = primecount(n, m, a);
printf("\n");
printf("The Number of Prime Numbers in the array is: %d", z);
printf("\n");
return 0;
}
Its output might look like
Enter the Number of Rows: 2
Enter the Number of Columns: 2
Enter the elements of the array: 1 2 3 4
The Number of Prime Numbers in the array is: 2

Why my C program stops running in the middle?

I am trying to calculate the minimum coin denomination using the dynamic programming approach, but my code stops running after the 2nd query even though it should run for 10 times if I provide T = 10. Why is it stopping?
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
int min(int x, int y)
{
return (x < y) ? x : y;
}
short int min_denom(short int N)
{
short int *table, i, j, x;
table = (short int*)malloc(N * sizeof(short int));
for(i = 0; i < N; i++)
table[i] = 1 + i;
for(i = 1; i <= (short int)sqrt(N); i++)
for(j = 0; j < N; j++)
if(j == i)
table[j] = min(1, table[j]);
else if(j > i)
table[j] = min(table[j - i - 1] + 1, table[j]);
x = table[N - 1];
free(table);
return x;
}
int main()
{
short int T, N, i;
scanf("%d", &T);
for(i = 1; i < T; i++)
{
scanf("%d", &N);
printf("%d\n", min_denom((short int)N));
}
scanf("%d", &N);
printf("%d\n", min_denom((short int)N));
return 0;
}
The output is:
10
100
10
500
22
Then it stops running automatically.
The code will run till the end if we just replace "%d" either with "%hd" or "%hi". The reason is since the input we are taking will be stored in a short int type of variable, so we must use the proper access specifier for that.
The changed portion is given below.
short int T, N, i;
scanf("%hi", &T);
for(i = 1; i < T; i++)
{
scanf("%hi", &N);
printf("%hi\n", min_denom((short int)N));
}
scanf("%hi", &N);
printf("%hi\n", min_denom((short int)N));

Calculating the mean of an array

#include<stdio.h>
#include<stdlib.h>
double mean(int i, int arr[])
{
int j, sum = 0;
for (j = 0; j < i; j++)
{
sum = arr[j] + sum;
}
return (float)sum/i;
}
int main()
{
int arr[100] = { NULL };
int i, n, sum = 0;
printf("How many numbers would you like to enter?");
scanf_s("%d", &n);
while (n > 100 || n < 0)
{
printf("Amount of numbers should be less than 0 and more than 100\n");
scanf_s("%d", &n);
}
for (i = 0; i < n; i++)
{
scanf_s("%d", &arr[i + 1]);
}
printf("%f", mean(i-1, arr[i]));
system("pause");
}
When I run the code it gives me a read access error. The problem is with the mean() function I created but I don't know what's wrong. Help?
When I run the code it gives me a read access error. The problem is with the mean() function
Although the mean() function produces read access error, the actual problem is here:
printf("%f", mean(i-1, arr[i]));
You are not passing an array to your function, but its element (it is one past the end of what was written, too, so even the value that you pass is undefined).
You need to pass i for the length, because your mean() treats it as an exclusive upper limit, and you also need to pass arr for the array:
printf("%f", mean(i, arr));
Indexing problem when reading the data also needs to be fixed - you need to remove + 1:
scanf_s("%d", &arr[i]);
// ^
Try this:
#include<stdio.h>
#include<stdlib.h>
double mean(int i, int arr[])
{
int j, sum = 0;
for (j = 0; j < i; j++)
{
sum = arr[j] + sum;
}
return (float)sum/i;
}
int main()
{
int arr[100] = { 0 };
int i, n, sum = 0;
printf("How many numbers would you like to enter?");
scanf("%d", &n);
getchar();
while (n > 100 || n < 0)
{
printf("Amount of numbers should be more than 0 and less than 100\n");
scanf("%d", &n);
getchar();
}
for (i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
getchar();
}
printf("%lf", mean(n, arr));
getchar();
}
Your call to the mean function was wrong. You have to pass the entire array, not just one element. Change arr[i] to arr.
Other minor modifications are what I did to make it run on my system. If it works for you otherwise, then great.

stack with with array passing from function in C

How can I use information from array that will be passed from function?
#include <stdio.h>
double get_number(double[]);
main {
double x, z[100];
char m;
do {
x = get_number[z];
printf("More numbers?");
scanf (" %c", &m);
}
while ((m == 'Y')||(m == 'y'))
}
double get_number(double arr[])
{
printf ("Please enter number : ?");
scanf("%d", &arr);
return arr;
}
For example
when user press couple times Y and filled array with two or three numbers.
How can I operate with those numbers? count them or just show them.
Use a for() loop.
for (int i = 0; i < array_size; i++) {
printf ("Number at index %d: %f", i, arr[i]);
}
Of course, you need to know the size beforehand. And don't use scanf() with an array like that, why not have:
double get_input ()
{
double number = 0;
printf ("Please enter number: ");
scanf ("%d", &number);
return number;
}
Define your array length to be something fixed:
#define ARRAY_SIZE 1024
if you need more than that, increase the value or consider using a list structure.
Hope this helps
EDIT
If you want to have a sum, for example, you need to first initialize the array:
double array[ARRAY_SIZE];
for (int i = 0; i < ARRAY_SIZE; i++) array[i] = 0;
Then you get the input:
char input;
int counter = 0;
do {
if (counter > ARRAY_SIZE) break; // dont cause a segmentation fault
scanf ("%c", &input);
double number = get_input();
array[counter] = number;
} while (input == 'Y' || input == 'y');
Then when thats done, just go through the array and sum it up:
int sum = 0;
for (int i = 0; i < ARRAY_SIZE; i++) {
sum += array[i];
}

Resources