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;
}
Hello I'm trying to write a program about subsets and my problem is that whenever I try to run the program, it's automatically saying that "Y is a not subset of X" even though it is a subset. Can someone help me with this? This is my code:
#include<stdio.h>
int main()
{
int i, j, size1, size2, flag;
printf("S U B S E T S\n\n");
printf("Enter number of digits for Array X: ");
scanf("%d", &size1);
int array1[size1];
printf("Enter number of digits for Array Y: ");
scanf("%d", &size2);
int array2[size2];
printf("\n");
printf("Enter %d digits for Array X: ", size1);
for(i = 0; i < size1; i++)
{
scanf("%d", &array1[i]);
}
printf("\n");
printf("Enter %d digits for Array Y: ", size2);
for(i = 0; i < size2; i++)
{
scanf("%d", &array2[i]);
}
printf("\n\n");
for(i = 0; i < size2; i++)
for(j = 0; j < size1; j++)
if(array2[i] == array1[j])
flag++;
if(flag == size1)
{
printf("Array X is a subset of Array Y");
}
else
{
printf("Array X is not a subset of Array Y");
}
return 0;
}
It is working with very minor changes. Just loop from 0 and use < instead of <=.
#include<stdio.h>
int isSubset(int arr1[], int arr2[], int m, int n) {
int i = 0;
int j = 0;
for (i = 0; i < n; i++) {
for (j = 0; j < m; j++) {
if (arr2[i] == arr1[j])
break;
}
if (j == m)
return 0;
}
return 1;
}
int main() {
int arr1[20];
int arr2[20];
int m, n;
printf("How many elements of X you want to store?\n");
scanf("%d", &m);
printf("How many elements of Y you want to store?\n");
scanf("%d", &n);
for (int i = 0; i < m; i++) {
printf("\nEnter %d X values to be stored:\n", m);
scanf("%d", &arr1[i]);
}
for (int j = 0; j < n; j++) {
printf("\nEnter %d Y values to be stored:\n", n);
scanf("%d", &arr2[j]);
}
m = sizeof(arr1) / sizeof(arr1[0]);
n = sizeof(arr2) / sizeof(arr2[0]);
if (isSubset(arr1, arr2, m, n))
printf("Y is a subset of X \n");
else
printf("Y is not a subset of X\n");
getchar();
return 0;
}
I have to find out if there is any pair of i,j such that array[i]^2 + array[j]^2 == x^2
.
If there are such pairs, I need to print all such (i,j). Otherwise, print “There are no such pairs”.
#include <stdio.h>
int main(){
int size=10, i, x,j;
int Array[size];
printf("What is the value of x:");
scanf("%d",&x);
for(i=0;i<size;i++){
printf("Enter array value :");
scanf("%d",&Array[i]);
}
for(i=0;i<size;){
for(j=i+1;j<size;j++)
if((Array[i]*Array[i])+(Array[j]*Array[j])==x*x) //how do I complete this for loop?
}
return 0;
}
Yo're almost there, why weren't you incrementing the value of i? Keep a counter to count the matched pairs, then print those or if nothing is found print whatever you want.
#include <stdio.h>
int main() {
int size = 10, i, x, j;
int Array[size];
printf("What is the value of x:");
scanf("%d", &x);
for (i = 0; i < size; i++) {
printf("Enter array value :");
scanf("%d", &Array[i]);
}
int counter = 0;
for (i = 0; i < size; i++) {
for (j = i + 1; j < size; j++)
if ((Array[i] * Array[i]) + (Array[j] * Array[j]) == x * x) {
printf("%d %d\n", Array[i], Array[j]);
counter++;
}
}
if (!counter) {
printf("There are no such pairs\n");
}
return 0;
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I wrote my code and tried compiling in Codeblocks but it is not working. While running the programming it was showing errors in "int det(, )". I also tried using "*a" but it did not work. All it does is ask me the size of the matrix and the values and then stops. I am writing the full program but I believe the error is in the part of int det(int n, int a[][]).
#include<math.h>
main()
{
int n,i,j;
printf("enter the size of the matrix");
scanf("%d", &n);
int a[n][n];
printf("enter the matrix \n");
for (i=0; i<n; i++)
{
for(j=0; j<n; j++)
{
printf(" \n");
scanf("%d", &a[i][j]);
}
}
printf("%d determinant is", det(a,n));
}
int det( int a[][n],int n)
{
int i, j,k,d,l=0 ;
if(n=2)
{
d = a[0][0]*a[1][1] - a[0][1]*a[1][0];
return (d);
}
else
for ( k = 0; k < n ; k++ )
{
int b[n-1][n-1];
for (i=1; i<n; i++)
{
for(j=0 && j!=k ; j<n; j++)
{
b[i][j]=a[i][j];
}
}
l = a[0][j]*pow(-1,j)*det(b,n-1)+l;
}
return(l);
}
Update:
Updated code:
#include<math.h>
#include<stdio.h>
int det( int n, int a[][n]);
int main(void)
{
int n,i,j;
printf("enter the size of the matrix ");
scanf("%d", &n);
int a[n][n];
printf("enter the matrix \n");
for (i=0; i<n; i++)
{
for(j=0; j<n; j++)
{
printf(" ");
scanf("%d", &a[i][j]);
}
// printf("\n");
}
printf(" determinant is %d\n", det(n,a));
}
int det( int n, int a[][n])
{
int i, aj,bj,k,d,p=0 ;
int sign =1;
if(n==2)
{
d = a[0][0]*a[1][1] - a[0][1]*a[1][0];
return d;
}
else
for ( k = 0; k < n ; k++ )
{
int b[n-1][n-1];
for (i=1; i<n; i++)
{
for(aj=0,bj=0 ; aj<n; aj++)
{
if(aj==k) continue;
b[i-1][bj]=a[i][aj];
++bj;
}
}
p = a[0][aj]*pow(-1,k)*det(n-1, b)+p;
}
return p;
}
[Edit by Spektre]
You got wrong index in the last computation. I would change your det code to (summary of my comments):
int det( int n, int a[][n])
{
if(n<=0) return 0; // stop recursion
if(n==1) return a[0][0]; // stop recursion
if(n==2) return a[0][0]*a[1][1] - a[0][1]*a[1][0]; // stop recursion
int i,aj,bj,k,p,sign,b[n-1][n-1];
for (p=0, sign=+1, k = 0; k < n ; k++, sign=-sign)
{
for (i=1; i<n; i++)
{
for (aj=0,bj=0 ; aj<n; aj++)
if (aj!=k)
{
b[i-1][bj]=a[i][aj];
++bj;
}
}
p= p + (sign*a[0][k]*det(n-1, b)); // here you had aj instead of k causing problems !!!
}
return p;
}
Sadly my compilers does not allow this kind of array passing and I would need to change it to either template or dynamic arrays which would be confusing for you ... So I tested on this and looks like it works:
const int N=3;
int A[N][N]=
{
{ 1,2,3 },
{ 2,3,1 },
{ 3,1,2 },
};
int det(const int n, int a[][n])
{
if(n<=0) return 0; // stop recursion
if(n==1) return a[0][0]; // stop recursion
if(n==2) return a[0][0]*a[1][1] - a[0][1]*a[1][0]; // stop recursion
int i,aj,bj,k,p,sign,b[N][N];
for (i=0;i<n;i++) for (k=0;k<n;k++) b[i][k]=0;
for (p=0, sign=+1, k = 0; k < n ; k++, sign=-sign)
{
for (i=1; i<n; i++)
{
for (aj=0,bj=0 ; aj<n; aj++)
if (aj!=k)
{
b[i-1][bj]=a[i][aj];
++bj;
}
}
p+= sign*a[0][k]*det(n-1,b); // here you had aj instead of k causing problems !!!
}
return p;
}
with result det(N,A)=-18 matching my own determinant functions.
You need a function prototype for det() before main(), and the size argument needs to precede the VLA in the function call. Also, you should be using size_t for array indices instead of int.
There is no reason to use pow() to alternate signs; instead use int sign = 1; and multiply by -1 when the sign needs to be alternated.
In the loop within det(), you have:
for(j=0 && j!=k ; j<n; j++) {}
with the intention of skipping over the kth column; instead you need to write this as:
for (j = 0; j < n; j++) {
if (j == k) continue;
...
}
But there is a further problem here with:
b[i][j]=a[i][j];
Since the indices for b[][] and a[][] are not the same, and in fact b is smaller than a, this will result in writing out of bounds to b. Instead you can declare separate column indices for the two matrices:
size_t aj, bj;
...
for (aj = 0, bj = 0; aj < n; aj++)
{
if (aj == k) continue;
b[i-1][bj] = a[i][aj];
++bj;
}
...
}
Finally, you multiply the determinant of b by the wrong element of a:
l = a[0][j]*pow(-1,j)*det(b,n-1)+l;
After making the other corrections, this should be:
l = sign * a[0][k] * det(n-1, b) + l;
Here is the complete modified code:
#include <stdio.h>
int det(size_t n, int a[n][n]);
int main(void)
{
size_t n,i,j;
printf("enter the size of the matrix: ");
scanf("%zu", &n);
int a[n][n];
printf("enter the matrix: \n");
for (i = 0; i < n; i++)
{
for(j = 0; j < n; j++)
{
scanf("%d", &a[i][j]);
}
}
printf("determinant is %d\n", det(n, a));
return 0;
}
int det(size_t n, int a[][n])
{
size_t i, aj, bj, k, d;
int l = 0;
int sign = 1;
if(n == 2)
{
d = a[0][0] * a[1][1] - a[0][1] * a[1][0];
return d;
}
else
for (k = 0; k < n ; k++)
{
int b[n-1][n-1];
for (i = 1; i < n; i++)
{
for (aj = 0, bj = 0; aj < n; aj++)
{
if (aj == k) continue;
b[i-1][bj] = a[i][aj];
++bj;
}
}
l += sign * a[0][k] * det(n-1, b);
sign *= -1;
}
return l;
}
Here are a couple of sample interactions:
λ> ./a.out
enter the size of the matrix: 3
enter the matrix:
1 2 3
4 5 6
7 8 9
determinant is 0
λ> ./a.out
enter the size of the matrix: 3
enter the matrix:
1 -2 3
4 -5 6
7 8 -9
determinant is 42
Update
OP has posted updated code, and this update is to address the new issues. First, main() must (for the most part) have one of two function signatures:
int main(void);
or
int main(int argc, char *argv[]); // equivalently int main(int argc, char **argv);
Now, you must either move the definition of det() before main(), or add a function prototype:
int det( int n, int a[][n]);
Since det() uses VLAs, the size argument must come before the array argument, so the function calls must change to:
printf("%d determinant is", det(n, a));
and
p = a[0][k]*pow(-1,k)*det(n-1, b)+p;
Finally, within the inner loop in det(), you must keep two indices, aj and bj, since a[][] and b[][] are different sizes, and the elements of a[][] and b[][] do not exactly correspond:
for(aj=0, bj=0 ; aj<n; aj++)
{
if(aj==k) continue;
b[i-1][bj]=a[i][aj];
++bj;
}
I would suggest not using pow() for the sign alternation for a number of reasons; it involves an unnecessary library call, and the return value of pow() is double.
Most of these points were made in the original answer. After making these changes, your code worked for me.
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;
}