Bitcoin Forum
November 10, 2024, 02:22:01 PM *
News: Latest Bitcoin Core release: 28.0 [Torrent]
 
   Home   Help Search Login Register More  
Warning: One or more bitcointalk.org users have reported that they strongly believe that the creator of this topic is a scammer. (Login to see the detailed trust ratings.) While the bitcointalk.org administration does not verify such claims, you should proceed with extreme caution.
Pages: [1]
  Print  
Author Topic: じじじ C-Programming Mania 0.001฿ Prize じじじ  (Read 816 times)
oksana28 (OP)
Sr. Member
****
Offline Offline

Activity: 402
Merit: 250



View Profile
July 02, 2016, 03:19:22 PM
 #1

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.

Code:

#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 Offline

Activity: 1302
Merit: 1005


New Decentralized Nuclear Hobbit


View Profile
July 03, 2016, 06:20:46 AM
 #2

Code:
void main()
{
int  j=0;
j = 2;

Do you mean int i=0 instead of j?

I just read
Quote
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)
Sr. Member
****
Offline Offline

Activity: 402
Merit: 250



View Profile
July 03, 2016, 06:30:48 AM
 #3



I just read
Quote
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.

Code:
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 Offline

Activity: 1302
Merit: 1005


New Decentralized Nuclear Hobbit


View Profile
July 03, 2016, 06:38:30 AM
 #4

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 Grin
oksana28 (OP)
Sr. Member
****
Offline Offline

Activity: 402
Merit: 250



View Profile
July 03, 2016, 06:41:13 AM
 #5

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 Grin

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 Offline

Activity: 1302
Merit: 1005


New Decentralized Nuclear Hobbit


View Profile
July 03, 2016, 06:55:36 AM
 #6

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)
Sr. Member
****
Offline Offline

Activity: 402
Merit: 250



View Profile
July 03, 2016, 06:57:43 AM
 #7

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 Offline

Activity: 1302
Merit: 1005


New Decentralized Nuclear Hobbit


View Profile
July 03, 2016, 07:02:00 AM
 #8

j=30 means that the value of j must print 30 not string "j=30"

lol jk. I know what you meant. Grin
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.

Sad I was hoping static will make the value unchangeable throughout the code.
ajareselde
Legendary
*
Offline Offline

Activity: 1722
Merit: 1000

Satoshi is rolling in his grave. #bitcoin


View Profile
July 03, 2016, 07:57:35 AM
 #9


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


Cheesy
oksana28 (OP)
Sr. Member
****
Offline Offline

Activity: 402
Merit: 250



View Profile
July 03, 2016, 08:11:42 AM
 #10


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


Cheesy

did you ever ran this program?? or just giving answer.
what is
Quote
exit(exit_code) 
?
edge777
Full Member
***
Offline Offline

Activity: 278
Merit: 104



View Profile
July 03, 2016, 11:36:32 AM
 #11

#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 Offline

Activity: 36
Merit: 0


View Profile WWW
July 03, 2016, 01:14:24 PM
 #12

Hehe, funny task.
Code:
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 Offline

Activity: 1652
Merit: 1016



View Profile
July 03, 2016, 08:36:28 PM
 #13

Hehe, funny task.
Code:
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)
Sr. Member
****
Offline Offline

Activity: 402
Merit: 250



View Profile
July 04, 2016, 01:26:51 PM
 #14

#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 Offline

Activity: 36
Merit: 0


View Profile WWW
July 05, 2016, 08:00:39 AM
 #15

Hehe, funny task.
Code:
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. Wink
Buffer Overflow
Legendary
*
Offline Offline

Activity: 1652
Merit: 1016



View Profile
July 05, 2016, 08:58:20 AM
 #16

Hehe, funny task.
Code:
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. Wink

Just tried it, it works now. Well done.

Never used conio.h anyway, the library isn't on my system.



edge777
Full Member
***
Offline Offline

Activity: 278
Merit: 104



View Profile
February 18, 2017, 02:46:36 PM
 #17

#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!!

Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!