I have 2 pointers to some Point structures. I wanted to calculate distance between 2 points (i dont need to calculate root of it) so i have this:
w[0]=X[l];
w[1]=X[l+1];
d=m(w[0]->x-w[1]->x)+m(w[0]->y-w[1]->y);
printf("--TEST %d %d %d\n",w[0]->x,w[1]->x,w[0]->x-w[1]->x);
Input: X[l] = (0,1), X[l+1] = (2,0)
Output: --TEST 0 2 -1
Why is that?
Edit:
This was part of function find which finds 2 points with the smallest distance between them. In the main i have this:
X=(Punkt**)malloc(sizeof(Punkt*)*n);
Y=(Punkt*)malloc(sizeof(Punkt)*n);
int x,y;
for(int i=0;i<n;++i) {
scanf("%d %d",&x,&y);
Y[i].x=x;
Y[i].y=y;
X[i]=(Punkt*)malloc(sizeof(Punkt*));
X[i]=&Y[i];
}
Quicksort(X,0,n-1);
Punkt **wynik=find(0,n-1);
printf("%d %d\n%d %d",wynik[0]->x,wynik[0]->y,wynik[1]->x,wynik[1]->y);
I've checked Quicksort, it works as it should. Function m: #define m(a) ((a)*(a))
Only find function have bug in that part. This works, but i dont want to make variables for each coordinate.
int trash1=w[0]->x;
int trash2=w[1]->x;
printf("--TEST %d %d %d\n",w[0]->x,w[1]->x,w[0]->x-w[1]->x,trash1-trash2);
Input: 2 points (0,1) (2,0)
Output: --TEST 0 2 -1 -2
I am trying to reproduce what you experience. Using this I get 0 - 2 = - 2 as result:
(In other words, not able to reproduce.)
#include <stdio.h>
#include <stdlib.h>
#define m(a) ((a)*(a))
typedef struct pkt {
int x;
int y;
} Punkt;
int main(void)
{
Punkt **X;
Punkt *Y;
Punkt *w[2];
int x, y;
int n = 2;
int i;
X = malloc(sizeof(Punkt*) * n);
Y = malloc(sizeof(Punkt) * n);
printf("Enter 2 integer point pairs: ");
for(i = 0; i < n; ++i) {
scanf("%d %d", &x, &y);
Y[i].x = x;
Y[i].y = y;
X[i] = &Y[i];
}
i = 0;
w[0] = X[i];
w[1] = X[i + 1];
i = m(w[0]->x - w[1]->x) + m(w[0]->y - w[1]->y);
printf("D: m(%d - %d) + m(%d - %d) = "
"%d + %d = "
"%d\n",
w[0]->x, w[1]->x, w[0]->y, w[1]->y,
m(w[0]->x - w[1]->x), m(w[0]->y - w[1]->y),
i);
printf("TEST: %d - %d = %d\n",
w[0]->x,
w[1]->x,
w[0]->x - w[1]->x);
free(X);
free(Y);
return 0;
}
Sample:
$ ./pq
Enter 2 integer point pairs: 0 1 2 0
D: m(0 - 2) + m(1 - 0) = 4 + 1 = 5
TEST: 0 - 2 = -2
Related
As I see it the numerical sequence consists of 2 separate sequences. This is the code that I have so far. I am not sure if you must use a while or a for loop. I am fairly new at coding so if someone please could help me.
if the entered value is 10 it must give the first 10 terms of the sequence, and if I enter 5 it must give me the first 5 terms of the sequence.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main() {
int a, n = 1, t, y = 1; // First Numerical Sequence
int b, m = 2, s, x = 2; // Second Numerical Sequence
int d, r, z; // Extra
printf("Enter A Tn : ");
scanf(" %d", &z);
printf("\n");
while (n <= z) {
a = 15;
r = pow(2, n - y);
d = (9 * (r - 1)) / (2 - 1);
t = a + d;
printf("%d\n", t);
n += 2;
y++;
}
while (m <= z) {
b = 12;
r = pow(2, m - x);
d = (9 * (r - 1)) / (2 - 1);
s = b + d;
printf("%d\n", s);
m += 2;
x++;
}
printf("\n");
return 0;
}
This will get the job done.
#include <stdio.h>
int main(){
int val,ic; //iteration count, will print ic*2 number
scanf("%d %d",&val,&ic);
for(int i = 0;i<ic;i++){
printf("%d ",val);
val-=3;
printf("%d ",val);
val*=2;
}
printf("\n");
}
How to compile & run:
C:\Users\stike\Desktop>rem assume you saved it in a.c
C:\Users\stike\Desktop>gcc -o a a.c
C:\Users\stike\Desktop>a
15
5
15 12 24 21 42 39 78 75 150 147
If you want to print the same sequence starting from 15 and o till a certain number which the user inputs, you can follow the following code.
Hope you understood the sequence pattern when a number is given it is printed and reduce the number by 3, then it is printed and then twice the number and printed, and again reduce by 3, likewise, it flows on.
#include <stdio.h>
int main() {
int endNum;
int beginNum = 15;
printf("Enter the end: ");//(lineA) here we initialize the variables with beginNum as 15
scanf("%d", &endNum); //(Line B) let the user to input endNum of the sequence,in the example it is 147
while ((beginNum-3) <= endNum) { // checks the condition
printf("%d ", beginNum);
if(beginNum==endNum) return 0; //check whether you print the end number.
beginNum -= 3; // reduce by 3
printf("%d ", beginNum);
beginNum *= 2; // multiply by 2
}
return 0;
}
if you don't need to user input a endNum just initialize the value 147 to variable endNum.
And delete the lines A and B.
Here's another approach using static variables
#include <stdio.h>
int next(void) {
static int last, n = 0;
if (n++ == 0) return last = 15; // 1st element of sequence
if (n % 2) return last = last * 2; // odd elements
return last = last - 3; // even elements
}
int main(void) {
for (int k = 0; k < 10; k++) {
printf("%d ", next());
}
puts("");
return 0;
}
I need to write a program that takes 2 digits(X and n) and then prints X with last n digits of X reversed.
For example
Input: 12345 3
Output: 12543
Input: 523 2
Output: 532
I already wrote a control mechanism for checking n is greater or equal than the number of digits of X
For example if inputs are 6343 and 7, program prints that inputs should be changed and takes input again.
My main problem is I couldn't find an algorithm for reversing last n digits. I can reverse any int with this code
int X, r = 0;
printf("Enter a number to reverse\n");
scanf("%d", &n);
while (X != 0)
{
r = r * 10;
r = r + n%10;
X = X/10;
}
printf("Reverse of the number = %d", r);
But I couldn't figure how two reverse just last digits. Can you give me any idea for that?
I couldn't figure how to reverse just last digits
Separate the number using pow(10,n) - see later code.
unsigned reverse_last_digits(unsigned x, unsigned n) {
unsigned pow10 = powu(10, n);
unsigned lower = x%pow10;
unsigned upper = x - lower;
return upper + reverseu(lower, n);
}
Create a loop that extracts the least-significant-digit (%10) and builds up another integer by applying that digit. (y = y*10 + new_digit)
unsigned reverseu(unsigned x, unsigned n) {
unsigned y = 0;
while (n-- > 0) {
y = y*10 + x%10;
x /= 10;
}
return y;
}
For integer type problems, consider integer helper functions and avoid floating point functions like pow() as they may provide only an approximate results. Easy enough to code an integer pow().
unsigned powu(unsigned x, unsigned expo) {
unsigned y = 1;
while (expo > 0) {
if (expo & 1) {
y = x * y;
}
expo >>= 1;
x *= x;
}
return y;
}
Test
int main() {
printf("%u\n", reverse_last_digits(12345, 3));
printf("%u\n", reverse_last_digits(523, 2));
printf("%u\n", reverse_last_digits(42001, 3));
printf("%u\n", reverse_last_digits(1, 2));
}
Output
12543
532
42100
10
Code uses unsigned rather than int to avoid undefined behavior (UB) on int overflow.
It is an easy one.
1. let say the number you want to reverse is curr_number;
2. Now, the places you want to reverse is x;
(remember to verify that x must be less than the number of digit of curr_number);
3. now, just take a temp integer and store curr_number / pow(10,x) ('/' = divide and pow(10,x) is 10 to the power x)
4. now, take a second number temp2, which will store curr_number-(temp * pow(10,x) )
5. reverse this temp2 (using your function)
6. now, answer = (temp * pow(10,x) ) + (temp2) //(note temp2 is reversed)
example with steps:
curr_number = 1234567
places you want to reverse is 3
temp = 1234567 / (10^3) i.e (1234567/1000) = 1234 (because it is int type)
temp2 = 1234567 - (1234*10^3) i.e 1234567 - 1234000 = 567
reverse(567) = 765
answer = (1234 * 10^3) + 765 = 1234765
Create two variables
lastn which stores the last n digits (345)
r which stores the reversed last n digits (543)
Subtract lastn from the original number (12345 - 345 = 12000)
Add r to the above number (12000 + 543 = 12543)
int c = 0; // count number of digits
int original = x;
int lastn = 0;
while (x != 0 && c < n) {
r = r * 10;
r = r + x % 10;
lastn += (x % 10) * pow(10, c);
x = x / 10;
c++;
}
printf("reversed: %d\n", original - lastn + r);
In case you don't have problems using char, you can do this
#include <stdio.h>
#include <string.h>
#define SIZE 10
int main() {
char n[SIZE]; // the Number;
int x; // number of last digits of n to reverse
int len; // number of digits of n
scanf("%s%d", n, &x);
len = strlen(n);
for(int i = 0; i < len; i++) {
i < len - x ? printf("%c", n[i]) : printf("%c", n[2*len -1 - i - x]);
}
return 0;
}
If you want you can make the program more readable by splitting the for in two
for(int i = 0; i < len - x; i++) {
printf("%c", n[i]);
}
for(int i = len-1; i >= len - x; i--) {
printf("%c", n[i]);
}
Note: the program won't work if n > x (i.e. if you want to swap more digits than you got)
I need to find out the solution of the expression presented on the picture (110), but can't define the exact formula which would satisfy the conditions.
That's the code I have, but it seems wrong and not finished:
int n = 1, i = 1, x = 1;
float j, k, z, result;
while (i<51)
{
z = n+2;
x = z+2;
k = 1./x;
n+=2;
j = 1./z+k;
i++;
}
result = 1./(1+j);
printf("\nThe result is: %f", result);
}
I would be very grateful for pointing out the mistakes!
Working from inside to outside, each step is the reciprocal of i plus the previous step, where i runs from 103 to 1 in steps of −2, and we start with a “previous step” of 0:
#include <stdio.h>
int main(void)
{
double x = 0;
for (int i = 103; 1 <= i; i -= 2)
x = 1/(i + x);
printf("%g\n", x);
}
Hi stackoverflow community! I'm having some problems with my code. I'm currently a student so basically I'm a beginner. Using Euclidean Algorithm, the code shown below should divide and divide two numbers until the quotient reach 0 but it just stops at the last dividing process just before the quotient turns 0. I don't know if the program crashes because of this. Hoping for a beginner-friendly reply from you guys. Thanks! (sorry if there's already an existing question like this)
Here's the code:
int quotient,quotient2,remainder,remainder2,x,y;
int foo()
{
printf("Enter a number: ");
scanf("%d", &x);
printf("Enter another number: ");
scanf("%d", &y);
if(y >= x){
quotient2 = y / x;
remainder2 = y % x;
printf("%d = %d(%d) + %d\n", y,x,quotient2,remainder2);
if(quotient2 != 0){
do{
y = x;
x = remainder2;
quotient2 = y / x;
remainder2 = y % x;
printf("%d = %d(%d) + %d\n", y,x,quotient2,remainder2);
} while(quotient2 != 0);
}
} else if(x > y){
quotient = x / y;
remainder = x % y;
printf("%d = %d(%d) + %d\n", x,y,quotient,remainder);
if(quotient != 0){
do{
x = y;
y = remainder;
quotient = x / y;
remainder = x % y;
printf("%d = %d(%d) + %d\n", x,y,quotient,remainder);
} while(quotient != 0);
}
}
system("pause");
return 0;
}
If your "Euclidean algorithm" is computing the GCD of two numbers, here is one way to do it. Note it does not divide by 0, it stops when the divisor would be 0.
So the point is, rather than guard against dividing by 0, that's the iteration's end condition anyway.
#include <stdio.h>
unsigned gcd(unsigned x, unsigned y) {
unsigned z;
if (x == 0 || y == 0) {
return 0;
}
while ((z = y % x) != 0) {
y = x;
x = z;
}
return x;
}
int main(void)
{
printf("20 ~ 20 : %u\n", gcd(20, 20));
printf("20 ~ 0 : %u\n", gcd(20, 0));
printf(" 0 ~ 20 : %u\n", gcd( 0, 20));
printf("20 ~ 16 : %u\n", gcd(20, 16));
printf("16 ~ 20 : %u\n", gcd(16, 20));
printf("20 ~ 15 : %u\n", gcd(20, 15));
printf("15 ~ 20 : %u\n", gcd(15, 20));
printf(" 1 ~ 2 : %u\n", gcd( 1, 2));
printf(" 2 ~ 1 : %u\n", gcd( 2, 1));
return 0;
}
Program output:
20 ~ 20 : 20
20 ~ 0 : 0
0 ~ 20 : 0
20 ~ 16 : 4
16 ~ 20 : 4
20 ~ 15 : 5
15 ~ 20 : 5
1 ~ 2 : 1
2 ~ 1 : 1
Note there is no need to swap the arguments. The algorithm works no matter which way round they are.
Notice that with x = remainder2;, x could take on the value of 0. Then the next quotient2 = y / x; remainder2 = y % x; both perform operations (divide by 0 and remainder 0) which are both undefined behavior. The program crash is certainly due to this.
if(y >= x){
quotient2 = y / x;
remainder2 = y % x;
printf("%d = %d(%d) + %d\n", y,x,quotient2,remainder2);
if(quotient2 != 0){
do{
y = x;
x = remainder2;
quotient2 = y / x;
remainder2 = y % x;
printf("%d = %d(%d) + %d\n", y,x,quotient2,remainder2);
} while(quotient2 != 0);
}
As code is swapping the roles of x,y with each operation, code could simplify to: (Also see What is gcd(0,a)gcd(0,a), where a is a positive integer?
unsigned gcd(unsigned a, unsigned b) {
while (b) {
a %= b;
if (a == 0) return b;
b %= a;
}
return a;
}
For my programming class we are making a main() function that calls multiple other functions. Two of these functions are numberStats() and triad(), both of which have their own variables, no global variables are used, and numberStats() is called immediately before triad(). Neither codes have similar variable names, but for some reason when I use the triad function, which asks the user to input 3 ints, it spits out the first int correctly but the 2nd and 3rd ints are the two ints inputted into the numberStats() function. Can anyone explain why this is happening and what I might be able to do to fix it? Thank you in advance, new blood here.
EDIT*
here's the output code: (if it's italicized then it's the numbers I entered in as input)
numberStats: Please enter two positive ints: 5 800 //I input 5 and 800
800 is larger
5 is smaller
800 + 5 = 805
800 - 5 = 795
800 * 5 = 4000
800 % 5 = 0
triad: Please enter three positive ints:
1 2 3 //I input 1 2 and 3
you entered 1, 5, and 800
Not all equal
Sorted order: 1, 5, 800
I obviously did not enter 1, 5, and 800 for the triad function, why would it tell me I did?
EDIT** Source code for numberStats() and triad():
void numberStats(void)
{
int var1, var2, a, b, c, d;
printf("numberStats: Please enter two positive ints: ");
scanf("%d %d",&var1, &var2);
if (var1 > var2){
printf("%d is larger\n",var1);
printf("%d is smaller\n",var2);
a = var1 + var2;
b = var1 - var2;
c = var1 * var2;
d = var1 % var2;
printf("%d + %d = %d\n",var1,var2,a);
printf("%d - %d = %d\n",var1,var2,b);
printf("%d * %d = %d\n",var1,var2,c);
printf("%d %% %d = %d\n",var1,var2,d);
}
else if (var1 < var2){
printf("\n%d is larger\n",var2);
printf("%d is smaller\n",var1);
a = var2 + var1;
b = var2 - var1;
c = var2 * var1;
d = var2 % var1;
printf("%d + %d = %d\n",var2,var1,a);
printf("%d - %d = %d\n",var2,var1,b);
printf("%d * %d = %d\n",var2,var1,c);
printf("%d %% %d = %d\n",var2,var1,d);
}
else if (var1 == var2){
printf("%d and %d are the same\n",var1,var2);
a = var1 + var2;
b = var1 - var2;
c = var1 * var2;
d = var1 % var2;
printf("%d + %d = %d\n",var1,var2,a);
printf("%d - %d = %d\n",var1,var2,b);
printf("%d * %d = %d\n",var1,var2,c);
printf("%d %% %d = %d\n",var1,var2,d);
}
}
void triad(void)
{
int x, y, z, low, mid, high;
printf("\ntriad: Please enter three positive ints: \n");
scanf("%d, %d, %d", &x, &y, &z);
printf("you entered %d, %d, and %d\n", x, y, z);
if (x == y && y == z){
printf("All equal\n");
}
if (x <= y && x <= z){
low = x;
if (y <= z){
mid = y;
high = z;
}
else{
mid = z;
high = y;
}
printf("Not all equal\n");
}
if (x >= y && x >= z){
high = x;
if (y <= z){
low = y;
mid = z;
}
else{
low = z;
mid = y;
}
printf("Not all equal\n");
}
if ((x >= y && x <= z) || (x <= y && x >= z)){
mid = x;
if (y >= z){
high = y;
low = z;
}
else{
high = z;
low = y;
}
printf("Not all equal\n");
}
printf("Sorted order: %d, %d, %d\n", low, mid, high);
}
The functions triad() and scanf() are causing the trouble:
int x, y, z, low, mid, high;
printf("\ntriad: Please enter three positive ints: \n");
scanf("%d, %d, %d", &x, &y, &z);
You're looking for commas in the data, but not entering them (you gave "1 2 3" but you needed to give "1,2,3" with commas (and optional spaces) separating the numbers). scanf() returned 1 instead of 3, but you ignored it. Don't! Check your inputs, every time.
As a result, you have quasi-random values in y and z, which is why you're running into problems.
A basic debugging technique is to print the values just read:
if (scanf("%d, %d, %d", &x, &y, &z) != 3)
…report error and bail out…
printf("x = %d, y = %d, z = %d\n", x, y, z);