problem with generating rand number between 0 and 1 using C language - c

I am trying to generate some numbers with the rand() function like below:
int main()
{
int i=0
for(i=0;i<20;i++)
{
printf("%f\n",rand_md());
}
return 0;
}
float rand_md()
{
return (float)rand()/(RAND_MAX+1.0);
}
But when I run gcc rand.c, I get the error like below:
rand.c: In function ‘main’:
rand.c:21: warning: format ‘%f’ expects type ‘double’, but argument 2 has type ‘int’
rand.c: At top level:
rand.c:36: error: conflicting types for ‘rand_md’
rand.c:21: error: previous implicit declaration of ‘rand_md’ was here
What's wrong with my code?
Best Regards,

When you use a function without declaring it first, C will assume it returns an int. To fix this, you have to declare the method signature for rand_md before you call it in main().
Add something like this before main or in a header file:
float rand_md();

double rand_md()
{
return (double )rand()/(RAND_MAX+1.0);
}
Now, rand_md returns a double instead of a float.
Also, since you haven't included it in your post, you should throw in a function prototype before main(). You also forgot to add a semicolon to int i = 0. The corrected code looks like this:
double rand_md();
int main()
{
int i=0;
for(i=0;i<20;i++)
{
printf("%f\n",rand_md());
}
return 0;
}
double rand_md()
{
return (double )rand()/(RAND_MAX+1.0);
}

You can use random(largest size of number) for this purpose.

Related

too many arguments for function error but function clearly takes two arguments

code to ask user to input base and power and prints result:
#include <stdio.h>
int main()
{
float v;
int power;
printf("Enter value of x:\t");
scanf("%f", &v);
printf("Enter power:\t");
scanf("%d", &power);
v = exp(v, power);
printf("%.2f", v);
}
float exp(float n, int i) {
float base = n;
int power = i;
float result = 1;
while (power != 0)
{
result = result * base;
--power;
}
return result;
}
compiler spits out:
x.c:11:6: error: too many arguments to function ‘exp’
11 | v = exp(v, power);
| ^~~
clearly:
float exp(float n, int i) {
takes two arguments. What's going on here?
It's because your exp function is not known by the compiler yet.
exp is built-in function in C.
The function prototype of exp() is: double exp(double x);
To make your program work, you should put your exp function above main function.
Two things are happening here.
You have not declared or defined exp() before use
Your compiler has a built-in for exp() with a different signature
I am not sure what compiler you are using but I would be surprised if there were not also warnings explaining this. GCC produces:
main.c: In function ‘main’:
main.c:12:11: warning: implicit declaration of function ‘exp’ [-Wimplicit-function-declaration]
v = exp(v, power);
^~~
main.c:12:11: warning: incompatible implicit declaration of built-in function ‘exp’
main.c:12:11: note: include ‘’ or provide a declaration of ‘exp’
main.c:12:11: error: too many arguments to function ‘exp’
main.c: At top level:
main.c:18:9: warning: conflicting types for built-in function ‘exp’
float exp(float n, int i) {
^~~
Providing a prototype solves the problem:
float exp(float n, int i) ;
int main()
{
...
}
However it is probably ill-advised. The standard library function exp() computes e (Euler's number, 2.7182818...) raised to the given power the single argument, so is semantically different that your exp().
Overriding a standard library function in this way is in any case ill advised even if it has the same semantics. I strongly suggest that you use a different name such as power() for example (not pow() - that is also a standard library function).

After returning a float value from a method and trying to print it, the printed value is always wrong (although the method does works as intended)

I wrote a simple method to calculate an arithmetic mean like so:
float arithmetic_mean(int a[], int n)
{
float sum = 0;
int i;
for (i = 0; i < n; i++)
{
sum += a[i];
}
sum = sum / (float)n;
return sum;
}
I wanted to check it in main:
int main()
{
int a[] = { 1,3,5,1,6,7 };
float check = arithmetic_mean(a, 6);
printf("%f", check);
return 0;
}
even though the value the method returns is correct (=3.833333 as checked by printing it before returning it), when I try to print it in main I get 9 as the output.
I'm really new to C language and stuff like this always seems to happen with floats - I'll write a method that works and return a float - and the returned value would be something seemingly random. What am I missing here? What am I doing wrong?
The problem (which I can reproduce with MSVC) is that you are calling the arithmetic_mean function (from main) before you have defined it. Thus, the compiler uses a default definition of the function, which is that it returns an int type ... and the actual (float) value returned just happens to have a bit pattern that represents 9 when interpreted as an int.
You can leave the definition of the function where it is (after main) so long as you provide a forward declaration of the function, so that the compiler knows what the return type is:
float arithmetic_mean(int a[], int n); // Forward declaration of the function!
int main()
{
int a[] = { 1,3,5,1,6,7 };
float check = arithmetic_mean(a, 6);
printf("%f\n", check);
return 0;
}
// The actual DEFINITION (including the body) of the function can follow...
Turning on compiler warnings would have helped you spot this! For example, without the forward declaration, MSVC gives this:
warning C4013: 'arithmetic_mean' undefined; assuming extern returning
int

Parameter Names (without types) in Function Declaration

So, I'm working on an assignment and my teacher didn't quite explain functions very well. I'll save some time and show the main segment where the error occurs:
#include <stdio.h>
6 int main(void)
7 {
8 double Depth;
9 double celsius_at_depth(Depth);
10 {
11 10*(Depth)+20;
12 }
And the error:
GHP#4.c:9:2: warning: parameter names (without types)
in function declaration [enabled by default]
double celsius_at_depth(Depth);
^
Sorry about formatting, I wanted it to be easier to see. isn't double supposed to be the parameter type for the function celsius_at_depth?
EDIT: I've looked up the error on the site, but I didn't quite see it in the same formatting as my code, so I felt it best to post anew
Defining a function inside another function is a non standard extension of gcc AFAIK, so it's not a good idea.
To declare the function you first need to move it outside main() so
double celsius_at_depth(double depth);
and then you can call it in main like this
#include <stdio.h>
int main(void)
{
double depth = 10;
printf("celsius_at_depth(%f) = %f\n", depth, celsius_at_depth(depth))
return 0;
}
then the function definition
double celsius_at_depth(double depth);
{
return 10 * depth + 20;
}
here you are using double as the return type for the function but u didnt use return in the funciton. this is one error.
#include <stdio.h>
int main(void)
{
double Depth;
double celsius_at_depth(Depth);
{
return (10*(Depth)+20);
}
//also call the funciton for returning the value
celsuis_at_depth(Depth) ;
printf("is the degree celcius \n");
}

The parameter x has not been declared

I am very new to programming and I always receive this error when defining functions:
The parameter IPWM_int has not been declared
There is no return type for the function, but it does not give any errors.
Here is the function that causes the problem (this is out of main() function):
int IntToASCII(IPWM_int)
{
uint8_t i;
for(i=0;i<5;i++)
{
IPWM_string[i]=0x30;
}
sprintf(IPWM_string,"%5d", IPWM_int);
return 0;
}
The correct function definition here is presumably (based on the %d formatting specifier and the variable name):
int IntToASCII(int IPWM_int)
The only way that this function definition could compile is if it were in ANSI C, and looked like this:
int IntToASCII(IPWM_int)
int IPWM_int;
{
[...]
Perhaps this explains why the code you adapted this from compiled?
You need to give the argument IPWM_int a type. It looks like you want IPWM_int to be the name of the parameter here so if this parameter is an int, for example, you would have:
int IntToASCII(int IPWM_int)
{
uint8_t i;
for(i=0;i<5;i++)
{
IPWM_string[i]=0x30;
}
sprintf(IPWM_string,"%5d", IPWM_int);
return 0;
}
int IntToASCII(int IPWM_int)
// ^^^ is required at function declaration and definition.
syntax:
return-type functionName(dataType arg1, datatype arg2, ....)
{
//function body
}

c language array problems

Hi
I'm new to c language i hava a problem :
i want to send a 2-d array to a function via pointer.
The function should return pointer to 2-d array.
I wrote the following code for this :
#include<stdio.h>
int* put(int *b);
int main()
{
int a[2][3],i,j;
system("clear");
put(a);
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
printf("\na[%d][%d]= %d",i,j,a[i][j]);
}
}
return 0;
}
int* put(int *b)
{
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
b[i][j]=i;
}
}
return b;
}
when i compile it with gcc2de.c it shows following errors :
2de.c: In function ‘main’:
2de.c:9: warning: passing argument 1 of ‘put’ from incompatible pointer type
2de.c:3: note: expected ‘int *’ but argument is of type ‘int (*)[3]’
2de.c: In function ‘put’:
2de.c:28: error: subscripted value is neither array nor pointer
2de.c: In function ‘main’:
2de.c:32: error: expected declaration or statement at end of input
Than i just change the code of function which is following :
#include<stdio.h>
int* put(int **b);
int main()
{
int a[2][3],i,j;
system("clear");
put(a);
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
printf("\na[%d][%d]= %d",i,j,a[i][j]);
}
}
return 0;
}
int* put(int **b)
{
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
b[i][j]=i;
}
}
return b;
}
when i complie it i got following errors:
2de.c: In function ‘main’:
2de.c:9: warning: passing argument 1 of ‘put’ from incompatible pointer type
2de.c:3: note: expected ‘int **’ but argument is of type ‘int (*)[3]’
2de.c: In function ‘put’:
2de.c:31: warning: return from incompatible pointer type
2de.c: In function ‘main’:
2de.c:32: error: expected declaration or statement at end of input
2de.c: In function ‘main’:
2de.c:9: warning: passing argument 1 of ‘put’ from incompatible pointer type
2de.c:3: note: expected ‘int **’ but argument is of type ‘int (*)[3]’
2de.c: In function ‘put’:
2de.c:31: warning: return from incompatible pointer type
2de.c: In function ‘main’:
2de.c:32: error: expected declaration or statement at end of input
what I'm doing wrong ?
can anybody tell me what is the way to pass 2d-array via pointers to a function ?
can anybody tell me how to return two d array via pointer in a function
The first error that you have is that you are not passing a correct type as declared by your function. So to clean up your code with the least amount of corrections, it would probably look something like this:
#include<stdio.h>
void put(int *b);
int main()
{
int a[2][3],i,j;
put(&a[0][0]);
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
printf("\na[%d][%d]= %d", i, j, a[i][j]);
}
}
printf("\n\n");
system("PAUSE"); // Not recommended, but works for now
return 0;
}
void put(int *b)
{
int count = 1;
int i, j;
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
//b[i][j]=i;
*(b + ((i*3) + j)) = count++;
}
}
}
The two major corrections are:
You pass in the start address of your 2-D array explicitly by addressing it as &a[0][0].
Also, note the pointer arithmetic that you'll have to use when you use an int *b as well.
Note also that since you're passing in a pointer, you're modifying the value at that address location. Thus there is no need to return a pointer back at all.
Hope it helps. Cheers!
where are you storing the return value from put ?
the declaration should be int** put( int **) according yo your code.
The first error you have is that you are trying to define a function inside another function. The simplest thing to do is to just define put where you declare it:
int put()
{
/* definition of put */
}
int main()
{
/* body calls put */
}
The second problem is that in neither code snippet are you passing a compatible parameter to put.
If you want to pass a to a function then you should note that arrays as arguments always decay to a pointer to their first element.
a has type int [2][3], i.e. an array of 2 arrays of 3 ints. This will decay to a pointer to an array of 3 ints or int (*)[3]. This should explain the compile error that you are getting. You should declare put either as:
void put(int (*b)[3]);
or as the completely equivalent:
void put(int b[][3]);
Because you cannot pass arrays by value the compiler will automatically convert a function declaration which takes an array parameter to one which takes the equivalent pointer parameter.
I've changed the return type to void as you don't use or need the return value as you are passing the parameter by pointer. You should remove return b; from your definition of put.
Hint: Don't think of int[2][3] as a 2-d array but as an array of arrays.
You can not return a 2d-array from a function in C, you can only return a 1d-array of pointers to the first elements of a 2d-array.
Maybe you could find useful this:
Pass 2d array to function in C?
or this:
C++ Returning multidimension array from function
1.you should declare or define the function before use it,it's different with other popular luanguage.
2.you do not need to return the pointer in the function put ,the data in array has be changed
3.you needed to notice the type ,the type of int array[][] is int **

Resources