Generate all possible non-repeating integers from 1 to limit - c

I'm having a hard time changing up this program. The algorithm is correct already for generating the non-repeating lists. However, I want the list to be generated from a range of the user's integer. (1 to n)
Ex: user inputs 5 -> prints (1 2 3 4 5) then asks for 5 integers to generate the combination(s).
How would I change this so that the combinations are found from (1-n) instead of individually entering the integers n times? Any help would be greatly appreciated.
(Sorry if it's messy, I'm a student :P)
#include<stdio.h> stdio.h
#include<stdlib.h> stdlib.h
int a1[50], a2[50]; // arrays
int count=-1, range;
int w; // user defined variable
void main()
{
printf("Please enter a number. (1-10): ");
scanf("%d", &w);
int x,y; // comparing
printf("You have entered %d\n\n",w);
for(range=1; range<=w; range++){
printf("%d", range);
printf(" ");
} // end for "range"
printf("\n");
for(x=0; x<w; x++){
a1[x]=0;
y=x+1;
scanf("%d\n\n" ,&a2[y]);
}// end for
combo(w);
} // end main
combo(int z) // function with algorithm to find combonations
{
while (w<1 || w>10){
printf("\nThat number is not in range. Please try again. \n\n");
printf("Please enter a number. (1-10): ");
scanf("%d", &w);
} // end while
int x;
a1[z]=++count;
if(count==w){
for(x=0; x<w; x++)
printf("%2d",a2[a1[x]]);
printf(" ");
} // end if
for(x=0; x<w; x++)
if(a1[x]==0)
combo(x);
count--;
a1[z]=0;
} // end "combo"

#include <stdio.h>
#include <string.h>
void swap (int *X, int *Y)
{
int temp;
temp = *X;
*X = *Y;
*Y = temp;
}
void print_array(int *a, int n) {
int i;
printf("\t=> ");
for(i = 0; i < n; i++){
printf("%d, ", a[i]);
}
printf("\n");
}
void mixmatch (int *Arr, int i, int n)
{
int j;
int *A = Arr;
if (i == n)
print_array(A,n+1);
else
{
for (j = i; j <= n; j++)
{
swap((A+i), (A+j));
mixmatch(A, i+1, n);
swap((A+i), (A+j));
}
}
}
int main()
{
int A[10];
int k;
int i;
printf("Enter a number between (1-10):");
scanf("%d", &k);
for(i = 0; i < k && i < 10; i++) {
printf("%d, ",i);
A[i] = i;
}
printf("\n");
mixmatch(A, 0, k-1);
return 0;
}
This is how you can modify it to incorporate integers and arrays of them.

Related

How to put commas on a series of numbers in a proper way

Yes I have searched through the net and also here but failed to find similar cases. I have here a program which prints prime numbers between 2 given integers. But I have to print them with commas in a proper way. like 1, 2, 3, 4 without having a comma on the last integer. Here is my code.
#include<stdio.h>
void main()
{
int x, y, i, j;
puts("Enter first number: ");
scanf("%d", &x);
puts("Enter second number: ");
scanf("%d", &y);
for(i=x; i<=y; i++)
{
for(j=2; j<=i; j++)
{if(i%j==0)
{
break;
}
}
if(i==j)
{
printf("%d, ", i);
}
}
}
I wanted to identify the total number of prime numbers printed in order to set a condition that if it is the last one, a comma will not print but I don't know if it will work. That is the only thing I can think of for now and your help guys will be really appreciated. Thank you!
As #kaylum pointed out, use a flag to "skip" printing the comma when printing the first number. Set that flag to false after the very first number you print.
#include <stdio.h>
void main() {
int x, y, i, j;
puts("Enter first number: ");
scanf("%d", &x);
puts("Enter second number: ");
scanf("%d", &y);
int is_first_time = 1;
for (i = x; i <= y; i++) {
for (j = 2; j <= i; j++) {
if (i % j == 0) {
break;
}
}
if (i == j) {
if (is_first_time) {
printf("%d", i);
is_first_time = 0;
}
else {
printf(", %d", i);
}
}
}
}
You can use a flag, as others have suggested, but you can also use a string as a separator, setting it to empty for the first element and any desired separator after that.
#include <stdio.h>
void print_join(const char *sep, int n, int a[n])
{
const char *s = "";
for (int i = 0; i < n; i++)
{
printf("%s%d", s, i);
s = sep;
}
printf("\n");
}
int main(void)
{
int a[] = {1, 2, 3, 4, 5};
int n = sizeof a / sizeof *a;
print_join(", ", n, a);
print_join(";", n, a);
print_join("", n, a);
print_join("--", n, a);
return 0;
}

Why am I getting segmentation fault and how do I solve this?

C program for bubble sort using a minimum of 4 functions.(input,output,compute,main)
No global variables allowed.
No printf or scanf in compute.
No printf or scanf in main
Input should not call compute.
compute should not call output.
I haven't really understood pointers and functions.
#include <stdio.h>
void input(int* size, int* arr[])
{
printf("Enter the size of the array: ");
scanf("%d",size);
printf("Enter the elements of the array\n");
for(int i = 0;i < *size; i++)
{
scanf("%d", arr[i]);
}
}
void swap(int *x,int *y)
{
int temp = *x;
*x = *y;
*y = temp;
}
void bubble_sort(int arr[100],int size)
{
for(int i = 0;i < size - 1;i++)
{
for(int j = 0;j < size - 1 - i;j++)
{
if(arr[j] > arr[j+1])
{
swap(&arr[j],&arr[j+1]);
}
}
}
}
void output(int size,int* arr)
{
printf("Sorted array\n");
for(int i = 0;i < size;i++)
{
printf("%d",arr[i]);
}
}
int main()
{
int* input_values[50];
int size;
input(&size, input_values);
bubble_sort(size,*input_values);
output(size, *input_values);
return 0;
}
No errors but showing segmentation fault.How do I solve this?
So your problem is here :
scanf(" %d", arr[i]);
You have to change this to :
scanf(" %d", &arr[i]);
This is the main problem, but there are a lot of others.
Also you have to change the order of parameters in
bubble_sort(size,*input_values);
to
bubble_sort(input_values,size);
and
output(size, *input_values);
to
output(size, input_values);
Also in order this to work at all i have changed the
scanf("%d", &arr[i]);
to
scanf(" %d", &arr[i]);
Actually your code is full of mistakes like the usage of scanf and the usage of pointers and arrays, the following is a workable version of you code see and compare:
#include <stdio.h>
void input(int* size, int arr[])
{
char chr;
printf("Enter the size of the array: ");
scanf( "%d%c", size, &chr );
printf("Enter the elements of the array\n");
for(int i = 0;i < *size; i++)
{
scanf("%d%c", &arr[i], &chr);
}
}
void swap(int *x,int *y)
{
int temp = *x;
*x = *y;
*y = temp;
}
void bubble_sort(int* size,int arr[])
{
for(int i = 0;i < *size - 1;i++)
{
for(int j = 0;j < *size - 1 - i;j++)
{
if(arr[j] > arr[j+1])
{
swap(&arr[j],&arr[j+1]);
}
}
}
}
void output(int* size,int arr[])
{
printf("Sorted array\n");
for(int i = 0;i < *size;i++)
{
printf("%d",arr[i]);
}
}
int main()
{
int input_values[50];
int s = 0;
int* size = &s;
input(size, input_values);
bubble_sort(size,input_values);
output(size, input_values);
return 0;
}

using output of a program as input in same program

I am writing a program to generate all possible permutations of a given series of numbers and then generate all possible binary trees from that permutations so, what I thought is having a program which generates permutations and stores the result to a file and then write further code to read line by line (which has all permutations ) and generate binary trees out of them, so right now I have written half program which generates permutation and it stores the result in file.
#include <stdio.h>
//function to print the array
void printarray(int arr[], int size)
{
FILE *fp;
int i,j;
fp=fopen("result.txt","w");
for(i=0; i<size; i++)
{
// printf("%d\t",arr[i]);
fprintf(fp,"%d\t",arr[i]);
}
printf("\n");
fclose(fp);
}
//function to swap the variables
void swap(int *a, int *b)
{
int temp;
temp = *a;
*a = *b;
*b = temp;
}
//permutation function
void permutation(int *arr, int start, int end)
{
if(start==end)
{
printarray(arr, end+1);
return;
}
int i;
for(i=start;i<=end;i++)
{
//swapping numbers
swap((arr+i), (arr+start));
//fixing one first digit
//and calling permutation on
//the rest of the digits
permutation(arr, start+1, end);
swap((arr+i), (arr+start));
}
}
int main()
{
//taking input to the array
int size;
printf("Enter the size of array\n");
scanf("%d",&size);
int i;
int arr[size];
for(i=0;i<size;i++)
scanf("%d",&arr[i]);
//calling permutation function
permutation(arr, 0, size-1);
return 0;
}
but the problem here in this program is that this program only stores one permutation and does not stores other permutations in result.txt file, how do I go on storing result this way. Also program does not ends a blank cursor blinking which gives a false impression of infinite while loop.
I had to press Ctrl+c to end the program how to get rid of this?
your fopen("result.txt","w"); truncates file each time opened.
use fopen("result.txt","a"); instead
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
#define N 10
void print(int *num, int n)
{
FILE *fp;
fp=fopen("result.txt","a");
int i;
for ( i = 0 ; i < n ; i++)
// printf("%d ", num[i]);
fprintf(fp,"%d ",num[i]);
fprintf(fp,"\n");
fclose(fp);
}
int main()
{
int num[N];
int *ptr;
int temp;
int i, n, j;
printf("\nHow many number you want to enter: ");
scanf("%d", &n);
printf("\nEnter a list of numbers to see all combinations:\n");
for (i = 0 ; i < n; i++)
scanf("%d", &num[i]);
for (j = 1; j <= n; j++) {
for (i = 0; i < n-1; i++) {
temp = num[i];
num[i] = num[i+1];
num[i+1] = temp;
print(num, n);
}
}
return 0;
}

How to pass arrays to functions

I am trying to write a program which calculates some bags and weights. I wrote it without using functions but I have to use functions and I am really bad at it.
The code normally works, but I just can't implement it with functions. It stops working after printing array A, and just 0s when printing array B.
My code is:
#include <stdio.h>
#include <math.h>
int f1(int N);
int f2(int N);
int f3(int N, float A[20]);
int main(void)
{
int N;
f1(N);
return 0;
}
int f1(int N)
{
for(;;)
{
printf("Enter N(the number of bags) (Between 1 and 20): ");
scanf("%d", &N);
if (N < 1 || N > 20)
{
continue;
}
else
{
break;
}
}
f2(N);
}
int f2(int N)
{
float A[20];
int i;
for(i=0; i<N;i++)
{
printf("Enter the weight of the bag with potatoes %d: ", i+1);
scanf("%f", &A[i]);
}
printf("\n\nThe weights of the initial bags (the A array):\n");
for(i=0; i<N;i++)
{
printf("%.1f " ,A[i]);
}
f3(N, &A[20]);
}
int f3(int N, float A[20])
{
int i;
float B[10];
printf("\n\nNow we equalize the weights of bags.\n");
if (N%2 == 0)
{
for(i=0;i<N/2 ;i++)
{
B[i] = fabsf(A[i] - A[N-1-i]);
}
}
else
{
for(i=0;i<N/2 ;i++)
{
B[i] = fabsf(A[i] - A[N-1-i]);
}
B[N/2] = A[N/2];
}
if (N%2 == 0)
{
for (i=0; i<N/2; i++)
{
if (A[i] < A[N-1-i])
{
A[N-1-i] = A[i];
}
else
{
A[i] = A[N-1-i];
}
}
}
else
{
for (i=0; i<N/2; i++)
{
if (A[i] < A[N-1-i])
{
A[N-1-i] = A[i];
}
else
{
A[i] = A[N-1-i];
}
}
A[N/2] = 0;
}
printf("\nThe weights of the new bags (the B array):\n");
if (N%2 == 0)
{
for(i=0; i<N/2 ;i++)
{
printf("%.1f " ,B[i]);
}
}
else
{
for(i=0; i<N/2 ;i++)
{
printf("%.1f " ,B[i]);
}
printf("%.1f", B[N/2]);
}
printf("\nThe new weights of the initial bags (the A array):\n");
for(i=0;i<N;i++)
{
printf("%.1f ", A[i]);
}
}
To pass an array to a function just use its name.
f3(N, &A[20]);
should be
f3(N, A);
To pass an array or pointer as an argument when calling a function in C, you just need to pass it name, in your case,
f3(N, A);
Also, when declaring the function, the length of the array doesn't matter, because C performs no bounds checking for formal parameters. Although it will work this way, it is best to change
int f3(int N, float A[20])
to
int f3(int N, float A[])
or
int f3(int N, float* A)

Its printing a extra zero at the end and I'm not sure why

It asks user for how man random numbers they wish to create and sort and for some reason its printing a zero at the end of the list and I have no clue why... It checks to make sure more than 1 number is entered and that less then 10,000 are entered.
#include <stdio.h>
#define MAX 10000
int randu(void);
void bubble(int a[], int num);
void swap(int*,int*);
int main (int argc, char *argv[]) {
int numsor,y;
int randoms[MAX];
int x=0;
if (argc == 1){
printf("How many numbers do you wish to sort? \n");
scanf("%d", &numsor);
}
if (argc == 2){
sscanf(argv[1], "%d", &numsor);
}
if (argc > 2){
printf("how many numbers do you wish to sort? \n");
scanf("%d", &numsor);
}
while (numsor<2 || numsor>10000){
if (numsor < 2){
printf("Error please enter a number more than 2. \n");
}
else{
printf("Error please enter a number less than 10,000. \n");
}
scanf("%d", &numsor);
}
y = randu();
for(x=0; x<numsor; x++){
randoms[x]=randu();
}
bubble(randoms,numsor);
for( x = 0; x <= numsor; x++){
printf( "[%d]\n", randoms[x]);
}
}
int randu(){
static int seed=17;
seed=(25179*seed+13849)%65536;
return seed;
};
void swap(int *a, int *b){
int temp;
temp=*a;
*a=*b;
*b=temp;
}
void bubble(int a[], int num){
int i,j;
for(i=0; i<num-1; i++)
for(j=num-1;i<j;j--)
if(a[j-1]>a[j])
swap(&a[j-1], &a[j]);
}
This:
for( x = 0; x <= numsor; x++){
should use <, since otherwise you will iterate one time too many.

Resources