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.
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
The below code shows the error, "Constant Expression Required." I tried some solutions but still I was unable to solve.
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
int coins( int S[], int m, int n ) {
int i, j, x, y;
int table[n+1][m];
for (i=0; i<m; i++)
table[0][i] = 1;
for (i = 1; i < n+1; i++) {
for (j = 0; j < m; j++) {
x = (i-S[j] >= 0)? table[i - S[j]][j]: 0;
y = (j >= 1)? table[i][j-1]: 0;
table[i][j] = x + y;
}
}
return table[n][m-1];
}
int main() {
int arr[20],n;
int m,i;
printf("********* MAKING CHANGE PROBLEM *********");
printf("\nEnter size of array:");
scanf("%d",&n);
for(i=0;i<n;i++){
scanf("%d",arr[i]);
}
m = sizeof(arr)/sizeof(arr[0]);
printf("The total number of combinations of coins that sum up to %d",n);
printf(" is %d ", coins(arr, m, n));
getch();
return 0;
}
Based on the different versions of C Language standard, following is not allowed, mostly the error is arising from here
int table[n+1][m];
instead use #define NSIZE 10 and #define MSIZE 20 and int table[NSIZE+1][MSIZE];etc
and scanf("%d",arr[i]); will cause problems use scanf("%d",&arr[i]);
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;
}
I am trying to do a selection sort using function called min().
This is my code:
#include <stdio.h>
#include <conio.h>
void main() {
int i, temp, arr[20], n, loc;
int min(int [], int, int);
printf("Enter a range of the array");
scanf("%d", &n);
for (i = 0; i < n; i++) {
printf("Enter elements");
scanf("%d", arr[i]);
}
for (i = 0; i < n; i++) {
loc = min(arr, i, n);
temp = arr[i];
arr[i] = arr[loc];
arr[loc] = temp;
}
min(int arr[], int i, int n) {
int j, loc, temp;
for (j = i + 1; j < n; j++) {
if (arr[i] > arr[j]) {
temp = j;
}
}
return (temp);
}
getch();
}
the compiler is giving one error when compiling.
it saying:
Error SELECTIONSORT.C 22: Expression Syntax.
my line number 22 is min(int arr[],int i, int n) according to my compiler Turbo C++.
Please guide me where I am going wrong.
Thanks for any help.
There are multiple problems in your code:
The function min must be defined outside the body of the main() function.
Note that it is considered bad style to declare function prototypes in a local scope. Either define the function before the main() function or put the prototype before the main() function.
Also the prototype for main() without arguments should be int main(void).
In function min, you must initialize temp to i, or use i directly.
You should print the array contents after the sort, otherwise the program has no effect.
Here is a corrected version:
#include <stdio.h>
#include <conio.h>
int min(int [], int, int);
int main(void) {
int i, temp, arr[20], n, loc;
printf("Enter a range of the array: ");
if (scanf("%d", &n) == 1) {
for (i = 0; i < n && i < 20; i++) {
printf("Enter element %d: ", i);
if (scanf("%d", &arr[i]) != 1)
break;
}
n = i; // n is the actual number of inputs
for (i = 0; i < n; i++) {
loc = min(arr, i, n);
temp = arr[i];
arr[i] = arr[loc];
arr[loc] = temp;
}
for (i = 0; i < n; i++) {
printf("%d\n" array[i]);
}
}
getch();
return 0;
}
int min(int arr[], int i, int n) {
int j;
for (j = i + 1; j < n; j++) {
if (arr[i] > arr[j]) {
i = j;
}
}
return i;
}
So I have this program to take in an integer(i) and then make a 2D array i x i
and that part I can get to work fine, but now I am trying to find the smallest number in the array and this is always returning 0.
int smallest_number(int b[MAXrow][MAXcol], int n)
{
int min = b[0][0];
int x,y;
for (x = 0; x < n; x++)
{
for (y = 0; y < n; y++)
{
if (min > b[x][y])
{
min = b[x][y];
}
}
}
return min;
}
You are returning the "min" variable too soon. Currently, it is in the outer loop. What you want is to have it outside of the other loop.
Like this:
int smallest_number(int b[MAXrow][MAXcol], int n)
{
int min = b[0][0];
int x,y;
for (x = 0; x < n; x++)
{
for (y = 0; y < n; y++)
{
if (min > b[x][y])
{
min = b[x][y];
}
}
}
return min;
}
You can try code below to find maximum & minimum from a matrix of any order.
Code :
#include<stdio.h>
void acc(int [5][5],int,int );
void maxmin(int [5][5],int,int);
void main()
{
int a[5][5],r,c;
printf("\n\t\t**Smallest & largest no. from matrix**\n");
printf("\n\tEnter the size of row:\n\t");
scanf("%d",&r);
printf("\tEnter the size of column:\n\t");
scanf("%d",&c);
acc(a,r,c);
maxmin(a,r,c);
}
void acc(int m[5][5],int r,int c)
{
int i,j;
printf("\n\tenter the elements of matrix\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("\telement[%d][%d]= ",i,j);
scanf("%d",&m[i][j]);
}
}
}
void maxmin(int m[5][5],int r, int c)
{
int max=m[0][0], min=m[0][0],i,j;
for (i=0; i<r; i++)
{
for (j=0; j<c; j++)
{
if(m[i][j]>max)
max= m[i][j];
if(m[i][j]<min)
min= m[i][j];
}
}
printf("\n\tGreatest no. is : %d\n",max);
printf("\n\tSmallest no. is : %d\n",min);
}