# Qiuz Problems (Which Question you Got?) • ### Question

• Hey, if i m correct, they have given different problems to different students in the Quiz. Let's post problems that we received in the Quiz. I received this Question :-

Adjective series is the product of digits giving the previous value of the series.For a given input value return the perfect and nearest adjective number series until it exists. Example 1: Input: unsigned int value = 6 Output: Returns {6, 16, 28, 47} Explanation: 6 can be product of 1 and 6 and 2 and 3. Hence the next possible number in the series could be 16 or 23 or 32 or 61. Since 16 is the smallest number of all the possible numbers it will be the next number in the series. Similarly 16 can be the product of 2 and 8 or 4 and 4. The possible numbers are 28, 82, 44. Since 28 is the smallest number it will be the next number in the series. By the same logic 47 is the next number. After 47 there is no number possible. Hence the series returned is {47, 28, 16, 6}. Example 2: Input: unsigned int value = 12 Output: Returns {12, 26}

Tuesday, February 20, 2007 12:33 PM

• Perfect Power

x is a perfect square if, for some integer b, x = b². Similarly, x is a perfect cube if, for some integer b, x = b³. More generally, x is a perfect pth power if, for some integer b, x = bp

Given an integer x you are to determine the largest p such that x is a perfect pth power. Write a program to find the perfect power for the given input number.

The method getPower has one non-negative integer value as input and returns b, p and flag value. If the given input has perfect power then return flag value as 1 else return flag value as 0. Suppose if the given number is not having a perfect power then return the value of b and p for the nearest perfect square. Return the value in the order of b, p and flag value.

Example 1: Input:
int value = 343 Output :
Returns {7,3,1}
Where b = 7, p = 3 and flag = 1 (343 has perfect power 73)

Example 2: Input:
int value = 95 Output:
Returns {10,2,0}
Where b = 10, p = 2 and flag = 0 (95 does not have perfect power, therefore the nearest perfect power values of 95 is 10 and 2 (102) = 100)

Wednesday, February 21, 2007 3:59 AM
• My Friends got
Problem :  SetBits

You are given two characters. You Write a program to change the bits of 2 characters.

Method setbits has four parameters x,y,p and n, x and y are 2 characters and p is the position and n is the number of bits. Function should returns c which is a copy of x with the n bits from pth position of x replaced with the rightmost n bits of y leaving the other bits unchanged. If the value of p or n is greater than 8 bit then return as 0.

Example 1:
Input
unsigned char x = Q
unsigned char y = K
int p = 4
int n = 3
Output
New character c = M
Bit Representation of 'Q' = 01010001 and 'K' = 01001011, 3 bits from 4th position of 'Q' replaced with the rightmost 3 bits of 'K' leaving the other bits unchanged the bit representation is 01001101. The character for the new bit is 'M'. Hence 'M' is the output.

Example 2:
Input
unsigned char x = %
unsigned char y = +
int p = 5
int n = 2
Output
New character c = -
Wednesday, February 21, 2007 4:00 AM
• My Friends got Problem : SetBitsYou are given two characters. You Write a program to change the bits of 2 characters.Method setbits has four parameters x,y,p and n, x and y are 2 characters and p is the position and n is the number of bits. Function should returns c which is a copy of x with the n bits from pth position of x replaced with the rightmost n bits of y leaving the other bits unchanged. If the value of p or n is greater than 8 bit then return as 0.Example 1:Input unsigned char x = Q unsigned char y = K int p = 4 int n = 3Output New character c = MBit Representation of 'Q' = 01010001 and 'K' = 01001011, 3 bits from 4th position of 'Q' replaced with the rightmost 3 bits of 'K' leaving the other bits unchanged the bit representation is 01001101. The character for the new bit is 'M'. Hence 'M' is the output.Example 2:Input unsigned char x = % unsigned char y = + int p = 5 int n = 2Output New character c = -

[code]
#include"Setbits.h"
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
void strpad(char str[], int len) {
int i;
strrev(str); for(i=len ; i<8 ; i++) str ='0';
str ='\0';
strrev(str);
}
unsigned char setbits(unsigned char x,int p,int n,unsigned char y) {
char xstr={0}, ystr={0};
char out={0}, c=0;
int i,j;
if( p<=0 || p>8 || n<=0 || n>8) return 0;
itoa(x,xstr,2); itoa(y,ystr,2);
if(strlen(xstr) >8 || strlen(ystr)>8) return 0;
strcpy(out,xstr);
for(i=p-1, j=strlen(ystr)-n ; i<(p-1)+n ; i++, j++)
out = ystr[j];
if(strlen(out)>8) return 0;
for(i=7, j=0 ; i>=0 ; i--, j++) c= c + ((out -'0') * (int)(pow(2,j)));
return c;
}
void dsmain() {
unsigned char x='Q',y='K', c; int p=4,n=3;
printf("Output = [%c]\n",setbits(x,p,n,y));
}

[/code]
Wednesday, February 21, 2007 4:02 AM
• I am also using firefox :P its strange that things dont work always. hehe

guys come up with more questions ....
Thursday, February 22, 2007 12:13 PM
• http://www.homeschoolmath.net/teaching/square-root-algorithm.php
Thursday, February 22, 2007 3:54 PM

### All replies

• My question was:
"Write a program to convert the given infix expression into prefix and postfix expressions"
Tuesday, February 20, 2007 1:47 PM
• Perfect Power

x is a perfect square if, for some integer b, x = b². Similarly, x is a perfect cube if, for some integer b, x = b³. More generally, x is a perfect pth power if, for some integer b, x = bp

Given an integer x you are to determine the largest p such that x is a perfect pth power. Write a program to find the perfect power for the given input number.

The method getPower has one non-negative integer value as input and returns b, p and flag value. If the given input has perfect power then return flag value as 1 else return flag value as 0. Suppose if the given number is not having a perfect power then return the value of b and p for the nearest perfect square. Return the value in the order of b, p and flag value.

Example 1: Input:
int value = 343 Output :
Returns {7,3,1}
Where b = 7, p = 3 and flag = 1 (343 has perfect power 73)

Example 2: Input:
int value = 95 Output:
Returns {10,2,0}
Where b = 10, p = 2 and flag = 0 (95 does not have perfect power, therefore the nearest perfect power values of 95 is 10 and 2 (102) = 100)

Wednesday, February 21, 2007 3:59 AM
• My Friends got
Problem :  SetBits

You are given two characters. You Write a program to change the bits of 2 characters.

Method setbits has four parameters x,y,p and n, x and y are 2 characters and p is the position and n is the number of bits. Function should returns c which is a copy of x with the n bits from pth position of x replaced with the rightmost n bits of y leaving the other bits unchanged. If the value of p or n is greater than 8 bit then return as 0.

Example 1:
Input
unsigned char x = Q
unsigned char y = K
int p = 4
int n = 3
Output
New character c = M
Bit Representation of 'Q' = 01010001 and 'K' = 01001011, 3 bits from 4th position of 'Q' replaced with the rightmost 3 bits of 'K' leaving the other bits unchanged the bit representation is 01001101. The character for the new bit is 'M'. Hence 'M' is the output.

Example 2:
Input
unsigned char x = %
unsigned char y = +
int p = 5
int n = 2
Output
New character c = -
Wednesday, February 21, 2007 4:00 AM
• My Friends got Problem : SetBitsYou are given two characters. You Write a program to change the bits of 2 characters.Method setbits has four parameters x,y,p and n, x and y are 2 characters and p is the position and n is the number of bits. Function should returns c which is a copy of x with the n bits from pth position of x replaced with the rightmost n bits of y leaving the other bits unchanged. If the value of p or n is greater than 8 bit then return as 0.Example 1:Input unsigned char x = Q unsigned char y = K int p = 4 int n = 3Output New character c = MBit Representation of 'Q' = 01010001 and 'K' = 01001011, 3 bits from 4th position of 'Q' replaced with the rightmost 3 bits of 'K' leaving the other bits unchanged the bit representation is 01001101. The character for the new bit is 'M'. Hence 'M' is the output.Example 2:Input unsigned char x = % unsigned char y = + int p = 5 int n = 2Output New character c = -

[code]
#include"Setbits.h"
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
void strpad(char str[], int len) {
int i;
strrev(str); for(i=len ; i<8 ; i++) str ='0';
str ='\0';
strrev(str);
}
unsigned char setbits(unsigned char x,int p,int n,unsigned char y) {
char xstr={0}, ystr={0};
char out={0}, c=0;
int i,j;
if( p<=0 || p>8 || n<=0 || n>8) return 0;
itoa(x,xstr,2); itoa(y,ystr,2);
if(strlen(xstr) >8 || strlen(ystr)>8) return 0;
strcpy(out,xstr);
for(i=p-1, j=strlen(ystr)-n ; i<(p-1)+n ; i++, j++)
out = ystr[j];
if(strlen(out)>8) return 0;
for(i=7, j=0 ; i>=0 ; i--, j++) c= c + ((out -'0') * (int)(pow(2,j)));
return c;
}
void dsmain() {
unsigned char x='Q',y='K', c; int p=4,n=3;
printf("Output = [%c]\n",setbits(x,p,n,y));
}

[/code]
Wednesday, February 21, 2007 4:02 AM
• Wnyone knows how to resolve the above smileys (emoticons) problem ???

i have about 2-3 more questions and solutions... so if this prob of that smiley is solved, ill most more of them....
Wednesday, February 21, 2007 4:04 AM
• Thanks ShadowMan for ur questions and solutions!
Try checking "This post contains a code sample" and if it still doesnt work, post ur Qstns and Solns, one can easily undrstand whts behind those smilies
Wednesday, February 21, 2007 3:04 PM
• My question was

"Write a program to convert the given infix expression into prefix and postfix expressions"

Wednesday, February 21, 2007 4:26 PM
• As you have not specified the details/prototypes of the functions to use, i have written the code entirely in main. may be you can identify it yoruself. and btw, this is the code for infix to postfix. i guess infix to prefix wouldn't be any trouble for you after understanding this.

[code]
#include<stdio.h>
#include<conio.h>
#include<ctype.h>

int PRIORITY(char ch)
{
if (ch=='(' || ch==')')
return 4;
else if (ch=='^')
return 3;
else if (ch=='*' || ch=='/')
return 2;
else if (ch=='+' || ch=='-')
return 1;
else
return 0;
}

void main()
{
char stack,p;
char str={0};
int top=-1,p_cnt=0;
int i,j,k,len;
clrscr();
printf("Enter any Infix Equation : ");
gets(str);
printf("\nSymbol\t\tStack\t\tPostfix");
len=strlen(str);
str[len]=')';
str[len+1]='\0';
for (i=0 ; str ; i++)
{
if (i==0)
{
stack[++top]='(';
printf("\n\t\t(\n\r");
}
printf("%c\t\t",str );
if ( isalnum(str ) )
{
p[p_cnt++]=str ;
}
else
{
if (str !=')')
{
if (PRIORITY(str ) > PRIORITY(stack[top]) || (stack[top]=='(') )
{
stack[++top]=str ;
}
else
{
p[p_cnt++]=stack[top];
stack[top]=str ;
}
}
else if (str ==')')
{
while (stack[top]!='(')
{
p[p_cnt++]=stack[top--];
}
top--;
}
}
for (j=0 ; j<=top ; j++)
{
printf("%c",stack[j]);
}
printf("\t\t");
for (j=0 ; j<p_cnt ; j++)
{
printf("%c",p[j]);
}
printf("\n");
}
printf("\nPostFix Notation : ");
for (j=0 ; j<p_cnt ; j++)
{
printf("%c",p[j]);
}
getch();
}
[/code]

Wednesday, February 21, 2007 4:58 PM
• Guys please post your questions, may be someone here can try to solve them if you haven't solved it.. I just cant wait to solve more C-problems ;)
Wednesday, February 21, 2007 4:58 PM
• Permutation
You have a group of non-zero positive integers, which are not necessarily unique. If you can insert '0' as a digit without restriction on the number of times it can be used in a combination, you can combine the digits and zero to create an infinite number of integers which have exactly those non-zero digits and several zeroes. For example, given the group of digits {1, 2}, you can create the numbers 12, 21, 102, 120, 201, 210, 1002, 1020, 1200, 2001, 2010, 2100, and so on.

As a further twist, you are given a large number and are asked to find the total number of combinations of the digits and zero less than that number.

You will be given the number of non-zero positive integers, an array listing the non-zero positive integers and a positive integer number Maxvalue. You have to return the total number of combinations possible with the integers and zero as stated above less than Maxvalue.

Constraints:
The integers are between 1 and 9, both inclusive.
A single integer with 0 alone is not a valid combination. In the above example 10 and 20 are not valid combinations.
The integers can be used only once in a combination. 0 can be repeated.

Example 1:
Input:
int size = 2
int values[] = {1,3}
char* n = "1035"

Output: Returns 8
Possible combinations: 13, 31, 103, 130, 301, 310, 1003, 1030

Example 2:
Input:
int size = 2
int values[] = {1,2}
char* n = "2500"

Output: Returns 12
Possible combinations: 12, 21, 102, 120, 201, 210, 1002, 1020, 1200, 2001, 2010, 2100

Example 3:
Input:
int size = 3
int values[] = {1,2,3}
char* n = "1030000040000"

Output: Returns 1414
Wednesday, February 21, 2007 5:04 PM
• SumSort

You are given 2 non-zero positive numbers. You are required to sort the sequence of numbers between these two numbers and including the numbers themselves. However the sorting logic is as follows: You have to add the digits of each number to arrive at a sum. You have to then arrange the original numbers themselves in the ascending order of such sums and print the result. If 2 numbers have identical sums then the numbers will be sorted among themselves.

# Constraints You will first sort them in ascending order by their digit sum (sum of their decimal digits).
# Ties are settled by the numbers values, lower numbers first.
# The position value cannot be greater than the difference between rangeHigh and rangeLow.

As input, you are given two numbers, rangeLow and rangeHigh and the position pos of the number you have to return from the sorted sequence. As output, you have to return the number at that position.

Example 1:
Input:
rangeLow = 1
rangeHigh = 10
pos = 1
Output: Returns 10
Explanation: The sorted range is: 1, 10, 2, 3, 4, 5, 6, 7, 8, 9. The value at position 1 is 10 (starting with position 0). Note that 10 comes before 2 since the digit sum of 10 is 1 whereas the digit sum of 2 is 2. Also note that 10 comes after 1 even though they have the same digit sum since 10 is greater than 1.

Example 2:
Input
rangeLow = 78
rangeHigh = 87
pos = 7
The sorted range is 80, 81, 82, 83, 84, 85, 86, 78, 87, 79.
Output: Returns = 78

Wednesday, February 21, 2007 5:05 PM
• Adjective series is the product of digits giving the previous value of the series.For a given input value return the perfect and nearest adjective number series until it exists. Example 1: Input: unsigned int value = 6 Output: Returns {6, 16, 28, 47} Explanation: 6 can be product of 1 and 6 and 2 and 3. Hence the next possible number in the series could be 16 or 23 or 32 or 61. Since 16 is the smallest number of all the possible numbers it will be the next number in the series. Similarly 16 can be the product of 2 and 8 or 4 and 4. The possible numbers are 28, 82, 44. Since 28 is the smallest number it will be the next number in the series. By the same logic 47 is the next number. After 47 there is no number possible. Hence the series returned is {47, 28, 16, 6}. Example 2: Input: unsigned int value = 12 Output: Returns {12, 26}

Solution

`#include<stdio.h>#include<math.h>#include<stdlib.h>#define INPUT 6/* change INPUT value to test with different INPUTS */unsigned getNum(unsigned int n1,unsigned int n2);int* getAdjective(int v);int *series; /* Holds series */void dsmain(){    unsigned int z;    z = INPUT; /* Change value of INPUT directive to different inputs */    series = getAdjective(z);    /* Display the series */    for(z=0;series != -1;z++)    {        printf("%d\n",series );    }}/*Creates Single integer from two numbers passed */unsigned getNum(unsigned int n1,unsigned int n2){    unsigned tmp,n2_digits=0;    tmp = n2;    while(tmp != 0)    {        n2_digits++;        tmp /= 10;    }    n1 = n1 * (unsigned int)pow(10,n2_digits)+n2;    return n1;}/* return AdjectiveSeries for given value*/int* getAdjective(int v){    static int cnt = 0;    int z;    unsigned int x,a,smallest;    smallest = 65000;    if(cnt == 0)    {        series = (int*)malloc(sizeof(int)*20);        series = v;        cnt++;    }    for(x=1;x*x < v;x++)    {        if(!(v%x))        {            if(getNum(x,v/x) <= getNum(v/x,x))            {                a = getNum(x,v/x);            }            else            {                a = getNum(v/x,x);            }            if(a < smallest)            {                smallest = a;            }        }    }    if(smallest < 99)    {        series[cnt++] = smallest;        getAdjective(smallest);    }    else    {         series[cnt++] = -1;    }    return series; }`

Wednesday, February 21, 2007 5:31 PM
• @ashish
how do u paste the code ina well proper format with color?  i have ticked the "This post contains a code sample" but still my code dont appear in god format nor in color. Do you put/add any tabs in front and end of the code ??

Wednesday, February 21, 2007 6:23 PM
• No I didnt did anything other than selecting "This post contains code sample". I too dont know how it happened. I m using FireFox.
Thursday, February 22, 2007 10:53 AM
• I am also using firefox :P its strange that things dont work always. hehe

guys come up with more questions ....
Thursday, February 22, 2007 12:13 PM
• Can anybody tell me how to find the square root of any number without using sqrt() function in C? or at least tell me the logic behind it ?
Thursday, February 22, 2007 2:41 PM
• http://www.homeschoolmath.net/teaching/square-root-algorithm.php
Thursday, February 22, 2007 3:54 PM
• • 