Given a randomly ordered array (arr) of n elements, function partitionArray(int arr[], int n, int x) partition the elements into two subsets such that elements <= x are in left subset and elements > x are in the right subset.
The first line of the test case will contain two numbers n (number of elements in a list ) and x (number to use for partition) separated by space.
The next line will contain N space-separated integers.
I am getting the wrong output for certain cases from the following Function.
Here's my code:
void partitionArray(int arr[], int n, int x)
{
int i, j, temp;
i = 0;
j = n-1;
while (i < j)
{
while (arr[i] <=x)
i++;
while (arr[j] > x)
j--;
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
i++;
j--;
}
}
For the cases I am getting the right output is:
10 6
28 26 25 5 6 7 24 29 6 10
For the cases I am not getting the right output is:
10 17
28 26 25 11 16 12 24 29 6 10
The output I am getting in this:
10
6
12
11
25
16
24
29
26
28
Expected Output:
10
6
12
11
16
25
24
29
26
28
10 6
28 26 25 11 5 7 24 29 6 10
The output I am getting in this:
6
25
5
11
26
7
24
29
28
10
Expected Output:
6
5
25
11
26
7
24
29
28
10
Can Anyone help me this
Below change will do:
if(i < j){
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
When swapping when the value of j is i+1 and arr[i]<x and arr[j]>x, after i++ and j-- from while loops, the value of j is i-1 in your code. Hence checking i<j before swapping is important.
Suppose input is
2 5
1 10
Your output will be
10 1
And the index has to be checked as index may run out of the size of the array.
while (i<n && arr[i]<=x)
i++;
while (j>=0 && arr[j]>x)
j--;
Example inputs:
5 7
5 3 2 4 1
5 3
7 6 9 5 6
Related
I have created a program to search for prime numbers. It works without problems until the entered number is smaller than 52, when it is bigger output prints out some blank (0) numbers and I don't know why. Also other numbers have blank output.
My code is:
#include <stdio.h> //Prime numbers
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <unistd.h>
int c[100], n, a[50], d, e, b = 1;
void sort() {
for (int i = 1; i < n; i++) {
if (c[i] > 1) {
a[b] = c[i];
printf("%d %d %d\n", a[1], b, i);
b++;
e = 2;
d = 0;
while (d <= n) {
d = c[i] * e;
c[d - 1] = 0;
e++;
}
}
}
}
int main() {
printf("Enter number as an limit:\n");
scanf("%d", &n);
for (int i = 0; i < n; i++) {
c[i] = i + 1;
}
sort();
printf("Prime numbers between 1 and %d are:\n", n);
for (int i = 1; i < b; i++) {
printf("%d ", a[i]);
}
return 0;
}
Here is output for 25:
Enter number as an limit:
25
2 1 1
2 2 2
2 3 4
2 4 6
2 5 10
2 6 12
2 7 16
2 8 18
2 9 22
Prime numbers between 1 and 25 are:
2 3 5 7 11 13 17 19 23
But for 83 is:
Enter number as an limit:
83
2 1 1
2 2 2
2 3 4
2 4 6
2 5 10
2 6 12
2 7 16
2 8 18
2 9 22
2 10 28
2 11 30
2 12 36
2 13 40
2 14 42
2 15 46
2 16 52
0 17 58
0 18 60
0 19 66
0 20 70
0 21 72
0 22 78
0 23 82
Prime numbers between 1 and 83 are:
0 3 5 7 11 0 17 19 23 29 31 37 0 43 47 53 0 61 67 71 73 79 83
Blank spots always spots after 17th prime number. And always the blank numbers are the same. Can you help me please what is the problem?
The loop setting entries in c for multiples of c[i] runs too far: you should compute the next d before comparing against n:
for (d = c[i] * 2; d <= n; d += c[i]) {
c[d - 1] = 0;
}
As a matter of fact you could start at d = c[i] * c[i] because all lower multiples have already been seen during the previous iterations of the outer loop.
Also note that it is confusing to store i + 1 into c[i]: the code would be simpler with an array of booleans holding 1 for prime numbers and 0 for composite.
Here is a modified version:
#include <stdio.h>
int main() {
unsigned char c[101];
int a[50];
int n, b = 0;
printf("Enter number as a limit:\n");
if (scanf("%d", &n) != 1 || n < 0 || n > 100) {
printf("invalid input\n");
return 1;
}
for (int i = 0; i < n; i++) {
c[i] = 1;
}
for (int i = 2; i < n; i++) {
if (c[i] != 0) {
a[b] = i;
//printf("%d %d %d\n", a[0], b, i);
b++;
for (int d = i * i; d <= n; d += i) {
c[d] = 0;
}
}
}
printf("Prime numbers between 1 and %d are:\n", n);
for (int i = 0; i < b; i++) {
printf("%d ", a[i]);
}
printf("\n");
return 0;
}
Output:
chqrlie$ ./sieve4780
Enter number as a limit:
25
Prime numbers between 1 and 25 are:
2 3 5 7 11 13 17 19 23
chqrlie$ ./sieve4780
Enter number as a limit:
83
Prime numbers between 1 and 83 are:
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79
Your problem seems to be caused by the fact that you have declared an array with size 50, but in fact it goes further than that: imagine you want to use Eratosthenes' procedure to find the first 10,000 prime numbers. Does this mean that you need to declare an array of size 10,000 first (or even bigger), risking to blow up your memory?
No: best thing to do is to work with collections where you don't need to set the maximum size at declaration time, like a linked list, a vector, ..., like that you can make your list grow as much as you like during runtime.
Given a Number N, Print the following pattern.
Input Format
The input contains a number N
Constraints
1 < N < 100
Output Format
The required pattern
for input 5 is
1
2 9
3 8 10
4 7 11 14
5 6 12 13 15
and for input 3 is
1
2 5
3 4 6
this is the code i have tried .. but the results are not the same
#include<stdio.h>
void pattern(int n)
{
for(int i=1; i<=n; i++)
{
int k = i;
for(int j=1; j<=i; j++)
{
printf("%d ",k);
k = n - j + k;
}
printf("\n");
}
}
int main()
{
int n = 5;
pattern(n);
return 0;
}
this is the result of the above code
1
2 6
3 7 10
4 8 11 13
5 9 12 14 15
How should I modify the above code to get the Expected Output?
This is an interesting problem, and not an easy one. I'm not going to write a program to solve it (in part because I'm too lazy), but I can describe how I would solve it.
You already have an outer loop for(int i=1; i<=n; i++) which counts down the rows, and an inner loop for(int j=1; j<=i; j++) which counts across the columns. Those are both fine.
Inside the inner loop I would test if(j % 2 == 1). If j % 2 is 1 we're in an odd-numbered column, and we want to count down the column. But if j % 2 is 0, we're in an even column, and we have to do it the other way.
First I would have a variable which is the number that's supposed to be at the top of the column (1, 9, 10, 14, or 15 in the n=5 case). I'd have to compute that number two different ways, one for the "odd" columns and one for the "even".
And then I'd use that number as a base to count down the odd columns, and up the evens. Specifically: I'd add i to it in the odd columns, but subtract i in the even columns. But actually that's not quite right, because i is not 1 at the top of columns other than 1, so what I'd actually have to add or subtract would be some function of i and j. But I think you can work this out.
Here are my three cents.:)
#include <stdio.h>
int main(void)
{
while ( 1 )
{
const unsigned int UPPER_LIMIT = 100;
printf( "Enter a non-negative number no greater than %u (0 - exit): ",
UPPER_LIMIT );
unsigned int n;
if ( scanf( "%u", &n ) != 1 || n == 0 ) break;
if ( !( n < UPPER_LIMIT ) ) n = UPPER_LIMIT - 1;
putchar( '\n' );
for ( unsigned int i = 0; i < n; i++ )
{
for ( unsigned int j = 0; j < i + 1; j++ )
{
unsigned int value = j % 2 == 0
? i + 1 + j * n - j * ( j + 1 ) / 2
: ( j + 1 ) * n - j * ( j + 1 ) / 2 - i + j;
printf( "%2u ", value );
}
putchar( '\n' );
}
putchar( '\n' );
}
return 0;
}
The program output might look the following way
Enter a non-negative number no greater than 100 (0 - exit): 10
1
2 19
3 18 20
4 17 21 34
5 16 22 33 35
6 15 23 32 36 45
7 14 24 31 37 44 46
8 13 25 30 38 43 47 52
9 12 26 29 39 42 48 51 53
10 11 27 28 40 41 49 50 54 55
Enter a non-negative number no greater than 100 (0 - exit): 9
1
2 17
3 16 18
4 15 19 30
5 14 20 29 31
6 13 21 28 32 39
7 12 22 27 33 38 40
8 11 23 26 34 37 41 44
9 10 24 25 35 36 42 43 45
Enter a non-negative number no greater than 100 (0 - exit): 8
1
2 15
3 14 16
4 13 17 26
5 12 18 25 27
6 11 19 24 28 33
7 10 20 23 29 32 34
8 9 21 22 30 31 35 36
Enter a non-negative number no greater than 100 (0 - exit): 7
1
2 13
3 12 14
4 11 15 22
5 10 16 21 23
6 9 17 20 24 27
7 8 18 19 25 26 28
Enter a non-negative number no greater than 100 (0 - exit): 6
1
2 11
3 10 12
4 9 13 18
5 8 14 17 19
6 7 15 16 20 21
Enter a non-negative number no greater than 100 (0 - exit): 5
1
2 9
3 8 10
4 7 11 14
5 6 12 13 15
Enter a non-negative number no greater than 100 (0 - exit): 4
1
2 7
3 6 8
4 5 9 10
Enter a non-negative number no greater than 100 (0 - exit): 3
1
2 5
3 4 6
Enter a non-negative number no greater than 100 (0 - exit): 2
1
2 3
Enter a non-negative number no greater than 100 (0 - exit): 1
1
Enter a non-negative number no greater than 100 (0 - exit): 0
Or using the recursive approach of calculating the output value the program can look the following way.
#include <stdio.h>
int main(void)
{
while ( 1 )
{
const unsigned int UPPER_LIMIT = 100;
printf( "Enter a non-negative number no greater than %u (0 - exit): ",
UPPER_LIMIT );
unsigned int n;
if ( scanf( "%u", &n ) != 1 || n == 0 ) break;
if ( !( n < UPPER_LIMIT ) ) n = UPPER_LIMIT - 1;
putchar( '\n' );
for ( unsigned int i = 0; i < n; i++ )
{
unsigned int value = i + 1;
for ( unsigned int j = 0; j < i + 1; j++ )
{
printf( "%2u ", value );
value += j % 2 == 0 ? 2 * ( n - i ) - 1 : 2 * ( i - j );
}
putchar( '\n' );
}
putchar( '\n' );
}
return 0;
}
For example its output for the entered number equal to 10 looks like
Enter a non-negative number no greater than 100 (0 - exit): 10
1
2 19
3 18 20
4 17 21 34
5 16 22 33 35
6 15 23 32 36 45
7 14 24 31 37 44 46
8 13 25 30 38 43 47 52
9 12 26 29 39 42 48 51 53
10 11 27 28 40 41 49 50 54 55
Enter a non-negative number no greater than 100 (0 - exit): 0
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
This code is designed by someone to change array [a1 a2...am b1 b2..bn ] to the array [b1 b2 ..bn a1 a2..am], but it involves the greatest common divisor which I can't get the point.
void Exchange(int a[],int m,int n,int s){
int p=m,temp=m+n;int k=s%p;
while(k!=0){temp=p;p=k;k=temp%p;}
for(k=0 ; k<p ;k++){ //below is where i cant't understand
temp=a[k];i=k;j=(i+m)%(m+n);
while(j!=k)
{a[i]=a[j];i=j;j=(j+m)%(m+n);}
a[i]=temp;
}
};
EDIT: "Properly" indented:
void Exchange(int a[], int m, int n, int s) {
int p = m, temp = m + n, k = s % p;
while (k != 0) {
temp = p;
p = k;
k = temp % p;
}
for (k = 0 ; k < p; k ++) { // below is where i cant't understand
temp = a[k];
i = k;
j = (i + m) % (m + n);
while (j != k) {
a[i] = a[j];
i = j;
j = (j + m) % (m + n);
}
a[i] = temp;
}
};
The code is using a single value of overhead to implement array rotation. If the lengths are mutually prime, a single pass suffices. If not, you have to repeat the shift cycle by the GCD of the lengths
I said earlier that there are other questions on SO that cover this. A look found SO 3333-3814 which deals with a single rotation. I did some messing with code to support that a while ago, demonstrating the need for GCD, but I didn't previously post it.
Here's the code — it uses C99 VLAs — variable length arrays.
#include <stdio.h>
static int gcd(int x, int y)
{
int r;
if (x <= 0 || y <= 0)
return(0);
while ((r = x % y) != 0)
{
x = y;
y = r;
}
return(y);
}
static void dump_matrix(int m, int n, int source[m][n])
{
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
printf("%4d", source[i][j]);
putchar('\n');
}
}
static void init_matrix(int m, int n, int source[m][n])
{
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
source[i][j] = (i + 1) * (j + 2);
}
}
static void rotate_1col(int n, int vector[n], int z)
{
z %= n;
if (z != 0)
{
int c = gcd(n, z);
int s = n / c;
for (int r = 0; r < c; r++)
{
int x = r;
int t = vector[x];
for (int i = 0; i < s; i++)
{
int j = (x + z) % n;
int v = vector[j];
vector[j] = t;
x = j;
t = v;
}
}
}
}
static void rotate_cols(int m, int n, int source[m][n], int z)
{
for (int i = 0; i < m; i++)
rotate_1col(n, source[i], z);
}
int main(void)
{
int m = 3;
for (int n = 2; n < 9; n++)
{
int source[m][n];
for (int z = 0; z <= n; z++)
{
init_matrix(m, n, source);
printf("Initial:\n");
dump_matrix(m, n, source);
rotate_cols(m, n, source, z);
printf("Post-rotate %d:\n", z);
dump_matrix(m, n, source);
putchar('\n');
}
}
return 0;
}
The code demonstrates different sizes of rotation on different sizes of array. Example sections of the output:
…
Initial:
2 3 4
4 6 8
6 9 12
Post-rotate 1:
4 2 3
8 4 6
12 6 9
…
Initial:
2 3 4 5
4 6 8 10
6 9 12 15
Post-rotate 3:
3 4 5 2
6 8 10 4
9 12 15 6
…
Initial:
2 3 4 5 6 7
4 6 8 10 12 14
6 9 12 15 18 21
Post-rotate 1:
7 2 3 4 5 6
14 4 6 8 10 12
21 6 9 12 15 18
Initial:
2 3 4 5 6 7
4 6 8 10 12 14
6 9 12 15 18 21
Post-rotate 2:
6 7 2 3 4 5
12 14 4 6 8 10
18 21 6 9 12 15
Initial:
2 3 4 5 6 7
4 6 8 10 12 14
6 9 12 15 18 21
Post-rotate 3:
5 6 7 2 3 4
10 12 14 4 6 8
15 18 21 6 9 12
…
Initial:
2 3 4 5 6 7 8 9
4 6 8 10 12 14 16 18
6 9 12 15 18 21 24 27
Post-rotate 4:
6 7 8 9 2 3 4 5
12 14 16 18 4 6 8 10
18 21 24 27 6 9 12 15
Initial:
2 3 4 5 6 7 8 9
4 6 8 10 12 14 16 18
6 9 12 15 18 21 24 27
Post-rotate 5:
5 6 7 8 9 2 3 4
10 12 14 16 18 4 6 8
15 18 21 24 27 6 9 12
Initial:
2 3 4 5 6 7 8 9
4 6 8 10 12 14 16 18
6 9 12 15 18 21 24 27
Post-rotate 6:
4 5 6 7 8 9 2 3
8 10 12 14 16 18 4 6
12 15 18 21 24 27 6 9
…
First of all, to get the result you said you expected, I have set m and n to be half the array size. I also assumed that s would be initialised to zero, in which case, the first while loop does not iterate. Also, there are several declarations missing in your code so my explanation makes some assumptions.
The variable p holds the number of array elements to swap;
// This is to keep the value to be overwritten by the swap
temp=a[k];
// This is the array index of the bottom half element to write the top half element to
i=k;
// this is to get the current index of the top half;
j=(i+m)%(m+n);
// This assignes the bottom index value with the top half value
while(j!=k)
{
// Write top half element to corresponding bottom half element
a[i]=a[j];
// We can now overwrite top half element; this assignes the index at wich to copy the bottom half element
i=j;
// This is to get out of the loop
j=(j+m)%(m+n);
}
// The bottom half element held at the beginning is now written to the top half at the corresponding index
a[i]=temp;
Hope this is the answer you were looking for. I arrived at this result by using a debugger and by stepping in the code line by line. I don't know if you know how to use a debugger but if not, then I highly recommend your lean how to use one; it it time well spent and it returns an awesome dividend :-)
This question already has answers here:
Function to left shift a 2D array in C
(3 answers)
Closed 8 years ago.
I am learning how to manipulate a 2D array in C and I have to apply a left shift to a matrix, so A[0][0] becomes the value at A[0][1] etc. This is the code and output that I got after a few tries; I think I may be getting close:
void LeftShift2D(int A[M][N]){
int i;
int j;
int temp;
for(i = 0; i < M; i++){
for(j = 0; j < N; j++){
temp = A[0][0];
A[i][j] = A[i+1][j+1];
}
}
A[i][j] = temp;
}
Given this as the input:
14 17 28 26 24
26 17 13 10 2
3 8 21 20 24
17 1 7 23 17
12 9 28 10 3
21 3 14 8 26
this is the desired output:
17 28 26 24 26
17 13 10 2 3
8 21 20 24 17
1 7 23 17 12
9 28 10 3 21
3 14 8 26 14
but this is the actual output I'm getting:
14 17 28 26 24
26 17 13 10 2
3 8 21 20 24
17 1 7 23 17
12 9 28 10 3
21 3 14 8 26
4
17 13 10 2 3
8 21 20 24 17
1 7 23 17 12
9 28 10 3 21
3 14 8 26 4
-1218716075 -1217196044 0 -1079502888 134514340
What's wrong?
By far the easiest way to do what you want is:
void LeftShift2D(int A[M][N])
{
int i;
int temp = A[0][0];
int *base = &A[0][0];
for (i = 0; i < M * N - 1; i++)
base[i] = base[i+1];
A[i] = temp;
}
This treats the array as a vector and shifts the data down one space, rotating the value originally at the start to the end. If you wanted to rotate each row separately, you'd use different code, more closely related to what you show:
void LeftShift2D(int A[M][N])
{
int temp;
for (int i = 0; i < M; i++)
{
int j;
int temp = A[i][0]
for (j = 0; j < N - 1; j++)
A[i][j] = A[i][j+1];
A[i][j] = temp;
}
}
Your code has various problems. The A[i][j] = A[i+1][j+1]; assignment accesses data out of bounds, and copies data 'diagonally', copying the cell to the bottom-right (or south-east) of the current cell, not the cell to the right (east) of the current cell. It has problems wrapping, too.
You have a buffer overflow here
A[i][j] = A[i+1][j+1];
you need to iterate to i < M - 1 and j < N - 1.
You are accessing invalid memory which explains the observed values, that also invokes undefined behavior so you could observe some strange behavior eventually.
Say I have a list of numbers:
89 12 18 4 6
and I want to implement an insertion sort and have it print every step of the sort onto the screen:
Sort 1. 12 89 18 4 6
Sort 2. 4 12 89 18 6
Sort 3. 4 6 12 89 18
Sort 4. 4 6 12 18 89
here's the code that I have so far, I'm confused as to where to insert the printf inside the loop.
void insertion_sort(FILE *fp, int ar[15])
{
int i, j, temp;
for (i = 0; i < 15; i++)
printf("%d\n", ar[i]);
for(i = 0; i < 15; i++) {
temp = ar[i];
for(j = i - 1; j >= 0 && ar[j] > temp; j--)
ar[j + 1] = ar[j];
ar[j + 1] = temp;
}
your scheme of sorting is actually Selection sort:
Sort 1. 12 89 18 4 6
Sort 2. 4 12 89 18 6
Sort 3. 4 6 12 89 18
Sort 4. 4 6 12 18 89
it finds the smallest number and places it at the beginning of the list.
A normal insertion sort would do the following:
Sort 1. 12 89 18 4 6
Sort 2. 12 18 89 4 6
Sort 3. 4 12 18 89 6
Sort 4. 4 6 12 18 89
and that is it finds 18 being less than 89 but greater than 12 and inserts 18 between 12 and 89 and the first iteration is done. Then repeat the process.
Here is my code:
void insertion(int *x,int n){ // int *x - array, n- array's length
int i,j,k,temp,elem; // i,j,k - counters, elem - to store the element at pos x[i]
for(i=0;i<n;i++){
elem=x[i]; // store the element
j=i;
while(j>0 && x[j-1]>elem){ // the magic(actual sorting)
x[j]=x[j-1];
j--;
}
x[j]=elem; // swap the elements
if(i>=1){ // here begins printing every sorting step, i>=1 because first time j is not greater than 0 so it just run through the loop first time
printf("sort %d. ",i); // printing the step
for(k=0;k<n;k++) // loop through array
printf("%d ",x[k]); // display the elements already sorted
printf("\n"); // when the array is displayed, insert a \n so that the next display will be on a new line
}
}
}
Put it at the end of the outer for statement just after ar[j + 1] = temp; and before the for loop ends