C programming Puzzles!!!

Status
Not open for further replies.

hafees

In the zone
Here is a c puzzle for u. :)

can u complete the following C program so that the output will look like
Code:
1
2
3
4
5
6
7
8
9
10

int main()
{

    ..... int k; //no other variable should be declared.
    printf("\n%d",k); //no other print statements.
    .............. // u need to write the remaining codes
    
} 

//No loops allowed
//No goto allowed.
//there should be no printing statements otherthan given
//u can't use another function.

Pls try to find the solution.
Some one can add more interesting C puzzles like this.
it will be good for newbies as well as pros

:)

p.s Actually i dont know where z the exact place it should go. pls move it to the appropriate section.
 
OP
hafees

hafees

In the zone
using the conditional operator u mean??
then
( x % 2 )? printf("Odd") : printf("Even");
otherwise u may use the bitwise operator

int x;
(x & 0x1)?printf("Odd") : printf("Even");

is that u mean???
 
OP
hafees

hafees

In the zone
Hi!
The first puzzle was actually asked in an interview to my friend. he couldnt answer it. He asked it to me. i also could nt find an answer first. But later it striked in my mind. and i found the solution :)

Now some one pls try it out!!
 

rajkumar_personal

Ignorance is BLISS !!
@hafees

Why don't u post the solution ?
Most of the ppl here could work that out in a few minutes !
I could do it too but I am not familiar with C but I am a C++ freak !
Ask for a solution in C++ and then I'll give the code out


BTW, u can post the solution and never ask such silly questions for which u already have an answer
 
OP
hafees

hafees

In the zone
Hi rajkumar_personal :)
Why don't u post the solution ?
because i posted the puzzle for the beginners in C. and if i posted it with an answer then no use. they wont think about it.
Most of the ppl here could work that out in a few minutes !
ofcourse may be. and i also want them to solve it. if i were u, i would ve really pleased if i could find a solution. and that will definitly give me a satisfaction as a programmer. Still there is no answer. It is not a lengthy program. just 1 or two lines. then y did nt they post the answer.
I could do it too but I am not familiar with C but I am a C++ freak Ask for a solution in C++ and then I'll give the code out!
every c program (almost)is valid in CPP also. dont u know that. in this puzzle the only thing u need to change is printf. yes u could use a cout instead of printf. (cout<<k; //no other couts && no more streams)
There is no C only code. the same thing applies to CPP also.

TW, u can post the solution and never ask such silly questions for which u already have an answer
From ur reply anyone can understand that u dont know the solution. be cool man. if u dont like the puzzle u just ignore it. i didnt tell u that u should answer it. it is ur wish. u could either answer it or just ignore it. Anyway it is not a silly question.

By the way i want to ask the other people about this. Do u believe the question was too silly to answer? or i did some silly things by putting it here. My intention was just to help the c programming students to get some help and interaction. :(
 
OP
hafees

hafees

In the zone
Yes parth. u r right!!! :)
i used this code

main(int k) //k will be zero if it is invoked with no
parameters
{
printf("\n%d",++k);
return k<=10 ? main(k) : 0;
}
 
OP
hafees

hafees

In the zone
Now try this...
u 've to write the most time efficient program for counting the occurances of each characters. For eg: if we give the input string as "C programming"
then the program should display the output as
a - 1
c - 1
g - 1
i - 1
m - 2
n - 1
o - 1
p - 1
r - 2
Space - 1

:)
 

parth

Broken In
thankx hafees

here is code for your other puzzel


you can modified it for symbols(#@$% etc)

or you for the capital latters


void main()
{
clrscr();
char a[20]="c programming";
int ans[273]={0};
int i=0,j,k;
while(a!='\0')
{
if(a==' ')ans[26]++;
else ans[a-97]++;
i++;
}
for(i=0;i<26;i++)
{
if(ans!=0)
{
printf("%c------------->%d \n",(i+97),ans);
}


}
if(ans[26]!=0)
printf("space--------->%d",ans[26]);
}


give more puzzle on personal massage
 

[lokesh]

Journeyman
Hey hafees...that's good work....i really appreciate it....but i find that something is wrong. The person who knows the answer [ parth in this case ] need not post the answer immediately, the entire thrill behind the puzzle just ends in a single post, so i think the person who knows the puzzle better PM you the answer, and you post whether his answer was right or wrong. And for the solution you can post it after you feel you have given enough time for everyone to try it out. :)
 
OP
hafees

hafees

In the zone
Hey hafees...that's good work....i really appreciate it....
Thanks yaar. i will try to post more. :D

The person who knows the answer [ parth in this case ] need not post the answer immediately, the entire thrill behind the puzzle just ends in a single post, so i think the person who knows the puzzle better PM you the answer, and you post whether his answer was right or wrong. And for the solution you can post it after you feel you have given enough time for everyone to try it out.
Hi man parth! pls keep this in mind. pm ur answer and we can wait for some more time (probably a week or 3 days).

Parth u solved it. i made some changes to ur code sothat it recognizes Capital letters and other ascii characters( i think u put extra code intentionally to count alphabets only. )

Code:
#define CASE_SENSITIVE 1 //comment if u want case insensitive count
int main() //i prefer int main()
{
	char a[]="c programming";
	int ans[255]={0}; // ascii values ranges from 0 to 255
	int i=0;
	clrscr();
	while(a[i]) //until the last character in string (NULL)
	{
		char ch=a[i];
		#ifndef CASE_SENSITIVE //converts to upper char if case sensitive
			ch=toupper(ch);
		#endif
		ans[ch]++; //ch is a character and it is an integer value
			   //i.e ascii value corresponding to the ascii char	
		i++;
	}
	for(i=0;i<255;i++)
	{
		if(ans[i]!=0 && i!=' ') //space count prints seperately
			printf("%c------------->%d \n",i,ans[i]);
	}
	if(ans[' ']!=0)  //if there is space character
		printf("space--------->%d",ans[' ']);
	return 0; //normal exit
}
 

BLITZ~KRIEG!

Broken In
programming puzzles

This is gr8 work m8!!! i suggest the mods create a separate section for programming puzzles.....

Anywayz, keep the prog languages as c++ and java ( java is damn similar to c++).

Here's one :

Let a sum of money be x.
Supposing u r a shopkeeper.
You have to generate the sum of money such that the notes are exactly 30 in number.(allowed notes 500,100,50,10)
 

tuxfan

Technomancer
Here's a new puzzle. Although may not be as short as some of the earlier ones.

Make SAVESCREEN() and RESTORESCREEN() functions in C :) I have done and use them in so many of my C programs under DOS. These ones are pretty useful when one needs to redraw a part of the screen and then restore it back. For example when one needs to display a message or pop up box somewhere in between the program.

Hint: Here're likely syntaxes of the functions. That is the syntax my functions have. There may be better options.
Code:
char *savescrn(t,l,b,r)
usage <char *var> = savescrn(l,t,r,b)

Code:
void restscrn(t,l,b,r,scrn)
 
OP
hafees

hafees

In the zone
Hi tuxfan! i had created a library since 2 yrs back as a part of my project. (for displaying menus & dialog boxes) Here is the code. if someone can one finds it useful, then i ll be satisfied.! may be someone can optimize and add new features.

Code:
/**********************************************************************
		functions for saving screen contents
		     programming - Hafees
          feel free to modify according to ur needs. and if any help is needed just pm me.
***********************************************************************/

#define SEGMENT 0xB000
#define OFFSET 0x8000

#include <dos.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
#include <stdio.h>
#include "screen.h"

int savescreen(int x1,int y1,int x2,int y2,char **buff)
{
	int i,j,countx,county;

	char *buffer;

	//add code to check whether co-ordinates are valid
	buffer=new char[(((x2-x1+1)*(y2-y1+1)*2)+1)];
	//buffer=(char*)malloc(((x2-x1+1)*(y2-y1+1)*2)+1); //allocates memory
	if(buffer==NULL)
		return -1; //returns -1 if memory is not allocated

	//reads and stores window contents to buffer
	countx=0;
	county=0;
	i= ((y1-1)*160) + ((x1-1)*2);
	for(j=0;;j++)
	{
		*(buffer+j)=peekb(SEGMENT,OFFSET+i);
		i++;
		countx++;
		if(countx/2>=x2-x1+1)
		{
			county++;
			countx=0;
			if(county>=y2-y1+1)
				break;
			i=((y1-1+county)*160+((x1-1)*2));
		}

	}
	*(buffer+j+1)=0; //terminates buffer
	*buff=buffer; //points to the new buffer
   	return strlen(buffer);
}

int restore_screen(int x1,int y1,int x2,int y2,char *buffer)
{
	int i,j,countx,county;
	if(buffer==NULL)
		return -1; //returns -1 if buffer is NULL

	//reads and re stores buffer contents to screen
	countx=0;
	county=0;
	i= ((y1-1)*160) + ((x1-1)*2);
	for(j=0;buffer[j];j++)
	{

		pokeb(SEGMENT,OFFSET+i,*(buffer+j));
		i++;
		countx++;
		if(countx/2>=x2-x1+1)
		{
			county++;
			countx=0;
			if(county>=y2-y1+1)
				break;
			i=((y1-1+county)*160)+((x1-1)*2);
			if(!buffer[j])
				break;
		}
	}

	return j/2; //returns number of characters restored
}

//function to restore screen with new attribute
int restore2(int x1,int y1,int x2,int y2,int attrib,char *buffer)
{
	int i,j,countx,county;
	if(buffer==NULL)
		return -1; //returns -1 if buffer is NULL

	//reads and re stores buffer contents to screen
	countx=0;
	county=0;
	i= ((y1-1)*160) + ((x1-1)*2);
	for(j=0;buffer[j];j++)
	{
		if(i%2) //if it is the attribute byte change the attribute
			pokeb(SEGMENT,OFFSET+i,attrib);
		else
			pokeb(SEGMENT,OFFSET+i,*(buffer+j));
		i++;
		countx++;
		if(countx/2>=x2-x1+1)
		{
			county++;
			countx=0;
			if(county>=y2-y1+1)
				break;

			i=((y1-1+county)*160)+((x1-1)*2);
		}
	}
	return j/2; //returns number of characters restored
}


int make_shadow(int x1,int y1,int x2,int y2)
{
	int i,j,countx,county;
	int bg,fg,attrib=0x0f;

	countx=0;
	county=0;
	i= ((y1-1)*160) + ((x1-1)*2);
	for(j=0;;j++)
	{
		if(i%2) //if it is the attribute byte change the attribute
			pokeb(SEGMENT,OFFSET+i,(char)attrib);
		i++;
		countx++;
		if(countx/2>=x2-x1+1)
		{
			county++;
			countx=0;
			if(county>=y2-y1+1)
				break;
			i=((y1-1+county)*160)+((x1-1)*2);
		}
	}
	return j/2; //returns number of characters restored
}


// to check save screen & restore screen

/*main()
{
	char *b,i;
	//saves entire screen
	savescreen(10,3,20,10,&b);
	clrscr();
	printf("\n Press any key to restore the screen with YELLOW text...");
	getch();
	restore2(10,3,20,10,0x3,b);
	getch();
}
*/
 

tuxfan

Technomancer
Cool hafees!! But the code that I have and you have given is different. That shows both are original :D

But I think my code is simpler and trusts the programmer a bit more. It doesn't check for memory allocation and assumes that a C programmer will at least have that much sense :)

Additionally, it doesn't use inbuilt C library functions like peekb() and strlen(). It directly accesses the VDU memory area. So not only reading screen, it will also do that for writing screen.

Here're small steps to use my savescreen function
WARNING: This code was made for C under DOS.
Declare a global variable before main()
Code:
char far *vidmem = 0xb0000000l;

Before using any of my screen manipulation functions, a call must be made to a function called setvduaddr() from main(). It will set the value of that global variable that can be used all throughout. Here's the code.



Code:
void setvduaddr()
{
     char far *mode_addr = 0x449;
     if ( *mode_addr == 7 )
          vidmem = 0xb0000000l;  /* Monochrome Mode */
     else
          vidmem = 0xb8000000l;  /* Any other Mode  */
}

Now all screen manipulation functions can be used. Here's savescreen()

Code:
char *savescrn(t,l,b,r)
/* usage <var> = savescrn(t,l,b,r) */
int t,l,b,r;
{
     int i,j;
     char *save, *rsave;
     i = ( b-t + 1 ) * ( r-l + 1 ) * 2 + 1;
     save = calloc(1,i);
     /*realloc(save,i);*/
     rsave = save;
     if ( l < 0 || t < 0 || r > 79 || b > 24 )
          return('\0');
     for ( i = t; i <= b; i++ )
     {
          for ( j = l; j <= r; j++ )
          {
	       *save = *(vidmem + i*160 + j*2);
	       save++;
               *save = *((vidmem + i*160 + j*2) + 1);
               save++;
          }
     }
     return(rsave);
}

Here's restore screen.

Code:
void restscrn(t,l,b,r,scrn)
int t,l,b,r;
char *scrn;
{
     int i,j;
     if ( l < 0 || t < 0 || r > 79 || b > 24 )
     {
          gotoxy(1,1);
          printf("Out of range   \n\n");
          return;
     }
     for ( i = t; i <= b; i++ )
     {
          for ( j = l; j <= r; j++ )
          {
               *(vidmem + i*160 + j*2) = *scrn;
               scrn++;
               *((vidmem + i*160 + j*2) + 1) = *scrn;
               scrn++;
          }
     }
}
 
Status
Not open for further replies.
Top Bottom