I just started learning, I didn't understand the book, so I asked for advice.
I am a beginner and don't have a good English.
Function: Combine two two-digit positive integers A and B to form an integer in C
Middle. The method of merging is: the ten digits and single digits of the A number are placed on the thousand and ten digits of the C number, and the ten and single digits of the B number are placed on the single and hundred digits of the C number.
For example: when a=45, b=12. After calling this function, c=4251.
Here is my code
#include <stdio.h>
void fun(int a, int b, long *c);
int main()
{
int a,b;
long c;
int state = 1;
printf("Enter a: ");
printf("(q to quit)");
while( scanf("%d",&a)==state)
{
printf("Enter b: ");
printf("(q to quit)");
while( scanf("%d",&b)==state)
{
fun(a, b, &c);
printf("The result is: %ld\n", c);
}
}
return 0;
}
void fun(int a, int b, long *c)
{
/**********Program**********/
*c = 100*(a%100)+b%100;
/********** End **********/
}
I tried removing the * and found that the result was 16. It is wrong but not know why
The parameter long *c means c is an address of a variable (in this case the variable in main() is called c and you need to call the function like this fun(a, b, &c)).
When you want to update the value stored at that address c the syntax is *c = .... If you do c = ... you are updating the address of the variable which has no external effect.
Alternatively, you could change your function to return a value and the call would then look like this:
c = fun(a, b);
and the function would be:
int fun(int a, int b) {
return 100*(a%100)+b%100;
}
Related
I'm working on my intro C course assignment and I'm tasked with the following...
Write code for a function that receives two parameters (a,and b) by value and has two more parameters (c and d) by reference. All parameters are double.
From main, use scanf to get two numbers, then call the function, and then display both returned values to the output in a printf statement.
The function works by assigning (a/b) to c and assigning (a*b) to d.
While my knowledge is basic, I believe I understand the gists
In main
//first and second double hold the scanf inputs
double first;
double second;
//unsure here - to reference c and d as parameters in the function, do I simply declare unfilled double variables here?
double *c;
double *d;
printf("Enter your first number\n");
scanf("%f\n", &first);
printf("Enter your second number\n");
scanf("%d\n", &second);
//call the function, first and second by value, &c / &d by reference - correct?
pointerIntro(first, second, &c, &d);
For the function...
float myFunction(double a, double b, double *c, double *d)
{
c = a/b;
d = a*b;
//printf statements
}
I apologize if the flow of this question is messy but its part of the process for me :P
So, for my formal questions
1. is it correct to initiate two double pointer variables (*c & *d) in main to be passed as reference in the function?
2. Am I right to call the function with the reference pointers by saying &c / &d?
3. Any other critiques of this questioning?
Variables 'c' and 'd' don't have to be pointers to pass them by reference. So you have two cases:
When you define 'c' and 'd' as pointers in main function you will pass them to function like this: pointerIntro(first, second, c, d); because they are already pointers and you don't need to send their reference, you just send them.
If you define 'c' and 'd' just as double variables double c, d; you will send them to the function by reference using '&' symbol like this: pointerIntro(first, second, &c, &d);.
Then in your function to actually set the values of 'c' and 'd' you will need to dereference the pointer to them like this: *c = a/b; *d = a*b;.
If you are not familiar you can check what dereferencing the pointer means here: What does "dereferencing" a pointer mean?
Code that should work:
#include <stdio.h>
void myFunction(double a, double b, double *c, double *d)
{
*c = a / b;
*d = a * b;
}
int main(void)
{
double a, b, c, d;
scanf("%lf", &a);
scanf("%lf", &b);
myFunction(a, b, &c, &d);
printf("%lf %lf", c, d);
}
You need to dereference c and d to assign them in myFunction():
*c = a/b;
*d = a*b;
Moreover from main, you need to create instances to which c and d refer to:
double c;
double d;
(you had created unitialised pointers with no instances).
Then in the the call:
pointerIntro(first, second, &c, &d);
The & address-of operator creates the reference arguments, referring to c and d in main().
It might be useful not to use the same symbol names c and d in main() and myFunction() to make it clearer what they are. For example:
void myFunction(double a, double b, double* cptr, double* dptr )
{
*cptr = a/b;
*dptr = a*b;
}
Note also, a function that returns nothing should be declared void.
A pointer is simply a normal variable that holds the address of something else as its value. In other words, a pointer points to the address where something else can be found. Where you normally think of a variable holding an immediate values, such as int a = 40;, a pointer (e.g. int *p = &a;) would simply hold the address where 40 is stored in memory.
If you need to access the value stored at the memory address pointed to by p, you dereference p using the unary '*' operator, e.g. int j = *p; will initialize j = 40).
If you want to obtain a variables address in memory, you use the unary '&' (address of) operator. If you need to pass a variable as a pointer, you simply provide the address of the variable as a parameter.
In your case that boils down to writing your function similar to:
void multdiv (double a, double b, double *c, double *d)
{
*c = a / b;
*d = a * b;
}
(note: you should ensure b != 0 before dividing a / b -- that is left to you)
In order to provide storage for the values you will take as input as well as the values you want to hold the results of the multiplication and division, you need four double values, e.g.
int main (void) {
double a, b, c, d; /* your doubles */
You then need to prompt the user for input and validate the user input by checking the return of the input function used, e.g.
fputs ("enter two double values: ", stdout); /* prompt */
fflush (stdout); /* flush stream (optional but recommended) */
if (scanf ("%lf %lf", &a, &b) != 2) { /* validate EVERY input */
fputs ("error: invalid double values.\n", stderr);
return 1;
}
(bonus: Why is the '&' used before a and b with scanf above? See first full paragraph under DESCRIPTION in man 3 scanf)
All that remains is to call your function to perform the calculations and output the results, e.g.:
multdiv (a, b, &c, &d); /* calculate c, d */
printf ("\nc = a / b => %.2f\nd = a * b => %.2f\n", c, d);
(note: as explained above the address of operator is used to pass the address of c and d as parameters)
Putting it altogether you could do:
#include <stdio.h>
void multdiv (double a, double b, double *c, double *d)
{
*c = a / b;
*d = a * b;
}
int main (void) {
double a, b, c, d; /* your doubles */
fputs ("enter two double values: ", stdout); /* prompt */
fflush (stdout); /* flush stream (optional but recommended) */
if (scanf ("%lf %lf", &a, &b) != 2) { /* validate EVERY input */
fputs ("error: invalid double values.\n", stderr);
return 1;
}
multdiv (a, b, &c, &d); /* calculate c, d */
printf ("\nc = a / b => %.2f\nd = a * b => %.2f\n", c, d);
return 0;
}
Example Use/Output
$ ./bin/multdivbyptrs
enter two double values: 4.0 2.0
c = a / b => 2.00
d = a * b => 8.00
Look things over and let me know if you have questions. Once you digest that a pointer is simply a normal variable that holds the address where something else is stored as its value -- things will start to fall into place. No magic..
It is perfectly valid. You can initialize and pass any number of pointer variables with their reference.
This is also valid..when you pass the variable address, you should store it into a pointers
you have to do some changes in your code,
You can assign directly a/b and a*b pointer variables *c & *d
Then you have to read double number with %lf format argument.
#include <stdio.h>
#include <string.h>
void myFunction(double a, double b, double *c, double *d)
{
*c = a/b; //change
*d = a*b; //change
printf("%lf %lf",*c,*d);
return;
//printf statements
}
int main()
{
//first and second double hold the scanf inputs
double first;
double second;
//unsure here - to reference c and d as parameters in the function, do I simply declare unfilled double variables here?
double *c;
double *d;
printf("Enter your first number\n");
scanf("%lf", &first); //change
printf("Enter your second number\n");
scanf("%lf", &second); //change
//call the function, first and second by value, &c / &d by reference - correct?
myFunction(first, second, &c,&d);
}
Here is the correct way to do it. The func() method
void func(double x,double y,double *z,double *w){
printf("pass by value = %lf, %lf",x,y);
printf("pass by reference = %lf, %lf",*z,*w);
}
The main() method
int main(void){
double first,second,val1,val2;
val1=3.1;
val2=6.3;
printf("Enter your first number\n");
scanf("%lf", &first);
printf("Enter your second number\n");
scanf("%lf", &second);
func(first,second,&val1,&val2);
}
I want to Prompt the user to enter 3 numbers. Then, swap the first number with the second one, the second number with the third and the third with the first by calling a function called "swap".
Functions in C cannot return more than one value so I decided to create a structure with pointers that I will later use in my function. Then, I created three-pointers that will store the address of each one of the numbers so I can dereference to these numbers in my function (as shown below)
Number.pa = *ppb;
Number.pb = *ppc;
Number.pc = *ppa;
Here's my code:
#include <stdio.h>
void swap(); // a = b, b = c, c = a
struct Numbers {
int *pa, *pb, *pc;
} ;
int main(void) {
struct Numbers Number; // Structure to hold the values of the three variables.
int a, b, c;
int *ppa, *ppb, *ppc;
printf("\n Please enter three integer numbers: ");
scanf("%d%d%d", &a, &b, &c);
ppa = &a; ppb = &b; ppc = &c;
swap(a, b, c, Number, *ppa, *ppb, *ppc);
printf("\n %d \t %d \t %d \n", Number.pa, Number.pb, Number.pc);
}
void swap(int a, int b, int c, struct Numbers Number, int *ppa, int *ppb, int *ppc) {
Number.pa = *ppb;
Number.pb = *ppc;
Number.pc = *ppa;
} ;
Most of the arguments to your swap function are either pointless or the work of sheer guessing (or both). The assignment effectively wants you to "rotate" values from a through c. So do that, and only that.
#include <stdio.h>
void swap(int *pa, int *pb, int *pc);
int main()
{
int a, b, c;
printf("\n Please enter three integer numbers: ");
if (scanf("%d %d %d", &a, &b, &c) == 3)
{
swap(&a, &b, &c);
printf("%d %d %d \n", a, b, c);
}
return 0;
}
void swap(int *pa, int *pb, int *pc)
{
int tmp = *pa;
*pa = *pb;
*pb = *pc;
*pc = tmp;
}
Stop reading more into an assignment than is there. If it sounds simple, it probably is. The warning was due to passing the value of dereferenced pointers to int (so int values) to a function expecting int pointers; not int values. As you can see, you don't need to do any of that (and didn't in the first place).
I have a question in C where I need to insert coefficients of a quadratic equation into a function and return the number of solutions and result.
Write a program that accepts a series of 3 real numbers, which are the
coefficients of a quadratic equation, and the program will print out
some solutions to the equation and the solutions themselves.
Guidelines:
Functions must be worked with one of the functions that
returns the number of solutions as a returned value, and returns the
solutions themselves through output parameters.
3 numbers must be
received each time. The input will be from a file (will end in EOF)
In the meantime I built the function without reading from a file just to see that it works for me, I built the function that returns the number of solutions but I got entangled in how to return the result as output parameter
here is my code for now:
int main ()
{
double a, b, c, root1,root2,rootnum;
printf("Enter coefficients a, b and c: ");
scanf("%lf %lf %lf",&a, &b, &c);
rootnum=(rootnumber(a,b,c);
printf("the number of roots for this equation is %d ",rootnum);
}
int rootnumber (double a,double b, double c)
{
formula=b*b - 4*a*c;
if (formula<0)
return 0;
if (formula==0)
return 1;
else
return 2;
}
In C, providing an "output parameter" usually amounts to providing an argument that is a pointer. The function dereferences that pointer and writes the result. For example;
int some_func(double x, double *y)
{
*y = 2*x;
return 1;
}
The caller must generally provide an address (e.g. of a variable) that will receive the result. For example;
int main()
{
double result;
if (some_func(2.0, &result) == 1)
printf("%lf\n", result);
else
printf("Uh oh!\n");
return 0;
}
I've deliberately provided an example that illustrates what an "output parameter" is, but has not relationship to the code you actually need to write. For your problem, you will need to provide two (i.e. a total of five arguments, three that you are providing already, and another two pointers that are used to return values to the caller).
Since this is a homework exercise, I won't explain WHAT values your function needs to return via output parameters. After all, that is part of the exercise, and the purpose is for you to learn by working that out.
Apart from a wayward parenthesis in the call and some other syntax errors, what you have so far looks fine. To print out the number of roots, you need to put a format specifier and an argument in your printf statement:
printf("the number of roots for this equation is %d\n", rootNum);
The %d is the format specifier for an int.
Here is your working code:
#include <stdio.h>
int rootnumber (double a,double b, double c)
{
double formula = (b*b) - (4*(a)*(c));
if (formula > 0) {
return 2;
}
else if (formula < 0) {
return 0;
}
else {
return 1;
}
}
int main (void)
{
double a, b, c;
printf("Enter coefficients a, b and c: ");
scanf("%lf %lf %lf",&a, &b, &c);
printf("The number of roots for this equation is %d ", rootnumber(a,b,c));
return 0;
}
It just need some sanity checking, its working now:
#include<stdio.h>
int rootnumber(double a, double b, double c);
int main ()
{
double a, b, c, root1,root2;
int rootnum;
printf("Enter coefficients a, b and c: ");
scanf("%lf %lf %lf",&a, &b, &c);
rootnum=rootnumber(a,b,c);
printf("the number of roots for this equation is %d", rootnum);
return 0;
}
int rootnumber(double a, double b, double c)
{
int formula= (b*b) - (4*a*c);
if (formula<0)
return 0;
if (formula==0)
return 1;
else
return 2;
}
I use the return command then try to print the value from the main. It returns a value of zero (0).
This program is about temperature conversion from Celsius to Fahrenheit.
Also how can you use a rounding function to round the answer to an integer so it is not a floating point number with decimals.
#include <stdio.h>
int Cel_To_Fah(int a, int b); // function declaration
int main (void)
{
int a;
int b;
printf(" Enter temperatrure: "); scanf("%d", &a);
Cel_To_Fah(a,b); // function call
printf("The temperature is: %d\n", b);
return 0;
} // main
int Cel_To_Fah(a,b)
{
b=1.8*a+32;
return b;
} // Cel_To_Fah
You just have to use the assignment operator:
b = Cel_To_Fah(a);
Your program has a lot of problems, though, including your Cel_To_Fah function not having a correct signature. You probably want something like:
int Cel_To_Fah(int a)
{
return 1.8 * a + 32;
}
You should probably get a good beginner C book.
No need of second argument to function(b).
You can do this by...
#include<stdio.h>
int Cel_To_Fah(int a); // function declaration, as it returns a values;
int main (void)
{
int a; int b;
printf(" Enter temperatrure: ");
scanf("%d", &a);
b = Cel_To_Fah(a); /* the returned value is stored into b, and as b is an integer so it is automatically rounded. no fraction point value can be stored into an integer*/
printf("The temperature is: %d\n", b);
return 0;
} // main
int Cel_To_Fah(int a)
{
return 1.8 * a + 32;
}
there are several issues. First you need to use float, not int, so that you can have values with a decimal point. otherwise your calculations will come out wrong. Also use 32.0 instead of 32 for the same reason.
Second, you need to understand that the a and b in your function are NOT the same as the a and b in main. They have the same name but are not in the same "scope". So changing the one in your function doesn't affect the one in main. That's why in main you have to say b=Cel... so that b in main will get the returned value.
finally, in c, you're supposed to put your functions above/before main. Otherwise it's technically not defined "yet", though some modern compilers will fix that for you. Read about function prototypes.
Since your function Cel_To_Fah(a,b); is returning a value (int type), you must have to assign it to a variable of its return type (int type).
int a;
int b;
printf(" Enter temperatrure: "); scanf("%d", &a);
b = Cel_To_Fah(a); // function call
printf("The temperature is: %d\n", b);
and your function should be
int Cel_To_Fah(a)
{
int b = 1.8*a+32;
return b;
} // Cel_To_Fah
And do not forget to change your function prototype to
int Cel_To_Fah(int a);
I saw two issues in your code. Firstly, it is variable type. I assume that you want Celsius as integer; but Fahrenheit = 1.8*Celsius+32 should be float. Therefore b should be float.
Secondly, you should not return a value from a function via its input parameters (unless you learn pointer or call by ref). I rewrite your code as following:
include<stdio.h>
float Cel_To_Fah(int a); // function declaration
int main (void)
{
int a;
float b;
printf(" Enter temperatrure: "); scanf("%d", &a);
b=Cel_To_Fah(a); // function call
printf("The temperature is: %.2f\n", b); //showing 2 decimal places
return 0;
} // main
float Cel_To_Fah(int a)
{
float b;
b=1.8*(float)a+32; //cast a from int to float
return b;
} // Cel_To_Fah
void getnums(int *a, int *b);
int main()
{
int a;
int b;
int c;
getnums(&a,&b);
c = a + b;
printf("a + b = %d\n", c);
return 0;
}
void getnums(int *a, int *b)
{
printf("a:? ");
scanf("%d", a);
printf("b:? ");
scanf("%d", b);
}
Why don't I need ampersands before the a and b in the scanfs? (The code currently works.)
Because scanf takes pointers as its arguments (so that it knows what variable to modify), and a and b are already pointers.
Whenever we scan some input it needs a memory location(i.e. address) to store that value, for simple variables we have to use & - ampersand - to provide that address.
Here, in function getnums, a and b are pointers so they will already contain address, so no need to write & to give the address.