#include <stdio.h>
int main() {
int a, b,c;
/* Input a and b */
scanf("%d %d %d", &a, &b,&c);
while(a != -1) {
int *x = &a;
int *y = &b;
int *z = &c;
printf("Original inputs: a:%d\tb:%d\tc:%d\n", a, b,c);
reorder(a,b,c);
swap(a,b);
printf("Rearranged inputs: a:%d\tb:%d\tc:%d\n\n", a, b,c);
break;
}
}
void reorder(int *x, int *y, int *z){
if(*x > *y)
{
int temp = *x;
*x = *y;
*y = temp;
}else if(*y > *z){
int temp = *y;
*y = *z;
*z = temp;
}else if(*x > *z){
int temp = *x;
*x = *z;
*z = temp;
}
}
void swap(int *px, int *py)
{
int temp;
temp = *px;
*px = *py;
*py = temp;
}
I am new to C and learning pointers am not sure how to implement pointers to swap 3 numbers in ascending order
This might give you a way to start:
#include <stdio.h>
void swap(int* a, int* b) {
int temp = *a;
*a = *b;
*b = temp;
}
void reorder(int* x, int* y, int* z) {
if (*x > *y) {
swap(x, y);
}
if (*y > *z) {
swap(y, z);
}
if (*z > *x) {
swap(z, x);
}
}
int main() {
int a, b, c;
scanf("%d %d %d", &a, &b, &c);
int *x = &a;
int *y = &b;
int *z = &c;
reorder(x, y, z);
}
You can use this code for your purpose :
#include <stdio.h>
void reorder(int *, int *, int *);
void swap(int *, int *);
void main()
{
int a, b, c;
printf("Enter three numbers : ");
while (scanf("%i %i %i", &a, &b, &c)==3)
{
reorder(&a, &b, &c);
printf("Now a is %d, b is %d and c is %d.\n\n", a, b, c);
printf("Enter three numbers : ");
}
}
void swap(int* a, int* b) {
int temp = *a;
*a = *b;
*b = temp;
}
void reorder(int* a, int* b, int* c) {
if (*c<*a&&*c<*b)
swap(a, c);
if (*b<*a&&*b<*c)
swap(a, b);
if (*c<*b)
swap(b, c);
}
Related
My function is unable to compare 2 pointer values. The main function has two variables a and b. Swap function swaps the value of a and b. In my max function I cant find the bigger number between c and a. It is not working properly as a always become the bigger number.
//swap two varibales
int swap(int *a, int *b )
{
int temp = *a;
*a= *b;
*b = temp;
printf("After swap: A = %d and B = %d", *a, *b);
}
This function takes the input of c and adds all three numbers.
int additon(int *a, int *b, int *c, int *total)
{
printf(" \nInput Numbers you want: ");
scanf("%d", c);
*total = *a + *b + *c;
printf("\n\nTotal value of numbers %d, %d, %d are = %d",*a,*b, *c, *total );
}
Function which compares between a and c [problem] :
int max(int *a, int *b, int *c,int *maxNum)
{
if( a > c)
{
printf("\n\n A = %d is bigger than C = %d", *a,*c);
}
else if( a < c)
{
printf("\n\n A = %d is smaller than C = %d",*a,*c);
}
}
Main function:
int main(int *total, int *maxNum)
{
int *a = 10;
int *b = 20;
int *c;
printf("Before swap: A = %d and B = %d\n", a,b);
swap(&a, &b);
additon(&a, &b, &c, &total);
max(&a, &b, &c, &maxNum);
return 0;
}
You need to dereference the pointers. Like this:
if( *a > *c)
{
printf("\n\n A = %d is bigger than C = %d", *a,*c);
}
else if( *a < *c)
{
printf("\n\n A = %d is smaller than C = %d",*a,*c);
}
In the previous version your were comparing the addresses the pointers held.
Why my pointer code is giving me wrong output ?.
Where, my actual code is :
void fun1(int *x, int *y){
*x = 50;
*y = 100;
fun2(&x, &y);
printf("%d %d ", x, y);
}
void fun2(int *x, int *y){
*x = 6;
*y = 7;
}
void main()
{
int x = 5;
int y = 10;
fun1(&x, &y);
printf("%d %d",x,y);
}
My expected output is like this : 6 7 6 7
It's giving me output like this : 6 7 50 100
Thanks.
In fun1 the expression &x is a pointer to the pointer. It's of type int **.
You should not use the address-of operator there, since x and y already are pointers.
An easy way to get the compiler to warn you about this is to declare thje function fun2 before you use it:
// Declare fun2 prototype
void fun2(int *x, int *y);
void fun1(int *x, int *y)
{
...
}
// Define fun2 implementation
void fun2(int *x, int *y)
{
...
}
Before use void fun2(int*, int*), you must declare or define it
In funxtion fun1, the line printf("%d %d ", x, y); should be printf("%d %d ", *x, *y);, for x and y are int*
In function fun1, the line fun2(&x, &y); should be fun2(x, y); for x and y are int*
The following code could work:
#include <stdio.h>
void fun2(int *x, int *y){
*x = 6;
*y = 7;
}
void fun1(int *x, int *y){
*x = 50;
*y = 100;
fun2(x, y);
printf("%d %d\n", *x, *y);
}
int main()
{
int x = 5;
int y = 10;
fun1(&x, &y);
printf("%d %d\n",x,y);
return 0;
}
Method 1. Point To Point To Int
void fun1(int *x, int *y){
*x = 50;
*y = 100;
fun2(&x, &y);
printf("%d %d ", *x, *y);
}
void fun2(int **x, int **y){
**x = 6;
**y = 7;
}
void main()
{
int x = 5;
int y = 10;
fun1(&x, &y);
printf("%d %d",x,y);
}
I have created a function that does the euclidean division in C and putting the quotient and remainder as function arguments.
code listing 1:
#include<stdio.h>
#include<stdlib.h>
void euclidean(int a, int b, int * q, int * r);
int main(int argc, char ** argv){
int * q;
int * r;
euclidean(5,4, q, r);
printf("q = %d, r = %d", *q, *r);
return 0;
}
void euclidean(int a, int b, int * q, int * r){
q = (int*)malloc(sizeof(int));
r = (int*)malloc(sizeof(int));
*q = a/b;
*r = a % b;
//printf("q = %d, r = %d", *q, *r); //this will show
return;
}
code listing 2:
#include<stdio.h>
#include<stdlib.h>
void euclidean(int a, int b, int * q, int * r);
int main(int argc, char ** argv){
int * q;
int * r;
euclidean(5,4, q, r);
printf("q = %d, r = %d", *q, *r);
return 0;
}
void euclidean(int a, int b, int * q, int * r)
{
*q = a / b;
*r = a % b;
//printf("q = %d, r = %d", *q, *r); //this won't show
return;
}
Both version aren't working. I compiled and ran the code on windows and the program is killed on runtime without printing anything (i.e. "q = 1, r = 4"). And my guess is if I had to compile and run it on linux, the terminal would gave me a "segmentation fault" error (not sure). I really don't see why it isn't working, especially with code listing 1. For code listing 2 one can argue that since the result of the operation are some sort of constant variable inside a function in which they were created, they could not be kept at the end of the function (I need confirmation on that too). Thanks
Keep It Simple, Stupid (there is such a principle).:)
#include<stdio.h>
#include<stdlib.h>
void euclidean(int a, int b, int * q, int * r);
int main(int argc, char ** argv){
int q;
int r;
euclidean(5,4, &q, &r);
printf("q = %d, r = %d", q, r);
return 0;
}
void euclidean(int a, int b, int * q, int * r)
{
*q = a / b;
*r = a % b;
}
If you want indeed to allocate memory in the function then the code will look like
#include<stdio.h>
#include<stdlib.h>
void euclidean(int a, int b, int * *q, int * *r);
int main(int argc, char ** argv){
int * q;
int * r;
euclidean(5,4, &q, &r);
printf("q = %d, r = %d", *q, *r);
free( q );
free( r );
return 0;
}
void euclidean(int a, int b, int * *q, int * *r){
*q = (int*)malloc(sizeof(int));
*r = (int*)malloc(sizeof(int));
**q = a/b;
**r = a % b;
}
You are not getting values for listing_2 because:-
void euclidean(int a, int b, int * q, int * r)
{
*q = a / b; <<<<<<<<<<<<<<<<<
*r = a % b;
//printf("q = %d, r = %d", *q, *r); //this won't show
return;
}
Neither you have allocated memory for q,r in main nor in euclidean. You were getting error as you are trying to deference a pointer not initialized to anything.
What you want is known as pass-by-reference, which allows the callee to modify the object the caller provides.
As C is purely pass-by-value, that is simulated with pointers:
The caller passes the address of the object the callee should modify, and the callee dereferences the received pointer to modify the intended target.
#include<stdio.h>
#include<stdlib.h>
void euclidean(int a, int b, int * q, int * r)
{
// Modify the target-objects q and r point to
*q = a / b;
*r = a % b;
}
int main() {
int q, r; // Not pointers, the objects themselves!
euclidean(5, 4, &q, &r); // Passing the addresses of q and r
printf("q = %d, r = %d", q, r);
}
No dynamic allocation at all.
I am trying to make a program that orders three double numbers by increasing value using pointers.
I am able to print the double values; however, they're not in the right order for most orders.
This is what I have:
#include <stdio.h>
void sort3(double *x, double *y, double *z);
int main()
{
double x,y,z;
printf("Enter three numbers: ");
scanf("%lf %lf %lf", &x,&y,&z);
sort3(&x,&y,&z);
return 0;
}
void sort3(double *x, double *y, double *z)
{
double temp, temp2, temp3, temp4, temp5, temp6, temp7;
if (y<x && y<z && z>x) // MSL
{
temp = *x;
*x = *y;
*y = temp;
printf("The order sequence is: %.1lf %.1lf %.1lf \n", *x, *y, *z);
}
else if (z<x && (x>y) && (y>z)){ // LMS
temp2 = *z;
*z = *x;
*x = temp2;
printf("The order sequence is: %.1lf %.1lf %.1lf \n", *x, *y, *z);
}
else if(z>y && y<x && x>z ) { // LSM
temp3 = *z;
*z = *x;
*x = temp3;
temp4 = *x;
*x = *y;
*y = temp4;
printf("The order sequence is: %.1lf %.1lf %.1lf \n", *x, *y, *z);
}
else if(z>x && y>z && y>x ) { // SLM
temp5 = *z;
*z = *y;
*y = temp5;
printf("The order sequence is: %.1lf %.1lf %.1lf \n", *x, *y, *z);
}
else if(x>z && y>z && y>x ){ // MLS
temp6 = *x;
*x = *y;
*y = temp6;
temp7 = *y;
*y = *x;
*x = temp7;
printf("The order sequence is: %.1lf %.1lf %.1lf \n", *x, *y, *z);
}
else{
printf("The order sequence is: %.1lf %.1lf %.1lf \n", *x, *y, *z);
} //SML
}
I am not sure where the problems are and how and how to fix them.
void swap(double *x, double *y){
double t = *x;
*x = *y;
*y = t;
}
void sort3(double *x, double *y, double *z){
if(*x > *y)
swap(x, y);
if(*x > *z)
swap(x, z);
if(*y > *z)
swap(y, z);
printf("The order sequence is: %.1lf %.1lf %.1lf \n", *x, *y, *z);
}
You are comparing pointers in your ifs. Change your comparison to use *x, *y, *z. Since they initially come from the stack(declared x,y,z in order), the order of the pointers value is always the same(already sorted).
However in your last case, you are swapping x and y 2 times so you end up not sorting.
Not directly an answer to the original question, but a simpler way to implement sort3():
void
sort3 (int *a, int *b, int *c)
{
int min, mid, max;
if (*a <= *b) {
if (*a <= *c) {
min = *a;
if (*b <= *c) {
mid = *b;
max = *c;
}
else {
mid = *c;
max = *b;
}
}
}
else
sort3(b, a, c);
*a = min;
*b = mid;
*c = max;
}
Here is the full code:
#include <stdio.h>
#include <stdlib.h>
void
sort3 (int *a, int *b, int *c)
{
int min, mid, max;
if (*a <= *b) {
if (*a <= *c) {
min = *a;
if (*b <= *c) {
mid = *b;
max = *c;
}
else {
mid = *c;
max = *b;
}
}
}
else
sort3(b, a, c);
*a = min;
*b = mid;
*c = max;
}
void
sort_print(int a, int b, int c)
{
printf("Before: %d, %d, %d\n", a, b, c);
sort3(&a, &b, &c);
printf("After: %d, %d, %d\n", a, b, c);
}
int
main(void)
{
sort_print(1, 2, 3);
sort_print(1, 3, 2);
sort_print(2, 1, 3);
sort_print(2, 3, 1);
sort_print(3, 1, 2);
sort_print(3, 2, 1);
exit(EXIT_SUCCESS);
}
And output:
$ ./a.out
Before: 1, 2, 3
After: 1, 2, 3
Before: 1, 3, 2
After: 1, 2, 3
Before: 2, 1, 3
After: 1, 2, 3
Before: 2, 3, 1
After: 1, 2, 3
Before: 3, 1, 2
After: 1, 2, 3
Before: 3, 2, 1
After: 1, 2, 3
Are you looking for something like this
void d_swap(double *a, double *b)
{
double tmp = *a;
*a = *b;
*b = tmp;
}
void sort3(double *x, double *y, double *z)
{
if (*x > *y) {
if (*x < *z) {
if (*y > *z) {
d_swap(y, z);
}
} else {
d_swap (x,z);
d_swap (y,z);
}
} else if (*y < *z) {
if (*x < *z) {
d_swap(x, y);
} else {
d_swap (x,y);
d_swap (y,z);
}
} else {
d_swap (x, z);
}
printf("The order sequence is: %.1lf %.1lf %.1lf \n", *x, *y, *z);
}
Not pretty but easy to understand :-p
i have code to array of func pointer
#include <stdio.h>
int sum(int a, int b);
int subtract(int a, int b);
int mul(int a, int b);
int div(int a, int b);
int (*p[4]) (int x, int y);
int main(void)
{
int result;
int i, j, op;
p[0] = sum; /* address of sum() */
p[1] = subtract; /* address of subtract() */
p[2] = mul; /* address of mul() */
p[3] = div; /* address of div() */
printf("Enter two numbers: ");
scanf("%d %d", &i, &j);
printf("0: Add, 1: Subtract, 2: Multiply, 3: Divide\n");
do {
printf("Enter number of operation: ");
scanf("%d", &op);
} while(op<0 || op>3);
result = (*p[op]) (i, j);
printf("%d", result);
return 0;
}
int sum(int a, int b)
{
return a + b;
}
int subtract(int a, int b)
{
return a - b;
}
int mul(int a, int b)
{
return a * b;
}
int div(int a, int b)
{
if(b)
return a / b;
else
return 0;
}
code for array of pointer to function:
#include <stdio.h>
int sum(int, int);
int product(int, int);
int subtract(int, int);
int main()
{
int i = 0;
int a = 10;
int b = 5;
int result = 0;
int (*pfun[3])(int, int);
pfun[0] = sum;
pfun[1] = product;
pfun[2] = subtract;
for( i = 0 ; i < 3 ; i++)
{
result = pfun[i](a, b);
printf("\nresult = %d", result);
}
result = pfun[1](pfun[0](a, b), pfun[2](a, b));
printf("\n\nThe product of the sum and the subtract = %d\n",result);
}
int sum(int x, int y)
{
return x + y;
}
int product(int x, int y)
{
return x * y;
}
int subtract(int x, int y)
{
return x - y;
}
now how to combine this two program. such that array of pointers pointing to func pointers and the func pointers may have different number of args? any suggestion.
You not only need to store function pointers with a variable number of arguments (that is not very difficult, you could use a union for instance), but you also need to make sure you call the functions with the correct argument, and that is a bit trickier given your design.
I suggest to use a stack instead. All your functions would only take the stack as an argument:
void sum(stack_t *stack);
void subtract(stack_t *stack);
void product(stack_t *stack);
And your array could be declared this way:
typedef void callback_t(stack_t *);
callback_t *p[] =
{
sum,
subtract,
product,
/* ... */
};
Then for instance sum would be implemented as such:
void sum(stack_t *stack)
{
if (depth(stack) < 2)
perror("Not enough arguments in stack!");
int b = popstack(stack);
int a = popstack(stack);
int c = a + b;
pushstack(stack, c);
}
But unary minus would be implemented this way:
void neg(stack_t *stack)
{
if (depth(stack) < 1)
perror("Not enough arguments in stack!");
int a = popstack(stack);
pushstack(stack, -a);
}
Each function decides how many arguments they need. The caller does not need to know.