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.
Related
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.
My selection sort successfully sorts certain numbers yet fails on some. The code seems very logical to me, I even printed step-by-step but somehow it does not work.
#include <stdio.h>
#include <string.h>
int number[] = {2, 5, 3, 1};
int length;
void sort(void);
void swap(int *xp, int *yp);
int main(void)
{
length = sizeof(number)/sizeof(number[0]);
for (int i = 0; i < length; i++)
{
printf("%i ", number[i]);
}
printf("\n");
sort();
}
void sort(void)
{
//number
for (int i = 0; i < length - 1; i++)
{
int max = i;
//printf("max:%i\n",max);
for (int j = i + 1; j < length; j++)
{
//printf("max:%i and j: %i\n",number[max], number[j]);
if (number[j] > number[max])
{
max = j;
//SWAP WINNER
swap(&number[max], &number[i]);
}
}
for (int k = 0; k < length; k++)
{
printf("%i ", number[k]);
}
printf("\n");
}
printf("\nnow:\n");
for (int k = 0; k < length; k++)
{
printf("%i ", number[k]);
}
printf("\n");
}
void swap(int *xp, int *yp)
{
int temp = *xp;
*xp = *yp;
*yp = temp;
}
With the result:
2 5 3 1
3 2 5 1
3 5 2 1
3 5 2 1
now:
3 5 2 1 ~/ $
Anyway, another question, why does it have to use pointers? Because it won't work without them. Any suggestions?
The bug is in this block:
if (number[j] > number[max])
{
max = j;
//SWAP WINNER
swap(&number[max], &number[i]);
}
after max = j, max is the index of the largest number found so far. But then, you swap that number with the number at i, so max no longer references the largest number.
Selection sort is a fairly simple algorithm and you are needlessly complicating it by using a separate variable to keep track of the max value's index.
My suggestions:
Remove the max variable.
Start the outer loop from the last index and decrement it down to the first so that index i will always point to the last index in the sub-array, that is the index at which the max number is to be placed.
Run the inner loop from the first index to i-1.
After making these changes, compare array[i] with array[j] in the inner loop and swap if array[j] is greater.
I should make new array out of existing one (ex. 1 0 4 5 4 3 1) so that the new one contains digits already in existing array and the number of their appearances.
So, the new one would look like this: 1 2 0 1 4 2 5 1 3 1 (1 appears 2 times, 0 appears 1 time.... 3 appears 1 time; the order in which they appear in first array should be kept in new one also); I know how to count no. of times a value appears in an array, but how do I insert the no.of appearances? (C language)
#include <stdio.h>
#define max 100
int main() {
int b, n, s, i, a[max], j, k;
printf("Enter the number of array elements:\n");
scanf("%d", &n);
if ((n > max) || (n <= 0)) exit();
printf("Enter the array:\n");
for (i = 0; i < n; i++)
scanf("%d", a[i]);
for (i = 0; i < n; i++) {
for (j = i + 1; j < n;) {
if (a[j] == a[i]) {
for (k = j; k < n; k++) {
a[k] = a[k + 1];
}}}}
//in the last 5 rows i've tried to compare elements, and if they are same, to increment the counter, and I've stopped here since I realised I don't know how to do that for every digit/integer that appears in array//
If you know that the existing array consists of digits between 0 and 9, then you can use the index of the array to indicate the value that you are incrementing.
int in[12] = {1,5,2,5,6,5,3,2,1,5,6,3};
int out[10] = {0,0,0,0,0,0,0,0,0,0};
for (int i = 0; i < 12; ++i)
{
++out[ in[i] ];
}
If you provide any code snippet, its easy for the community to help you.
Try this, even you optimize the no.of loops :)
#include <stdio.h>
void func(int in[], int in_length, int *out[], int *out_length) {
int temp[10] = {0}, i = 0, j = 0, value;
//scan the input
for(i=0; i< in_length; ++i) {
value = in[i];
if(value >= 0 && value <= 9) { //hope all the values are single digits
temp[value]++;
}
}
// Find no.of unique digits
int unique_digits = 0;
for(i = 0; i < 10; ++i) {
if(temp[i] > 0)
unique_digits++;
}
// Allocate memory for output
*out_length = 2 * unique_digits ;
printf("digits: %d out_length: %d \n",unique_digits, *out_length );
*out = malloc(2 * unique_digits * sizeof(int));
//Fill the output
for(i = 0, j = 0; i<in_length && j < *out_length; ++i) {
//printf("\n i:%d, j:%d val:%d cout:%d ", i, j, in[i], temp[in[i]] );
if(temp[in[i]] > 0 ) {
(*out)[j] = in[i];
(*out)[j+1] = temp[in[i]];
temp[in[i]] = 0; //Reset the occurrences of this digit, as we already pushed this digit into output
j += 2;
}
}
}
int main(void) {
int input[100] = {1, 0, 4, 5, 4, 3, 1};
int *output = NULL, output_length = 0, i = 0;
func(input, 7, &output, &output_length );
for(i=0; i < output_length; i+=2) {
printf("\n %d : %d ", output[i], output[i+1]);
}
return 0;
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I got this problem statement from a programming puzzle book. I am able to find out the horizontal palindromes, but only if the entire row is a palindrome.
How can I fulfill this? Also how can be diagonal palindromes acquired?
A pseudo code is okay too, I just need the basic logic behind this. I will perform the rest.
Thank You.
The trick behind finding the horizontal palindromes is the take an entire row and then split it into various strings. Once that is done, you need to check if that string is a palindrome. For vertical strings, you need to do the same thing for the columns.
Now for the diagonal ones, you need to start from a point at the edge and then move forward diagonally (+[1][1]) for going to the bottom right until you reach the end. Now keep doing for every tactical point of every edge which will help you get all the diagonal strings, the next thing you need to do is to split these strings and check if each of these short strings are a palindrome or not.
This would come under dynamic programming most likely. Although I am confused it might come under greedy approach as well. I'll confirm it once with my professor.
Here is the code I did back when I was trying to solve the same thing -
#define PALLEN 2
#include <stdio.h>
#include <string.h>
int a[10][10];
/*int a[5][5] = {
{ 1, 2, 1, 3, 5 } ,
{ 4, 5, 6, 7, 4 } ,
{ 4, 5, 5, 4, 1 } ,
{ 1, 9, 2, 1, 4 } ,
{ 1, 9, 4, 1, 5 }
};*/
int n=0;
void checkPalindrome(char*);
void diagonalPal();
void stringSpliter(char*);
int main() {
int i, j, k, l, x;
int c = 0;
int jmp;
int ptr = 0;
int diag;
char recycler[20];
char diaglist[25];
char revdiaglist[25];
system("cls");
printf("\nEnter the dimension (n) of this square matrix i.e. (n*n) - ");
scanf("%d", &n);
printf("\nNow enter the elements for this %d*%d matrix - ", n,n);
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d", &a[i][j]);
for(i=0;i<n;i++){
for(j=0;j<n;j++){
printf("-%d-", a[i][j]);
}
printf("\n");
}
printf("\nHorizontal Palindromes");
for (i = 0; i < n; i++) {
for (j = n-1, k = PALLEN; j > 0; j--, k++) {
while (c < j) {
jmp = c;
memset(recycler, 0, 20);
ptr = 0;
for (l = 0; l < k; l++) {
recycler[ptr] = a[i][jmp]; //0,0 -- 0,1
ptr++;
jmp++;
}
checkPalindrome(recycler);
c++;
}
c = 0;
}
}
printf("\n\nVertical Palindromes");
for (i = 0; i < n; i++) {
for (j = n-1, k = PALLEN; j > 0; j--, k++) {
while (c < j) {
jmp = c;
memset(recycler, 0, 20);
ptr = 0;
for (l = 0; l < k; l++) {
recycler[ptr] = a[jmp][i]; //0,0-- 1,0
ptr++;
jmp++;
}
checkPalindrome(recycler);
c++;
}
c = 0;
}
}
printf("\n\nDiagonal Palindromes");
diagonalPal();
}
void stringSpliter(char *a){
int i,j,k,ptr,jmp,c=0,l;
int len;
len = strlen(a);
char recycler[20];
for (j = len-1, k = PALLEN; j > 0; j--, k++) {
while (c < j) {
jmp = c;
memset(recycler, 0, 20);
ptr = 0;
for (l = 0; l < k; l++) {
recycler[ptr] = a[jmp]; //0,0 -- 0,1
ptr++;
jmp++;
}
checkPalindrome(recycler);
c++;
}
c = 0;
}
}
void diagonalPal(){
int i, x=0, j, k, ptr=0;
char diagrecycler[20];
for(i = 0; i < n; i++){
memset(diagrecycler, 0, 25);
ptr = 0;
for(j = i, k = 0; j < n, k < n; j++, k++){
diagrecycler[ptr++] = a[j][k];
}
stringSpliter(diagrecycler);
}
for(i = 1; i < n; i++){
memset(diagrecycler, 0, 25);
ptr = 0;
for(j = 0, k = i; j < n, k < n ;j++, k++){
diagrecycler[ptr++] = a[j][k];
}
stringSpliter(diagrecycler);
}
}
void checkPalindrome(char *string){
int isPalindrome = 1, i=0;
char rev[20];
strcpy(rev, string);
strrev(rev);
isPalindrome = strcmp(rev, string);
if(isPalindrome == 0){
printf("\n");
while(string[i]!='\0') printf("%d", string[i++]);
}
}
// Output
/*Enter the dimension (n) of this square matrix i.e. (n*n) - 4
Now enter the elements for this 4*4 matrix - 1 2 3 4
5 2 1 6
8 1 1 8
9 5 3 2
-1--2--3--4-
-5--2--1--6-
-8--1--1--8-
-9--5--3--2-
Horizontal Palindromes
11
8118
Vertical Palindromes
22
11
3113
Diagonal Palindromes
121
212
G:\Code snippets\C programmes>*/
#include<stdio.h>
int main(){
int a[9],i,j,r,t,min,c=0;
for(r=0;r<9;r++)
scanf("%d",&a[r]);
for (j=0;j<9;j++) {
min=a[j];
for(i=j;i<9;i++) {
if(a[i] < min ) {
c=i;
min=a[i];
}
}
t=a[j];
a[j]=min;
a[c]=t;
}
for(r=0;r<9;r++)
printf("%d",a[r]);
}
This is the code which i have to arrange the numbers entered byt the user in ascending order.
If input is 1 2 3 2 4 1 5 6 3 output is 1 1 2 2 3 3 4 5 6 but i want the output to be 1 2 3 4 5 6 i.e. duplicate entries deleted.Please help me.
If the range of the numbers is given then you can do it by using a boolean array which will store 1 to the corresponding index of the element.
#include <stdio.h>
#include <stdbool.h>
#define NUM_RANGE 10
int main(){
int num;
bool freq[NUM_RANGE + 1] = {0};
for(int r = 0; r < 9; r++){
scanf("%d",&num);
freq[num] = 1;
}
for (int i = 0; i < NUM_RANGE + 1; i++)
if(freq[i])
printf("%d ", i);
}
#include<stdio.h>
int main(){
int a[] = {1, 2, 3, 2, 4, 1, 5, 6, 3};
int n = sizeof(a)/sizeof(*a);
int i, j, t;
for (j=0;j<n-1;j++){
for(i=j+1;i<n;){
if(a[i] == a[j]){
t = a[i];
a[i] = a[--n];
a[n] = t;
continue;
}
if(a[i] < a[j]){
t = a[i];
a[i] = a[j];
a[j] = t;
}
++i;
}
}
for(i=0;i<n;i++)
printf("%d ", a[i]);
return 0;
}
So, this is the procedure you can follow.
You sort your array (as you have already done). Your sorting algorithm has O(n^2) worst-case-running time where n is the number of Elements in your array. If you care about running time, the optimal running time which can be achieved is O(n logn) [MergeSort].
Next, we need to find the duplicates and delete them.
Since you have already ordered them just loop through your array and check that every number a[i] and the next number a[i+1] are different. If they are not, delete it and fill the empty space by moving all the rest of the array one forward.
So:
for(i = 0; i < 9; i++){
if(a[i] == a[i+1]){
deletNumber(i); //deletes number at position i in the array and shifts the
//rest of the array so the empty space is filled.
}
}
void deleteNumber(int i){
int j;
for(j = i; j<8; j++){
a[j] = a[j++];
}
}