C and C++ challenges

Pages: 12
Hello,

Just for info

I found the C and C++ challenges in this following link http://www.prog2impress.com/puzzles.html which is good for beginners.

Thanks,
Jaaam
Last edited on
Meh.
Most of the problems either depend on the platform, use conio.h, or use non-standard extensions (typeless main(), void main()).
I imagine they were written using Borland's compiler.
Some of the questions don't even make sense. "What is the output of the following code if array name starts with 65486"? What's that supposed to mean?

Nothing really challenging (I'm pretty sure I could find some of these in production code), but okay if you're a beginner, I guess (except for the standard violations, that is).

Oh, also, duplicate thread.
closed account (S6k9GNh0)
I can duplicate the functions using proper or close to code:

1. An error occurs. Explain.
1
2
3
4
5
6
7
8
9
10
#include <stdio.h>

int main()
{
    int i = 300;
    char *ptr = &i;
    *++ptr = 2;
    printf("%d",i);
    getchar();
}

Last edited on
Those 'challenges' are in C, not in C++
closed account (S6k9GNh0)
They're still good fun and apply to C++ as well.
not really:
22.what is the error if not specify the output?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
f(a,b)
int a,b;
{
	return (a=(a==b));
}
main()
{
    int process(),f();
    clrscr();
    printf("the value of process :%d\n",process(f,7,4));
    getch();
}
process(hai,val1,val2)
int (*hai)();
int val1,val2;
{
	return ((*hai)(val1,val2));
}
Would never compile using a C++ compiler
closed account (S6k9GNh0)
hahahaha... Yeah some of these are kinda sketchy...lolol
1
2
3
4
5
f(a,b)
int a,b;
{
	return (a=(a==b));
}


How OLD are some of these puzzles??
Hi, Most of these puzzles are trivial and in fact its only for the beginner. As a beginner we can learn a lot. Nice one
NO, you can't learn from them:
1st challenge:
1. what is the output? Definitely the output is not what you think! so think more.
1
2
3
4
5
6
7
8
main()
{
    int i = 300;
    char *ptr = &i;
    *++ptr = 2;
    printf("%d",i);
    getch();
}
That depends on the computer you are working on, not on C ( nor C++ )
All the challenges (as I already mentioned) are in C written in a way which is incompatible with C++
And as helios already said:
Some of the questions don't even make sense. "What is the output of the following code if array name starts with 65486"? What's that supposed to mean?
agreed.

Nonstandard code fragments, poor programming practices, ancient and outdated syntax. Better off not learning from this since what you learn will be wrong.
To Helios
Whatever you said is correct except for the below one
1
2
3
4
5
6
7
8
. what is the output of the following code if array name starts with 65486.

void main()
{
	int num[] = {10,11,12,13};
	printf("%u %u",num,&num);
}

I could find the answer for 3 rd question and it means a lot
The answer is 65486 65486
65486 denotes the starting address of array.
Last edited on
Whatever you said is correct except for the below one
"Whatever I said"? What do you mean? It's pretty clear what I said.
And no, it's not correct. The question says "... if the array name starts with 65486". Why would you interpret "name" as meaning anything other than "name", such as "address"?
It doesn't make sense for an identifier to start with a digit; and if it does mean "address", then it's improperly redacted, which makes the question invalid, anyway. You can't just go around writing questions that don't make sense and hope your readers will guess what was going through your mind when you made that written disaster.

I don't even want to think what that guy's code must be like.
I don't even want to think what that guy's code must be like.


He is probably trying to prepare newbie programmers for what life is like in the industry...XP
65486 is the starting address of the array!!??

OS and Compiler+version, please. Never assume the address of your variables in a C program (not unless you have the absolute directive in use).
Dear firedraco,

I don't understand what you mean by the following
 
He is probably trying to prepare newbie programmers for what life is like in the industry...XP

Please elaborate.
Thank you.
Oh, god. I just noticed problem 2 (either that or it was updated, so I'll copy it here, just in case it changes).
2. Fill find_addr() so that it finds the address of variable i and j in main(). main() should't be touched
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include<stdio.h>
#include<conio.h>

find_addr()
{
    /*fill your code here*/
}

void main()
{
    int i,j;  
    clrscr();
    find_addr();
    getch();
}

After we ignore the standard compliance issues, any solution to this problem would be a horrible hack.
Last edited on
closed account (S6k9GNh0)
Is it even possible? The function doesn't know the variables exist since it's not in the functions scope plus nothing is passed. How would you go about that? :$
You assume that you know the stack frame the compiler generates (which of course will depend on compiler options and optimization levels).

1
2
3
4
5
void f(){
	int *i=(int*)(unsigned(&i)+92),*j=(int*)(unsigned(&j)+92);
	std::cout <<*i<<","<<*j<<std::endl;
	return;
}

Unsurprisingly, this only compiles and runs correctly on VC++. It breaks if I so much as change the build options.

I feel dirty. I mean, the time I managed to change what a reference points to, that was bad enough. But accessing something in a different stack frame? It goes beyond ugly.
Pages: 12