oksana28 (OP)
|
|
July 02, 2016, 03:19:22 PM |
|
Well , I want to start some coding topics here as i belong to former. I will post some easy questions which you guys need to answer , the time allotted will be 24 hrs , you can give multiple answers but the console output must be the same what is required in question. #include<stdio.h> #include<conio.h> void alex() { //your code here } void main() { int j=0; j = 2; for (int i = 1; i<10; i++) { if (i % 2 == 0) { j++; } } alex(); j = 3; printf("%d", j); getch(); }
do whatever you want with alex() but dont touch the code of main() , i just want you to print j=30 at last.will see if anyone can solve this or all guys here are just working as signature workers. Hint: The program contains some dummy loop and you just need to focus on what you want and unnecessary overheads , ignore them. the answer must be with explanation with full description , i dont want just code(no stackoverflow) , in case you are not able to convince me you will miss the prize.Good Luck!
|
|
|
|
ndnh
Legendary
Offline
Activity: 1302
Merit: 1005
New Decentralized Nuclear Hobbit
|
|
July 03, 2016, 06:20:46 AM |
|
void main() { int j=0; j = 2; Do you mean int i=0 instead of j? I just read The definition void main() is not and never has been C++, nor has it even been C. Won't j always be 3? alex(); j = 3; printf("%d", j); getch();
|
|
|
|
oksana28 (OP)
|
|
July 03, 2016, 06:30:48 AM |
|
I just read The definition void main() is not and never has been C++, nor has it even been C. Won't j always be 3? alex(); j = 3; printf("%d", j); getch(); yes , j will always be 3 if you check by inserting break point. but the question is you need to print 30 by configuring the alex(). the question is valid and very conceptual. void main() { int j=0; j = 2; Do you mean int i=0 instead of j? [/quote] no , i have assigned the value to j two times , anyways it is to confuse you and at last again i have assigned value to j..all is to confuse the programmer. the similar question was posted in google code jam
|
|
|
|
ndnh
Legendary
Offline
Activity: 1302
Merit: 1005
New Decentralized Nuclear Hobbit
|
|
July 03, 2016, 06:38:30 AM |
|
yes , j will always be 3 if you check by inserting break point. but the question is you need to print 30 by configuring the alex(). the question is valid and very conceptual. no , i have assigned the value to j two times , anyways it is to confuse you and at last again i have assigned value to j..all is to confuse the programmer. the similar question was posted in google code jam Interesting. I wanted to be sure it is really intended to be j. My knowledge in C++ is very limited. But since you said "easy question" I had to try
|
|
|
|
oksana28 (OP)
|
|
July 03, 2016, 06:41:13 AM |
|
yes , j will always be 3 if you check by inserting break point. but the question is you need to print 30 by configuring the alex(). the question is valid and very conceptual. no , i have assigned the value to j two times , anyways it is to confuse you and at last again i have assigned value to j..all is to confuse the programmer. the similar question was posted in google code jam Interesting. I wanted to be sure it is really intended to be j. My knowledge in C++ is very limited. But since you said "easy question" I had to try yeah , i will be glad if you can solve it , the concept i am talking about is not just limited to c/c++ but is is also applicable in c#,f# not sure about java. the question is very simple yet people often ignore their first lesson. Good luck!
|
|
|
|
ndnh
Legendary
Offline
Activity: 1302
Merit: 1005
New Decentralized Nuclear Hobbit
|
|
July 03, 2016, 06:55:36 AM |
|
Will this work?
#include<stdio.h> #include<conio.h> void alex() { static int j = 30; } void main() { int j=0; j = 2; for (int i = 1; i<10; i++) { if (i % 2 == 0) { j++; } } alex(); j = 3; printf("%d", j); getch(); }
|
|
|
|
oksana28 (OP)
|
|
July 03, 2016, 06:57:43 AM |
|
i think you guys are talking the question wrong i dont want your answer to be like this -> but the required result has to be like this -> j=30 means that the value of j must print 30 not string "j=30"Will this work?
#include<stdio.h> #include<conio.h> void alex() { static int j = 8; } void main() { int j=0; j = 2; for (int i = 1; i<10; i++) { if (i % 2 == 0) { j++; } } alex(); j = 3; printf("%d", j); getch(); }
even if you make j to static then also there is no use of it because after the function alex() , you are assigning j=3 , think carefully its a booby trap. i will be glad if anyone solves it and same you will feel.
|
|
|
|
ndnh
Legendary
Offline
Activity: 1302
Merit: 1005
New Decentralized Nuclear Hobbit
|
|
July 03, 2016, 07:02:00 AM |
|
j=30 means that the value of j must print 30 not string "j=30"
lol jk. I know what you meant. New answer. even if you make j to static then also there is no use of it because after the function alex() , you are assigning j=3 , think carefully its a booby trap. i will be glad if anyone solves it and same you will feel.
I was hoping static will make the value unchangeable throughout the code.
|
|
|
|
ajareselde
Legendary
Offline
Activity: 1722
Merit: 1000
Satoshi is rolling in his grave. #bitcoin
|
|
July 03, 2016, 07:57:35 AM |
|
#include<stdio.h> #include<conio.h> void alex() { printf("30"); exit(exit_code) } void main() { int j=0; j = 2; for (int i = 1; i<10; i++) { if (i % 2 == 0) { j++; } } alex(); j = 3; printf("%d", j); getch(); }
|
|
|
|
oksana28 (OP)
|
|
July 03, 2016, 08:11:42 AM |
|
#include<stdio.h> #include<conio.h> void alex() { printf("30"); exit(exit_code) } void main() { int j=0; j = 2; for (int i = 1; i<10; i++) { if (i % 2 == 0) { j++; } } alex(); j = 3; printf("%d", j); getch(); } did you ever ran this program?? or just giving answer. what is exit(exit_code) ?
|
|
|
|
edge777
|
|
July 03, 2016, 11:36:32 AM |
|
#include<stdio.h> #include<conio.h> #include<stdlib.h> void alex() { printf("30"); getch(); exit(0) ; } void main() { int j=0; j = 2; for (int i = 1; i<10; i++) { if (i % 2 == 0) { j++; } } alex(); j = 3; printf("%d", j); getch(); }
explanation :
1. "exit(exit_code) " this is not right. and moreover you need to include the proper header file for them to work. and the header file in this case is "stdlib.h"
2. In exit code you need to have an integer value.
3. getch() in alex() function because in order to see the result we need to hold the screen before it exits and that is why I have included "getch" function in alex() .
|
|
|
|
xHire
Newbie
Offline
Activity: 36
Merit: 0
|
|
July 03, 2016, 01:14:24 PM |
|
Hehe, funny task. void alex() { printf(" 0\r"); } It simply prints a space for the digit 3 that will be printed later in main() and also prints the zero that is missing to form output "30". "\r" returns carriage to the beginning of the line so that the digit 3 is printed instead of the space.
|
|
|
|
Buffer Overflow
Legendary
Offline
Activity: 1652
Merit: 1016
|
|
July 03, 2016, 08:36:28 PM |
|
Hehe, funny task. void alex() { printf(" 0\r"); } It simply prints a space for the digit 3 that will be printed later in main() and also prints the zero that is missing to form output "30". "\r" returns carriage to the beginning of the line so that the digit 3 is printed instead of the space. That doesn't work in Linux console. The carriage return erases the previous characters. Likewise, alternatively using the "\b" backspace does the same erasing.
|
|
|
|
oksana28 (OP)
|
|
July 04, 2016, 01:26:51 PM |
|
#include<stdio.h> #include<conio.h> #include<stdlib.h> void alex() { printf("30"); getch(); exit(0) ; } void main() { int j=0; j = 2; for (int i = 1; i<10; i++) { if (i % 2 == 0) { j++; } } alex(); j = 3; printf("%d", j); getch(); }
explanation :
1. "exit(exit_code) " this is not right. and moreover you need to include the proper header file for them to work. and the header file in this case is "stdlib.h"
2. In exit code you need to have an integer value.
3. getch() in alex() function because in order to see the result we need to hold the screen before it exits and that is why I have included "getch" function in alex() .
ahh....good! you are correct ! your method is different for what i thought. anyways congrats, let me know your BTC address. thank you
|
|
|
|
xHire
Newbie
Offline
Activity: 36
Merit: 0
|
|
July 05, 2016, 08:00:39 AM |
|
Hehe, funny task. void alex() { printf(" 0\r"); } It simply prints a space for the digit 3 that will be printed later in main() and also prints the zero that is missing to form output "30". "\r" returns carriage to the beginning of the line so that the digit 3 is printed instead of the space. That doesn't work in Linux console. The carriage return erases the previous characters. Likewise, alternatively using the "\b" backspace does the same erasing. If you replace the getch() function, which is from conio.h, by an equivalent function getchar() from stdio.h, you get the result as stated even in Linux console. At least I didn't need to #include another header file like the accepted solution.
|
|
|
|
Buffer Overflow
Legendary
Offline
Activity: 1652
Merit: 1016
|
|
July 05, 2016, 08:58:20 AM |
|
Hehe, funny task. void alex() { printf(" 0\r"); } It simply prints a space for the digit 3 that will be printed later in main() and also prints the zero that is missing to form output "30". "\r" returns carriage to the beginning of the line so that the digit 3 is printed instead of the space. That doesn't work in Linux console. The carriage return erases the previous characters. Likewise, alternatively using the "\b" backspace does the same erasing. If you replace the getch() function, which is from conio.h, by an equivalent function getchar() from stdio.h, you get the result as stated even in Linux console. At least I didn't need to #include another header file like the accepted solution. Just tried it, it works now. Well done. Never used conio.h anyway, the library isn't on my system.
|
|
|
|
edge777
|
|
February 18, 2017, 02:46:36 PM |
|
#include<stdio.h> #include<conio.h> #include<stdlib.h> void alex() { printf("30"); getch(); exit(0) ; } void main() { int j=0; j = 2; for (int i = 1; i<10; i++) { if (i % 2 == 0) { j++; } } alex(); j = 3; printf("%d", j); getch(); }
explanation :
1. "exit(exit_code) " this is not right. and moreover you need to include the proper header file for them to work. and the header file in this case is "stdlib.h"
2. In exit code you need to have an integer value.
3. getch() in alex() function because in order to see the result we need to hold the screen before it exits and that is why I have included "getch" function in alex() .
ahh....good! you are correct ! your method is different for what i thought. anyways congrats, let me know your BTC address. thank you Oh shoot! lol don't know what the fuck was i doing, btw if you're still seeing this here's my address: 1GKbp7H3xJpWwbTzLJBKcqxm7KXZLGTNjQ thanks buddy!!
|
|
|
|
|