473,396 Members | 1,608 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Can someone explain the misbehaviour of this code

I was trying to solve a problem then i observed a misbehaviour in my code,it is asking for input continuosly how much input i give and this is only happening for some particular inputs.
Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2.   void fun(int a[],int n,int k)
  3.     {
  4.         int i,j,t;
  5.         int b[1000];
  6.          for(i=0;i<n;i++)
  7.         {
  8.             b[i]=1;
  9.             while(a[i]!=1&&a[i]!=0)
  10.             {
  11.                 if(a[i]%2==1)
  12.                 b[i]++;
  13.                 a[i]=a[i]/2;
  14.             }
  15.                      if(a[i]==0)
  16.                      b[i]=0;
  17.         }
  18.         for(i=0;i<k;i++)
  19.         {
  20.             int max=i;
  21.             for(j=i;j<n-1;j++)
  22.             {
  23.                 if(b[j]>b[max])
  24.                 {
  25.                     max=j;
  26.                 }
  27.             }
  28.             t=a[i];
  29.             a[i]=a[max];
  30.             a[max]=t;
  31.             t=b[i];
  32.             b[i]=b[max];
  33.             b[max]=t;
  34.         printf("%d",a[i]);
  35.         }
  36.     }
  37.     int main()
  38.     { 
  39.         int t,n,k,i,j,a[1000];
  40.         scanf("%d",&t);
  41.         for(i=0;i<t;i++)
  42.         {
  43.             scanf("%d %d",&n,&k);
  44.             for(j=0;j<n;j++)
  45.             {
  46.                  scanf("%d",&a[j]);
  47.             }
  48.             fun(a,n,k);
  49.         }
  50.     }        
  51.  
  52.  
Aug 26 '15 #1

✓ answered by donbock

I suggest you add code after lines 40 and 43 to test the entered values of t, n, and k to make sure they make sense.

What should happen if a[i] is negative? Should the tests on lines 9 and 11 also check for -1?

1 1485
donbock
2,426 Expert 2GB
I suggest you add code after lines 40 and 43 to test the entered values of t, n, and k to make sure they make sense.

What should happen if a[i] is negative? Should the tests on lines 9 and 11 also check for -1?
Aug 27 '15 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

11
by: Maciej Nadolski | last post by:
Hi! I can`t understand what php wants from me:( So: Cannot send session cache limiter - headers already sent (output started at /home/krecik/public_html/silnik.php:208) in...
2
by: rked | last post by:
I get nameSPAN1 is undefined when I place cursor in comments box.. <%@ LANGUAGE="VBScript" %> <% DIM ipAddress ipAddress=Request.Servervariables("REMOTE_HOST") %> <html> <head> <meta...
5
by: J Allen Horner | last post by:
I'm just starting to learn C, and the tutorial I'm reading uses this code as an example: ----------- #include <stdio.h> #define MAX 10 int a; int rand_seed=10;
3
by: Lenn | last post by:
Hello, I have the following example of AsyncCallback from a C# book which I wanted to implement in my project: //Class with AsyncDelegate public class AsyncProcess { public AsyncProcess() {
1
by: Tran Hong Quang | last post by:
Hi, In header file, I see this declaration: #define DECLARE_VTBL(iname) iname vt##iname; then, in C file, there are below codes: typedef struct _Abc { .......... } abc;
8
by: Beany | last post by:
I managed to find a search example on the forum but i dont understand the following code properly........ can someone explain this to me: Private Sub QuickSearch_AfterUpdate() ...
5
by: Nhd | last post by:
#include <iostream> #include <sstream> #include <string> using namespace std; int main(int argc, char* argv) { int Count1 = 1; int Count2 = 1; int Count = 0;
10
by: jinnejeevansai | last post by:
I was writing a code to create posix threads print function is printing p,q for 4 loops but it is not printing for fifth loop but the loop is called. Can someone explain the reason. ...
1
by: jinnejeevansai | last post by:
I was trying to write a program to copy data in one file to another file,but my code was not responding,it was asking for input,can someone explain the problem in my code. #include<stdio.h>...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.