Given a array x with n integer components, write functions that allow performing the following operation: Carry out the circular permutation of the given array
I tried to do without poiters for the code, as it ends up with only arrays
I think the problem is that the array in the pcircular is lost and can't pass the value to the write function
Note: for array > 6 it will not work
#include <stdio.h>
int ArraySize(){ // get the size of the array
int size;
printf("What's the array size? ");
scanf("%d", &size);
return size;
}
void readArray(int size, int array[]){ /*put the values in the first array*/
for (int i = 0; i < size; i++){
printf("What is the \033[1;36m%d\033[m value? ", i+1);
scanf("%d", &array[i]);
}
}
void pcircular(int size, int array[size][size]){ //Circular permutation function
for (int j = 0; j <= size; j++){
/* printf("("); */
for (int i = 0; i < size; i++){
if (i == size - 1){
array[j+1][0] = array[j][i];
/* printf("%d", array[j+1][0]); */
}
else{
array[j+1][i+1] = array[j][i];
/* printf("%d", array[j+1][i+1]);
printf(", "); */
}
}
/* printf(")"); */
}
}
void writeArray(int size, int array[size][size]){ //Write the Array
for (int i = 0; i <= size; i++){
printf("(");
for (int j = 0; j < size; j++){
/* printf("\ni = %d j = %d\n", i, j); */
printf("%d", array[i][j]);
if (j != size-1){
printf(", ");
}
}
printf(")");
}
}
void main(){
int size = ArraySize();
int list[size][size]; // create the array
readArray(size, list[0]);
pcircular(size, list);
writeArray(size, list);
}
Input:
What's the array size? 6
What is the 1 value? 1
What is the 2 value? 2
What is the 3 value? 3
What is the 4 value? 4
What is the 5 value? 5
What is the 6 value? 6
Expected Output:
(1, 2, 3, 4, 5, 6)(6, 1, 2, 3, 4, 5)(5, 6, 1, 2, 3, 4)(4, 5, 6, 1, 2, 3)(3, 4, 5, 6, 1, 2)(2, 3, 4, 5, 6, 1)(1, 2, 3, 4, 5, 6)
Real Output:
(
write your pcircular function as follow:
void pcircular(int size, int array[size][size]){ //Circular permutation
for (int j = 1; j <= size; j++){
for (int i = 0; i < size; i++){
array[j][i ] = array[j - 1][(i + 1) % size];
}
}
}
but the main problem is here:
int list[size + 1][size]; // create the array
since you are creating one more row than the number, indeed the last row is copy of the first row.
if you dont change the pcircular function, and use your version, you should as well add to second dimention too.
Related
I have a multidimensional array with 3 rows and 4 columns. The program should use reverseRow()function to reverse a specific row from an array. Like, let's say user's input is 2, then it should reverse second row and print it.
I have tried a swap method, but it didn't work. I also tried using pointers, but it didn't work as well. Can someone explain it for me? How do I reverse a specific row?
#include <stdlib.h>
int arr[3][4] = {
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}};
int n = sizeof(arr) / sizeof(arr[0]);
void reverseRow(int low, int high)
{
if (low < high)
{
int temp = arr[low][0];
arr[low][0] = arr[high][0];
arr[high][0] = temp;
reverseRow(low + 1, high - 1);
for (int i = 0; i < n; i++)
for (int j = 3; i > 0; j--)
printf("%d ", arr[i][j]);
}
}
void printMenu()
{
printf("\n");
printf("You can choose one of these services: \n");
printf("1. Get the elements of a specific row reversed \n");
printf("Please select one to try ");
int answer;
scanf("%d", &answer);
switch (answer)
{
case 1:
reverseRow(0, n - 1);
break;
case 2:
printf("Bye!\n");
break;
default:
printf("please select carefully! \n");
break;
}
}
int main()
{
printMenu();
return 0;
}
Best regards.
You're reversing the first column, not a user-selected row.
You're not passing the row number to the function.
The loop that prints the array is printing all the columns in reverse order, and it's using n as the number of rows, not columns. I've renamed the variables to be clearer and fixed the printing loop.
#include <stdio.h>
#include <stdlib.h>
int arr[3][4] = {
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}};
int rows = sizeof(arr) / sizeof(arr[0]);
int cols = sizeof(arr[0]) / sizeof(arr[0][0]);
void reverseRow(int rownum, int low, int high)
{
if (low < high)
{
int temp = arr[rownum][low];
arr[rownum][low] = arr[rownum][high];
arr[rownum][high] = temp;
reverseRow(rownum, low + 1, high - 1);
} else {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("%d ", arr[i][j]);
}
printf("\n");
}
}
}
void printMenu()
{
printf("\n");
printf("You can choose one of these services: \n");
printf("1. Get the elements of a specific row reversed \n");
printf("Please select one to try ");
int answer;
scanf("%d", &answer);
switch (answer)
{
case 1:
printf("Please select row number: ");
int row;
scanf("%d", &row);
if (row >= rows || row < 0) {
printf("Invalid row\n");
break;
}
reverseRow(row, 0, cols - 1);
break;
case 2:
printf("Bye!\n");
break;
default:
printf("please select carefully! \n");
break;
}
}
int main()
{
printMenu();
return 0;
}
What is the row of a two-dimensional array?
It is a one dimensional array.
So what you need is to write a function that reverses a one-dimensional array.
An iterative function can look the following way
void reverseRow( int *a, size_t n )
{
for ( size_t i = 0; i < n / 2; i++ )
{
int tmp = a[i];
a[i] = a[n-i-1];
a[n-i-1] = tmp;
}
}
A recursive function can look the following way
void reverseRow( int *a, size_t n )
{
if ( !( n < 2 ) )
{
int tmp = a[0];
a[0] = a[n-1];
a[n-1] = tmp;
reverseRow( a + 1, n - 2 );
}
}
And either function is called like for example
reverseRow( arr[1], 4 );
that reverses the second row of the original array.
Or if the number of row (starting from 0) is stored in some variable as for example row then the function is called like
reverseRow( arr[row], 4 );
To output the reversed row you need to write a separate function.
Here is a demonstration program.
#include <stdio.h>
void reverseRowIterative( int *a, size_t n )
{
for (size_t i = 0; i < n / 2; i++)
{
int tmp = a[i];
a[i] = a[n - i - 1];
a[n - i - 1] = tmp;
}
}
void reverseRowRecursive( int *a, size_t n )
{
if (!( n < 2 ))
{
int tmp = a[0];
a[0] = a[n - 1];
a[n - 1] = tmp;
reverseRowRecursive( a + 1, n - 2 );
}
}
void printRow( const int *a, size_t n )
{
for (size_t i = 0; i < n; i++)
{
printf( "%2d ", a[i] );
}
putchar( '\n' );
}
int main( void )
{
enum { M = 3, N = 4 };
int arr[M][N] =
{
{ 1, 2, 3, 4 },
{ 5, 6, 7, 8 },
{ 9, 10, 11, 12 }
};
for ( size_t i = 0; i < M; i++ )
{
reverseRowIterative( arr[i], N );
printRow( arr[i], N );
}
putchar( '\n' );
for (size_t i = 0; i < M; i++)
{
reverseRowRecursive( arr[i], N );
printRow( arr[i], N );
}
putchar( '\n' );
}
The program output is
4 3 2 1
8 7 6 5
12 11 10 9
1 2 3 4
5 6 7 8
9 10 11 12
How to separate the even position number of an array from the odd position number in C.
Example
int arr[]= {2,3,4,5,6,7,8,9,1};
int odd[]= {2,4,6,8,1};
int even[] = {3,5,7,9};
Use % to get the remainder. If the remainder is nonzero, then the index is odd, otherwise even. But index starts from 0 and not 1, thus the first element's index is 0 and is even. if you want to sort according to that (seems to be you do), add 1 to index.
#include <stdio.h>
int main() {
int arr[] = {2, 3, 4, 5, 6, 7, 8, 9, 1}; // our array
const size_t max_size = sizeof(arr) / sizeof(arr[0]);
int odd[max_size];
size_t odd_cnt = 0;
int even[max_size];
size_t even_cnt = 0;
for (size_t i = 0; i != max_size; ++i) {
if ((i + 1) % 2) { // if (i + 1) % 2 is nonzero, i + 1 is odd
odd[odd_cnt++] = arr[i];
} else {
even[even_cnt++] = arr[i];
}
}
for (size_t i = 0; i != odd_cnt; ++i) {
printf("%d ", odd[i]);
}
printf("\n");
for (size_t i = 0; i != even_cnt; ++i) {
printf("%d ", even[i]);
}
printf("\n");
return 0;
}
I write a generalized C language program that given a sequence of numbers, sorts, even numbers in ascending order, odd numbers in descending order and places all even numbers in the initial part of an array then odd numbers.
Example: 2, 5, 1, 0, 4, 7, 9, 3, -2, 10, 20, 15
Expect: -2, 0, 2, 4, 10, 20, 15, 9, 7, 5, 3, 1
Six functions are required. The solution must be provided using only the mentioned functions.No global variables shall be declared. Use appropriate data types, return types and function arguments.
Input() – takes total number of elements and values as input from the user. Stores the values in “input” array.
SortEven() – sorts the even numbers in ascending order and stores them in an array named “even”
SortOdd() – sorts the odd numbers in descending order and stores them in an array named “odd”
Merge() – places all the even numbers in the initial part of array named “result” then odd numbers.
Display() – displays the contents of “result” array.
main() – calls the Input() module to begin the execution.
Program:
#include <stdio.h>
int main() {
input();
}
int input() {
int n;
printf("Enter The Number Of Elements You Want To Enter : ");
scanf("%d", &n);
int a[n], i, ev = 0, od = 0;
for (i = 0; i < n; i++) {
printf("Enter Number : ");
scanf("%d", &a[i]);
if (a[i] % 2 == 0) {
ev++;
} else {
od++;
}
}
sorteven(a, ev, od, n);
}
int sorteven(int a[], int ev, int od, int n) {
int i, j = 0, swap, even[ev];
for (i = 0; i < n; i++) {
if (a[i] % 2 == 0) {
even[j] = a[i];
j++;
}
}
for (i = 0; i < ev - 1; i++) {
for (j = 0; j < ev - i - 1; j++) {
if (even[j] > even[j + 1]) {
swap = even[j];
even[j] = even[j + 1];
even[j + 1] = swap;
}
}
}
sortodd(a, ev, od, n, even);
}
int sortodd(int a[], int ev, int od, int n, int even[]) {
int i, k = 0, swap, odd[od], j;
for (i = 0; i < n; i++) {
if (a[i] % 2 != 0) {
odd[k] = a[i];
k++;
}
}
for (i = 0; i < od - 1; i++) {
for (j = 0; j < od - i - 1; j++) {
if (odd[j] < odd[j + 1]) {
swap = odd[j];
odd[j] = odd[j + 1];
odd[j + 1] = swap;
}
}
}
merge(a, ev, od, n, even, odd);
}
int merge(int a[], int ev, int od, int n, int even[], int odd[]) {
int merge[n], i;
for (i = 0; i < ev; i++) {
merge[i] = even[i];
}
for (i = ev; i < n; i++) {
merge[i] = odd[i];
}
display(merge, n);
}
int display(int merge[], int n) {
int i;
printf("OUTPUT : ");
for (i = 0; i < n; i++) {
printf(" %d ", merge[i]);
}
}
When looking in the source code, the problem is located in the merge() function.
Both sorteven() and sortodd()are well implemented, but when merging both sub-arrays, the for(i=ev;i<n;i++) is not correct.
To add the odd[] array at the end of the even[] array, write:
int j;
// point to the first item of odd[]
for(i=ev,j=0;i<n;i++,j++)
{
merge[i]=odd[j];
}
Instead of:
only odd[0] to odd[od-1] are allocated and defined.
for(i=ev;i<n;i++)
{
merge[i]=odd[i];
}
Outputs of:
{ 2, 5, 1, 0, 4, 7, 9, 3, -2, 10, 20, 15 };
Are:
OUTPUT : -2 0 2 4 10 20 15 9 7 5 3 1
I am attempting to make a program that takes an array and reverts it backwards however the program must do this to the array in groups of three. So if the user enters the numbers 1, 2, 3, 4, 5, 6 into the array the program will then output: 3, 2, 1, 6, 5, 4.
When I run the current program I get: 3 2 1 4 5 6. If anyone could help me figure out why that would be great as I am a little confused.
Here is my code:
int * numbersProcessFour(int *x, int size)
{
int i = 0, three = 3, six = 6, nine = 9;
if (size < 4)
{
for (i; i < three; i++)
{
reverse_array(x, three);
printf("%d ", x[i]);
}
}else if (size > 3 && size < 7)
{
for (i; i < three; i++)
{
reverse_array(x, three);
printf("%d ", x[i]);
}
for (i; i < 6; i++)
{
reverse_array(x, three);
printf("%d ", x[i]);
}
}
else if (size > 6 && size < 10)
{
for (i; i < three; i++)
{
reverse_array(x, three);
printf("%d ", x[i]);
}
for (i; i < 6; i++)
{
reverse_array(x, three);
printf("%d ", x[i]);
}
for (i; i < 9; i++)
{
reverse_array(x, three);
printf("%d ", x[i]);
}
}
}
void reverse_array(int *x, int length)
{
int i, temp;
for (i = 0; i<length / 2; i++)
{
temp = x[i];
x[i] = x[length - i - 1];
x[length - i - 1] = temp;
}
}
Continuing from your comment to fluter's answer, you may be over thinking it a bit. In order to swap the 1st and 3rd element in each 3-element partition of an array, you simply need to step though the array 3-elements at a time. You need to decide how you will handle any final partial partition, but since your goal is to swap the 1st and 3rd, there is no 3rd in anything less than a full partition, so the logical choice is to ignore any final partial partition.
A variant of what you and fluter have done incorporating a swap would be:
/* reverse 1st and 3rd element in each group of 3 */
void rev3 (int *a, size_t sz)
{
if (sz < 3) return;
size_t i;
for (i = 0; i < sz; i += 3) {
if (sz - i < 3) break;
swap (&a[i], &a[i+2]);
}
}
You can put it together with:
#include <stdio.h>
void rev3 (int *a, size_t sz);
void swap (int *a, int *b);
int main (void) {
int a[] = {1,2,3,4,5,6,7,8,9};
size_t i;
rev3 (a, sizeof a/sizeof *a);
for (i = 0; i < sizeof a/sizeof *a; i++) printf (" %2d", a[i]);
putchar ('\n');
return 0;
}
void swap (int *a, int *b)
{
int t = *a;
*a = *b;
*b = t;
}
Example Use
When compiled and run it will give you the swap (reversal) of the 1st and 3rd elements throughout the array that you specify in your problem.
$ ./bin/revarr3
3 2 1 6 5 4 9 8 7
There is no difference whether you use a separate swap or whether you include that operation in your reversal function. There is also no need to incur the additional overheard of calling a recursive function when a procedural approach will work. Look over all the answers and compare/contrast the differing ways to accomplish your goal.
You have branches for each multiple of 3, that is inefficient. One way to solve it is you can take the array as a split by 3 smaller arrays, and reverse on them. Also, reversing an array of 3 elements is the same as swap the 1st and the 3rd element.
int i;
int temp;
for (i = 0; i < count; i += 3) {
if (i+2 >= count)
break;
temp = arr[i];
arr[i] = arr[i+2];
arr[i+2] = temp;
}
A generalized version of numberProcessFour might look like this.
int reverse_array_mod(int *input, size_t size, int mod)
{
int i, smod;
/* Error: return modulus if size cannot be divided by mod */
if(size%mod)
return size%mod;
smod = size/mod;
for(i=0; i<smod; i++)
reverse_array(input+i*mod, mod);
/* return 0 on success */
return 0;
}
Test
int main(int argc, char **argv)
{
int a[] = {0, 1, 2, 3, 4, 5};
int i, err, mod;
for(mod=1; mod<5; mod++) {
err = reverse_array_mod(a, 6, mod);
if(err) {
fprintf(stderr, "Error %d, modulus %d invalid\n", err, mod);
return err;
}
for(i=0; i<6; i++)
printf("%d\n", a[i]);
printf("\n");
}
return 0;
}
Result:
0
1
2
3
4
5
1
0
3
2
5
4
3
0
1
4
5
2
Error 2, modulus 4 invalid
try this
int *numbersProcessFour(int *x, int size) {
int i;
for(i = 0; i + 3 < size; i += 3){
reverse_array(x + i, 3);
}
if(size - i > 1)
reverse_array(x + i, size - i);
return x;
}
Here's a loop to sort an array from min to max, I need the result of this loop to be put into another array so I can filter and remove the numbers that occur only once and find the last member of what's left.
Here's the code I have so far:
#include<stdio.h>
#include<conio.h>
#define buffas 1024
void main() {
int arr[buffas],i,j,element,no,temp;
printf("\nEnter the no of Elements: ");
scanf("%d", &no);
for(i=0; i<no; i++) {
printf("\n Enter Element %d: ", i+1);
scanf("%d",&arr[i]);
}
for(i=0; i<no; i++) {
for(j=i; j<no; j++) {
if(arr[i] > arr[j]) {
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
printf("\nSorted array:");
for(i=0; i<no; i++) {
printf("\t%d",arr[i]);
}
getch();
}
How do I change the
printf("\t%d",arr[i]);
To fill another array and then sort that to remove single entries and leave ony those that repeat at least once.
eg. the first aray is
2 2 1 6 9 9
and after the second sorting the result should be
2 2 9 9
#include <stdio.h>
#define buffas 16
int main(void)
{
/* Instead of original input and sorting code */
int arr[] = { 1, 2, 2, 6, 9, 9, 10, 10, 10, 11, 12, 13, 14 };
int no = sizeof(arr) / sizeof(arr[0]);
/* Code to copy only duplicated elements in arr */
int copy[buffas];
int n = 0;
for (int i = 0; i < no; i++)
{
int j;
for (j = i + 1; j < no; j++)
{
if (arr[i] != arr[j])
break;
}
if (j - i > 1)
{
for (int k = i; k < j; k++)
copy[n++] = arr[k];
i = j - 1;
}
}
/* Print results for verification */
for (int i = 0; i < n; i++)
printf("c[%d] = %d\n", i, copy[i]);
return 0;
}
The code has been run with various lengths of sorted array and different data in the array; it seems to be correct. The code above produces the output:
c[0] = 2
c[1] = 2
c[2] = 9
c[3] = 9
c[4] = 10
c[5] = 10
c[6] = 10
Note that the code uses the C99 feature of declaring variables in a for loop control statement; if you're on Windows and without C99 support, you'll need to declare i and k outside the loops. If you're using GCC, you need to add -std=c99 or a similar option.