Shorthand for return function - c

The code below demonstrate in C language so anyone could easily understand the need. But I use to know many other programming language as well.
Running the code as [1] will give me an error in some programming language & some programming language give warning
[1]:
int get_value(int number) {
if (number == 0) { return 12; }
else if (number == 1) { return 21; }
// return;
}
So, I must code something like [2] to clear the error/warning
[2]:
int get_value(int number) {
int ret = 0;
if (number == 0) { ret = 12; }
else if (number == 1) { ret = 21; }
return ret;
}
QUESTION:
How do I shorthanded the code in [2] as the code in [1] without any error/warning. And the expected code as short as [1]

imagine that the parameter number = 3
then what would you return ?
you can simply write at the end of the function return 0;
so:
int get_value(int number) {
if (number == 0) { return 12; }
else if (number == 1) { return = 21; }
return 0;
}
so you should think of different values of the parameter called number.

In the first code snippet compilers issue a message because the function returns nothing if number is not equal to 0 or 1.
You could write for example
int get_value(int number) {
if (number == 0) { return 12; }
else if (number == 1) { return 21; }
else { return 0; }
}
Or you could write
int get_value(int number) {
int ret[] = { 12, 21 };
if ( 0 <= number && number <= 1 ) { return ret[number]; }
else { return 0; }
}
Or you could use the conditional operator like for example
int get_value(int number) {
return number == 0 ? 12 : number == 1 ? 21 : 0;
}

By universal convention (including in C), 0 is regarded as false and all other values are true.
So it may make sense to code:
int get_value(int number) {
if (number == 0) {
return 12;
}
return 21;
}
As others point out you don't appear to have defined a return value for all possible inputs. It maybe that your function is only ever called with 0 or 1. If that's the case the parameter name number is poorly chosen.
But "defensive programming" says you should deal with all cases.
You either map all inputs into legitimate return values or introduce an error handler or trap.
Here's an error trap version using the assert() macro.
#include <assert.h>
int get_value(int number) {
assert((number==0)||(number==1));
if (number == 0) {
return 12;
}
return 21;
}
If the condition provided to the assert() macro is false, execution ends printing a diagnostic message.

These warnings/errors arise because your function declaration states that it should always return an int:
int get_value(int number) {
In the first version of your code, it would not do that for numbers other than 0 or 1:
int get_value(int number) {
if (number == 0) { return 12; }
else if (number == 1) { return 21; }
}
For example, what would this do if you called it with number set to 2?
The second example always returns an int, thus honouring the "contract" defined by the function declaration:
int get_value(int number) {
if (number == 0) { return 12; }
else if (number == 1) { return 21; }
return 0;
}
In some (non-C) programming languages -- such as Ruby or Javascript -- the former would be allowed due to nil/None/undefined being used as an implicit return value, but this is because function declarations do not specify that they "must" return an int value in those languages:
// Javascript example:
function get_value(number) {
if (number == 0) { return 12; }
else if (number == 1) { return 21; }
}
get_value(2) // Returns `undefined`.

How do I shorthanded the code..??
You can study the problem, and use branchless programming to give you the results that you want.
#include <stdio.h>
int get_value( unsigned int n ) {
return (n<2)*(0x2AC>>(5*n))&0x1F;
}
int main() {
for( int i = 0; i < 10; i++ )
printf( "%i: %2d %2d\n", i, get_value( i ), (i<2)*(0x2AC>>(5*i))&0x1F );
return 0;
}
0: 12 12
1: 21 21
2: 0 0
3: 0 0
4: 0 0
5: 0 0
6: 0 0
7: 0 0
8: 0 0
9: 0 0

Related

Check if binary search tree contains more evens numbers than odds

I want to implement a function to check if a BST (of int values) contains more odds it should return 2, if it contains more evens return 1 and 0 otherwise - recursively.
I was trying to create help function with inputs of even and odd counter but the function returns wrong output.
My code:
int evenOdd(Tnode* root) {
if (root == NULL)
return 0 ;
return eVenOddHelp(root , 0 , 0) ;
}
int eVenOddHelp(Tnode* root , int even , int odd) {
if (root == NULL) {
if (even > odd)
return 1 ;
else if (odd > even)
return 2 ;
return 0 ;
}
else {
if (root->data % 2 == 0) {
eVenOddHelp(root->left , even + 1 , odd) ;
eVenOddHelp(root->right , even + 1 , odd) ;
}
else {
eVenOddHelp(root->left , even , odd + 1) ;
eVenOddHelp(root->right , even , odd + 1) ;
}
}
}
IMO the easiest way is to use the return value, if return positive there are more evens, if negative there are more odds, 0 otherwise:
int evenOdd(Tnode* root) {
int result = eVenOddHelp(root);
if(result > 1) return 1; // More evens
else if(result == 0) return 0; // the same
else return 2; // More odds
}
int eVenOddHelp(Tnode* root) {
if (root != NULL) {
int isEven = (root->data % 2 == 0) ? 1 : -1;
return eVenOddHelp(root->left) + eVenOddHelp(root->right) + isEven;
}
return 0;
}
Comparing like this:
if (root == NULL) {
if (even > odd)
return 1 ;
else if (odd > even)
return 2 ;
return 0 ;
}
Will produce wrong values because there is several times where the root == NULL is true, you will comparing evens and odds in different subtrees. And not at the end of the entire tree.
There's an easier way to do this by using counters passed in as mutable pointers:
void modCounter(Tnode* node, int* even, int* odd) {
if ((node->data % 2) == 0) {
(*even)++;
}
else {
(*odd)++;
}
if (node->left) {
modCounter(node->left, even, odd);
}
if (node->right) {
modCounter(node->right, even, odd);
}
}
Note that it just alters the pointed values, so you call it like this:
int evenOdd(Tnode* root) {
if (root == NULL)
return 0;
int even = 0;
int odd = 0;
modCounter(root, &even, &odd);
if (even > odd) {
return 1;
}
if (odd > even) {
return 2;
}
return 0;
}
To use a more C-based approach you could even pass in an array of two int values, as in:
void modCounter(Tnode* node, int* counters, int mod) {
++counters[node->data % mod];
// ...
}
Where you call it like this:
int counters[2];
modCounter(root, &counters[0], 2);

Made a script on the Luhn's Alg. Unable to understand how to create and call functions clearly for revised script

I am new to programming. I am currently taking online lectures quite rigorously and completed a task using Luhn's Algorithm. It is just one script that runs straight through, but for my future-self I want to learn to code more efficiently as projects get bigger.
That is where my problem comes in. I cannot seem to understand how to define or call functions correctly and unable to revise my script into something more "efficient".
(Everything is already submitted and my script completes the task perfectly, according to the bot, so I am not trying to half-arse my work here just to be clear.)
This is the completed script with only the main function that runs straight through and took me about 12-15 hours to get it working without error from the beginning. Everything is written in C
#include <stdio.h>
#include <cs50.h>
#include <math.h>
int main(void)
{
// this grabs the number and verifies the correct amount of digits
int count;
long number = 0;
do
{
number = get_long("Number: ");
count = (number == 0) ? 1 : (log10(number) + 1);
if (count < 13 || count == 14 || count > 16)
{
printf("INVALID\n"); // This is to satisfy the uni. bot command check. Need a EOF for numbers with the wrong amount of digits.
return (0);
}
}
while (count < 13 || count == 14 || count > 16);
//printf("Digits: %i\n", count); // test print for debugging
//This finds the first two digits of input number
long int two = number;
while (two >= 100)
{
two = two / 10;
}
//printf("First two numbers: %li\n", two); // test print for debugging
// verifies card using mod10 (Luhn's)
long sum = 0;
long bigdigit = 0;
//printf("\nLUHN Number: %li\n\n", number); // test print for debugging
if (count == 13 || count == 15)
{
count += 1;
}
for (int i = count; i > 0; i--)
{
if (i % 2 == 0)
{
sum += (number % 10);
}
else
{
bigdigit = (2 * (number % 10));
sum += (bigdigit / 10 + bigdigit % 10);
}
number = (number / 10);
//printf("\nI : %i\n", i); // test print for debugging
//printf("Sum: %li\n", sum); // test print for debugging
//printf("Number: %li\n", number); // test print for debugging
}
if (sum % 10 == 0)
{
printf("VALID\n");
}
else
{
printf("INVALID\n");
return (0);
}
// checks what type of card
if (two == 34 || two == 37)
{
printf("AMEX\n");
return (0);
}
else if (two >= 51 && two <= 55)
{
printf("MASTERCARD\n");
return (0);
}
else if (two >= 40 && two <= 49)
{
printf("VISA\n");
return (0);
}
else
{
printf("INVALID\n");
return (0);
}
}
I was trying to split it into 3 functions of main to call.
long input_number();
bool luhn_check();
void company_check();
I am stuck with the second function and not sure if the third should be a void or not.
"Revised" Script v2
#include <stdio.h>
#include <cs50.h>
#include <math.h>
long input_number(long CCN);
int counter(long CCN, int count);
bool luhn_check(long CCN, int count);
long firsttwo(long CCN, long two);
void card_type(long two);
int main()
{
long CCN = 0;
int count = 0;
long two = 0;
CCN = input_number(CCN);
count = counter(CCN, count);
//printf("CCN: %li\n", CCN); //debugging purposes
//printf("Digits: %i\n", count); //debugging purposes
luhn_check(CCN, count);
two = firsttwo(CCN, two);
//printf("First Two: %li\n", two); //debugging purposes
card_type(two);
}
// this grabs the number and verifies the correct amount of digits
long input_number(long CCN)
{
int count = 0;
do
{
CCN = get_long("Number: ");
count = (CCN == 0) ? 1 : (log10(CCN) + 1);
if (count < 13 || count == 14 || count > 16)
{
//printf("INVALID\n"); // This is to satisfy the uni. bot command check. Need a EOF
//return (0);
}
}
while (count < 13 || count == 14 || count > 16);
return (CCN);
}
int counter(long CCN, int count)
{
do
{
count = (CCN == 0) ? 1 : (log10(CCN) + 1);
}
while (count < 13 || count == 14 || count > 16);
return (count);
}
// verifies card using mod10 (Luhn's)
bool luhn_check(long CCN, int count)
{
long sum = 0;
long bigdigit = 0;
//printf("\nLUHN Number: %ld\n\n", CCN); // test print for debugging
if (count == 13 || count == 15)
{
count += 1;
}
for (int i = count; i > 0; i--)
{
if (i % 2 == 0)
{
sum += (CCN % 10);
}
else
{
bigdigit = (2 * (CCN % 10));
sum += (bigdigit / 10 + bigdigit % 10);
}
CCN = (CCN / 10);
//printf("\nI : %i\n", i); // test print for debugging
//printf("Sum: %li\n", sum); // test print for debugging
//printf("Number: %li\n", CCN); // test print for debugging
}
if (sum % 10 == 0)
{
printf("VALID\n");
return (true);
}
else
{
printf("INVALID\n");
return (false);
}
}
// grabs the first two numbers
long firsttwo(long CCN, long two)
{
two = CCN;
//printf("TWO CCN: %li\n", two); // debugging purposes
while (two >= 100)
{
two = two / 10;
}
return (two);
}
// finds card type and ends
void card_type(long two)
{
if (two == 34 || two == 37)
{
printf("AMEX\n");
//return (0);
}
else if (two >= 51 && two <= 55)
{
printf("MASTERCARD\n");
//return (0);
}
else if (two >= 40 && two <= 49)
{
printf("VISA\n");
//return (0);
}
else
{
printf("INVALID\n");
//return (0);
}
}
I have completed the second version of the script with your suggestions, bar the str of input, I will try to tackle that method in the next version as I have not handled that type yet.
Besides running the program with a string rather than b, is there anything I could have done more efficiently?
Let me start with a few notes on your function input_number:
long input_number()
{
// this grabs the number and verifies the correct amount of digits
int count;
[ .... ]
while (count < 13 || count == 14 || count > 16);
// Once this while loop starts, it will never terminate!
// if count starts at 10 (which is less than 13), the body says "do nothing".
// so count will continue to be unchanged, and the while-loop will continue to run.
return (0); // Code after a return statement will never be reached
// So your printf-statement below will NEVER happen.
// It also seems unlikely you want to return Zero.
// You probably want to return count, or some other value.
printf("Digits: %i\n", count); // test print for debugging
}

Recursive function in C to determine if digits of an integer are sorted ascending, descending or neither

I need to write a recursive function that returns 1 if digits of a whole number are ascending (left to right), return -1 if descending or return 0 if neither.
My solution attempt returns 0 every time and I know why but I don't know how to get around it.
Here's my code:
#include <stdio.h>
int check_order(int n)
{
if (n % 10 > n / 10 % 10)
{
return check_order(n / 10);
if (n == 0)
{
return 1;
}
}
else if (n % 10 < n / 10 % 10)
{
return check_order(n / 10);
if (n == 0)
{
return -1;
}
}
else
{
return 0;
}
}
int main()
{
int n;
printf("enter a whole number (n > 9):");
scanf_s("%d", &n);
printf("function returned: %d\n", check_order(n));
}
Here's a simple recursion:
int f(int n){
if (n < 10)
return 0;
int dr = n % 10; // rightmost digit
n = n / 10;
int dl = n % 10; // second digit from the right
int curr = dl < dr ? 1 : -1; // current comparison
if (dl == dr) curr = 0; // keep strict order
if (n < 10)
return curr;
return curr == f(n) ? curr : 0; // are the comparisons consistent?
}
Explain your algorithm?
Suppose you use the following:
You are given a number.
You need to turn that number into a sequence of digits.
If you are given a number, you can convert that number to a sequence of digits.
If you are given a sequence of digits, use
that.
Compare each pair of digits -> ascending, descending, or neither.
Combine the results from each pair, sequentially/recursively.
We can use a string to make the digit comparisons easier, and accept very long sequences of digits.
We can use an enum(erated) type to represent the ordering.
How do you combine the results? Define a function that combines the order of two adjacent, overlapping pairs, then you can combine results.
#include <stdio.h>
#include <string.h>
typedef enum { descending=-1, other=0, ascending=1 } order_t;
order_t pair_order(int a, int b) {
if( a < b ) return ascending;
if( a > b ) return descending;
return other;
}
//strict (increasing/decreasing)
order_t strict_order( order_t x, order_t y ) {
if( x == y ) return x;
return other;
}
//monotone (increasing/decreasing)
order_t monotone_order( order_t x, order_t y ) {
if( x == y ) return x;
if( other == x ) return y;
if( other == y ) return x;
return other;
}
order_t check_order( char* p, int remain ) {
//printf("p:%s\n",p); //uncomment to watch progress
if( remain<2 ) return other;
if( remain==2 ) return pair_order(p[0], p[1]);
return strict_order( pair_order(p[0], p[1]), check_order(p+1, remain-1) );
//return monotone_order( pair_order(p[0], p[1]), check_order(p+1, remain-1) );
}
char* order_name[] = {
"descending",
"other",
"ascending"
""
};
int main()
{
char line[666] = "none";
while ( strlen(line) > 0 ) {
printf("enter a number (at least 2 digits):");
fgets(stdin,line,sizeof(line)-1);
if( strlen(line) > 0 && line[strlen(line)-1] == '\n' )
line[strlen(line)-1] = '\0';
order_t order = check_order(line);
printf("function returned: (%d)%s\n", order, order_name[order+1]);
}
}
I think you were started on the right track but need to flesh out your code more. My solution borrows on that of #ChuckCottrill as I like his enum but I don't like that he doesn't play the ball as it lays (i.e. converts to a string instead of dealing with the int.) I also borrow the nice test examples of #ggorlen but I don't like that solution either as it can take multiple passes through the number to figure out the answer when only one pass should be needed:
#include <stdio.h>
typedef enum { descending=-1, other=0, ascending=1 } order_t; // a la #ChuckCottrill
order_t check_order(int n)
{
if (n > 9) {
int right = n % 10;
int left = n / 10 % 10;
if (right > left) {
n /= 10;
if (n > 9) {
return (ascending == check_order(n)) ? ascending : other;
}
return ascending;
}
if (right < left) {
n /= 10;
if (n > 9) {
return (descending == check_order(n)) ? descending : other;
}
return descending;
}
}
return other;
}
int main() { // a la #ggorlen
printf("12345: %d\n", check_order(12345));
printf("54321: %d\n", check_order(54321));
printf("54323: %d\n", check_order(54323));
printf("454321: %d\n", check_order(454321));
printf("1: %d\n", check_order(1));
printf("12: %d\n", check_order(12));
printf("21: %d\n", check_order(21));
}
OUTPUT
> ./a.out
12345: 1
54321: -1
54323: 0
454321: 0
1: 0
12: 1
21: -1
>
A version that works for any length since it takes the string as parameter.
And feeding the recursive function with previous status (ascending or descending) allows for some shorter code and less functions.
int check_order(char *str, int index, int previous) {
char current = str[index]; // char at index
char next = str[index+1]; // char at index+1
if (current == 0 || next == 0) {
return previous; // End of string
}
// Ascending or descending?
int status = next > current ? 1 : (next < current ? -1 : 0);
if (status == 0 || index > 0 && status != previous) {
// If neither -1/1 nor status == previous (while not initial call)
return 0;
}
return check_order(str, index+1, status); // Check from next index
}
The main function must ensure the string is at least 2 chars
int main(int argc, char **argv) {
char *str = *++argv;
// Some optional checks on str here... (like this is a number)
int status = 0; // Default value if string length < 2
if (strlen(str) >= 2) {
status = check_order(str, 0, 0);
}
printf("Check order for %s is %d\n", str, status);
return 0;
}
Code after a return statement like this is unreachable:
return check_order(n / 10);
if (n == 0)
{
return -1;
}
Beyond this, you're on the right track of checking the current digit against the next digit, but I don't see a clear base case (when n < 10, that is, a single digit).
Trying to check ascending and descending in one recursive function is difficult to manage. In particular, communicating state between stack frames and determining which cases are still valid at a given call suggests that the return value is overworked.
To save having to return a struct or use an enum or magic numbers as flags, I'd write two general helper functions, ascending_digits and descending_digits.
#include <stdbool.h>
#include <stdio.h>
bool ascending_digits(int n) {
if (n < 10) return true;
if (n % 10 < n / 10 % 10) return false;
return ascending_digits(n / 10);
}
bool descending_digits(int n) {
if (n < 10) return true;
if (n % 10 > n / 10 % 10) return false;
return descending_digits(n / 10);
}
int check_order(int n) {
if (ascending_digits(n)) return 1;
if (descending_digits(n)) return -1;
return 0;
}
int main() {
printf("12345: %d\n", check_order(12345));
printf("54321: %d\n", check_order(54321));
printf("54323: %d\n", check_order(54323));
printf("454321: %d\n", check_order(454321));
printf("1: %d\n", check_order(1));
printf("12: %d\n", check_order(12));
printf("21: %d\n", check_order(21));
return 0;
}
Output:
12345: 1
54321: -1
54323: 0
454321: 0
1: 1
12: 1
21: -1
Not only are these functions easier to understand and maintain individually, they're also more reusable than if they were inseparably tied together.
This doesn't handle negative numbers--you could apply abs and go from there if you want. Same goes for handling equal values; this implementation accepts numbers such as 1223 but you could use <= to enforce strict ordering.

Test result fails for my code of Inspect Bits function

Below code is for a test sample given in https://www.testdome.com/for-developers/solve-question/9780
The question is: Implement the inspect_bits function that checks if given number contains 2 or more consecutive ones in its binary representation. If it does, the function should return 1. Otherwise, it should return 0.
For example, inspect_bits(13) should return 1 as it contains 2 consecutive ones in its binary representation (1101).
My code is:
#include <stdlib.h>
#include <stdio.h>
int inspect_bits(unsigned int number)
{
unsigned int ref = 1;
int comp;
for (int i = 0; i< sizeof(number) * 8; i++)
{
int a = number& (ref << i);
printf("%d: a is %d\n", i, a);
int b = number& (ref << (i + 1));
printf("%d: b is %d\n", i, b);
if ((a != 0) && (b != 0))
{
return 1;
}
}
return 0;
}
#ifndef RunTests
int main()
{
printf("%d", inspect_bits(13));
}
#endif
The result seems ok, but the system tells:
Various numbers: Wrong answer
Can you help to modify my code?
Regards
To be honest, I think it's an issue with the test site itself. Your code returns the proper results for each test case given to it, and I even modified the code as such:
int inspect_bits(unsigned int number)
{
for (int i = 0; i < sizeof(number) * 8; ++i) {
if (((number & (1 << i)) != 0) && ((number & (1 << (i + 1))) != 0)) {
return 1;
}
}
return 0;
}
The test cases return 1 where there are 2 binary values together and works for 3 and above; however, running this code on the test site and it gives the error that the Various Numbers test fails.
Interestingly, using this code:
int inspect_bits(unsigned int number)
{
while (number >= 3) {
if ((number & 3) == 3) { return 1; }
number >>= 1;
}
return 0;
}
Which does basically the same thing, only using bit-shifting on a single number, and the test passes 100% ..
You could submit an e-mail explaining the error; but beyond that, I'm not sure what else it could be.
Hope that helps.
int flag = 0;
int inspect_bits(unsigned int number)
{
int *arr;
int i = 0;
number = convert(number);
while(number)
{
arr[i] = number % 10;
number /= 10;
i++;
}
for(int j = 0; j < i-1; j++)
{
if(arr[j] == arr[j+1])
{
flag = 1;
return flag;
}
}
return flag;
}
int convert (int num)
{
if(num == 0)
{
return 0;
}
else
{
return (num % 2 + 10 * convert(num / 2));
}
}
This is what I did and it said Various Words: Wrong Answer. It appears to be an issue with the test site. Some other questions on their site evaluates questions incorrectly. The ones that I've come across are all C programs. C++ works fine in my experience.
By my experience in testdome almost any exercise right solution has to do with efficiency of the algorithm
This code worked for me:
#include <stdlib.h>
#include <stdio.h>
int inspect_bits( unsigned int number ) {
do {
if( ( number&3 )==3 ) return 1;
} while( number>>=1 );
return 0;
}
#ifndef RunTests
int main () {
printf( "%d", inspect_bits( 13 ) );
}
#endif
In the code you posted, the for loop checks all the bits from the function's input argument 'number'. That's not enough efficient.
The point is that we don't have to wait until the complete number has been completely right shifted.
They say, we must check if there are 2 or more consecutive ones in its binary representation, in other words, function returns 1 if a minimum of 2 consecutive bits with value 1 are found, and the fewer value with 2 consecutive ones is a decimal 3 ( 3 = 0b00000011 ).
So we are able to check it comparing the number with 3 using an AND gate, and right shift to 'number' until it happens.
Let's take a different number than the example's one:
221 = 0b11011101 we just only need to compare 3 times and shift it 2 times.
0b11011101 (221)
& 0b00000011 ( 3)
------------------
= 0b00000001 ( 1)
0b11011101(221) >> 1 = 0b01101110(110)
0b01101110 (110)
& 0b00000011 ( 3)
------------------
= 0b00000010 ( 2)
0b01101110(110) >> 1 = 0b00110111(55)
0b00110111 (55)
& 0b00000011 ( 3)
------------------
= 0b00000011 ( 3) ----> FOUND! return 1

Why can't I enter more inputs after entering bigger than 9 x values?

#include <stdio.h>
main()
{
int n;
int x;
int h=24;
int s=9;
scanf("%d",&n);
while (n>0)
{
n=n-1;
scanf("%d",&x);
while (x<=9)
{
if (x==1)
printf("2\n");
if (x==2)
printf ("3\n");
if (x==3)
printf ("5\n");
if (x==4)
printf("7\n");
if (x==5)
printf("11\n");
if (x==6)
printf("13\n");
if (x==7)
printf("17\n");
if (x==8)
printf("19\n");
if (x==9)
printf("23\n");
break;
}
while (23<h<542 && x>9)
{
h=h+1;
if ( (h%2)!=0 && (h%3)!=0 && (h%5)!=0 && (h%7)!=0 && (h%11)!=0 && (h%13)!=0 && (h%17)!=0 && (h%19)!=0 && (h%23)!=0 )
{
s=s+1;
if (x==s)
printf("%d\n",h);
}
}
}
}
The question for the code is to enter n which will be the number of the following inputs. Each input must give the xth prime number.
example:
input:
3
,4
,20
,50
output:
7
,71
,229.
x can be between 1 and 100. (the first 100th prime numbers)
Now my problem is with x>9.after entering one value for it, it won't accept anymore values for x.
I would like to know why this happens and how to fix it.
I'm also very new to programming and haven't learned arrays yet.(I know scanfs aren't the best thing but that's all I've learned so far)
This line:
while (23<h<542 && x > 9)
is creating an infinite loop when x is greater than 9. 23 < h < 542 doesn't test whether h is between 23 and 542. That expression is equivalent to (23 < h) < 542. (23 < h) is 1 if 23 is less than h, which is always the case because h starts as 24 and the loop increases it. And 1 is always less than 542.
What you want is:
if (x > 9) {
while (h < 542)
...
}
}
There's no need to test x each time through the loop, because it never changes within the loop. And there's no need to test 23 < h, because that's always true.
When you do need to check whether a variable is between two numbers, the way to do it is with 23 < h && h < 542.
Another problem is that when you enter multiple numbers higher than 9, you're not resetting h and s back to their initial values before the while loop that looks for higher primes. You should initialize those values right before the loop, not just at the top of the program.
You can also make use of cascaded if/else if/else so you don't need to test x at the end (or you could use switch/case).
Here's the full rewritten program:
#include <stdio.h>
main() {
int n;
int x;
scanf("%d",&n);
while (n>0) {
n=n-1;
scanf("%d",&x);
if (x==1) {
printf("2\n");
} else if (x==2) {
printf ("3\n");
} else if (x==3) {
printf ("5\n");
} else if (x==4) {
printf("7\n");
} else if (x==5) {
printf("11\n");
} else if (x==6) {
printf("13\n");
} else if (x==7) {
printf("17\n");
} else if (x==8) {
printf("19\n");
} else if (x==9) {
printf("23\n");
} else {
int h = 24;
int s = 9;
while (h<542) {
h=h+1;
if ( (h%2)!=0 && (h%3)!=0 && (h%5)!=0 && (h%7)!=0 && (h%11)!=0 && (h%13)!=0 && (h%17)!=0 && (h%19)!=0 && (h%23)!=0 ) {
s=s+1;
if (x==s) {
printf("%d\n",h);
}
}
}
}
}
}
DEMO

Resources