A function receives two integer pointers, int* a and int* b. Set the value of *a to their sum, and *b to their absolute difference.
There is no return value, and no return statement is needed.
I got the values for *a but I'm unable to get the code for *b.
#include <stdio.h>
void update(int *a,int *b);
int main() {
int a, b;
int *pa = &a, *pb = &b;
scanf("%d %d", &a, &b);
update(pa, pb);
printf("%d\n%d", a, b);
return 0;
}
void update(int *a,int *b)
{
*a+=*b;
*b=*a-*b;
}
You may use a temporally variable to store value of *a and then use it:
int tmp = *a;
*a += *b;
*b = abs(tmp - *b);
A little cleanup. Only the function needs pointers. Your main does not.
#include <stdio.h>
#include <stdlib.h> // abs()
void sum_and_diff( int * a, int * b )
{
int sum = *a + *b;
int diff = abs( *a - *b );
*a = sum;
*b = diff;
}
int main(void)
{
int a, b;
printf( "a? " ); scanf( "%d", &a );
printf( "b? " ); scanf( "%d", &b );
sum_and_diff( &a, &b );
printf( "sum = %d\n", a );
printf( "absolute difference = %d\n", b );
return 0;
}
A simple answer (the simplest?), that doesn't require temporary variables or external functions like abs(), is:
void update(int *a, int *b)
{
*a += *b;
*b = *a-*b-*b;
}
It fixes your code by also subtracting the "extra" *b you just added to *a
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.
These are two functions I used for swapping variables using pointers while performing selection sort using the function selectionSort(int *,int). But after sorting, some elements of the array become zero.
void selectionSort(int *x,int len){
int i,j,max;
for(i=len-1;i>=0;i--){
max = 0;
for(j=1;j<=i;j++){
if(x[j]>x[max]){
max = j;
}
}
swap(x+max,x+i);
}
}
void swap(int *a,int *b){
//This one works perfectly
int temp;
temp=*b;
*b=*a;
*a=temp;
}
void swap(int *a,int *b){
//This one gives unexpected results
*a=*a+*b;
*b=*a-*b;
*a=*a-*b;
}
Swapping two integers using arithmetic operators can result in integer overflow. Better to stick with the traditional approach.
BTW, you can use old school bitwise XOR operator for swapping (used to swap values in registers) but it would not give you any benefit over the method that uses a temporary variable. These days compiler are smart enough to optimize the code.
if (*a == *b) // If both integers are same then do not perform swap operation
return;
*a ^= *b;
*b ^= *a;
*a ^= *b;
The second swap algorithm does not work if both pointers point to the same variable which happens here if max == i:
swap(x+max,x+i);
Demonstration:
#include <stdio.h>
#include <string.h>
#include <assert.h>
void swap(int *a, int *b) {
//This one works perfectly
int temp;
temp = *b;
*b = *a;
*a = temp;
}
void swap_KO(int *a, int *b) {
//This one gives unexpected results
*a = *a + *b;
*b = *a - *b;
*a = *a - *b;
}
int main()
{
int a = 22, b = 33;
swap(&a, &b);
assert(a == 33 && b == 22); // OK
swap_KO(&a, &b);
assert(a == 22 && b == 33); // OK
swap(&a, &a); // OK
assert(a == 22);
swap_KO(&a, &a); // won't work
assert(a == 22);
}
#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);
}
So I'm new to C, I'm trying to make procedure to swap value of 2 variables.
When I run this the swap didn't work.
#include <stdio.h>
#include <stdlib.h>
void swap(int A,int B)
{
A = A + B;
B = A - B;
A = A - B;
}
int main(void){
int num1,num2;
printf("insert first number :\n");
scanf("%d",&num1);
printf("insert second number :\n");
scanf("%d",&num2);
swap(num1,num2);
printf("%d %d\n",num1,num2);
return 0;
}
That is because arguments are passed by value and modifying them in callee won't affect caller's local variables. Use pointers to modify ones.
#include <stdio.h>
#include <stdlib.h>
void swap(int* A,int* B)
{
*A = *A + *B;
*B = *A - *B;
*A = *A - *B;
}
int main(void){
int num1=0,num2=0; /* initialize for in case scanf() fails */
printf("insert first number :\n");
scanf("%d",&num1);
printf("insert second number :\n");
scanf("%d",&num2);
swap(&num1,&num2);
printf("%d %d\n",num1,num2);
return 0;
}
It didn't work because when you call the procedure, it is made a copy of the arguments to the new scope.
The right way is with the address of the variables.
Change to:
swap(&num1,&num2);
and
void swap(int *A,int *B)
{
*A = *A + *B;
*B = *A - *B;
*A = *A - *B;
}
This code is working but its not exactly what I want. Is anyone have any idea how to make it correct and without q sort?. The idea is to understand how to use pointers.
The three numbers should be random between -3 and 12. The code below is something similar, and the closest I have found. Any help would be much appreciated. Thanks in advance!!.
#include <stdio.h>
#include <stdlib.h>
//functions
int compare(const void *a, const void *b)
{
const int *ia = a;
const int *ib = b;
if (*ia < *ib)
return -1;
else if (*ia > *ib)
return +1;
return 0;
}
//qsort function
void sort3(int *a, int *b, int *c)
{
int temp[3];
temp[0] = *a;
temp[1] = *b;
temp[2] = *c;
qsort(temp, 3, sizeof(int), &compare);
*a = temp[0];
*b = temp[1];
*c = temp[2];
}
//random function
int rand_int(int a, int b)
{
return rand()%(b-a+1)+a;
}
int main(void)
{
//declaration of variables
int a,b,c;
int rand_int(int a, int b);
srand(time(0));
a = rand_int(-3,12);
b = rand_int(-3,12);
c = rand_int(-3,12);
printf("%i %i %i\n", a, b, c);
sort3(&a, &b, &c);
printf("%i %i %i\n", a, b, c);
return 0;
}
You don't need the compare() function if you don't want to use qsort().
You can rewrite sort3() like this:
void compare_and_swap(int *a, int *b) {
int t;
if (*a > *b) {
t = *a;
*a = *b;
*b = t;
}
}
void sort3(int *a, int *b, int *c) {
compare_and_swap(a, b);
compare_and_swap(a, c);
compare_and_swap(b, c);
}
This is actually a "bubble sort".
This is a lot of trouble to go for to sort 3 integers. Use if statements.
If the goal is actually to understand pointers, they seem intimidating but they're not so bad. Basically, they're a number that happens to be an address. You can manipulate like them numbers, but if you dereference them (with *), you can get the value there. This cuts both ways, though, because there's not much stopping you from dereferencing a value - which probably crashes your program (or more scarily, maybe not).
As long as you keep in mind what's an address and what's a value, you should be OK.