C: I keep getting errors that say error: expected expression - c

I keep getting five errors in my code that say error: expected expression when talking about the variables in my function. I have looked on this site and other places but I can't find what's wrong.
Here's the code:
#include <stdio.h>
#define TRUE 1
#define FALSE 0
void separate_digits (long int n);
void print_array ( int a[10] );
int digits_different ( int a[10] );
int divisible ( int a[10], int n );
int main ()
{
long int n;
printf("Enter a positive integer or 0 (zero) to end:");
scanf("%ld\n", &n);
while ( n != 0)
{
if (n < 0)
{
printf("Wrong input\n");
}
else
{
separate_digits (long int n);
}
printf("Enter a positive integer or 0 (zero) to end:");
scanf("%ld\n", &n);
}
printf("*** Program Terminated ***\n");
}
void separate_digits (long int n)
{
int a[10] = {0};
int digit;
int num = n;
while ( num > 0)
{
digit = num % 10;
++a[digit];
num = num / 10;
}
print_array ( int a[10] );
if ( a[0] != 0 )
{
printf("Wrong input for the second part.\n");
printf("Input should not contain zero.\n");
}
else if ( digits_different ( int a[10] ) == FALSE )
{
printf("Wrong input for the second part.\n");
printf("Input should not contain each digit more than once.\n");
}
else if ( divisible ( int a[10], int n ) == FALSE )
{
printf("%1ld is not divisible by its digits.\n", n);
}
else
{
printf("%1ld is divisible by its digits.\n", n);
}
}
void print_array ( int a[10] )
{
int i;
printf("\n\n\n");
printf("Digits: 0 1 2 3 4 5 6 7 8 9\n");
printf("Occurrences: ");
for (i = 0; i < 10; i++)
{
printf("a[i] ");
}
}
int digits_different ( int a[10] )
{
int i;
for (i = 0; i < 10; i++)
{
if (a[i] > 1)
return FALSE;
else
return TRUE;
}
}
int divisible ( int a[10], int n )
{
int i;
int num;
for (i = 0; i < 10; i++)
{
if (a[i] == 0)
continue;
else if (num % i != 0)
return FALSE;
}
return TRUE;
}
Here's the errors I keep getting:
Lab_Assignment_6_Sarah_H.c:27:21: error: expected expression
separate_digits (long int n);
^
Lab_Assignment_6_Sarah_H.c:53:16: error: expected expression
print_array ( int a[10] );
^
Lab_Assignment_6_Sarah_H.c:61:31: error: expected expression
else if ( digits_different ( int a[10] ) == FALSE )
^
Lab_Assignment_6_Sarah_H.c:67:24: error: expected expression
else if ( divisible ( int a[10], int n ) == FALSE )
^
Lab_Assignment_6_Sarah_H.c:67:35: error: expected expression
else if ( divisible ( int a[10], int n ) == FALSE )

You shouldn't be prepending your variables with a type name when you actually pass them to a function.
That's only required in the declarations for those functions, as you've done near the top of the code.

The function call syntax only takes the variable name, not the type.
separate_digits (long int n);
should be
separate_digits (n);
Also, print_array ( int a[10] ); is wrong, you only need to pass the array name to pass the array, like
print_array ( a );
and so on.
Related, quoting C11, chapter ยง6.5.2.1, Postfix operators, function call syntax
postfix-expression ( argument-expression-listopt )
and
argument-expression-list:
assignment-expression
argument-expression-list , assignment-expression
there, the "type" is not to be mentioned in argument list, it is
An argument may be an expression of any complete object type.
In other words, each element in the argument-expression-list need to have a type which is not to be written.

separate_digits (long int n); is not syntactically correct. This is confusing the compiler.
Drop the long int to give separate_digits (n);

Related

write a program that computes and displays the factorial of a positive integer n entered by the user

Been working on this question for class. Not sure what I'm doing wrong.
Seems like I just don't have the correct format. My professor wants the output to look like "5!=1 * 2* 3* 4* 5=120"
Can someone help me with this? Below is what I have so far:
#include <iostream>
#include <stdio.h>
int main() {
int n, i, fact;
printf("Enter a positive integer: \n");
scanf("%d", &n);
if (n < 0)
printf("Error! Factorial of a negative number doesn't exist.");
else {
for( i = 1; i <= n; ++i) {
fact *= i;
n= n * (n-1);
}
printf("Factorial of %d = ", &n, &fact) ;
}
return 0;
}
remove #include <iostream> if it is the C code
Use functions.
Initialize local variables (you do not and it is undefined behaviour)
Compile C program using C compilers
unsigned long long fact(unsigned val, char *buff)
{
unsigned result = 1;
*buff++ = '1';
*buff = 0;
for(unsigned c = 2; c <= val; c++)
{
buff += sprintf(buff, "x%u", c);
result *= c;
}
return result;
}
int main(void)
{
unsigned x;
char y[1000];
unsigned long long res;
if(scanf("%u", &x) == 1){res = fact(x, y); printf("factoral(%u) = %s = %llu\n", x, y, res);}
else printf("Invalid number!!!\n");
}
or without printing steps
unsigned long long fact(unsigned val)
{
unsigned result;
switch(val)
{
case 0:
case 1:
result = 1;
break;
case 2:
result = 2;
break;
default:
result = 2;
while(val > 2) result *= val--;
break;
}
return result;
}
int main(void)
{
unsigned x;
if(scanf("%u", &x) == 1) printf("factioral(%u) = %llu\n", x, fact(x));
else printf("Invalid number!!!\n");
}
https://godbolt.org/z/Tf5431zco
For starters it does not make a sense to declare the variable n as having a signed integer type. It is better to declare it as having unsigned integer type. For example
unsigned int n, i, fact;
Secondly the variable fact was not initialized. You should initialize it by the value equal to 1.
So the above declaration could look like
unsigned int n, i;
unsigned long long fact = 1;
In this for loop
for( i = 1; i <= n; ++i) {
fact *= i;
n= n * (n-1);
}
the statement
n= n * (n-1);
does not make a sense and shall be removed.
In the printf call you have to use values of variables instead of pointers to the variables like
printf("Factorial of %u = %llu\n", n, fact) ;
Pay attention to that neither declaration from the header <iostream> is used in your program So whether it is a C program or a C++ program nevertheless remove this header.
Here is a demonstration C program.
#include <stdio.h>
int main( void )
{
unsigned int n = 0;
unsigned long long int fact = 1;
printf( "Enter a positive integer: " );
scanf( "%u", &n );
printf( "%u! = ", n );
for ( unsigned int i = 1; i <= n; i++ )
{
fact *= i;
if ( i != 1 ) printf( " * " );
printf( "%u", i );
}
printf( " = %llu\n", fact );
}
The program output might look like
Enter a positive integer: 5
5! = 1 * 2 * 3 * 4 * 5 = 120
If you are allowed to use only variables of the type int as you wrote in a comment to the answer then the program can look like
#include <stdio.h>
int main( void )
{
int n = 0;
int fact = 1;
printf( "Enter a positive integer: " );
scanf( "%d", &n );
if ( n < 0 )
{
puts( "Error! Factorial of a negative number doesn't exist.");
}
else
{
printf( "%d! = ", n );
for ( int i = 1; i <= n; i++ )
{
fact *= i;
if ( i != 1 ) printf( " * " );
printf( "%d", i );
}
printf( " = %d\n", fact );
}
}
To output multipliers in the reverse order like
5! = 5 * 4 * 3 * 2 * 1 = 120
rewrite the for loop the following way
for ( int i = n; i != 0; i-- )
{
fact *= i;
if ( i != n ) printf( " * " );
printf( "%d", i );
}

Recursive Persistence of a number

I am trying to find the persistence of a number. When you multiply the digits of a number together, eventually you will arrive at a single digit number. Persistence is the number of cycles that takes. I am trying to find that using a recursive Function. Here is my code:
#include <stdio.h>
int persistence(int x);
int main(int argc, char *argv[])
{
int x, per = 0, t;
char c;
while((c = getchar())!= EOF)
{
printf("Enter a number:\n");
scanf("%d", &x);
while(x>10)
{
t = persistence(x);
printf("\n%d", persistence(t));
per++;
}
printf("\n%d\n\n", per);
}
return 0;
}
int persistence(int x)
{
if(x<10)
{
return x;
}
else
{
return (x%10 * persistence(x/10));
}
}
The function persistence itself should return the value of the multiplicative persistence.
It can be defined the following way as it is shown in the demonstrative program.
#include <stdio.h>
size_t persistence( unsigned int x )
{
const unsigned int Base = 10;
if ( ! ( x < Base ) )
{
unsigned int n = 1;
do { n *= x % Base; } while ( x /= Base );
return 1 + persistence( n );
}
else
{
return 0;
}
}
int main(void)
{
unsigned int x = 39;
printf( "persistence( %u ) = %zu\n", x, persistence( x ) );
return 0;
}
The program output is
persistence( 39 ) = 3

Void Factorial Using Pointers in C

I want to do a void factorial function in C that uses pointers. This is my code but it won't run.
#include <stdio.h>
void factorial(int, int*);
int main()
{
int n;
int r = 0;
printf("Enter n : ");
scanf("%d", &n);
if (n < 0)
{
printf("No factorial for negative");
}
else
{
factorial(n , &r);
printf("factorial of %d is %d", n, r);
}
}
void factorial(int n , int *r)
{
if (n == 0 || n == 1)
{
*r= 1;
}
else
{
*r= n* factorial((n-1), r);
}
}
The error I get is in the last line of the code saying : invalid operands of type "int" and "void" to binary operator * , what does that mean and how to fix it?
Well, if factorial() returns void (which is basically , returning nothing) , this line does not make any sense
*result = num * factorial ((num-1), result);
It's because factorial() returns void.
Try:
factorial((num-1), result);
*result= num *(*result);
Instead of that line.
You cannot use return value of a function if it is declared to return void.
If you want to declare function this way then your factorial function can be:
void factorial(int num , int *result)
{
if (num == 0 || num == 1)
{
//*result = 1;
return;
}
else
{
*result *= num;
factorial ((num-1), result);
}
But also in main function you should change int res = 0 into int res = 1.
void Factorial(int *a,int *result){
if (*a == 0 || *a == 1){
return;
}
else{
*result *= *a;
*a -= 1;
Factorial(a,result);
}}

Scanning numbers from an array

So I created an dynamic array in C. The array contains only numbers. How can I scan the 1D array I created to print how many numbers are greater than the last one in the array? I created the code for scanning the file only, which is the following:
#include <stdio.h>
#include <limits.h>
int process( FILE *f ) {
int a, b;
static int total = 1;
if ( f == 0 ) return 0;
if ( fscanf( f, "%d", &a ) != 1 ) return INT_MIN;
if ( (b= check_file ( f )) == INT_MIN ) return a;
if ( a > b )
{total++; return printf( "%d > %d and total now is %d\n", a, b, total ), a; }
return b;
}
int main( void ) {
FILE *fp= fopen( "xxx.txt", "r" );
process( fp );
fclose( fp );
return 0;
}
Can I make the same with the array? How can I do that? Which commands should I use?
Dynamic array:
#include <stdio.h>
#include <stdlib.h>
int main ()
{
int t_size;
FILE * fp;
fp = fopen ("xxx.txt", "r");
fscanf(fp, "%d", &t_size);
printf("Create a size of %d array\n", t_size);
int* my_array = NULL;
my_array = malloc(t_size*sizeof(*my_array));
if (my_array==NULL) {
printf("Error allocating memory!\n"); //print an error message
return 1; //return with failure
getchar();
}
int i =0;
for ( i = 0; i < t_size; i++ )
{
fscanf(fp, "%d",&my_array[i]);
}
//if all is working smoothly...
printf("All up and running! Array created! :D\n");
for(i = 0; i < t_size; i++ )
{
printf(" %d : %d\n", i, my_array[i]);
}
getchar();
free(my_array);
}
You have to:
Add all the numbers from your file to the array (int arr[N], where N is number of the elements)
Find the value of the last element in the array (arr[N-1])
Go through the array and count how many are greater then the last one.
#include <stdio.h>
#include <limits.h>
int process( int *array, int *endp) {
int a, b;
if ( array == 0 ) return 0;
if ( array == endp ) return INT_MIN;
else a = *array++;
if ( (b= process( array, endp )) == INT_MIN ) return a;
if ( a > b ) return printf( "%d > %d\n", a, b ), a;
return b;
}
int main( void ) {
int array[] = { 4, 3, 2, 1};
int t_size = 4;
process(array, array + t_size);
return 0;
}

Segmentation fault core dump

#include <stdio.h>
void ScanArray (int* , int*);
int Pair (int* , int);
int main () {
int a [15] , n;
ScanArray (a , &n);
// printf("\nHello World !!!\n");
if(Pair(a , n) == 0)
printf("The array fulfills the requirements");
else
printf("The array does not fulfills the requirements");
return 0;
}
void ScanArray (int *a , int *n) {
int i , f = 0;
do {
printf("Enter the number of integers : ");
scanf("%d",n);
if (*n > 15)
printf("Enter number bellow 15 \n");
else
f=1;
} while(f == 0);
printf("Enter the %d integers : ",*n);
for (i = 0 ; i < *n ; i++)
scanf("%d",a+i);
}
int Pair (int *a , int n) {
if (n <= 1)
return 0;
else
if (*a-*(a+1) != 1 && *a-*(a+1) != -1)
return 1;
else
return Pair(a++ , n--);
}
don't know why it's not working.
Segmentation fault (core dump).
else
return Pair(a++ , n--);
Using postfix increment and decrement operators will result in the recursive call processing the same values. You should either use prefix operators, or even better, just add and subtract 1.
else
return Pair(a + 1 , n - 1);
I say that's better because it's misleading to think modifying the values matters; the key to recursion is that the recursive calls will have their own copies of a and n, so modifying them in a parent has no effect on the children.

Resources