C variable parameters - c

I have this homework to do: "Determine the minimum of 10 precision double numbers from a string (implicit values or from the KB) using a function with a variable number of parameters. The first 7 values will be considered initially, next the last 3 and at the end these 2 values." Well I made it all but I don't know why it gives me some strange results. Here's the code:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <conio.h>
double min(double,...);
void main(){
double a,b,c;
printf("Introduceti numerele: ");
scanf("%lf%lf%lf",&a,&b,&c);
printf("\nMinimul este %lf",min(10,1.34,4.34,7,5.23,6.23,2,8.232,a,b,c));
_getch();
}
double min(double x,...){
int i;
double y;
va_list ap;
va_start(ap,x);
y=va_arg(ap,double);
for(i=0;i<x;i++){
if(y>va_arg(ap,double))
y=va_arg(ap,double);
}
va_end(ap);
return y;
}
Also i don't know why the compiler knows about what argument is next cause i is not used in va_arg(ap,double).
for(i=0;i<x;i++){
if(y>va_arg(ap,double))
y=va_arg(ap,double);

The first parameter in the call to your function min is the number of arguments, and it has the type int:
#include <stdarg.h>
double min( int numberOfArgs, ... )
// ^^^
{
va_list argptr;
va_start( argptr, numberOfArgs ); // initialize argument pointer
double minData = va_arg( argptr, double ); // initialize the minimum with the first argument
// and increment argument pointer
for ( int i = 1; i < numberOfArgs; i ++ ) // for all of the following arguments
{
double data = va_arg( argptr, double ); // get argument and increment argument pointer
if ( data < minData ) // test if argument is less than mnimum
minData = data;
}
va_end( argptr );
return minData;
}
Ensure that all of your arguments in the argument list are floating point values of type double:
int main()
{
double a, b, c;
printf("Introduceti numerele: ");
scanf_s("%lf%lf%lf", &a, &b, &c);
double minVal = min( 10, 1.34, 4.34, 7.0, 5.23, 6.23, 2.0, 8.232, a, b, c)
// ^^ ^^
printf("\nMinimul este %lf", minVal);
return 0;
}

Related

Passing incompatible pointer types to a function

I am learning how to use functions passing through pointers.
I have a function that chops a double d into two parts, the whole_part and the fraction_part. So “425.25” would be chopped into “425” and “.25”.
I'm getting errors related to passing through pointer types. I'm not sure where problems like this arise. Any help would be appreciated!
#include <math.h>
#include <stdio.h>
#include <limits.h>
void chop(double, long *, double *);
int main(void)
{
double d;
long * whole_part;
double * fraction_part;
printf("enter a double ");
scanf("%lf", &d);
// Call the function
chop(d, &whole_part, &fraction_part);
printf("%.10lf chopped is %ln and %.10lf", d, whole_part, fraction_part);
return 0;
}
void chop(double d, long *whole_part, double *fraction_part)
{
*whole_part = (int)d;
*fraction_part = d - (double)whole_part;
}
Initial problems:
long whole_part and double fraction_part should not be initialized in main as their variable pointers.
The function chop() should call the pointer to whole_part when attempting to use it to determine fraction_part.
#include <stdio.h>
#include <limits.h>
void chop(double, long *, double *);
int main(void)
{
double d;
long whole_part;
double fraction_part;
printf("enter a double ");
scanf("%lf", &d);
// Call the function
chop(d, &whole_part, &fraction_part);
printf("%.10lg chopped is %li and %.10lg\n", d, whole_part, fraction_part);
return 0;
}
void chop(double d, long *whole_part, double *fraction_part)
{
*whole_part = (int)d;
*fraction_part = d - (double)*whole_part;
}

Compiler Error C2440 in C

I'm finishing a program that I found in my C book but I ran into some issues.
I'm just about done but I keep getting this error
Error 1 error C2440: 'function' : cannot convert from 'double [15]' to '
Why am i getting this compiler error?
void arrayRead(double, int*);
void arrayList(double, int*);
void arraySum(double, int*, int*);
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<ctype.h>
int main()
{
int num, sum = 0;
double array[15];
printf("How many doubles (numbers) would you like in the array (20 max)?\n");
scanf("%d", &num);
printf("Thank you! Now give me %d different doubles (numbers) please!\n", num);
arrayRead(array, &num);
printf("Here are all of your integers again!\n");
arrayList(array, &num);
arraySum(array, &num, &sum);
printf("The sum of these numbers = %d\n", sum);
return 0;
}
void arrayRead(double array[], int* num)
{
for (int i = 0; i < *num; i++)
{
scanf("%lf", &array);
}
}
void arrayList(double array[], int*num)
{
for (int i = 0; i < *num; i++)
{
printf("%.2f\n", array);
}
}
void arraySum(double array[], int*num, int* sum)
{
for (int i = 0; i < *num; i++)
{
*sum = array + *sum;
}
}
best to place prototypes after any #include statements,
just in case the prototype uses something defined in the header file
The size of the array must be a positive number,
so use '%u' to input a positive numbe
and define the max variable as unsigned
The parameters to functions and the parameters passed when calling those functions need to have matching types (or the function type be a 'promotion' of the passed type.
since the array is doubles, all the references should also be double, like the sum variable.
appropriate horizontal white space makes the code much easier to read
do not #include header files those contents are not being used.
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
//#include <ctype.h>
// prototypes
void arrayRead( double*, unsigned );
void arrayList( double*, unsigned );
void arraySum ( double*, unsigned, double* );
int main( void )
{
unsigned num;
double sum = 0.0;
printf( "How many doubles (numbers) would you like in the array (20 max)?\n" );
if( 1 != scanf( "%u", &num ) )
{
perror( "scanf for size of array failed" );
exit( EXIT_FAILURE );
}
// implied else, scanf successful
// need to add check that user entered value is in range 1...20
double array[ num ];
printf( "Thank you! Now give me %u different doubles (numbers) please!\n", num );
arrayRead( array, num );
printf( "Here are all of your integers again!\n" );
arrayList( array, num );
arraySum( array, num, &sum );
printf("The sum of these numbers = %lf\n", sum);
return 0;
} // end function: main
void arrayRead( double array[], unsigned num )
{
for ( unsigned i = 0; i < num; i++ )
{
scanf("%lf", &array[i]);
}
} // end function: arrayRead
void arrayList( double array[], unsigned num )
{
for ( unsigned i = 0; i < num; i++ )
{
printf("%.2f\n", array[i]);
}
} // end function: arrayList
void arraySum( double array[], unsigned num, double* sum )
{
for ( unsigned i = 0; i < num; i++ )
{
*sum = array[i] + *sum;
}
} // end function: arraySum
Here is the output from a simple run of the answer code:
How many doubles (numbers) would you like in the array (20 max)?
5
Thank you! Now give me 5 different doubles (numbers) please!
1 1 1 1 1
Here are all of your integers again!
1.00
1.00
1.00
1.00
1.00
The sum of these numbers = 5.000000
The functions you have declared have different signatures than the functions you have defined.
Be careful in the declaration you use double instead of double *. It is not the same.
When you call the arrayRead function and company, the compiler sees only the declared functions as the defined functions are after the calls. So the parameters doesn't match.
By the way, in the defined functions you forgot to write the index. You are using the pointer and this can't work. You need to write array[i] inside each loop.

Multidimensional arrays in functions in c

I have this exercise to make a transpose of a matrix in C. I made a function to check for type n*n but when I'm trying to ask the user for the matrix I don't know how I should declare the array. And I'm getting this compile error "type of formal parameter 1 is incomplete" in the function on the [n2] part.
The parameters of the functions for multi dimensional arrays shouldn't be like this -> int matrix[][n2]. or is cause i'm using a variable and not a constant or a pre defined size. ?
#include <stdio.h>
#define prompt "Dimenção da matriz (nxn) >>"
#define prompt_1 "Introduza os valores : "
void getType( int *n1, int *n2 );
void getMatrix( int matrix[][n2], int lim1, int lim2);
//void trans(int matrix[][n2]);
int main(int argc, char const *argv[]) {
int n1, n2;
getType(&n1, &n2);
int matrix[n1][n2];
//printf("%dx%d\n", n1, n2);
getMatrix(matrix, n1, n2);
//trans(matrix);
return 0;
}
void getType(int *n1, int *n2){
printf("%s", prompt );
scanf("%dx%d", &(*n1), &(*n2));
}
void getMatrix( int matrix[][n2], int lim1, int lim2){
printf("%s\n", prompt_1 );
for(int line = 0; line < lim1; line++ ){
for(int column = 0; column < lim2; column++){
printf("Linha %d coluna %d ->", line, column );
scanf("%d", &matrix[line][column]);
}
}
}
The signature should be:
void getMatrix( int lim1, int lim2, int matrix[lim1][lim2] )
You are allowed omit the lim1 inside square brackets but it is good documentation to include it.
The main point is that the variable inside the square brackets must either be a parameter from earlier in the parameter list, or some other variable in scope (which can only be a global variable, but that's usually a bad idea).
Also it would be good to check scanf return value otherwise you may create matrix with garbage dimension.

pointer to function c language basic

This is my first time working with pointers to function.
What I'm trying to do is create a function called essay, that gets a pointer to another function , an integer num, and then num doubles.
The function essay, will multiply the arguments, and then return the value of the function i recieved as an argument, with the product.
This sounds complex but it really is quite simple.
Example:
essay(sin,2,pi,1/2) will return the value of sin(pi/2)
this is my code...for some reason it doesnt let me send the pointer to the function sin. Says no instance of overloaded function sin matches argument list, but this is exactly how i saw my teacher do it...I think.
#include <stdio.h>
#include <conio.h>
#include <stdarg.h>
#include <math.h>
double (*pfunc)(double);
double essay(double* pfunc(double),double num, ... )
{
int i;
double product=1,result;
va_list arguments;
va_start(arguments,num);
for(i=0;i<num;i++)
product*=va_arg(arguments,double);
va_end(arguments);
result=*(pfunc(product));
return result;
}
void main()
{
double x,y;
x=3.14159265358979323846;
y=0.5;
printf("%lf",essay(sin,2,x,y));
getch();
}
this is wrong
double essay(double* pfunc(double),double num, ... )
Here you're passing a function as a parameter which return a pointer to double, that not make sense, should be:
double essay(double (*pfunc)(double),double num, ... )
Here you're passing a pointer to a function which returns a double and receive a double as a parameter
#include <stdio.h>
#include <stdarg.h>
#include <math.h>
// removed extra var
double essay(double (*pfunc)(double), double num, ...) // added parens
{
int i;
double product = 1, result;
va_list arguments;
va_start(arguments, num);
for (i = 0; i < num; i++)
product *= va_arg(arguments, double);
va_end(arguments);
result = pfunc(product); // removed extra parens
return result;
}
void main()
{
double x, y;
x = 3.14159265358979323846;
y = 0.5;
printf("%lf", essay(sin, 2, x, y));
}
This is what I'd do, assuming a C99 compiler that accepts variable declarations in for loops and at arbitrary points in a block of code. Note the use of the typedef for the function pointer type (MathFunc2 would be for a function that takes two arguments, etc), and the use of int (rather than double) for the number of values in the argument list.
#include <math.h>
#include <stdarg.h>
#include <stdio.h>
typedef double (*MathFunc1)(double);
static double essay(MathFunc1 function, int num, ...)
{
double product = 1.0;
va_list arguments;
va_start(arguments, num);
for (int i = 0; i < num; i++)
product *= va_arg(arguments, double);
va_end(arguments);
double result = (*function)(product);
return result;
}
int main(void)
{
double x = 3.14159265358979323846; // M_PI?
double y = 0.5;
printf("%f\n", essay(sin, 2, x, y));
}

How can I pass any number of arguments in User define function in C?

How can I pass any number of arguments in User define function in C?what is the prototype of that function?It is similar to printf which can accept any number of arguments.
Look here for an example.
#include <stdlib.h>
#include <stdarg.h>
#include <stdio.h>
int maxof(int, ...) ;
void f(void);
main(){
f();
exit(EXIT SUCCESS);
}
int maxof(int n args, ...){
register int i;
int max, a;
va_list ap;
va_start(ap, n args);
max = va_arg(ap, int);
for(i = 2; i <= n_args; i++) {
if((a = va_arg(ap, int)) > max)
max = a;
}
va_end(ap);
return max;
}
void f(void) {
int i = 5;
int j[256];
j[42] = 24;
printf("%d\n",maxof(3, i, j[42], 0));
}
Such a function is calle variadic, but such functions are much less useful than they might at first seem. The wikipedia page on the topic is not bad, and has C code.
The basic problem with such functions is that the number of parameters cannot in fact be variable - they are must be fixed at compile time by a known parameter. This is obvious in printf:
printf( "%s %d", "Value is", 42 );
The number of % specifiers must match the number of actual values, and this is true for all other uses of variadic functions in C, in one form or another.

Resources