Swap in C - Printing - c

I just implemented my swap function but it does not print anything. Do you know why does the line printf does not execute?
#include <stdio.h>
int swap(int x, int y) {
scanf("%d", &x);
printf("%d, x is",x);
scanf("%d", &y);
int temp = x;
x = y;
y = temp;
printf("After Swapping: x = %d, y = %d", x, y);
return 0;
}
int main() {
swap(6,5);
}

You should not take user input inside the swap function. Its purpose should be to swap two integers only. You can move the scanf statements to main function.
#include <stdio.h>
int swap(int x, int y){
int temp = x;
x = y;
y = temp;
printf("After Swapping in swap function: x = %d, y = %d", x, y);
return 0;
}
int main(void){
int x, y;
scanf("%d", &x);
printf("%d, x is", x);
scanf("%d", &y);
printf("%d, y is", y);
swap(x, y);
printf("After Swapping in main function: x = %d, y = %d", x, y);
}
But the above code has a major issue. Though the swap function prints the integers passed as they are swapped but the fact is x and y in the main remains unaffected.
In this case to make it work, using pointers would be helpful
void swap(int *ptrx, int *ptry){
int temp = *ptrx;
*ptrx = *ptry;
*ptry = temp;
}
In the main function call the swap as swap(&x, &y);

Use this code for swapping.
#include <stdio.h>
void swap(int x, int y)
{
int z;
z = x;
x = y;
y = z;
printf("After Swapping: x = %d, y = %d", x, y);
}
int main()
{
swap(6,5);
return 0;
}
And I don't understand why you need to scan x & y

Related

why do i get 0 instead the result of calculation x - y and x/y in C

In this code I thought I would get the result of calculation x / y and x - y but the program shows 0 for i and j. What is wrong?
#include <stdio.h>
float calculate(float, float);
float i, j;
int main()
{
float a, b;
printf("Enter two numbers:\n");
scanf("%f%f", &a, &b);
printf("\nThe results are: %f %f %f\n", calculate(a, b), i, j);
return 0;
}
float calculate(float x, float y)
{
float r;
r = x * y;
i = x / y;
j = x - y;
return r;
}
It is undefined behavior, when you call the calculate() function within the same printf and i as well as j are calculated within that printf (same function). By the way, it is not a good idea to use global variables ( i, j ) ... For test purpose only, you could calculate() before the next printf of i and j.
You can test that behavior with:
#include <stdio.h>
float calculate(float, float);
float i, j;
int main()
{
float a, b;
printf("Enter two numbers:\n");
scanf("%f%f", &a, &b);
printf("\nThe results are: %f", calculate(a, b));
printf(" %f %f\n", i, j);
return 0;
}
float calculate(float x, float y)
{
float r;
r = x * y;
i = x / y;
j = x - y;
return r;
}
It might be related to the parsing, reference, and execution order of the arguments in printf function. The printf function uses the arguments right-to-left direction. You can easily check the order through below codes.
#include <stdio.h>
float calculate(float, float);
float i, j;
int main()
{
float a, b;
printf("Enter two numbers:\n");
scanf("%f%f", &a, &b);
//printf("\nThe results are: %f %f %f\n", calculate(a, b), i, j);
printf("\nThe results are: %f %f %f\n", i, j, calculate(a, b));
return 0;
}
float calculate(float x, float y)
{
float r;
r = x * y;
i = x / y;
j = x - y;
return r;
}

Why my pointer code is giving me wrong output in C?

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);
}

Convert a printed number using printf as a variable

I have
int main ()
{
int x=69057;
int y=23
printf("%d", x);
printf("%d", y);
return 0;
}
And it prints 6905723. How can I convert the printed number into an integer? I can't do
int z=6905723
since in the original program I don't know what value x and y have.
What you essentially want to do here is multiply x by the smallest power of 10 that's larger than y, and then add y to it.
Assuming x and y are small enough to not overflow the long result, you could do something like this:
int x = 69057;
int y = 23
int temp = y;
long result = x;
while (temp > 0) {
result *= 10;
temp /= 10;
}
result += y;
printf("%ld\n", result);
Here's how you can get the answer into int z. It works even if you don't know x and y before. You just need to make sure that the answer will actually fit into an int.
int main ()
{
int x=69057;
int y=23
char buff[512];
sprintf(buff, "%d%d", x, y);
int z = atoi(buff); /* Now z is equal to 6905723 */
return 0;
}
You will run into a problem if y is a negative number, but it isn't clear from your question what you'd like to happen in that situation.
try this. very easy and simple
#include <stdio.h>
#include <conio.h>
#include <math.h>
int main()
{
int x,y,z,a,b;
x=69057;
y=23;
printf("x=%d y=%d\n",x,y);
a = log10(y) + 1;
for (b=0;b<a;b++)
{
x*=10;
}
z=x+y;
printf("z=%d",z);
return 0;
}
You can write a function to do it for you:
#include <stdio.h>
int my_function(int x, int y);
int main()
{
int x = 69057;
int y = 23;
int z = my_function(x, y);
printf("%d\n", z);
return 0;
}
int my_function(int x, int y)
{
int tmp = y;
do {
x *= 10;
} while (tmp /= 10);
return x + y;
}

C- function for printing each base exponent For example, integerPower( 3, 4 ) = 3 * 3 * 3 * 3

I can't seem to get my program running
Write a function integerPower(base, exponent) that
returns the value of
Baseexponent. For example, integerPower( 3, 4 ) = 3 * 3 * 3 * 3. Assume that
exponent is a positive, nonzero
integer, and base is an integer. Function integerPower should use for to
control the calculation.
Do not use any math library functions.
i have this program
#include<stdio.h>
int power(int b, int e){
int x
for (x = 0; x <= e; x++)
b=b;
return b;
}
void main(){
int b = 0;
int e = 0;
scanf("input a base %d\n", &b);
scanf("input an exponent %d\n", &e);
power(b,e);
printf("%d" , b);
}
In the loop
for (x = 0; x <= e; x++)
b=b;
the line b=b; is useless. It just assign the value of b to itself. You need to multiply b by e times. For this you need to take another variable with initial value 1 and multiply it by b at each iteration of loop to get be.
Change your function to this
int power(int b, int e){
int x, y = 1;
for (x = 1; x <= e; x++)
y = y*b; // Multiply e times
return y;
}
#include<stdio.h>
int power(int b, int e){
int x;
int result = 1;
for (x = 0; x < e; x++)
result*=b;
return result;
}
int main(){
int b = 0;
int e = 0;
printf("Input a base ");
scanf("%d", &b);
printf("Input an exponent ");
scanf("%d", &e);
b = power(b,e);
printf("%d" , b);
return 0;
}
First problem is with the scanf function. you are using "input a base ". For this to output you have to use printf("input a base ").
Try using the below function to calculate the value of base raised to the power of exponent
int ipower(int b, int e)
{
int x, tmp = 1;
for (x = 0; x < e; x++)
tmp *= b;
return tmp;
}
Here the for loop is iterated for e times that is from 0 to e-1. "tmp" will hold the result.
Make sure you don't change the value of "b/e"(base/exponent) inside the loop, else will lead to wrong result.
doing b*=b in function body won't help either. And why initialize b and e to zero when you're supposed to input them from the user?
Try this:
#include<stdio.h>
int power(int b, int e) {
int temp = b;
int x;
for (x = 1; x < e; x++)
b *= temp;
return b;
}
void main() {
int b ;
int e;
scanf_s(" %d", &b);
scanf_s(" %d", &e);
int h= power(b, e);
printf("%d", h);
}
int power(int b, int e){
int x,t=1
for (x = 1; x <= e; x++)
t*=b;
return t;
}
if user give e= 1 then also its work.

LCM And GCD in C using recursion or normal method

While trying to do the GCD and LCM program from programming simplified...I am facing problems with the results. I did everything correct(according to me) and even checked word by word but the problem still persists...I am pasting the code of normal method only.
#include <stdio.h>
int main()
{
int a, b, x, y, t, gcd, lcm;
printf("Enter first number :");
scanf("%d", &a);
printf("Enter first number :");
scanf("%d", &b);
a = x;
b = y;
while (b != 0)
{
t = b;
b = a % b;
a = t;
}
gcd = a;
lcm = (x * y)/gcd;
printf("Greatest common divisior of %d and %d = %d\n", x, y, gcd);
printf("Lowest common divisior of %d and %d = %d\n", x, y, lcm);
getch();
}
At least this part is fundamentally wrong:
int a, b, x, y, t, gcd, lcm;
printf("Enter first number :");
scanf("%d", &a);
printf("Enter first number :");
scanf("%d", &b);
a = x;
b = y;
So you're declaring x and y uninitialized, then you're assigning them to a and b - now a and b don't contain the values the user entered, but some garbage. You probably want
x = a;
y = b;
instead.
Better try this. This is simpler to run.
#include<stdio.h>
int GCD(int,int);
void main(){
int p,q;
printf("Enter the two numbers: ");
scanf("%d %d",&p,&q);
printf("\nThe GCD is %d",GCD(p,q));
printf("\nThe LCM is %d",(p*q)/(GCD(p,q)));
}
int GCD(int x,int y){
if(x<y)
GCD(y,x);
if(x%y==0)
return y;
else{
GCD(y,x%y);
}
}
Try it
#include<stdio.h>
int main(){
int a,b,lcm,gcd;
printf("enter two value:\n");
scanf("%d%d",&a,&b);
gcd=GCD(a,b);
lcm=LCM(a,b);
printf("LCM=%d and GCD=%d",lcm,gcd);
return 0;
}
int GCD(int a, int b){
while(a!=b){
if(a>b){
a=a-b;
}else{
b=b-a;
}
}
return a;
}
int LCM(int a, int b){
return (a*b)/GCD(a,b);
}

Resources