I have some array with numbers. When I want to write all combination with 3 digits I wrote this. But now I need edit this code to return all combination with 1 to numbers.size digits. How should I edit it ?
int items[] = {1, 2, 3, 4, 5};
int itemSize = 5;
for (int i = 0; i < itemSize - 2; i++) {
for (int j = i + 1; j < itemSize - 1; j++) {
for (int k = j + 1; k < itemSize; k++)
printf("%d%d%d\n", items[i], items[j], items[k]);
}
}
it is not perfect solution, but it is well enough to work. you can get all sub array from an array, from 1 element to length array
void permute(char* previous, char *a, int i, int n,int nmax)
{
int j;
if (i == n)
{
char c = a[nmax];
a[nmax] = 0;
if (strstr(previous,a) == 0)
{
printf("%s\n", a);
strncpy(previous,a,n);
}
a[nmax] = c;
}
else
{
for (j = i; j < n; j++)
{
char c = a[i];
a[i] = a[j];
a[j] = c;
permute(previous,a, i+1, n,nmax);
c = a[i];
a[i] = a[j];
a[j] = c;
}
}
}
void subarrays(char *a, int len)
{
int i=1;
char *previous = strdup(a);
*previous = 0;
for(i=1;i<len+1;i++)
{
permute(previous,a,0,len,i);
}
}
int main(void) {
char *arr = strdup("abcde");
subarrays(arr,strlen(arr));
return EXIT_SUCCESS;
}
Since you're already doing a web search for this, here is a generic solution.
#include <stdio.h>
int next_combination(size_t *I, size_t k, size_t n)
{
size_t i, j;
i = k-1; /* find next element to increment */
while(I[i] == (n-k+i)){
--i;
if(i == (size_t)-1){ /* if done */
for(i = 0; i < k; i++) /* return with initial combination */
I[i] = i;
return(0);
}
}
I[i] += 1; /* increment element */
for(j = i+1; j < k; j++) /* create increasing string */
I[j] = I[i]+j-i;
return(1); /* return with new combination */
}
int main(int argc, char **argv)
{
int A[5] = {1, 2, 3, 4, 5};
size_t I[5];
size_t i, k, n;
n = sizeof(A)/sizeof(A[0]); /* n things */
for(k = 1; k <= n; k++){ /* n things k at a time */
for(i = 0; i < k; i++) /* create initial combination */
I[i] = i;
do{ /* display combinations */
for(i = 0; i < k; i++)
printf("%2d", A[I[i]]);
printf("\n");
}
while(next_combination(I, k, n));
}
return(0);
}
Related
My code is this:
#include <stdio.h>
int array_second_positive(int A[], int size) {
int j, i;
for (i = 0; i < size / 2; ++i) {
A[i] = A[0];
for (j = 0; j < size; ++j) {
A[j] = A[j + 1];
}
}
for (i = 0; i < size; ++i) {
int z = 0;
int counter = 0;
while (z < size) {
if (A[z] > 0) {
counter++;
}
if (counter == 2) {
return A[z];
}
z++;
}
}
}
int main(void) {
int size = 6;
int array[] = { 3, -14, 15, 2, 6, 5 };
int result = array_second_positive(array, size);
}
I am trying to find the second positive number in the second half of an array. If we assume array size is even and we have definitely at least two positive number in the second half but I get no results.
Here is my code. While choosing values, if I try to put 9 values it dumps garbage value. It has happened while doing quick sort as well
#include <stdio.h>
void printArray(int* A, int n) {
for (int i = 0; i < n; i++) {
printf("%d ", A[i]);
}
printf("\n");
}
int maximum(int A[],int n) {
int i, max = A[0];
for (i = 1; i < n; i++) {
if (A[i] > max) {
max = A[i];
}
}
return max;
}
void countSort(int A[], int n) {
int i, max = maximum(A, n);
int count[max + 1], B[n];
for (i = 0; i < max + 1; i++) {
count[i] = 0;
}
for (i = 0; i < max + 1; i++) {
count[A[i]]++;
}
for (i = 1; i < n; i++) {
count[i] += count[i - 1];
}
for (i = n - 1; i >= 0; i--) {
B[--count[A[i]]] = A[i];
}
for (i = 0; i < n; i++) {
A[i] = B[i];
}
}
int main(){
int A[] = {1, 4, 6, 2, 3, 2, 3, 2, 7};
int n = 9;
printArray(A, n); // Printing the array before sorting
countSort(A, n); // Function to sort the array
printArray(A, n); // Printing the array before sorting
return 0;
}
This code uses the wrong limit:
for(i=0;i<max+1;i++) {
count[A[i]]++;
}
It effectively iterates through the elements of A, which has n elements, not max+1.
I am trying to create a reverse bubble sort algorithm. It works but the first output is a big crazy number that i dont understand. The remaining outputs seem to be sorted in descending order. Where is my code wrong?
#include <stdio.h>
void ft_rev_int_tab(int *tab, int size)
{
int i;
int j;
int k;
i = 0;
while (i < size)
{
j = 0;
while (j < size -i)
{
if (tab[j] < tab[j+1])
{
k = tab[j];
tab[j] = tab[j+1];
tab[j + 1] = k;
}
j++;
}
i++;
}
}
int main(void)
{
int tab[] = {9, 320, 0, 113, 15};
int size = sizeof(tab) / sizeof(*tab);
ft_rev_int_tab(tab, size);
for (int i = 0; i < size; i++)
{
printf ("%d\n", tab[i]);
}
}
i = 0; before while (i < size) is wrong. The condition j < size -i is equivalent to j < size when i is zero. In this case j will be at most size-1 and now tab[j+1] is out-of-range.
It should be changed to i = 1;.
void ft_rev_int_tab(int *tab, int size) {
int i;
int j;
int k;
i = 0;
while (i < size) {
j = 0;
while (j < size - i - 1) {
if (tab[j] < tab[j + 1]) {
k = tab[j];
tab[j] = tab[j + 1];
tab[j + 1] = k;
}
j++;
}
i++;
}
}
Your condition in the nested while loop should be (j < size - i - 1)
because you only need to check the value from tab[0] to tab[i-1]. Your if and swap checks tab[j] and tab[j+1], so when i is equal to the size of the array, you will compare it with a value outside of the array. In your example is the tab[5]'s value.
#include <stdio.h>
#include <string.h>
int ai, aj; // ai and aj to store the value of i and j respectively
int maxx(int a, int b) { // to return max of the two numbers
return (a <= b) ? b : a;
}
void LongestPal(char a[], int n) { // to find longest palindrome
int i, j, max = 0;
int p[1000][1000] = { 0 };
for (j = 0; j < n; j++)
for (i = 0; i <= j; i++) {
if (i == j) { // for one string having only one character
p[i][j] = 1;
if (max < p[i][j]) {
max = p[i][j];
ai = i;
aj = j;
}
}
if (j == (i + 1)) { // for string having two characters
if (a[i] == a[j]) { // if the string is like "aa","bb" etc.
p[i][j] = 2;
if (max < p[i][j]) {
max = p[i][j];
ai = i;
aj = j;
}
} else { // if string like "ab","ba" etc.
p[i][j] = 1;
if (max < p[i][j]) {
max = p[i][j];
ai = i;
aj = j;
}
}
} else { // for all other type of strings
if (a[i] == a[j]) { // if a longer palindrome found
p[i][j] = p[i-1][j-1] + 2;
if (max < p[i][j]) {
max = p[i][j];
ai = i;
aj = j;
}
} else { // if no longer palindrome is present
p[i][j] = maxx(p[i+1][j], p[i][j-1]);
if (max < p[i][j]) {
max = p[i][j];
ai = i;
aj = j;
}
}
}
}
}
int main() {
int i, j, n;
char a[1000];
printf("Just enter the string hoss!\n");
scanf("%s", &a);
n = strlen(a);
LongestPal(a, n);
for (i = ai; i <= aj; i++)
printf("%c", a[i]);
return 0;
}
In this program I wanna find longest palindrome Subsequence, but unable to run program
I have written comments for each case
This program for printing longest palindrome subsequence isn't working, When I run it, the Windows console stops working after taking input.
Your program fails because it allocates too much data with automatic storage (aka on the stack). int p[1000][1000] uses 4MB, probably too much for your system default stack size. You can try and use less space by allocating this array as:
int p[n][n];
This is allowed in C99, but your compiler might not support C99.
Your algorithm is a little complicated. Why not enumerate all positions for i and j and just verify with an auxiliary function if you have a palindrome there and keep track of the longest one:
#include <stdio.h>
#include <string.h>
int isPalindrome(const char *a, int i, int j) {
for (; i < j; i++, j--) {
if (a[i] != a[j])
return 0;
}
return 1;
}
void getLongestPalindrome(const char *a, int n, int *ai, int *aj) {
int i, j, maxi, maxj;
for (maxi = maxj = i = 0; i < n; i++) {
for (j = n - 1; j > i + maxj - maxi; j--) {
if (isPalindrome(a, i, j)) {
maxi = i;
maxj = j;
break;
}
}
}
*ai = maxi;
*aj = maxj;
}
int main(void) {
char a[1000];
int i, j;
printf("Enter the string: ");
if (scanf("%999s", a) == 1) {
getLongestPalindrome(a, strlen(a), &i, &j);
printf("longest palindrome at %d..%d: %.*s\n", i, j, j - i + 1, a + i);
}
return 0;
}
I am trying to create C program to read a text file and sort it by ascending order. The example of text file is
2
3; 2, 5, 7
6; 4, 7, 8, 9, 5, 2
with the first line indicated the number of rows, the number after the ";" indicated elements each rows and elements separated by ",".
So my idea is to create a dynamic jagged array with rows as the first number, then point each row to the different array with element. Sort the pointer arrays first then sort elements of each arrays. This is what I have tried so far
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int SortLists();
int main ()
{
int i,j = 0;
char filename[10]; //name of the file
char line[100];
int rows = 3; //I have to initialized this to test my code first
int cols;
int **jaggedArr; //jagged array
jaggerArr = malloc (rows*sizeof(int*)) ;
printf("Enter the file name with .txt : ");
scanf("%s", filename);
FILE *filePtr = fopen(filename, "r");
int num;
if (filePtr != NULL)
{
while (fgets(line, sizeof(line), filePtr) != NULL) //read each line of the text
{
cols = atoi(strtok(line, ";")); //use strtk to break elements
for (i = 0; i < rows; i++)
{
for (j = 0; j < cols; j++)
{
jaggedArr[i][j] = atoi(strtok(line, ",")); //parse into the jagged array
}
}
}
fclose(filePtr);
}
}
int SortLists(int list[], int size) //sort method
{
int i,j,temp;
for (i = 0; i < size; ++i)
{
for (j = i + 1; j < size; ++j)
{
if (list[i] > list[j])
{
temp = list[i];
list[i] = list[j];
list[j] = temp;
}
}
}
}
As a beginner in C, I am not familiar with the idea of pointer, which a lot different with C#.
Sorry for my bad English as its not my first language. Thank you so much for helping me.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define S_(x) #x
#define S(x) S_(x)
void SortLists(int list[], int size);
int main(void){
char filename[FILENAME_MAX+1];
char line[100];
int rows, cols;
printf("Enter the file name with .txt : ");
scanf("%" S(FILENAME_MAX) "[^\n]%*c", filename);
FILE *filePtr = fopen(filename, "r");
if(!filePtr)
return EXIT_FAILURE;
fscanf(filePtr, "%d ", &rows);
int **jaggedArr;
jaggedArr = malloc (rows * sizeof(int*));
int *sizeArr = malloc(rows * sizeof(int));
int r = 0, c;
while (fgets(line, sizeof(line), filePtr) != NULL){
sizeArr[r] = cols = atoi(strtok(line, ";"));
jaggedArr[r] = malloc(cols * sizeof(int));
for (c = 0; c < cols; ++c){
jaggedArr[r][c] = atoi(strtok(NULL, ","));
}
SortLists(jaggedArr[r++], cols);
}
fclose(filePtr);
//check print and deallocation
for(r = 0;r < rows; ++r){
for(c = 0; c < sizeArr[r]; ++c)
printf("%d ", jaggedArr[r][c]);
printf("\n");
free(jaggedArr[r]);
}
free(jaggedArr);
free(sizeArr);
return 0;
}
void SortLists(int list[], int size){
int i,j,temp;
for (i = 0; i < size-1; ++i){
for (j = i + 1; j < size; ++j){
if (list[i] > list[j]){
temp = list[i];
list[i] = list[j];
list[j] = temp;
}
}
}
}
for extra question.
#include <stdio.h>
void SortLists(int list[], int size);
void SortRows(int *jaggedArr[], int size, int *rowSize);
int main(void){
int row1[] = {4,7,8,9,5,2};
int row2[] = {2,5,7};
int *jaggedArr[] = { row1, row2};
int rows = 2;
int sizeArr[] = {6,3};
int i, r, c;
for(i=0;i<rows;++i)
SortLists(jaggedArr[i], sizeArr[i]);
for(r = 0;r < rows; ++r){
for(c = 0; c < sizeArr[r]; ++c)
printf("%d ", jaggedArr[r][c]);
printf("\n");
}
printf("\n");
SortRows(jaggedArr, rows, sizeArr);
for(r = 0;r < rows; ++r){
for(c = 0; c < sizeArr[r]; ++c)
printf("%d ", jaggedArr[r][c]);
printf("\n");
}
return 0;
}
void SortLists(int list[], int size){
int i,j,temp;
for (i = 0; i < size-1; ++i){
for (j = i + 1; j < size; ++j){
if (list[i] > list[j]){
temp = list[i];
list[i] = list[j];
list[j] = temp;
}
}
}
}
void SortRows(int *jaggedArr[], int size, int *rowSize){
int i,j,temp,*tempp;
for (i = 0; i < size-1; ++i){
for (j = i + 1; j < size; ++j){
if (rowSize[i] > rowSize[j]){
//swap in pairs
temp = rowSize[i];
rowSize[i] = rowSize[j];
rowSize[j] = temp;
tempp = jaggedArr[i];
jaggedArr[i] = jaggedArr[j];
jaggedArr[j] = tempp;
}
}
}
}
#include <stdio.h>
void SortLists(int list[], int size);
void SortRows(int *jaggedArr[], int size);
int main(void){
int row1[] = {6, 4,7,8,9,5,2};//The first element represents the number of elements.
int row2[] = {3, 2,5,7};
int *jaggedArr[] = { row1, row2};
int rows = 2;
//int sizeArr[] = {6,3};//Not required
int i, r, c;
for(i=0;i<rows;++i)
SortLists(jaggedArr[i]+1, jaggedArr[i][0]);
SortRows(jaggedArr, rows);
for(r = 0;r < rows; ++r){
for(c = 1; c <= jaggedArr[r][0]; ++c)
printf("%d ", jaggedArr[r][c]);
printf("\n");
}
return 0;
}
void SortLists(int list[], int size){
int i,j,temp;
for (i = 0; i < size-1; ++i){
for (j = i + 1; j < size; ++j){
if (list[i] > list[j]){
temp = list[i];
list[i] = list[j];
list[j] = temp;
}
}
}
}
void SortRows(int *jaggedArr[], int size){
int i,j,*tempp;
for (i = 0; i < size-1; ++i){
for (j = i + 1; j < size; ++j){
if (jaggedArr[i][0] > jaggedArr[j][0]){
tempp = jaggedArr[i];
jaggedArr[i] = jaggedArr[j];
jaggedArr[j] = tempp;
}
}
}
}