Click here to Skip to main content
15,887,464 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
i want to check if the the numbers in array are power of 2,
i wrote the folowing code but it doesn't work it skips the part that chiks if the number is power of two and prints the last sentence.
also if someone can help me in how to check if the input is number and not any other charctar.
thank you!

What I have tried:

#include <stdio.h>
#include <stdlib.h>
int main()
{
    int x;
    int i;
    int k;
    int count=0;
    int a;
    int sum=0;
    printf("Enter size of input:\n");
    scanf("%d",&x);
    int *numbers=malloc(sizeof(int)*x);
    if (x<0){
      printf("Invalid size\n");
    }
    else {
       printf("Enter numbers:\n");
       for(i=0;i<x;++i){
         scanf("%d",&numbers[i]);
       }
    }
    for(k=0;k<x;++k)
    {
        count=0;
        a=numbers[k];
        while (((numbers[k] % 2) == 0) && numbers[k] > 1){ /* While x is even and > 1 */
             numbers[k]/= 2;
             ++count;
        }
        if (numbers[k]==0){
             printf("The number %d is a power of 2:%d=2^%d\n",a,a,count);
             sum+=a;
        }
    }
    printf("Total exponent num is %d\n",sum);
  return 0;
}
Posted
Updated 5-Apr-17 2:40am
Comments
PIEBALDconsult 4-Apr-17 20:35pm    
A spot of debugging should do the trick. I particularly think you should examine numbers[k] after the while loop and before you test for zero.
Member 13106348 4-Apr-17 20:37pm    
i tried debugging didn't work :\ i still don't know the problem .. i changed the if statmnet to if (numbers[k]==0) and now it works only for input that is one number.
PIEBALDconsult 4-Apr-17 20:42pm    
What was it before?
What set of numbers did you test with? What result did you get?
[no name] 4-Apr-17 21:44pm    
Yes I know what you mean. You ran the program and thought you was debugging it but didn't bother using breakpoints to step through your code and examine the variables. So, no you did not debug your program to see what it was doing.
[no name] 4-Apr-17 20:36pm    
Learning how to use the debugger is a *really* useful skill that you must learn.

Quote:
i tried debugging didn't work

This quote suggest that you don't know how to use the debugger. the debugger only show you what your code is doing, you have to inspect variables and check if everything is consistent with your expectations.
For a fast starting, you can find tutos on youtube.

This line is in wrong place because it will try to allocate memory whatever is the value of x. You need to make sure that x is positive before memory allocation.
C++
int *numbers=malloc(sizeof(int)*x);


When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute, it is an incredible learning tool.

Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
 
Share this answer
 
v4
You know, the C programming language does not prevent you defining functions...
Try
C
#include <stdio.h>
#include <stdlib.h>


int powoftwo(int x)
{
  int count;

  for ( count = 0; x && ((x & 1) == 0); ++count )
    x >>= 1;

  if ( x == 1)
    return count;

  return -1;
}


int main()
{
    int x;
    int i;
    int k;
    printf("Enter size of input:\n");
    scanf("%d",&x);
    int *numbers= NULL;
    if (x<0){
      printf("Invalid size\n");
      return -1;
    }
    else
    {
      numbers = malloc(sizeof(int)*x);
      if ( ! numbers)
      {
        printf("memory allocation failure\n");
      }

       printf("Enter numbers:\n");
       for(i=0;i<x;++i){
         scanf("%d",&numbers[i]);
       }
    }
    for(k=0;k<x;++k)
    {
      int exp = powoftwo(numbers[k]);
      if ( exp > 0)
        printf("The number %d is a power of 2:%d=2^%d\n", numbers[k],numbers[k],exp);

    }
    free(numbers);
    return 0;
}
 
Share this answer
 
Comments
PIEBALDconsult 5-Apr-17 9:35am    
And getting the exponents?
CPallini 5-Apr-17 15:28pm    
powoftwo returns
-1, if the passed value is not a power of two.
the exponent, if the passed value is a power of two.
W Balboos, GHB 5-Apr-17 11:47am    
Correct me if I'm wrong, but a number is only a power of two if one and only one bit is set to 1. So, you need to check all bits in the int.

eg: 1010b = 10d, not a power of two

So your function must iterate through the entire value (typing it, I would imagine, to get the length in bits), then &1 and sum the results.

When done, if >1 then it's NG, If you count 0 as a power of two - that's a different deal.
CPallini 5-Apr-17 15:31pm    
You are not wrong. Neither powoftwo is.
if you discard all the zeros to the left of the first one by shifting it to the right, then it is a power of two only if it is exactly equal to 1.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900