Punctuation Error in factorial function? - factorial

I am getting the following error: Expected ";" at end of declaration
I get this in the line: int factorial (int q) {
ideas?
Thanks,
Steve
#include <iostream>
using namespace std;
int n=0;
int factorial(int q);
int combination(int i, int j);
int main() {
cout <<"Enter n "<<endl;
cin >>n;
for (int i = 0; i <= n-1; i++) {
for (int j = 0; j<=i; j++) {
cout << combination (i, j) << endl;
}
}
return 0;
}
int combination(int i, int j) {
return factorial(i) / (factorial(j)*factorial(i-j));
{
int factorial (int q) {
int result = 1;
if (q == 0) {
result = 1;
} else {
for (int l=1; l <= q; l++) {
result *=l;
}
}
return result;
}
This will not compile because of the error.

Your combination function above is not closed properly - you mistyped the closing bracket. It should be:
int combination(int i, int j) {
return factorial(i) / (factorial(j)*factorial(i-j));
}

Related

why the output of this program is wrong?.Frequency of number

Program for the frequency of a number
Please help me with this code to get clear output. I am a beginner
I have made the program using an array. I don't know whether it is correct or not. Made with my own logic
int count(int a)
{
int c;
while(a>=1)
{
c++;
a=a/10;
}
return c;
}
int main()
{
//program to find frquency of the number
int a,n,d;
int b[100];
int e[100];
scanf("%d",&a);
n=count(a);
for(int i=n;a>0;i--)
{
b[i]=a%10;
a=a/10;
}
for(int i=1;i<=n;i++)
{
d=b[i];
e[d]++;//most probably this part error occurs
printf("%d\n",d); //used this this to confirm that i have correctly stored value in d.
}
for(int i=1;i<=n;i++)
{
printf("%d ",e[i]);
}
return 0;
}
The line int c; should be int c = 0;
The line int e[100]; should be int e[100] = {0};
The following code could work:
#include <stdio.h>
int count(int a) {
int c = 0;
while (a >= 1) {
c++;
a = a / 10;
}
return c;
}
int main() {
// program to find frquency of the number
int a, n, d;
int b[100];
int e[100] = {0};
scanf("%d", &a);
n = count(a);
for (int i = n; a > 0; i--) {
b[i] = a % 10;
a = a / 10;
}
for (int i = 1; i <= n; i++) {
d = b[i];
e[d]++; // most probably this part error occurs
printf("%d\n", d); // used this this to confirm that i have correctly
// stored value in d.
}
for (int i = 1; i <= n; i++) {
printf("%d ", e[i]);
}
return 0;
}
Also, you can do it use snprintf:
#include <stdio.h>
int main() {
int a;
int max = -1;
char buf[100];
int count[10] = {0};
scanf("%d", &a);
snprintf(buf, sizeof(buf), "%d", a);
for (int i = 0; buf[i] != '\0'; ++i) {
int temp = buf[i] - '0';
++count[temp];
if (temp > max)
max = temp;
}
for (int i = 0; i <= max; ++i)
printf("%d ", count[i]);
return 0;
}

Getting error: expression must have pointer-to-object type

I am getting the error in my last line of code (on the j variable). What could possibly be the issue? I would appreciate any help. Thank you in advance!
#include <stdio.h>
#include <stdlib.h>
void function(int *arr, int m, int n);
int main()
{
int array[4][2], i, j;
function((int*)array, 4, 2);
for (i = 0; i < 4; i++) {
for (j = 0; j < 2; j++) {
printf("%d ", array[i][j]);
if (j == 1)
printf("\n");
}
}
system("pause");
return 0;
}
void function(int *arr, int m, int n)
{
int i, j, element;
for (i = 0; i < 4; i++) {
for (j = 0; j < 2; j++) {
printf("Give element: ");
scanf_s("%d", &element);
arr[i][j] = element;
}
}
}

I do not know why it is showing error in passing multidimensional array

I had been trying to find out the reason why this error is happening but found out nothing.
This is the code for n-queen problem from backtracking. But this doesn't matter. I want to find out why this error is occurring and how to resolve it.
#include <iostream>
using namespace std;
bool attack_checking(int arr[][s], int s, int i, int j) {
for(int x = 0; x < s; x++)
if(arr[i][x] == 1)
return true;
for(int x = 0; x < s; x++)
if(arr[x][j] == 1)
return true;
for(int x = 0; x < s; x++) {
for(int y = 0; y < s; y++) {
if(x == i && y == j)
continue;
if((x + y) == (i + j)) {
if(arr[x][y] == 1)
return true;
}
if((x - y) == (i - j)) {
if(arr[x][y] == 1)
return true;
}
}
}
return false;
}
bool queen(int arr[][s], int s, int n) {
if(n == 0)
return true;
for(int i = 0; i < s; i++) {
for(int j = 0; j < s; j++) {
if(attack_checking(arr, s, i, j))
continue;
arr[i][j] = 1;
if(queen(arr, s, n - 1))
return true;
arr[i][j] = 0;
}
}
return false;
}
int main() {
int n, s;
cin>>n;
s = n;
int arr[s][s];
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++)
arr[i][j] = 0;
bool ans = queen(arr, s, n);
if(ans)
{
cout<<"YES\n";
for(int i = 0; i<n; i++){
for(int j = 0; j<n; j++){
cout<<arr[i][j]<<" ";
}
cout<<"\n";
}
}
cout<<"NO";
return 0;
}
new.C:5:32: error: use of undeclared identifier 's'
bool attack_checking(int arr[][s], int s, int i, int j) {
^
new.C:32:22: error: use of undeclared identifier 's'
bool queen(int arr[][s], int l) {
^
2 errors generated.
This:
bool attack_checking(int arr[][s], int s, int i, int j)
^
is not allowed in C++. It is allowed in C as far as I know, although you'd have to declare the parameters before you use them.
One solution is to use a vector<vector<int>> instead of a multidimensional int array.

Error in selection sort with function in C

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;
}

What's wrong with my Checkprime.c program?

I'm trying to make a program that checks whether a given number is a prime number or not. However, my program just gives me the 2 time table, and I don't know why.
Here is my main class:
#include <stdio.h>
#include "defs.h"
#include "checkprime.c"
int Prime[MaxPrimes];
int main()
{
int UpperBound;
int N;
int *ba = &UpperBound;
printf("enter upper bound\n");
scanf("%d",ba);
Prime[2] = 1;
for (N = 3; N <= *ba; N+= 2)
{
CheckPrime(N);
if (Prime[N]== 1) printf("%d is a prime\n",N);
}
}
Here is my checkprime.c
#include "defs.h"
#include "externs.h"
int CheckPrime(int K)
{
int J;
J = 2;
while (1)
{
if (Prime[J] == 1)
{
if (K % J == 0)
{
Prime[K] = 0;
return 0;
}
J++;
}
break;
}
Prime[K] = 1;
}
There are some problems in CheckPrime with the loop exit conditions. Use the following instead:
int CheckPrime(int K)
{
int J;
for (J=2; J*J <= K; J++) {
if (Prime[J] == 1) {
if (K % J == 0) {
Prime[K] = 0;
return 0;
}
}
}
Prime[K] = 1;
return 1;
}
The rest of it should work with this change.

Resources