Pointers and interchange with C - c

I have a general idea of how to do this, as you can see by the code I did below. The only problem I am having is finishing the interchange part. Basically, what I am trying to do is move the value of the lowest variable into the 1st variable, the second middle value to the 2nd variable, and the biggest to the 3rd variable.
I know I do this with the interchange and using temp somehow, but how would I complete that, because with three values temp would get overridden somehow. What am I missing? So basically a = 4.0, b = 7.0, c = 1.0, c (1.0) needs to go into a, a (4.0) needs to go into b, and b (7.0) needs to go into c.
Thanks!
#include <stdio.h>
void interchange(double * x, double * y, double * z);
int main(void)
{
double a = 4.0, b = 7.0, c = 1.0;
printf_s("Originally a = %d, b = %d, and c = %d.\n", a, b, c);
interchange(&a, &b, &c);
printf_s("Now, a = %d, b = %d, and c = %d.\n", a, b, c);
return 0;
}
void interchange(double * x, double * y, double * z)
{
double temp;
temp = *z;
*y = *z;
* = temp
// what am I missing here? I cant get my head around this above ^^^
}
Thanks for the guidance!

Something like:
void swap(double* first, double* last){
double temp = *first;
*first = *last;
*last = temp;
}
void interchange(double * x, double * y, double * z){
if(*x > *y) swap(x, y);
if(*y > *z) swap(y, z);
if(*x > *y) swap(x, y);
}

the simplest way is:
if (*x > *y) {
temp = *x; // store value of x
*x = *y; // overwrite x with y
*y = temp; // overwrite y with x (which is in temp)
}
// now we sure that x <= y
if (*y > *z) {
temp = *z;
*z = *y;
*y = temp;
}
// now we sure that x <= z and y <= z, but we don't know x/y relationships
if (*x > *y) {
temp = *x;
*x = *y;
*y = temp;
}
// now x <= y <= z

Related

C - Passing pointer as an argument to a swap function

#include<stdio.h>
void swap (char *x, char *y)
{
char *t = x;
x = y;
y = t;
}
int main()
{
char *x = "geeksquiz";
char *y = "geeksforgeeks";
char *t;
swap(x, y);
printf("(%s, %s)", x, y);
t = x;
x = y;
y = t;
printf("n(%s, %s)", x, y);
return 0;
}
I would expect that the original pointers would swap but it isn't the case here even though i pass the pointer to the function, The reason for it is that it makes local pointers ?
How do i swap the original pointers using the function?
The reason for it is that it makes local pointers ?
Yes. x, y and t are all local variables and x and y are copies of the original pointers.
How do i swap the original pointers using the function?
Swapping pointers probably doesn't make any sense, but if you some reason need to do it, then:
void swap (char** x, char** y)
{
char* t = *x;
*x = *y;
*y = t;
}
If you want to swap pointers you need to pass references to them (pointer to pointer)
void swap (char **x, char **y)
{
char *t = *x;
*x = *y;
*y = t;
}
usage:
swap(&x, &y);
or you can define the macro:
#define SWAP(x,y,type) do {type t; t = (x); (x) = (y); (y) = t; } while(0)
usage:
SWAP(x,y, char *);
or
#define SWAP(x,y) do {const void * t; t = (const void *)(x); (x) = (y); (y) = t; } while(0)
usage:
SWAP(x,y);

How can I fix the swapBack function to get the result I intended?

I am trying to implement a swap back function that will swap the values back to the original position. This is the instruction I have to do accordingly:
Implement a second swapping
function, swapBack(int v[2]), that takes the vector v = [x, y] as an input and swaps its entries. Includes
the new function into the program above to swap back the values of x and y.
#include <stdio.h>
void swap(int *pX, int * pY){
int temp = *pX;
*pX = *pY;
*pY = temp;
}
void swapBack(int x, int y){
swap(&x,&y);
}
int main() {
int x = 10;
int y = 5;
swap(&x, &y);
printf("x = %d, y = %d\n", x, y);
swapBack(&x, &y);
printf("x = %d, y = %d\n", x, y);
return x + y;
}
However, both printf produce the same results and I am not sure how to fix it? thanks
You're defining swapBack wrong. It's supposed to take an array (which decays to a pointer) of two integers.
This should be about what you want:
void swapBack(int v[2])
{
int temp = v[0];
v[0] = v[1];
v[1] = temp;
}
A program to test this:
#include <stdio.h>
void swapBack(int v[2])
{
int temp = v[0];
v[0] = v[1];
v[1] = temp;
}
int main(void)
{
int v[2] = {10, 5};
printf("v[0] = %d, v[1] = %d\n", v[0], v[1]);
swapBack(v);
printf("v[0] = %d, v[1] = %d\n", v[0], v[1]);
printf("v[0] + v[1] = %d\n", v[0] + v[1]);
return 0;
}

Extended Euclidean algorithm convention [In C]

I was looking for a Extended Euclidean algorithm in C. I found the following piece of code:
int gcdExtended(int a, int b, int *x, int *y)
{
if (a == 0)
{
*x = 0;
*y = 1;
return b;
}
int x1, y1;
int gcd = gcdExtended(b%a, a, &x1, &y1);
*x = y1 - (b/a) * x1;
*y = x1;
return gcd;
}
so for gcdExtended(0,0) it will return: x=0 and y=1. But I saw some other versions of the algorithem which return x=0 and y=0. Both mathematically are true. But, is there a convention for this issue?

Function changes pointer arguments, but calling side can't see change

i want to understand the nature of pointers work. And have a simple example of swap function. First is working as expected, the second fails.
I can't understand the second function swap2(). I'm swapping the addresses successfully but after the function exit the values stay the same...
Why?
void swap1(int *x, int *y);
void swap2(int *x, int *y);
void startSwapExample() {
int a = 10;
int b = 20;
printf("a = %i, b = %i\n", a, b);
swap1(&a,&b);
printf("a = %i, b = %i\n", a, b);
printf("\n================\n");
int c = 10;
int d = 20;
printf("c = %i, d = %i\n", c, d);
swap2(&c,&d);
printf("c = %i, d = %i\n", c, d);
}
void swap1(int *x, int *y) {
printf("x = %i, y = %i\n", *x, *y);
int temp = * x;
* x = * y;
* y = temp;
printf("x = %i, y = %i\n", *x, *y);
}
void swap2(int *x, int *y) {
printf("x = %i, y = %i\n", *x, *y);
int * temp = x;
x = y;
y = temp;
printf("x = %i, y = %i\n", *x, *y);
}
The output of the program:
a = 10, b = 20
x = 10, y = 20
x = 20, y = 10
a = 20, b = 10
================
c = 10, d = 20
x = 10, y = 20
x = 20, y = 10
c = 10, d = 20
Process finished with exit code 0
In swap2, you are swapping the pointer x and y, both of which only lives within the scope of the swap2 function.
When the function returns, pointer x and y no longer exist and whatever operation you did on the pointer (as opposed to the value pointed by the pointer) has no effect.
swap2() function is not working according to your expectation, reason is below.
void swap2(int *x, int *y) {
printf("x = %i, y = %i\n", *x, *y);
int * temp = x;
x = y;// x holding y
y = temp;// y holding x but not in main() function, only in swap2()
printf("x = %i, y = %i\n", *x, *y); // here you are getting expected output but not in main() function
}
in swap2() function it's swapping but modification is not affecting in main() function because temp as well as x & y are local pointers & and you are not de-referencing anything , so in main() it will be same.

segmentation fault (core dumped) in recursive algorithm

This is a program to calculate a definite integral using numerical quadrature method (I don't know if this is the right translation):
#include <math.h>
#include <stdio.h>
float f(float x){
float y;
y = 4/(+x*x);
return y;
}
void quadra(float a, float b, float *Q, float *E, float f(float)){
float q1, q2, m, h, fa, fb;
h = b - a;
fa = f(a);
fb = f(b);
q1 = (fa+fb)*h/2.;
m = (a+b)/2.;
q2 = ( (fa+2*f(m)+fb) ) *h/4;
*Q = q2;
*E = fabs(q2-q1)/3;
}
void scambia(float *x, float *y) {
float z;
z = *x;
*x = *y;
*y = z;
return;
}
void sort(float x[], int n) {
int flag=1, k=n-1, i;
while (flag == 1 && k > 0) {
flag = 0;
for (i=0; i<k; i++) {
if (x[i]>x[i+1]) {
scambia(&x[i], &x[i+1]);
flag = 1;
}
}
k = k-1;
}
return;
}
int intautri(float A, float B, float TOL, int MAXFUN, float *Q, float *E, int *N, float FUN(float)){
void sort(float [], int);
void quadra(float, float, float*, float*, float(float));
float Q1,Q2,c,d,Iold,Eold,E0,E1,E2,alist[100],blist[100],qlist[100],elist[100];
int n, flag;
quadra(A, B, &Q1, &E0,FUN);
*N = 3;
n = 1;
alist[n] = A;
blist[n] = B;
qlist[n] = Q1;
elist[n] = E0;
*Q = Q1;
*E = E0;
if (*E<=TOL || *N>=MAXFUN){
flag = 1;
}else{
c = alist[n];
d = blist[n];
Iold = qlist[n];
Eold = elist[n];
n = n-1;
quadra(c, (c+d)/2, &Q1, &E1, FUN);
quadra((c+d)/2, d, &Q2, &E2, FUN);
*Q = *Q - Iold + Q1 + Q2;
*E = *E - Eold + E1 + E2;
*N = *N + 6;
intautri(A , B, TOL, MAXFUN, Q, E, N, FUN);
alist[n+1] = c;
blist[n+1] = (c+d)/2;
qlist[n+1] = Q1;
elist[n+1] = E1;
alist[n+2] = (c+d)/2;
blist[n+2] = d;
qlist[n+2] = Q2;
elist[n+2] = E2;
n = n+2;
sort(alist, n);
sort(blist, n);
sort(qlist, n);
sort(elist, n);
flag = 0;
}
;
return flag;
}
int main(){
int intautri(float, float, float, int, float *, float *, int*, float(float));
float TOL, MAXFUN, A, B,Q,E;
int N, J;
float f(float);
A = 0;
B = 1;
TOL = 0.0001;
MAXFUN = 200;
J = intautri(A, B, TOL, MAXFUN, &Q, &E, &N, f);
printf("%d\n", J);
printf("%d\n", N);
printf("%f\n", Q);
return 0;
}
Anyway I compile without problems that code, but when I run it "segmentation fault (core dumped)" appears on terminal. I know that error could depend by memory allocation but I don't understand what is wrong...can you help me??
You have an exit condition in intautri:
if (*E<=TOL || *N>=MAXFUN){
but in the beginning of the function you also have:
*N = 3;
This means that your second exit condition will never fire. Without attempting to understand the logic of the code, I believe that you should:
Initialize N in main().
Remove the *N = 3 assignment in intautri().

Resources