Click here to Skip to main content
15,947,411 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Given an array of size n and multiple values around which we need to left rotate the array.

Input:
First line consists of T test case. First line of every test case consists of N and K, N denoting number of elements of array and K denoting the number of places to shift. Second line of every test case consists of elements of array.

Output:
Single line output, print the rotated array.

Constraints:
1<=T<=100
1<=N<=10^4
1<=K<=10^4

Example:
Input:
1
5 14
1 3 5 7 9
Output:
9 1 3 5 7

My code is attached here , although I am getting right answer but there is slight difference(.000004) in desired output and expected output (its regarding use of precision function), what should I modify here?

What I have tried:

My code is-

C++
#include<iostream>
#include<cmath>
#include<iomanip>
using namespace std;
int main()
{

    int t;
    cin>>t;
     
    while(t--)
    {
        int set_size,i;
        cin>>set_size;
        int arr[set_size];
        for(i=0;i<set_size;i++)
        {
            cin>>arr[i];
        }
        
    unsigned int pow_set_size = pow(2, set_size);
	int counter, j;
    float sum_avg=0;
    cout<<fixed<<setprecision(6);
	/*Run from counter 000..0 to 111..1*/
	for(counter = 0; counter < pow_set_size; counter++)
	{
	
           float avg,sum=0;int no=0;
			
	for(j = 0; j < set_size; j++)
	{
		/* Check if jth bit in the counter is set
			If set then pront jth element from set */
			
			
		if(counter & (1<<j))
			{
			      //  cout<<arr[j];
			        sum+=arr[j];
			        no++;
//			        int no=0,sum=0,digit;
//			        float avg;
//			        while(arr[j]!=0)
//			        {
//			          digit=arr[j]%10;
//			          sum=+digit;
//			          arr[j]/=10;
//			          no++;
//			          
//			        }
//			        avg=sum/no;
//			        
//			
//			    overall_sum+=avg;
//			    cout<<"overall_sum="<<overall_sum<<endl;
			}
			
			
			
	}
	
	if(sum!=0)
	{avg=sum/no;
	cout<<"avg= "<<avg;
	    sum_avg+=avg;
	}
	cout<<endl;
	
    }
  // cout<<fixed<<setprecision(6);
   cout<<sum_avg<<endl;
    }
    return 0;
    
}




 //1
//3
//2 3 5

//Its Correct output is:
//23.333333

//And Your Output is:
//23.333334
Posted
Updated 19-Jul-17 5:35am
Comments
Patrice T 19-Jul-17 12:29pm    
The statement you pasted is not related to your problem or your code.
chunky77 19-Jul-17 12:43pm    
I think may be I am not using precision( ) function properly.

1 solution

Floting point numbers on a computer are usually approximations, and their precision depends upon whether they are single or double length values. See What Every Computer Scientist Should Know About Floating-Point Arithmetic[^].
 
Share this answer
 

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