Related
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
When i run my code in online GCC compiler i get no errors and program gets compiled but when i try to run it in VSCODE it gives me error that function declaration is not allowed here.
Code.
#include<stdio.h>
#include<stdlib.h>
#include<limits.h>
int main()
{
int n, x, y, i, j;
printf("How many numbers do you want to enter? ");
scanf("%d",&n);
int *a=(int*)malloc(n*sizeof(int));
printf("Enter the numbers: ");
for(int i=0; i<n; i++)
scanf("%d",&a[i]);
printf("Enter the value of the two distance x and y: ");
scanf("%d %d",&x,&y);
int minDist(int arr[], int n, int x, int y)
{
int i, j;
int min_dist = INT_MAX;
for (i = 0; i < n; i++) {
for (j = i + 1; j < n; j++) {
if ((x == arr[i] && y == arr[j]
|| y == arr[i] && x == arr[j])
&& min_dist > abs(i - j)) {
min_dist = abs(i - j);
}
}
}
if (min_dist > n) {
return -1;
}
return min_dist;
}
printf("\nThe Minimum Distance between %d and %d is %d.\n",x, y,minDist(a,n,x,y));
free(a);
return 0;
}
You're declaring minDist inside main. Nested functions are a GNU extension. Other compilers might not allow them.
Take it and move it before the main function.
Here is your code
#include<stdio.h>
#include<stdlib.h>
#include<limits.h>
int minDist(int arr[], int n, int x, int y);
int minDist(int arr[], int n, int x, int y)
{
int i, j;
int min_dist = INT_MAX;
for (i = 0; i < n; i++) {
for (j = i + 1; j < n; j++) {
if ((x == arr[i] && y == arr[j]
|| y == arr[i] && x == arr[j])
&& min_dist > abs(i - j)) {
min_dist = abs(i - j);
}
}
}
if (min_dist > n) {
return -1;
}
return min_dist;
}
int main()
{
int n, x, y, i, j;
printf("How many numbers do you want to enter? ");
scanf("%d",&n);
int *a=(int*)malloc(n*sizeof(int));
printf("Enter the numbers: ");
for(int i=0; i<n; i++)
scanf("%d",&a[i]);
printf("Enter the value of the two distance x and y: ");
scanf("%d %d",&x,&y);
printf("\nThe Minimum Distance between %d and %d is %d.\n",x, y,minDist(a,n,x,y));
free(a);
return 0;
}
Following C standards, your program is not wrong.
You've used nested functions and that's supported in C. The reason to why your program worked just fine when compiled using gcc and not in VS Code is that
Nested functions are supported as an extension in GNU C, but are not supported by GNU C++
according to this(which was also pointed out by #luther in his answer).
VS Code's C/C++ extension uses the g++ command to compile C and C++ programs (yes, you can use both gcc and g++ to compile .c and .cpp files). g++ will compile .c and .cpp files, but will treat them as C++ files. So, now it's clear why your program does not work when compiled and run in VS Code.
Check this to look at VS Code's C/C++ extension and this to know the difference between gcc and g++.
If you really want to have your program compile and run in VS Code then you can choose to do one of these 2:
You can place the the minDist() before the main() function in your code like this:
#include<stdio.h>
#include<stdlib.h>
#include<limits.h>
int minDist(int arr[], int n, int x, int y)
{
int i, j;
int min_dist = INT_MAX;
for (i = 0; i < n; i++) {
for (j = i + 1; j < n; j++) {
if ((x == arr[i] && y == arr[j]
|| y == arr[i] && x == arr[j])
&& min_dist > abs(i - j)) {
min_dist = abs(i - j);
}
}
}
if (min_dist > n) {
return -1;
}
return min_dist;
}
int main()
{
int n, x, y, i, j;
printf("How many numbers do you want to enter? ");
scanf("%d",&n);
int *a=(int*)malloc(n*sizeof(int));
printf("Enter the numbers: ");
for(int i=0; i<n; i++)
scanf("%d",&a[i]);
printf("Enter the value of the two distance x and y: ");
scanf("%d %d",&x,&y);
printf("\nThe Minimum Distance between %d and %d is %d.\n",x, y,minDist(a,n,x,y));
free(a);
return 0;
}
You may declare the function before the main() function and have the function's body after the main() function. Like this:
#include<stdio.h>
#include<stdlib.h>
#include<limits.h>
int minDist(int arr[], int n, int x, int y); // declaration of the minDist() function
int main()
{
int n, x, y, i, j;
printf("How many numbers do you want to enter? ");
scanf("%d",&n);
int *a=(int*)malloc(n*sizeof(int));
printf("Enter the numbers: ");
for(int i=0; i<n; i++)
scanf("%d",&a[i]);
printf("Enter the value of the two distance x and y: ");
scanf("%d %d",&x,&y);
printf("\nThe Minimum Distance between %d and %d is %d.\n",x, y,minDist(a,n,x,y));
free(a);
return 0;
}
int minDist(int arr[], int n, int x, int y)
{
int i, j;
int min_dist = INT_MAX;
for (i = 0; i < n; i++) {
for (j = i + 1; j < n; j++) {
if ((x == arr[i] && y == arr[j]
|| y == arr[i] && x == arr[j])
&& min_dist > abs(i - j)) {
min_dist = abs(i - j);
}
}
}
if (min_dist > n) {
return -1;
}
return min_dist;
}
Edit: Many thanks to Erik Postpischil for pointing out that I have not completely answered the question.
c program is compiled in top to bottom so before using function in main you have to specify that there is a function that exist. for this write function prototype in up or write entire function body before main().
#include<stdio.h>
#include<stdlib.h>
#include<limits.h>
int minDist(int arr[], int n, int x, int y);
int main()
{
int n, x, y, i, j;
printf("How many numbers do you want to enter? ");
scanf("%d",&n);
int *a=(int*)malloc(n*sizeof(int));
printf("Enter the numbers: ");
for(int i=0; i<n; i++)
scanf("%d",&a[i]);
printf("Enter the value of the two distance x and y: ");
scanf("%d %d",&x,&y);
printf("\nThe Minimum Distance between %d and %d is %d.\n",x,y,minDist(a,n,x,y));
free(a);
return 0;
}
int minDist(int arr[], int n, int x, int y)
{
int i, j;
int min_dist = INT_MAX;
for (i = 0; i < n; i++) {
for (j = i + 1; j < n; j++) {
if ((x == arr[i] && y == arr[j]
|| y == arr[i] && x == arr[j])
&& min_dist > abs(i - j)) {
min_dist = abs(i - j);
}
}
}
if (min_dist > n) {
return -1;
}
return min_dist;
}
Why are you declaring the function inside the main function?
Take it and move it before the main function.
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));
The objective: Add only the pieces of the matrix that are part of a full X (upper and lower triangle).
1 1 1
0 1 0
1 1 1
Like this, middle one should add only once.
I can't add the lower triangle properly. Help much appreciated :)
void write(int niz[20][20], int n){
int i, j;
for(i=0; i<n; i++){
for(j=0; j<n; j++){
scanf("%d", &niz[i][j]);
}
}
}
void x(int niz[20][20], int n){
//Upper triangle
int i, j, pr=n, suma=0;
for(i=0; i<n/2 + n%2; i++,pr--){
for(j=i; j<pr; j++){
suma += niz[i][j];
}
}
printf("%d\n",suma);
//Lower triangle
pr = n;
for(i=n; i>n/2 + n%2; i--,pr--){
printf("%d",pr);
for(j=n-i; j<pr; j++){
printf("\n%d", niz[i][j]);
suma += niz[i][j];
}
}
printf("%d", suma);
}
int main()
{
int n;
printf("Matrix dimensions: ");
scanf("%d", &n);
printf("Numbers in the matrix: \n");
int niz[n][n];
write(niz, n);
x(niz, n);
}
Instead of writing separate functions for each lower, upper & diagonals you can do all together with little tricks, but it works only if row == column and thats's what you want I think.
int main() {
/* it can be anything like a[3][3] or a[7][7] and elements can
be all one or all 2 or any number */
int arr[5][5] = { {1,1,1,1,1},
{0,0,1,0,0},
{0,0,1,0,0},
{0,0,1,0,0},
{1,1,1,1,1} };
int row = sizeof(arr)/sizeof(arr[0]);
int col = sizeof(arr[0])/sizeof(arr[0][0]);
int sum = 0;
for(int index = 0; index < row; index++) {
for(int sub_index = 0; sub_index < col; sub_index++) {
if(index == 0 || (index == row-1) || sub_index == row/2)
sum = sum + arr[index][sub_index];
}
}
printf("sum = %d \n",sum);
return 0;
}
Its fine if it helps you otherwise write your own logic.
There are some mismatches between the declarations and types of the arguments passed to OP's function. While in main they declare a variable length array, named niz:
int n;
// ...
int niz[n][n];
The posted signature of both write and x requires an int niz(*)[20]. It should be changed to:
void write(int n, int niz[n][n]);
// this ^^^ may be a size_t, just remember to write it before the array
About the pattern you have to follow for the sum, I can't say to fully understand your requirement, but if I'm not completely wrong, it could be done this way:
#include <stdio.h>
#include <stdlib.h>
void read_matrix(int n, int niz[n][n])
{
for(int i=0; i<n; i++) {
for(int j=0; j<n; j++) {
scanf("%d", &niz[i][j]);
}
}
}
// Separate the calculation from the printing
int hourglass_sum(int n, int niz[n][n])
{
int sum = 0;
int i = 0;
//Upper triangle
for(int k = n; i < k; ++i, --k) {
for(int j = i; j < k; ++j) {
sum += niz[i][j];
}
}
//Lower triangle
for(int k = i + 1; i < n; ++i, ++k) {
for(int j = n - i - 1; j < k; ++j) {
sum += niz[i][j];
}
}
return sum;
}
int main()
{
int n;
printf("Matrix dimensions: ");
scanf("%d", &n);
int niz[n][n];
read_matrix(n, niz);
printf("\nSum: %d", hourglass_sum(n, niz));
}
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.