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
Related
Input 1: 90009
Input 2: 8999
Input 3: 94738
(Output: 90009 94738 8999)
see the pattern
it sorts the value of 3 integer inputs in a such a way one input continues another by matching the starting digit and the ending digit of another integer
#include <stdio.h>
void swap( int *x , int * y)
{
double t = *x;
*x = *y;
*y = t;
}
int sort(int *x, int *y, int *z)
{
int getZ;
getZ = *z;
while(getZ>=10)
getZ=getZ/10;
if(*x % 10 == *y/ 1000)
swap(y,x);
if(*x%10 != *z/1000)
swap(z,y);
if(*y % 10 == getZ)
swap(z,y);
printf("Sorted Version is %d %d %d",*x,*y,*z);
}
For some reason im not getting the order that i want. Is there algorithm to write this code with?
The simplest way is just to check every permutation. This code can be more compactly written, but here's a very straightforward implementation:
#include <stdio.h>
int first_digit(int n) {
while (n > 10) n /= 10;
return n;
}
int last_digit(int n) {
return n % 10;
}
int sort(int *x, int *y, int *z) {
int n[3] = {*x, *y, *z};
int permutations[][3] = {
{0, 1, 2},
{0, 2, 1},
{1, 0, 2},
{1, 2, 0},
{2, 0, 1},
{2, 1, 0},
};
for (int i = 0; i < sizeof(permutations) / sizeof(*permutations); i++) {
int *idx = permutations[i];
if (last_digit(n[idx[0]]) == first_digit(n[idx[1]]) && last_digit(n[idx[1]]) == first_digit(n[idx[2]])) {
*x = n[idx[0]];
*y = n[idx[1]];
*z = n[idx[2]];
return 1;
}
}
return 0;
}
int main() {
int x=126, y=456, z=6001;
if (sort(&x, &y, &z)) {
printf("sorted version is %d %d %d\n", x, y, z);
} else {
printf("sort not possible for %d %d %d\n", x, y, z);
}
return 0;
}
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);
}
#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);
}
I don't know yet what is the problem with the code:
#include <stdio.h>
#define cutoff 3
int swap(int *x, int *y)
{
int *tmp;
tmp = x;
x = y;
y = tmp;
return *x, *y;
}
void qsort(int a[], int left, int right)
{
int i, j;
int pivot;
if (left + cutoff <= right) // JUST TO ENSURE THAT THE ARRAY'S SIZE IS >= CUTOFF.
{
pivot = median(a, left, right);
i = left;
j = right - 1;
for (;;)
{
while (a[i] < pivot)
i++;
while (a[j] > pivot)
j--;
if (i < j)
swap(&a[i], &a[j]);
else
break;
}
swap(&a[i], &a[right - 1]); // RESTORE PIVOT
qsort(a, left, i-1);
qsort(a, i+1, right);
}
//else
// PERFORM INSERTION SORT
}
void quicksort(int a[], int n)
{
int i;
qsort(a, 0, n - 1);
printf("THE SORTED ARRAY IS: ");
for(i=0;i<n;i++)
printf("%d ", a[i]);
}
int median(int a[], int left, int right)
{
int center = (left + right) / 2;
if(a[left] > a[center])
swap(&a[left], &a[center]);
if(a[left] > a[right])
swap(&a[left], &a[right]);
if(a[center] > a[right])
swap(&a[center], &a[right]);
swap(&a[center], &a[right - 1]); // HIDE PIVOT.
return a[right - 1]; // RETURN PIVOT.
}
void main()
{
int a[100], i, n;
printf("ENTER THE SIZE: ");
scanf("%d", &n);
printf("ENTER THE UNSORTED ARRAY: ");
for (i=0;i<n;i++)
scanf("%d", &a[i]);
quicksort(a, n);
}
The output is the same as teh input and soemtimes it is taking more than the input size. I think that the problem lies in the median function, choosing the pivot.
int swap(int *x, int *y)
{
int *tmp;
tmp = x;
x = y;
y = tmp;
return *x, *y;
}
You're assigning the pointers, not their values. Use this instead
void swap(int *x, int *y)
{
int tmp;
tmp = *x;
*x = *y;
*y = tmp;
}
Also, the return means nothing since the values are swapped by the pointers, and you cannot also return 2 values at a time. The return type should be void like #Yu Hao said
The main problem is in your swap() function
int swap(int *x, int *y)
{
int *tmp;
tmp = x;
x = y;
y = tmp;
return *x, *y;
}
It only swaps the value of the pointer x and y themselves, but not the value that x and y points. Remember that functions in C are always pass-by-value. You need to swap *x and *y.
void swap(int *x, int *y)
{
int tmp;
tmp = x;
*x = *y;
*y = tmp;
}