Wiki User
∙ 12y ago#include <stdio.h> #include <conio.h> #include <math.h> void main() { float a, b, c; float root1, root2,de,d; printf("enter the values of a, b and c"); scanf("%f%f%f", &a,&b,&c); de=(b*b)-4*(a*c); d=sqrt(de); root1=(-b+d) /(2.0*a); root2=(-b-d) /(2.0*a); printf(" roots of the equation are %f %f", root1,root2); getch(); }
Wiki User
∙ 12y agoHere is an example of a C program that calculates the roots of a quadratic equation using two pointer parameters:
#include <stdio.h>
#include <math.h>
void calculateRoots(float a, float b, float c, float* root1, float* root2) {
float discriminant;
discriminant = b*b - 4*a*c;
if(discriminant > 0) {
*root1 = (-b + sqrt(discriminant)) / (2*a);
*root2 = (-b - sqrt(discriminant)) / (2*a);
printf("Roots are real and different.\n");
} else if(discriminant == 0) {
*root1 = *root2 = -b / (2*a);
printf("Roots are real and the same.\n");
} else {
printf("Roots are complex and different.\n");
}
}
int main() {
float a, b, c, root1, root2;
printf("Enter coefficients a, b, and c: ");
scanf("%f %f %f", &a, &b, &c);
calculateRoots(a, b, c, &root1, &root2);
printf("Root 1: %.2f\n", root1);
printf("Root 2: %.2f\n", root2);
return 0;
}
In this program, the calculateRoots
function takes the coefficients a
, b
, and c
of the quadratic equation as well as two pointer parameters root1
and root2
to store the roots. It calculates the roots using the quadratic formula and updates the values pointed to by the pointers. The main
function prompts the user to enter the coefficients, calls the calculateRoots
function, and then prints the roots.
In C++, parameters are variables declared in the function's declaration and definition that receive values passed in from the function call. They are used to pass values or data into a function to be used within the function's code. Parameters allow functions to be more flexible and reusable by accepting different inputs without needing to modify the function's code.
// declare a function int* function(int, int); or int* (function)(int, int); // declare a pointer to a function int* (*pointer_to_function)(int, int);
Example: int x; -- integer int *px= &x; -- pointer to integer int **ppx= &px; -- pointer to pointer to integer int ***pppx= &ppx; -- pointer to pointer to pointer to integer
void is type of pointer that usually means that you can make it point to any data type. When you make a pointer point to somewhere its data type should match with the place where you want it to point. When you dont know the data type where it will point to then you can declare a void pointer and make it point to the data type it want.
Pointer to Pointer is a double pointer, denoted by (**). Pointer stores the address of the variable and pointer to pointer stores the address of a pointer variable and syntax can be given as int **ptr2ptr;
In C++, parameters are variables declared in the function's declaration and definition that receive values passed in from the function call. They are used to pass values or data into a function to be used within the function's code. Parameters allow functions to be more flexible and reusable by accepting different inputs without needing to modify the function's code.
When a variable is passed by value, the function receives a copy of the variable. When a variable is passed by reference, the function receives a reference, or pointer, to the original data.
// declare a function int* function(int, int); or int* (function)(int, int); // declare a pointer to a function int* (*pointer_to_function)(int, int);
You don't use 'VOID', but 'void'. It means different things, such as:- void as function type means no return value- void as function parameter means no parameters- 'void *' as pointer-types means generic pointer
Overloading refers to defining multiple functions of the same name with different numbers/types of parameters. So no, you cannot overload a pointer.
The this operator is not a c operator. It is a c++ keyword. It is equivalent to an r-value pointer to the current instance of an object. It is useful when resolving between object members and method parameters.
No. Function parameters are passed by value. Always. Even the so called "call by reference" is a value - the value of the pointer or the address of the object - but what is placed in the parameter list is a value.
1. pointer to a constant means you can not change what the pointer points to 2. constant pointer means you can not change the pointer.
Example: int x; -- integer int *px= &x; -- pointer to integer int **ppx= &px; -- pointer to pointer to integer int ***pppx= &ppx; -- pointer to pointer to pointer to integer
A pointer only holds an address information (location) in the memory. if a pointer holds points another pointer then it is a pointer to an other pointer. Pointer holds an address in the memory so in that address there is an other location information that shows another location.
pointer in C have following advantages- 1.Pointer is a very useful concept for creating the important C data structures i.e. linked list, stack, queues and trees, which are very powerful in certain situations. 2.Pointers are very useful when we have to reflect more than one variable change in the calling function after call takes place i.e. though we can not return more than one value from a called function but we can always pass references (pointer variables) to the variables in calling function as parameters to the function.So all the manipulation using these pointer variables will be reflected in called funtion. 3.Pointer provides a lower level view of memory, it adds to our understanding of the things going on in your computer memory.
void is type of pointer that usually means that you can make it point to any data type. When you make a pointer point to somewhere its data type should match with the place where you want it to point. When you dont know the data type where it will point to then you can declare a void pointer and make it point to the data type it want.