How to merge array elements with token merging ## operator??? - c

I want to merge 3, 7, 1 to get 371. I want to know how to merge it???
#include <stdio.h>
int main(void)
{
int a[] ={3, 7, 1};
return(0);
}

I think rather than "merge", you want to convert array into an integer value, just like join operation that we see in Java variants. You can easily achieve this by iterating over the array :
#include <stdio.h>
int joinArray(int a[], int N) {
int i, res = 0;
for(i = 0; i < N; i++)
res = res*10 + a[i];
return res;
}
int main() {
int a[] = {3, 7, 1};
printf("merged res : %d\n", joinArray(a, 3));
return 0;
}

You could try something like this;
#include <stdio.h>
int main(void)
{
int a[] ={3, 7, 1};
int i, result = 0;
for(i=0;i<sizeof(a)/sizeof(a[0]);i++)
result += a[i] * pow(10, sizeof(a)/sizeof(a[0])-i-1);
printf("result %d\n", result);
return(0);
}

Related

printing multidimensional array in c

I am trying to print an array with specific dimensions, but it prints out wrong entities when it's run.
//code
#include <stdio.h>
int my_array[2] [4] = {
{1, 2, 3, 4}, {5, 6, 7, 8}
};
void print_array(const int h, const int w, char array[][w]) {
int nRow = h;
int nColumn = w;
for(int i = 0; i < nRow; i++) {
printf("--- Row %d --- \n", i);
for(int j = 0; j < nColumn; j++) {
printf("Column [%d] = %d \n", j, array[i] [j]);
}
}
}
int main(int argc, char **argv)
{
const int array_width = 4;
const int array_height = 2;
print_array(array_height, array_width, my_array);
return 0;
}
After compiling it prints out the next result:
Change char array[][w] to int array[][w] in the function print_array, which expects integer array. The compiler would have issued incompatible type warning, but that's easy to miss! Try to compile the program with zero warnings.

Find max, min and sum of an array using function

I want to create a programm to find maximum, minimum and summation for a given array, but when I take my code to compiler, it returned expressed expression before 'int' and too few arguments to all the functions.
And here is my code, please tell me why it doesn't work.
#include <stdio.h>
#include <stdlib.h>
void print_array(int array[], int len) {
// print array on screen
int b;
printf("Array: ");
for (b = 0; b < len - 1; b++) {
printf("%d, ", array[b]);
}
if (b >= len - 1) {
printf("%d\n", array[b]);
}
}
// function
int min(int *x, int len) {
int mintemp = x[0], i;
for (i = 1;i < len;i++) {
if(x[i] < mintemp) {
mintemp = x[i];
}
}
return mintemp;
}
int max(int *y, int len) {
int maxtemp = y[0], j;
for (j = 1;j < len;j++) {
if(y[j] > maxtemp) {
maxtemp = y[j];
}
}
return maxtemp;
}
int sum(int *z, int len) {
int sumtemp = 0, k;
for (k = 0;k < len;k++) {
sumtemp = sumtemp + z[k];
}
return sumtemp;
}
int main() {
int array[] = {3, 9, 1, 2, 5, 8, 7, 6, 4, 10, 11};
int len = 11;
print_array(array, len);
// print other elements on screen
printf("Maximum: %d\n", max(int *x, int len));
printf("Minimum: %d\n", min(int *y, int len));
printf("Summation: %d\n", sum(int *z, int len));
return 0;
}
The problem was with calling the functions. Function call in C is like this
function_name(argument 1, argument 2, argument 3, ......, argument n);
There is no need to specifying the data type along the parameters in the call like you were doing in your code.
Also you were passing pointers x, y and z that do not point to your array.
I have corrected the code as shown below.
int main() {
int array[] = {3, 9, 1, 2, 5, 8, 7, 6, 4, 10, 11};
int len = 11;
print_array(array, len);
// print other elements on screen
printf("Maximum: %d\n", max(array, len)); // pass array and len
printf("Minimum: %d\n", min(array, len));
printf("Summation: %d\n", sum(array, len));
return 0;
}
If you want to pass the array to functions via pointers you can do it like this.
int main() {
int array[] = {3, 9, 1, 2, 5, 8, 7, 6, 4, 10, 11};
int len = 11;
int *x = array;
int *y = array;
int *z = array;
print_array(array, len);
// print other elements on screen
printf("Maximum: %d\n", max(x, len));
printf("Minimum: %d\n", min(y, len));
printf("Summation: %d\n", sum(z, len));
return 0;
}
Also there is no need to declare 3 different pointers and pass it to the functions, you can declare just one and reuse it with subsequent calls.
Hope this helps.!!

Completing my addarray () formula

I apologize for my vagueness in advance-this is my first post and I can really use some help.
The assignment is as follows:
/* Write a function named addarray() that returns the sum of the
elements of an array of int values. Your functions should take two
parameters, the array and the number of elements in the array. Make
your function work with the following program; */
/* arraysum.c
*
* Synopsis - displays the value returned by the function addarray()
* with 2 different sets of parameters.
*
* Objective - To provide a test program for the addarray() function.
* Your answers should be 55 and 0.
*
*/
#include <stdio.h>
int addarray(int [], int, int);
void main() {
int array1[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int array2[4] = {0, 0, 0, 0};
printf("The sum of array1 = %d\n", addarray(array1, 0, 10));
printf("The sum of array2 = %d\n", addarray(array2, 0, 4));
}
This is my solution aid:
int addarray(int s[], int i, int n) {
int sum = 0;
for (i = 0; i < n; i++) {
sum += s[i];
}
return sum;
}
I cant seem to figure out how to get the proper result. Any help would be appreciated.
This is what I have completed so far:
#include <stdio.h>
int addarray(int array1[], int num_elements);
void print_array(int array1[], int num_elements);
void main(void)
{
int array1[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int sum;
printf("\nArray:\n");
print_array(array1, 10);
sum = addarray(array1, 10);
printf("The sum is %d\n .", sum);
}
int addarray(int array1[], int num_elements)
{
int i, sum=0;
for (i=0; i<num_elements; i++)
{
sum = sum + array1[i];
}
return(sum);
}
void print_array(int array1[], int num_elements)
{
int i;
for(i=0; i<num_elements; i++)
{
printf("%d ", array1[i]);
}
printf("\n");
}
I cant figure out how to get a second array to be summed up.
Such as Array2.
int is a reserved word.you can't give a variable the name int.besides,the assignment says the function should take two parameters,not 3.check this :
#include <stdio.h>
int addarray(int arr[],int size);
void main() {
int array1[10] = {1,2,3,4,5,6,7,8,9,10};
int array2[4] = {0,0,0,0};
printf("The sum of array1 = %d\n", addarray(array1,10));
printf("The sum of array2 = %d\n", addarray(array2,4));
}
int addarray(int arr[],int size)
{
int sum = 0 , n ;
for( n = 0 ; n < size ; n++ )
{
sum += arr[n];
}
return sum;
}

Find the union of two sets of 10 digit numbers

I'm trying to find the union of two sets of 10 digit numbers, I'm passing along three int arrays: first, second, and comp (this will hold the union set).
So far, I've added first and second into one array. I was thinking about finding the matching indices in comp[] then filter through deleting them. I figure there's a much easier way. Can anyone give me a hint?
Basically I'm taking
first[] = [1,2,3,4,5,6,7,8,9,10];
second[] = [1,2,3,4,11,12,13,14,15,16];
and I want to return
comp[] = [5,6,7,8,9,10,11,12,13,14,15,16];
The numbers won't necessarily be in order.
int compound(int first[],int second[],int comp[]){
int i=0;
int indicies[20];
for(int j = 0; j<SIZE; j++){
comp[i]=first[j];
i++;
}
for(int k = 0; k<SIZE; k++){
comp[i]=second[k];
i++;
}
int z=0;
for(int l = 0; l<SIZE*2; l++){
for(int m = 0; m<SIZE*2; m++){
if(comp[l]==comp[m]){
indicies[z]=m;
z++;
}}}
return 0;
}
A first good step is (nearly) always sorting.
Sort both input-sets (unless you know they are already sorted).
Then iterate over both at once (two indices) and only add those elements to the output which fulfill your criteria (seems to be union minus intersection, thus only in one).
Bonus: The output-set will be sorted.
I suggest you start by writing a contains(int[], int) method like
#include <stdio.h>
#include <stdbool.h>
bool contains(int arr[], int val) {
int offset;
for (offset = 0; arr[offset] != '\0'; offset++) {
if (arr[offset] == val) return true;
}
return false;
}
Then your compound method could be implemented using it with something like
int compound(int first[],int second[],int comp[]){
int i=0;
int j;
for(j = 0; first[j] != '\0'; j++){
int val = first[j];
if (contains(second, val) && !contains(comp, val))
comp[i++] = val;
}
return i;
}
Finally, you can test it like
int main(int argc, char *args[]) {
int a[] = {1,2,3,'\0'};
int b[] = {2,3,4,'\0'};
int c[3];
int count = compound(a,b,c);
int i;
for (i = 0; i < count; i++) {
printf("%i\n", c[i]);
}
}
Output is
2
3
If the numeric range is small you could do this:
#include <stdio.h>
#define MAX 20 // small numeric range
#define sz(a) (sizeof(a)/sizeof(*(a)))
int xunion(int *a, int sa, int *b, int sb, int *c) {
int n[MAX] = {0};
for (int i=0; i<sa; i++) n[a[i]] = 1;
for (int i=0; i<sb; i++) n[b[i]] = 1;
int j=0;
for (int i=0; i<MAX; i++) if (n[i]) c[j++] = i;
return j;
}
void prn(int *a, int s) {
while (s-- > 0) printf("%d ", *a++);
putchar('\n');
}
int main() {
int a[] = {6, 3, 4, 7, 5};
int b[] = {2, 4, 5, 7, 6, 3};
int c[MAX];
prn(a, sz(a));
prn(b, sz(b));
int n = xunion(a, sz(a), b, sz(b), c);
prn(c, n);
return 0;
}

check if array contains all array values from another array in C

I tried to make a program that will check if an an array contains all the array values from another array. So, if that's true, the program will return 1 using a value for it. If it isn't true, it will return 0(I named it p). I failed to make that program though. Could you please help me?
#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 the above inner loop was not broken at all then
arr2[i] is not present in arr1[] */
if (j == m)
return 0;
}
/* If we reach here then all elements of arr2[]
are present in arr1[] */
return 1;
}
int main()
{
int arr1[] = {11, 1, 13, 21, 3, 7};
int arr2[] = {11, 2, 7, 1};
int m = sizeof(arr1)/sizeof(arr1[0]);
int n = sizeof(arr2)/sizeof(arr2[0]);
if(isSubset(arr1, arr2, m, n))
printf("arr2[] is subset of arr1[] ");
else
printf("arr2[] is not a subset of arr1[]");
getchar();
return 0;
}
Ideone Link up and running : http://ideone.com/4u9oQm

Resources