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. -
#include<stdio.h>
-
void fun(int a[],int n,int k)
-
{
-
int i,j,t;
-
int b[1000];
-
for(i=0;i<n;i++)
-
{
-
b[i]=1;
-
while(a[i]!=1&&a[i]!=0)
-
{
-
if(a[i]%2==1)
-
b[i]++;
-
a[i]=a[i]/2;
-
}
-
if(a[i]==0)
-
b[i]=0;
-
}
-
for(i=0;i<k;i++)
-
{
-
int max=i;
-
for(j=i;j<n-1;j++)
-
{
-
if(b[j]>b[max])
-
{
-
max=j;
-
}
-
}
-
t=a[i];
-
a[i]=a[max];
-
a[max]=t;
-
t=b[i];
-
b[i]=b[max];
-
b[max]=t;
-
printf("%d",a[i]);
-
}
-
}
-
int main()
-
{
-
int t,n,k,i,j,a[1000];
-
scanf("%d",&t);
-
for(i=0;i<t;i++)
-
{
-
scanf("%d %d",&n,&k);
-
for(j=0;j<n;j++)
-
{
-
scanf("%d",&a[j]);
-
}
-
fun(a,n,k);
-
}
-
}
-
-
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
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?
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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;
|
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()
{
|
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;
|
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()
...
|
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;
|
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.
...
|
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>...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
|
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...
|
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...
| |