Concerning my determination for output.It's 1 40 1 While using C it displays output as 0 41 1 How's that possible?What wrong step I'm going into?
#include<stdio.h>
#include<conio.h>
void main(void)
{
clrscr();
int n,a,b;
n = 400;
a = n % 100; //remainder operation
b = n / 10; //division operation
n = n % 10; //remainder operation
printf("%d %d %d",n++,++b,++a); //post-,pre-,pre- increment used
getch();
}
What your compiler prints is correct. Here is the program flow:
#include<stdio.h>
#include<conio.h>
void main(void)
{
clrscr();
int n,a,b;
n = 400; // n has value 400
a = n % 100; // a has value 0
b = n / 10; // b has value 40
n = n % 10; // n has value 0
// n++ evaluates to 0, afterwards n has the value 1
// ++b evaluates to 41, afterwards b has the value 41
// ++a evaluates to 1, afterwards a has the value 1
printf("%d %d %d",n++,++b,++a);
// Thus, 0 41 1 is printed.
getch();
}
Notice especially that the postfix-incrememnt operator n++ returns the value of n unchanged and then changes n. That's why 0 is printed in the first column.
Related
Problem:
In this question I tried to solve a bitwise operator problem. Giving two number as input
Input will be two number. Here is some input:
n = 3
k = 3
Then I need to construct "a" and "b" in some way(for instance when "a = 1" then b will be one past a to <= n. The results of the comparisons are below:
a b and or xor
1 2 0 3 3
1 3 1 3 2
2 3 2 3 1
Here is my code:
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
//Complete the following function.
void calculate_the_maximum(int n, int k)
{
// Two dimentional array for storing the value of and or xor
int rowOfArray = 3;
int sets[rowOfArray][k];
//For loop for a
for (int i = 0; i < k; i++)
{
int a = i + 1;
//For loop for b
for (int j = a; j < n; j++)
{
int b = j;
b++;
printf("{%i, %i}\n", a, b);
//Storing and operation result to the array
sets[0][j - 1] = a&b;
printf("And: %i\n", sets[0][j - 1]);
//Storing or operation result to the array
sets[1][j] = a|b;
printf("Or: %i\n", sets[1][j]);
//Storing xor opertaion result to the array
sets[2][j + 1] = a^b;
printf("Xor: %i\n", sets[2][j + 1]);
}
}
//Find the biggest number in array
for (int i = 0; i < rowOfArray; i++)
{
int big;
for (int j = 0; j < k; j++)
{
big = 0;
printf("Big1: %i\n", big);
if (big < sets[i][j])
{
big = sets[i][j];
printf("Big2: %i\n", big);
}
}
printf("big3: %i\n", big);
if (big < k)
{
printf("%i\n", big);
}
else
{
printf("%i\n", 0);
}
}
}
int main() {
int n, k;
scanf("%d %d", &n, &k);
calculate_the_maximum(n, k);
return 0;
}
I used too many printf function to show that what have I done wrong. I stored the value as I expected but later in the for loop when I tried to print the specific position integer I didn't get the correct result even sometimes I got bigger and negative number too.
Here is the output:
3 3
{1, 2}
And: 0
Or: 3
Xor: 3
{1, 3}
And: 1
Or: 3
Xor: 2
{2, 3}
And: 2
Or: 3
Xor: 1
Big1: 0
Big1: 0
Big2: 2
Big1: 0
Big2: 120329728
big3: 120329728
0
Big1: 0
Big2: 1986993953
Big1: 0
Big2: 3
Big1: 0
Big2: 3
big3: 3
0
Big1: 0
Big1: 0
Big2: 32765
Big1: 0
Big2: 3
big3: 3
0
As is, for the input 3 3, j holds the values 1, 2, and 2, so clearly sets[2][j + 1] is Undefined Behaviour since it accesses the subarray at index 3, when valid indices are [0, 2].
Given the source problem, the issues are more clear.
k is simply a limit on output, and should not be used as a bound for iteration, or for calculating the storage required for the number of k-combinations.
It is unnecessary to store all the results at once. For each combination, each value of the operation a ? b (where ? is a bitwise operator) can be tested against the value k and the currently stored maximum value for the given operator.
Here is a quick solution. Compile with -DDEBUG to see additional output.
#include <stdio.h>
void setmax(int *dest, int value, int limit)
{
if (value < limit && value > *dest)
*dest = value;
}
void calc(int n, int k)
{
int and = 0;
int or = 0;
int xor = 0;
#ifdef DEBUG
printf("INFO: a b and or xor\n");
#endif
for (int a = 1; a < n; a++) {
for (int b = a + 1; b <= n; b++) {
setmax(&and, a & b, k);
setmax(&or, a | b, k);
setmax(&xor, a ^ b, k);
#ifdef DEBUG
printf("INFO: %-3d %-3d %-3d %-3d %-3d\n",
a, b, a & b, a | b, a ^ b);
#endif
}
}
printf("%d\n%d\n%d\n", and, or, xor);
}
int main(void)
{
int n, k;
if (2 != scanf("%d%d", &n, &k))
return 1;
calc(n, k);
}
Sample runs:
./a.out <<< "3 3"
2
0
2
./a.out <<< "5 4"
2
3
3
If you run the code, you'll get stack corruption error which is due to indexing the array beyond its allocated size. Accessing memory that's not for your program is undefined behavior. Anything might happen. The program may crash or it may not. For my specific compiler and the fact that I ran the code in debug mode and not release mode, the program crashed with the error I mentioned.
Now to fix the error, from what you explained, you have three columns for and, or and xor. So you need to reverse the dimensions of set (set[k][rowOfArray], better change the name to rowSize or n_columns or so). Also reverse the indexing, e.g. change set[0][j-1] to set[j-1][0] and so on. I'm not sure what you're trying to do in the second part, though.
We were having a quiz of sorts and had the following question where we had to find the error, or if there's none, the output of the given code:
#include <stdio.h>
int main(void) {
int n = 012;
b: printf("%d\n",n--);
if(n!=0){
n--;
goto b;
}
return 0;
}
I don't see anything wrong with this in theory but this leads to an infinite loop with the variable going way below 0. Could anyone help me with this?
#include <stdio.h>
int main(void) {
int n = 012;
b: printf("%d\n",n);
if(n!=0){
n--;
goto b;
}
return 0;
}
This would work. The version you posted does not work because it will never get to 0. It is subtracting once in the if statement, and once in the printf.
You should only have one n-- statement. otherwise even if it reaches 0, when it get the first n-- it will be decremented one more time and will become -1. (so it will never match 0).
Hence, please change your code to
#include <stdio.h>
int main(void) {
int n = 012;
b: printf("%d\n",n--);
if(n!=0){
goto b;
}
return 0;
}
The condition if (n != 0) is always getting satisfied.
On each iteration the number n decreases by 2.
So when n = 2, it gets decremented once after printf("%d\n",n--)
then the condition if (n != 0) is true, as n = 1
Inside the condition block n is decremented once again, n = 0
So the next time before reaching the condition if (n != 0), n equals to -1,
which leads to an infinite loop.
In C 012 is an octal base number, in decimal it is equal to 10.
This code:
#include <stdio.h>
int main(void) {
int n = 012;
b: printf("%d\n",n--);
if(n!=0){
n--;
goto b;
}
return 0;
}
Does this:
print n (10)
subtract 1 of n (n = 10 - 1 = 9)
check if n != 0 (9 != 0 -> true)
subtract 1 of n (n = 9 - 1 = 8)
jump to b
print n (8)
subtract 1 of n (n = 8 - 1 = 7)
check if n != 0 (7 != 0 -> true)
subtract 1 of n (n = 7 - 1 = 6)
jump to b
print n (6)
subtract 1 of n (n = 6 - 1 = 5)
check if n != 0 (5 != 0 -> true)
subtract 1 of n (n = 5 - 1 = 4)
jump to b
print n (4)
subtract 1 of n (n = 4 - 1 = 3)
check if n != 0 (3 != 0 -> true)
subtract 1 of n (n = 3 - 1 = 2)
jump to b
print n (2)
subtract 1 of n (n = 2 - 1 = 1)
check if n != 0 (1 != 0 -> true)
subtract 1 of n (n = 1 - 1 = 0)
jump to b
print n (0)
subtract 1 of n (n = 0 - 1 = -1)
check if n != 0 (-1 != 0 -> true)
subtract 1 of n (n = -1 - 1 = -2)
jump to b
print n (-2)
...
...
... goes on infinitely
Note that when n will never equal to 0 when the program checks if n != 0, because in this evaluation n is always an odd number since two subtractions by 1 are made every time.
Change from b: printf("%d\n",n--); to b: printf("%d\n",n);.
or
Change from
if(n!=0){
n--;
goto b;
to
if(n!=0){
n;
goto b;
Because n-- equals n = n - 1.
Your program:
#include <stdio.h>
int main(void) {
int n = 012;
b: printf("%d\n",n--); // n = n - 1
if(n!=0){
n--; // n = n - 1
goto b;
}
return 0;
}
for out put [10, 8, 6, 4,2, 0]
#include <stdio.h>
int main(void) {
int n = 012;
b: printf("%d\n",n--);
if(n>0){
n--;
goto b;
}
return 0;
}
for [10,9,8,7,6,5,4,3,2,1]
#include <stdio.h>
int main(void) {
int n = 012;
b: printf("%d\n",n--);
if(n>0){
//n--;
goto b;
}
return 0;
}
I have a task (currently studying the loop statement so I'm in the beginner phase)that asks to make a program to reverse an integer number so it must have the do statement .
The output should be (example):
Enter a number: 4568
The reversal is: 8654
Please put in mind that since I'm following my book so far I've studied and know the very basics + selection and loop statements. I have very limited choices so no arrays.
The book suggests to make a do loop to divide repeatedly the number by 10 until it reaches 0 this is what I did so far (code not completed) :
int main(void)
{
int n,x;
printf("Enter a number: ");
scanf("%d", &n);
printf("The reversal is: ");
x = n % 10;
printf("%d",x); /*outputs the last number digit in the first place*/
do{
....
n /= 10; /* for example if I divide the number 56222 by ten the output would come out as
5622 562 56 5*/
....
}while (n!=0);
return 0;
}
I found a way to put the last digit in the first place as you can see but
I'm struggling to figure out how to reverse the rest of the numbers after dividing the number by 10 repeadetly with this very limited choices.
What should I put in the do statement?
Many thanks in advance .
int main(void)
{
int n,x;
printf("Enter a number: ");
scanf("%d", &n);
printf("The reversal is: ");
int rev = 0;
do{
x = n % 10;
rev = rev * 10 + x;
n /= 10;
}while (n!=0);
printf ("%d", rev);
return 0;
}
here you need a new integer rev whose value is 0 initially. Lets take
example of 432
n = 432
when you do x = n % 10 x = 2
so when you do rev = rev * 10 + x rev is 0 and value of rev will be 2
n /= 10 make n = 43
-->so in the next iteration
n = 43
x = n % 10 results in x = 3
rev value now is 2
so rev = rev * 10 + x results in 2 * 10 + 3
so rev = 23
n /= 10 results in n = 4
-->in the last iteration n = 4
x = n % 10 results in x = 4 rev value now is 23
so rev = rev * 10 + x results in 23 * 10 + 4 so rev = 234
n /= 10 results in n = 0
so when you print rev you get the answer 234 reverse of 432
I'm new to programming and currently learning C from The C Programming Language by Brian W Kernighan and Dennis M. Ritchie. I have a difficult at section 1.8. Why when I'm trying to change
for (i = 1; n > 0; --n)
into
for (i = 1; n > 0; ++n)
the output is,
0 1 1
1 2 3
2 2 3
3 2 3
4 2 3
5 2 3
6 2 3
7 2 3
8 2 3
9 2 3
Why this is happen?
Here is the full code,
#include <stdio.h>
int power(int m, int n);
main(){
int i;
for (i = 0; i < 10; ++i)
printf("%d %7d %7d\n", i, power(2,i), power(3,i));
return 0;
}
int power(int base, int n){
int p;
for ( p = 1; n > 0; --n)
p = p * base;
return p;
}
the syntax of for loop is
for (initialization expr; test expr; update expr)
which means, the first expression will be executed once,
the test expression is checked every time for ture or false every time the loop starts, and update expression is evaluated unconditionally on every time loop start.
in this case, n-- is evaluated before checking n > 0, which means the loop will stop when n becomes 0. but if you change it to n++, n will keep on increasing every time of the loop, until the integer is overflowed.
you can change the loop to the following to get output:
for (i = 1; i <= n; ++i)
In the power methods' for-loop you decrement n until in some iteration of the loop n is zero (no longer n > 0). If you now change the loop to increment n every iteration, n will continue growing until you get an integer overflow (Wikipedia: Integer overflow) and n becomes negative at which point your program will terminate.
Can anyone explain to me how does this loop works? I understand that the first operator counts its remainder, the second counts its division result, but I can't understand how does it sum them using the loop? Here's the code:
// Calculate the sum of the digits of the number N.
int N, S, Z;
S = 0;
printf("Input N\n");
scanf("%d", &N);
while(N != 0) {
Z = N % 10;
N = N / 10;
S = S + Z;
}
printf("Sum = %d\n", S);
This while loop adds all the digit of your number referred in by N. It add's all the digit by taking remainder of number when divided by 10. And everytime, it eliminates the last digit of the number. So if your number is 326, it will work like:
326 != 0
Z = 6
N = 32
S = 6
32 != 0
Z = 2
N = 3
S = 8
3 != 0
Z = 3
N = 0
S = 11
0 == 0 come out of loop
print value of S i.e. 11
It's basically a sum of digits of an integer number.
Example:
input ==> 1234
output ==> 4+ 3+ 2 + 1 = 10
Code Break down:
Initialize S [sum] to 0.
Loop:
Z = N % 10; , store the remainder of N after %10 into Z.
N = N / 10; , divide the contents on N by 10 and store the result back in N.
S = S + Z;, sum the content of S with the value in Z.
after that, check the modified value of N is 0 or not. If not, continue [1,2,3..)
Suggestion:
Always check the success of scanf("%d", &N);. If by any chance, scanf() fails, your code is trying to access uninitialized variable N, which may very well lead to undefined behaviour.
The loop will execute until the n value become zero. For example
N=123
Then the first time values of the variables is
Z:3 : N:12 : S:3
Second time
Z:2 : N:1 : S:5
Third time
Z:1 : N:0 : S:6
Finally the answer of S will be 3+2+1=6.
Let's for a example take 657:
Z = N % 10; // This line will store 7 in Z
N = N / 10; // this line will convert N to 65
S = S + Z; // and finally this line will add 0+7