Issue with Union of characters program in c - c

So I'm trying to write a program that finds the union, intersection, and determining if b is a subset of a for two groups of characters entered.
So if I enter
abcd for a[]
and
ac for b[]
It should print:
intersection: ac
union: abcd
"b is a subset of a".
My code is working for the most part, but my union function isn't giving an output. Do you have any suggestions for fixing it? I ran through it and it seems to be executing, but d isn't printing for someone... is my boolean statement wrong?
My code:
#include <stdio.h>
#include "simpio.h"
#include "genlib.h"
#include "strlib.h"
#define n 26
/* typedef enum letters {a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z};
letters letter; */
bool intersection(bool a[], bool b[], bool c[]);
void GetSet(bool set[]);
void init(bool a[]);
void printArray(bool set[]);
void getunions(bool a[], bool b[], bool d[]);
void contain(bool a[], bool b[]);
main()
{
bool a[n], b[n], c[n], d[n];
init(a);
init(b);
init(c); /* intersection */
init(d); /* union */
printf("Entries for a = \n");
GetSet(a);
printf("Entries for b = \n");
GetSet(b);
intersection(a,b,c);
printArray(c);
getunions(a,b,d);
printArray(d);
contain(a,b);
getchar();
}
void init(bool set[])
{
int i;
for(i=0;i<n;i++)
{
set[i]=FALSE;
}
}
void GetSet(bool set[])
{
int i;
string str=GetLine();
int len=StringLength(str);
for(i=0;i<len;i++)
{
set[str[i]-97]=TRUE;
}
}
bool intersection(bool a[], bool b[], bool c[])
{
int i;
for(i=0;i<n;i++)
{
if(b[i]&&a[i]==TRUE) c[i]=TRUE;
}
printf("\n\nThe intersection is\n");
return c;
}
void getunions(bool a[], bool b[], bool d[])
{
int i;
for(i=0;i<n;i++)
{
if(a[i]==TRUE||b[i]==TRUE)
d[i]==TRUE;
}
}
void printArray(bool set[])
{
int i;
for(i=0;i<n;i++)
{
if(set[i])
printf("%c", i+97);
}
}
void contain(bool a[], bool b[])
{
int i;
bool flag;
for(i=0;i<n;i++)
{
if(a[i]&&b[i]) flag=TRUE;
}
if(flag) printf("\n\nb is a subset of a.\n");
else printf("\n\nb is not a subset of a.\n");
}

In getunions, d[i]==TRUE; should be d[i]=TRUE;. You want an assignment, not a comparison.
As an aside, I think the contain function is incorrect. A) it does not initialize flag, and B) it says "b is a subset of a" as long as a and b share at least one common element.

Related

Passing multidimensional arrays to a function

The point of the program is to send data from 1 array to another array I'm not sure what's wrong with how I'm passing it. It should enter the data in 1 array then call upon the copy function and puts itself there and then the array is traversed.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<limits.h>
#include<math.h>
#include<ctype.h>
#include<stdbool.h>
double copy_arr(double source[n][u],double target[n][u],int n,int u);
int main(void)
{
double source[3][5]={{1.1,2.2,3.3,4.4,5.5},
{1.1,2.2,3.3,4.4,5.5},
{1.1,2.2,3.3,4.4,5.5}};
double target1[3][5];
copy_arr(source,target1,3,5);
int j;
int i;
for(i=0;i<3;i++)
{
for(j=0;j<5;j++)
{
printf("%f 1",target1[i][j]);
}
}
return 0;
}
double copy_arr(double source[][],double target[][],int n,int u)
{
int i,j;
for(i=0;i<n;i++)
{
for(j=0;j<u;j++)
{
target[i][j] = source[i][j];
}
}
return target[n][u];
}
Your function prototype is wrong because compiler has not seen n and u yet. Your program does not even compile.
Change
double copy_arr(double source[n][u],double target[n][u],int n,int u);
to
double copy_arr(int n,int u,double source[n][u],double target[n][u]);
Or you could do hardcoded array size
#define SIZE_ARR 5
void copy_arr(double source[][SIZE_ARR], double target[][SIZE_ARR], int n, int u);
int main(void)
{
double source[3][SIZE_ARR]={{1.1,2.2,3.3,4.4,5.5},
{1.1,2.2,3.3,4.4,5.5},
{1.1,2.2,3.3,4.4,5.5}};
double target1[3][SIZE_ARR];
copy_arr(source,target1, 3, 5);
int j;
int i;
for(i=0;i<3;i++)
{
for(j=0;j<5;j++)
{
printf("%f 1",target1[i][j]);
}
}
return 0;
}
void copy_arr(double source[][SIZE_ARR], double target[][SIZE_ARR], int n, int u)
{
int i,j;
for(i=0;i<n;i++)
{
for(j=0;j<u;j++)
{
target[i][j] = source[i][j];
}
}
}

Merge two arrays without sorting using array

I am trying to merge to arrays without sorting (add one then another) using pointer method but its just printing the first array and then garbage values. What i am trying to do is just combine 2 arrays in one big array. No sorting required(at least for now).
void getarray(int*,int);
int merge(int*,int*,int,int,int*);
main()
{
int a[10],b[10],c[20];
int i,j,n,m,size;
clrscr();
printf("Enter no. of elements in FIRST array: ");
scanf("%d",&n);
getarray(a,n);
printf("Enter no. of elements in SECOND array: ");
scanf("%d",&m);
getarray(b,m);
merge(a,b,n,m,c);
printf("\nMerged Array: \n");
for(i=0;i<n+m;i++)
{
printf("\t%d\t",c[i]);
}
getch();
return 0;
}
void getarray(int *x, int y)
{
int i;
for(i=0;i<y;i++)
{
scanf("%d",x+i);
}
}
int merge(int *a, int *b,int n,int m,int *c)
{
int i,j;
for(i=0;i<n;i++)
{
*(c+i) = *(a+i);
}
for(j=i;j<i+m;j++)
{
*(c+j) = *(b+j);
}
}
Alternatively you can use (assuming c is large enough):
void merge(int *a, int *b,int n,int m,int *c) {
memcpy(c, a, sizeof(int)*n);
memcpy(c+n, b, sizeof(int)*m);
}
You would need to include string.h.
int merge(int *a, int *b,int n,int m,int *c)
{
int i,j;
for(i=0;i<n;i++)
{
*(c+i) = *(a+i);
}
for(j=0;j<m;j++)
{
*(c+n+j) = *(b+j);
}
}

function within function by reference in c

i want to pass a function within function by reference in c.They both use the same parameters.This is the code
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
void AtoB(int *A, int *B, int *C, int n,int *h1,int *h2,int *h3 );
void AtoC(int *A, int *B, int *C, int n,int *h1,int *h2,int *h3 );
void BtoC(int *A, int *B, int *C, int n,int *h1,int *h2,int *h3 );
void function2(int *A, int *B, int *C, int n);
int main(){
int n;
int e;
int h3=1;
int h2=1;
int h1=0;
int min;
int *A;
int *B;
int *C;
printf("Give me the number of disks:");
scanf("%d",&n);
A = (int *) calloc(n,sizeof(int));
B = (int *) calloc(n,sizeof(int));
C = (int *) calloc(n,sizeof(int));
min=pow(2,n)-1;
for (e=0;e<n;e++){
A[e]=e+1;
}
if (n%2==0){
for (e=0;e<min/3;e++){
AtoB(A,B,C,n,&h1,&h2,&h3);
}
}
free(A);free(B);free(C);
return 0;
}
int function1(int Z[],int n){
int j,i,k,a;
for (i=0;i<n;i++){
k=n-Z[i];
for (j=0;j<n;j++){
if(k==j){
for(a=0;a<2*Z[i]-1;a++){
printf("%d",Z[i]);
}
}
else if((j==n-1)&&(Z[i]==0)){
printf("|");
}
else{
printf(" ");
}
}
printf("\n");
}for(i=0;i<2*n-1;i++){
printf("-");
}
printf("\n\n");
return 0;
}
void function2(int A[],int B[],int C[],int n){
printf("A\n");
function1(A,n);
printf("B\n");
function1(B,n);
printf("C\n");
function1(C,n);
}
void AtoB(int A[],int B[],int C[],int n,int *h1,int *h2,int *h3){
if (B[n-1]==0){
printf("A->B\n");
B[n-1]=A[*h1];
A[*h1]=0;
*h1=*h1+1;
function2(A,B,C,n);}
else if (A[n-1]==0){
printf("A->B\n");
A[0]=B[n-*h2];
B[n-*h2]=0;
*h2=*h2-1;
function2(A,B,C,n);
}
AtoC(A,B,C,n,&h1,&h2,&h3);
}
void AtoC(int A[], int B[], int C[], int n,int *h1,int *h2,int *h3 ){
}
The program must solve hanoi tower while showing the tower structure.
I am getting error: passing argument 5 of 'AtoC' from incompatible pointer type.Thanks in advance.
You can't do like that, In AtoB function you have declared h1 as a pointer and in AtoC function you are passing address of that pointer. Both functions should have same declaration like that *h1,*h2,*h3.

Selection sort on array in C

I'm trying to create a simple(?) selection sort program in C that selects the largest integer of an integer array and places it in the location a[n-1], places the second largest number in a[n-2], etc until the smallest number is placed in a[0]. I've run through the below code on paper and it seems like it should work, but when I compile it I'm getting faulty results. Am I missing something obvious?
/* The program implements selection sort*/
#include <stdio.h>
#include "simpio.h"
#define n 5
void GetArray(int a[]);
void SelectionSort(int a[]);
int FindMax(int a[], int high);
void swap(int a[], int p1, int p2);
void PrintArray(int a[]);
main()
{
int a[n];
GetArray(a);
SelectionSort(a);
PrintArray(a);
getchar();
}
void GetArray(int a[])
{
int i;
for(i=0;i<n;i++)
{
printf("Enter integer# %d", i+1);
a[i]=GetInteger();
}
}
void SelectionSort(int a[])
{
int i, max;
for(i=0;i<n;i++)
{
max=FindMax(a,i);
swap(a,max,(n-1-i));
}
}
int FindMax(int a[], int high)
{
int i, index;
index=high;
for(i=high;i<n;i++)
{
if(a[i]>a[index])
index=i;
}
return index;
}
void swap(int a[], int p1, int p2)
{
int temp;
temp=a[p2];
a[p2]=a[p1];
a[p1]=temp;
}
void PrintArray(int a[])
{
int i;
for(i=0;i<n;i++)
printf("a[%d]=%d\n", i, a[i]);
}
Change these method to:
void SelectionSort(int a[])
{
int i, max;
for(i=0;i<n;i++)
{
max=FindMax(a,n-i-1);
swap(a,max,n-i-1);
}
}
int FindMax(int a[], int high)
{
int i, index;
index=high;
for(i=0;i<high;i++)
{
if(a[i]>a[index])
index=i;
}
return index;
}
I actually tested my answer and it works.
Selection sort is process of comparing minimum element from the list and placing from the least index.
Now consider below code snippet.
public void selectionSort(int[] elements) {
for(int i=0;i<elements.length;i++) {
int minPosition = i;
for(int j=i+1;j<elements.length;j++) {
if(elements[minPosition]>elements[j])
minPosition = j;
}
int temp = elements[i];
elements[i] = elements[minPosition];
elements[minPosition] = temp;
}
}
Thanks for reading, let me know feedback to improve from myside
Shouldn't:
max=FindMax(a,i);
swap(a,max,(n-1-i));
Be:
max=FindMax(a,i);
swap(a,max,i);
otherwise, next time through the loop, you'll find the same max value in the top position in the array.
A very basic implementation of selection sort
#include<stdio.h>
main()
{
int i,j,n=7,a[]={1,2,5,3,8,9,5},key;
for(j=1;j<n;j++)
{
key=a[j]; //a[j] is the new element to be added to the sorted
//sequence
i=j-1;
while(i>=0 && key<a[i]) //traverse through the sorted sequence
{a[i+1]=a[i];i--;} //until the place of key is found
a[i+1]=key;
}
for (j=0;j<n;j++)
printf("%d",a[j]);
}
#include<stdio.h>
#include<conio.h>
int removex(int arr[],int small,int n)
{
int i=0;
for(;i<n;i++)
if(arr[i]==small) //searching that no to delete
break;
for(;i<n-1;i++)
arr[i]=arr[i+1]; //delete by overloading no
return n-1;
}
void selectSort(int arr[],int sort[],int n)
{
int j=0,k=0,small;
while(n!=0)
{
small=arr[0];
for(j=0;j<n;j++)
if(arr[j]<small)
small=arr[j]; //finding smallest no
sort[k++]=small;
n=removex(arr,small,n); //removing that from list as we included that no into sorted list
}
}
void main()
{
int arr[10],arr2[10],i,n;
clrscr();
printf("Enter how many elements");
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
selectSort(arr,arr2,n);
printf("sorted list is\n");
for(i=0;i<n;i++)
printf("%d\n",arr2[i]);
getch();
}

Print 2d array by calling a function print_array with argument the 2d array

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define length 100
void print_array();
int main()
{
int m,n,i,j;
int A[length][length];
printf("Give dimensions of array (up to 100x100):\ni:\n");
scanf("%d",&i);
printf("j:\n");
scanf("%d",&j);
srand(time(NULL));
for (m=0;m<i;m++)
{
for (n=0;n<j;n++)
{
A[m][n]=rand()%45+1;
printf("A[%d,%d]=%d\n",m,n,A[m][n]);
}
}
print_array(i,j,A);
return 0;
}
void print_array(int i,int j,int A[][j])
{
printf("\n");
int m,n;
for (m=0;m<i;m++)
{
for (n=0;n<j;n++)
{
printf("A[%d,%d]=%d\n",m,n,A[m][n]);
}
}
}
Hello. I am trying to print a 2d array by calling a function print but when I run the program I get:
For the first printf() the correct values:
A[0,0]=25
A[0,1]=19
A[0,2]=13
A[1,0]=4
A[1,1]=17
A[1,2]=43
A[2,0]=7
A[2,1]=37
A[2,2]=20
But when with the 2nd printf() within the function call of print_array I get:
A[0,0]=25
A[0,1]=19
A[0,2]=13
A[1,0]=0
A[1,1]=0
A[1,2]=0
A[2,0]=0
A[2,1]=0
A[2,2]=0
Seems like I miss something with pointers... Thanks.
This is C99, right?
The problem is that you're confusing the array size.
The main program has int A[length][length], but then you call the function with a dynamic size for the final dimension, A[][j]. If j != length, then the function will index the array incorrectly.
I would recommend representing the array in the function call as a bare pointer to the first element, and doing the indexing manually:
void print_array(const int *A, size_t width, size_t height)
{
for(size_t i = 0; i < height; ++i)
{
for(size_t j = 0; j < width; ++j)
printf("A[%zu][%zu] = %d\n", i, j, A[i * width + j]);
}
}
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <time.h>
#define length 100
void print_array(int i,int j,int A[length][length]);
int main()
{
int m,n,i,j;
int A[length][length];
printf("Give dimensions of array (up to 100x100):\ni:\n");
scanf("%d",&i);
printf("j:\n");
scanf("%d",&j);
srand(time(NULL));
for (m=0;m<i;m++)
{
for (n=0;n<j;n++)
{
A[m][n]=rand()%45+1;
printf("A[%d,%d]=%d\n",m,n,A[m][n]);
}
}
print_array(i,j,A);
return 0;
}
void print_array(int i,int j,int A[length][length])
{
printf("\n");
int m,n;
for (m=0;m<i;m++)
{
for (n=0;n<j;n++)
{
printf("A[%d,%d]=%d\n",m,n,A[m][n]);
}
}
}
Apart from the above, you need to do some input validation for the dimension (i and j). They should not exceed the length. If it exceeds the length then you will run into problems.
void print_array(int i,int j,int A[][j])
should be
void print_array(int i,int j,int A[][length])
When you tell the compiler that the actual array has different dimensions than what you earlier specified, it accesses the wrong elements. A is always 100x100, you just don't fill all of it.
You need to define the size of the array int A[][] in the function signature. Use this function instead :
void print_array(int i,int j,int A[length][length])
{
printf("\n");
int m,n;
for (m=0;m<i;m++)
{
for (n=0;n<j;n++)
{
printf("A[%d,%d]=%d\n",m,n,A[m][n]);
}
}
}
First you should forward declare your function with the correct prototype
void print_array(int n,int m, int A[n][m]);
And then use it with the correct dimensions
print_array(length, length, A);
Other than that:
use size_t for sizes instead of int
usual naming conventions have i and j as indices and n and m
as sizes. Sticking to such conventions makes code easier to read for others.
You've declared A as int A[length][length], But in the print_array function you've declare it as A[][j] which means in your function A have a different number of colones which leads to this error.
change this line
void print_array(int i,int j,int A[][j])
with this one
void print_array(int i,int j,int A[][length])
http://www.cplusplus.com/doc/tutorial/arrays/
array and matrix printing functions are little similar but tricky..
but i have worked on it and found out some thing and i think it will be useful for all
here is a simple C code for Function for printing matrix
#include<stdio.h>
void printmatrix(int l,int x,int y,int *p_arr);
void putvalue(int l,int x,int y,int *r_arr);
/* ******** Function to insert some random value in to a Matrix ************ */
void putvalue(int l,int x,int y,int *r_arr)
{
int i,j;
for(i=0;i<x;i++)
{
for(j=0;j<y;j++)
{
r_arr[(l*i)+j]=i+j+10;
}
}
}
/* *********Function to Print any Matrix ********** */
void printmatrix(int l,int x, int y,int *p_arr)
{
int i,j;
printf("\n");
for(i=0;i<x;i++)
{
printf("\n");
for(j=0;j<y;j++)
{
printf(" %d",p_arr[(l*i)+j]);
}
}
}
/* ****** Main ****** */
void main()
{
int i,j,l;
int Big_arr[80][100],Small_arr[20][40];
// ****** I have taken two different size arrays *****
int x,y;
clrscr();
printf("\n Enter x and y values under 20X40:"); // Because of screen size of Output
scanf("%d %d",&x,&y);
if((x!=0)&&(y!=0))
{
printf("\n %dX%d",x,y);
l=sizeof(Big_arr[0])/sizeof(Big_arr[0][0]); // **** l is the length of a single row of matrix
putvalue(l,x,y,(int*)Big_arr);
printf("\n Printing Big_arr");
printmatrix(l,x,y,(int*)Big_arr);
l=sizeof(Small_arr[0])/sizeof(Small_arr[0][0]);
putvalue(l,x,y,(int*)Small_arr);
printf("\n Printing Small_arr");
printmatrix(l,x,y,(int*)Small_arr);
}
else
printf("\n ***** Enter valied x and y values *****");
getch();
}
hope u like it.

Resources