Program that determines whether an array is a subset of another array - c

Hello I'm trying to write a program about subsets and my problem is that whenever I try to run the program, it's automatically saying that "Y is a not subset of X" even though it is a subset. Can someone help me with this? This is my code:
#include<stdio.h>
int main()
{
int i, j, size1, size2, flag;
printf("S U B S E T S\n\n");
printf("Enter number of digits for Array X: ");
scanf("%d", &size1);
int array1[size1];
printf("Enter number of digits for Array Y: ");
scanf("%d", &size2);
int array2[size2];
printf("\n");
printf("Enter %d digits for Array X: ", size1);
for(i = 0; i < size1; i++)
{
scanf("%d", &array1[i]);
}
printf("\n");
printf("Enter %d digits for Array Y: ", size2);
for(i = 0; i < size2; i++)
{
scanf("%d", &array2[i]);
}
printf("\n\n");
for(i = 0; i < size2; i++)
for(j = 0; j < size1; j++)
if(array2[i] == array1[j])
flag++;
if(flag == size1)
{
printf("Array X is a subset of Array Y");
}
else
{
printf("Array X is not a subset of Array Y");
}
return 0;
}

It is working with very minor changes. Just loop from 0 and use < instead of <=.
#include<stdio.h>
int isSubset(int arr1[], int arr2[], int m, int n) {
int i = 0;
int j = 0;
for (i = 0; i < n; i++) {
for (j = 0; j < m; j++) {
if (arr2[i] == arr1[j])
break;
}
if (j == m)
return 0;
}
return 1;
}
int main() {
int arr1[20];
int arr2[20];
int m, n;
printf("How many elements of X you want to store?\n");
scanf("%d", &m);
printf("How many elements of Y you want to store?\n");
scanf("%d", &n);
for (int i = 0; i < m; i++) {
printf("\nEnter %d X values to be stored:\n", m);
scanf("%d", &arr1[i]);
}
for (int j = 0; j < n; j++) {
printf("\nEnter %d Y values to be stored:\n", n);
scanf("%d", &arr2[j]);
}
m = sizeof(arr1) / sizeof(arr1[0]);
n = sizeof(arr2) / sizeof(arr2[0]);
if (isSubset(arr1, arr2, m, n))
printf("Y is a subset of X \n");
else
printf("Y is not a subset of X\n");
getchar();
return 0;
}

Related

How can I move all elements in an array with a K number and make the numbers rotate when it comes to the end?

#include <stdio.h>
#include <stdlib.h>
int main()
{
int N, K, i;
printf("Enter size of array: ");
scanf("%d", &N);
printf("Please enter value of K: ");
scanf("%d", &K);
int arr[N];
for (i = 0; i < N; i++)
{
scanf("%d", &arr[i]);
}
for (i = 0; i < N; i++)
{
int temp = arr[i];
arr[i + K] = arr[i];
arr[i] = temp;
}
for (i = 0; i < N; i++)
{
printf("%d", arr[i]);
}
return 0;
}
I know the current code is totally wrong It was just another test.
Basically what I need to do is something like this:
the array before: arr[N]={1,2,3,4,5,6,7,8,9,10}
the array after if K is 2: arr[N]={10,9,1,2,3,4,5,6,7,8}
Like #whozcraig pointed out for in-place rotation of array members.
Define a function to reverse(in-place) array members in a range :
static inline void
arr_reverse (const int start, const int end, int arr[]) {
for (int ai = start, zi = end; ai < zi; ++ai, --zi) {
int tmp = arr[ai];
arr[ai] = arr[zi];
arr[zi] = tmp;
}
}
Then you call it like :
K %= N;
if (K != 0) {
arr_reverse (0, N-1, arr);
arr_reverse (0, K-1, arr);
arr_reverse (K, N-1, arr);
}
I write something like this but it creates new array instead of modifying current one but it works.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int N, K, i;
printf("Enter size of array: ");
scanf("%d", &N);
printf("Please enter value of K: ");
scanf("%d", &K);
int arr[N], newArr[N];
for (i = 0; i < N; i++)
{
scanf("%d", &arr[i]);
}
for (i = 0; i < K; i++)
{
newArr[i] = arr[N-1-i];
}
for (int x = 0; i < N; i++)
{
newArr[i] = arr[x];
x++;
}
for (i = 0; i < N; i++)
{
printf("%d ", newArr[i]);
}
return 0;
}
Thank you guys, for all the comments and answers. I've tried them and they worked.
I have found a way to do it as well. It's bit different. I did it with a function which moves all the elements with 1 position and then repeat the func as much as needed (K times).
#Cheatah helped me come up with it. I will post it in case somebody likes this solution in the future.
Here it is:
#include <stdio.h>
#include <stdlib.h>
int moveOnePos(int num, int array[num])
{
int temp = array[num - 1];
for (int b = num - 1; b > 0; b--)
{
array[b] = array[b - 1];
}
array[0] = temp;
return 0;
}
int main()
{
int N, K, i;
printf("Please enter size of array: ");
scanf("%d", &N);
printf("Please enter K: ");
scanf("%d", &K);
int arr[N];
for (i = 0; i < N; i++)
{
scanf("%d", &arr[i]);
}
for (i = 0; i < K; i++)
{
moveOnePos(N, arr);
}
for (i = 0; i < N; i++)
{
printf("%d", arr[i]);
}
return 0;
}

Unexpected output while printing 2 array (matrix)

output is printing only few elements of matrix that to in a wrong order
i think something is wrong with the printing loop but unable to figure it out
#include <stdio.h>
int main()
{
int x=0, y=0;
int a[x][y];
printf("enter the number of rows in first matrix:\n");
scanf("%d", &x);
printf("enter the number of columns in first matrix:\n");
scanf("%d", &y);
printf("enter elements of first matrix\n");
for (int i = 0; i < x; i++)
{
for (int j = 0; j < y; j++)
{
printf("enter element %d %d\n", i, j);
scanf("%d", &a[i][j]);
}
}
for (int p = 0; p < x; p++)
{
for (int q = 0; q < y; q++)
{
printf("%d\t", a[p][q]);
}
printf("\n");
}
return 0;
}
You're declaring the matrix before receiving the size of the matrix (you're variables x and y). In your code, the matrix is declared as a[0][0].
Solution:
#include <stdio.h>
int main() {
int x, y;
printf("enter the number of rows in first matrix:\n");
scanf("%d", &x);
printf("enter the number of columns in first matrix:\n");
scanf("%d", &y);
int a[x][y];
printf("enter elements of first matrix\n");
for (int i = 0; i < x; i++) {
for (int j = 0; j < y; j++) {
printf("enter element %d %d\n", i, j);
scanf("%d", &a[i][j]);
}
}
for (int p = 0; p < x; p++) {
for (int q = 0; q < y; q++) {
printf("%d\t", a[p][q]);
}
printf("\n");
}
return 0;
}
You're initializing your 2d array with 0 rows and 0 columns.
You need to move int a[x][y] down under scanf("%d", &y);
#include <stdio.h>
int main() {
int x, y;
printf("enter the number of rows in first matrix:\n");
scanf("%d", &x);
printf("enter the number of columns in first matrix:\n");
scanf("%d", &y);
int a[x][y];
printf("enter elements of first matrix\n");
for (int i = 0; i < x; i++) {
for (int j = 0; j < y; j++) {
printf("enter element %d %d\n", i, j);
scanf("%d", &a[i][j]);
}
}
for (int p = 0; p < x; p++) {
for (int q = 0; q < y; q++) {
printf("%d\t", a[p][q]);
}
printf("\n");
}
return 0;
}

I need help please

This program gives me zero primes in the array after running it. This is a program that takes 2d array [n x m] then calculate how many prime numbers are there in the 2d array.
int isprime(int n)
{
int k;
if (n <= 1)
return 0;
for (k = 2; k <= (n / 2); k++){
if (n % k == 0)
return 0;
}
return 1;
}
}
int primecount(int x, int y, int a[x][y]){
int r, c, count = 0;
for(r = 0; r < x; r++) {
for(c = 0; c < y; c++) {
if(isprime(a[r][c]))
{
count++;
}
}
}
return count;
}
int main()
{
int n, m, i, j, z;
printf("Enter the Number of Rows: ");
scanf("%d", &n);
printf("\n");
printf("Enter the Number of Columns: ");
scanf("%d", &m);
printf("\n");
int a[n][m];
printf("Enter the elements of the array: \n");
for(i = 0; i < n; i++)
{
for(j = 0; j < m; j++)
scanf("%d", &a[i][j]);
}
z = primecount(n, m, a[n][m]);
printf("\n");
printf("The Number of Prime Numbers in the array is: %d", z);
printf("\n");
return 0;
}
For starters instead of this call
z = primecount(n, m, a[n][m]);
you need to write
z = primecount(n, m, a);
In this call of the function isprime as in the call shown above
if(isprime(r, c, a[r][c]))
the expression a[r][c] is a scalar object of the type int. However the function isprime expects a two-dimensional array instead of a scalar object of the type int.
int isprime(int p, int q, int a[p][q])
^^^^^^^^^^^
Just declare the function like
int isprime( int x );
and correspondingly change its definition.
The function will be called like
if( isprime( a[r][c] ) )
Pay attention to that the logic of the function isprime is incorrect. It returns logical true for values equal to 1 and 0 though such values are not prime numbers.
Also you need to deal with an array with elements of the type unsigned int. Otherwise the user can enter negative values.
Here is your updated program.
#include <stdio.h>
int isprime(int n)
{
int k;
if (n <= 1)
return 0;
for (k = 2; k <= (n / 2); k++)
{
if (n % k == 0)
return 0;
}
return 1;
}
int primecount(int x, int y, int a[x][y]) //function to count prime numbers
{
int r, c, count = 0;
for(r = 0; r < x; r++)
{
for(c = 0; c < y; c++)
{
if(isprime(a[r][c]))
{
count++;
}
}
}
return count;
}
int main()
{
int n, m, i, j, z;
printf("Enter the Number of Rows: ");
scanf("%d", &n);
printf("\n");
printf("Enter the Number of Columns: ");
scanf("%d", &m);
printf("\n");
int a[n][m];
printf("Enter the elements of the array: \n");
for(i = 0; i < n; i++)
{
for(j = 0; j < m; j++)
scanf("%d", &a[i][j]);
}
z = primecount(n, m, a);
printf("\n");
printf("The Number of Prime Numbers in the array is: %d", z);
printf("\n");
return 0;
}
Its output might look like
Enter the Number of Rows: 2
Enter the Number of Columns: 2
Enter the elements of the array: 1 2 3 4
The Number of Prime Numbers in the array is: 2

Add or substract negative numbers in C

So i've been trying to do a code that adds the integers of a row in an array (this integers can be negative for example)
So you will have something like this:
input:
3 2 (Number of rows and columns)
12 7
-3 6
-2 -5
output:
19
3
-7
What I've done is this:
#include <stdio.h>
int main ()
{
static int array[10][10];
int i, j, m, n, sum = 0;
printf("Enter the order of the matrix\n");
scanf("%d %d", &m, &n);
printf("Enter the co-efficients of the matrix\n");
for (i = 0; i < m; ++i)
{
for (j = 0; j < n; ++j)
{
scanf("%d", &array[i][j]);
}
}
for (i = 0; i < m; ++i)
{
for (j = 0; j < n; ++j)
{
sum = sum + array[i][j] ;
}
printf("%d\n",sum);
sum = 0;
}
}
I can't find a way to "add" negative numbers, what should I change/add to this code? Thanks again :)
#include <stdio.h>
int main (){
int array[10][10];
int i, j, m, n, sum = 0;
printf("Enter the order of the matrix\n");
scanf("%d %d", &m, &n);
printf("Enter the co-efficients of the matrix\n");
for (i = 0; i < m; ++i){
for (j = 0; j < n; ++j){
scanf("%d", &array[i][j]);
}
}
for (i = 0; i < m; ++i){
for (j = 0; j < n; ++j){
sum = sum + array[i][j] ;
}
printf("%d\n",sum);
sum = 0;
}
return 0;
}

How to delete duplicated values in array in C?

I want to delete duplicates values in array. For example: array[1,5,6,1,3,5,9] I want to have array[6,3,9].
I have written this, but I am having troubles:
#include<stdio.h>
main() {
int array[50], i, j, k=0, c=0, array2[50], n;
printf("Enter array dimension: "); scanf("%d", &n);
for (i = 0; i < n; i++) {
printf("array[%d]= ", i); scanf("%d", &array[i]);
}
for (i = 0; i < n; ) {
for (j = i + 1; j < n; j++) {
if (array[i] == array[j])
i++;
else {
array2[k++] = array[i];
c++;
}
}
}
for (k = 0; k < c; k++) {
printf("%d ", array2[k]);
}
system("pause");
}
You should start by describing your problem in pseudo code, and breaking it into smaller pieces.
Start from scratch by deleting all these redundant variables, and try to implement the following algorithm:
for each element in inputArray
if not elementIsDuplicate(element)
add to outputArray
That's a single for loop. The elementIsDuplicate is separate function.
Now try to implement the function elementIsDuplicate. This function also contains a single loop, takes input parameters (int* array, int n, int currentIdx) and returns 1 or 0 indicating whether the element at currentIdx occurs anywhere else in the array.
#include<stdio.h>
int main(){
int array[50], i, j, k=0, c, n, array2[50] = {0};
printf("Enter array dimension: "); scanf("%d", &n);
for (i = 0; i < n; ++i){
int num, dup = 0;
printf("array[%d]= ", i); scanf("%d", &num);
for(j = 0; j < k; ++j){
if(array[j] == num){
array2[j] = dup = 1;
break;
}
}
if(!dup){
array[k++] = num;
}
}
for (c=i=0; i < k; ++i){
if(!array2[i])
printf("%d ", array[c++] = array[i]);
}
printf("\n");
/*
for(i=0;i<c;++i)
printf("%d ", array[i]);
printf("\n");
*/
system("pause");
return 0;
}
#include<stdio.h>
main()
{
int n, a[50], b[50], count = 0, c, d;
printf("Enter number of elements in array\n");
scanf("%d",&n);
printf("Enter %d integers\n", n);
for(c=0;c<n;c++)
scanf("%d",&a[c]); //enter array elements
for(c=0;c<n;c++)
{
for(d=0;d<count;d++)
{
if(a[c]==b[d])
break;
}
if(d==count)
{
b[count] = a[c];
count++;
}
}
printf("count is: %d\n",count);
printf("Array obtained after removing duplicate elements\n");
for(c=0;c<count;c++)
printf("%d\n",b[c]);
return 0;
}
This will remove multiple duplicates from the desired array..
Example: if the input array is: 3 6 5 6 2 8 6 5 9 8 6 ,,then the output array will be: 3 6 5 2 8 9

Resources