Denis
BAN USERI am a Master's student at SFSU,San Francisco. Looking for a Job Starting from 2017 and a summer internship for 2016.
My Major is computer science.
Using C:
It sorts the user provided array first and then just alternately add the elements in the array.
#include <stdio.h>
void sort(int arr[],int left,int right)
{
int i= left, j= right;
int pivot = arr[((left+right)/2)];
int tmp=0;
/* partition */
while(i<=j)
{
while(arr[i]<pivot)
i++;
while(arr[j]>pivot)
j--;
if(i<=j)
{
tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
i++;
j--;
}
};
/* recursion */
if(left<j)
sort(arr,left,j);
if(i<right)
sort(arr,i,right);
}
int main()
{
int i;
int arr[7] = {1,2,3,4,5,6,7};int max=0,min=0;
int result[7];
int len = (sizeof(arr)/sizeof(arr[0]));
//printf("%d\n",arr[0]);
sort(arr,0,len-1);
//printf("%d\n",arr[0]);
for(i=0;i<len;i++)
{
if(i%2==0)
{
result[i] = arr[(len-1-max)];
max++;
}
else
{
result[i] = arr[min];
min++;
}
printf("%d\n",result[i]);
}
return 0;
}
++ is an increment operator. increment operator before the variable has the higher priority than any other operator involved with the variable. It is called pre-increment operator. If increment operator is after the variable than it is called the post increment operator It has the lower priority.
In the given code you have pre-increment operator's so when you apply plus operations first the value of the variable is increased then it is added to the another variable. like
z = (++y) + ++x + ++x; //here first the value of y increase than it is added to the value of x like the normal plus operation. Similarly for the other x varialbes.
But if it was like this:
z = ++y + x++ + ++x;
Here in case of first x it will add the value of x first to y and that value is added to the second x variable.But by the time control goes to the second x,the value of x is already increased by 1 in the post increment operator. So, the addition would be like this
z = 9 + 1 + 3
If we have the sample data as given above -
Find the unique variables in the data and we can make a table like this below
A B C D
-5 5 0 0
0 -8 8 0
0 0 -9 9
-10 0 0 10
0 11 0 -11
12 0 -12 0
Sum-3 8 -13 8
So finally we can take out the -3 and -13 Rs from A and C and add 8 and 8 into A and D
So, the total number of transactions = 4.
Also, we can verify the calculation by checking the Total of all the sum which should be zero.
Sum of A+B+C+D=0
-3+8-13+8=0
{{
int x=0;
boolean negative = false;
String out="";
System.out.println("Please enter the input");
Scanner sc = new Scanner(System.in);
x = sc.nextInt();
if(x<0){
negative = true;
}
x = Math.abs(x);
while(x>0){
out = x%10+out;
x =x/10;
}
if(negative)
out = "-"+out;
System.out.println(out);
}}
Does the i has to be less than j? Because A[i]-A[j] can be negative if i is greater than j in this array. which is less than i-j and fulfills the condition.
- Denis September 28, 2016