0

So far I have the program does what it needs to do. My issue now is that I have two arrays and when I print them I get 0s for empty elements. I want the empty elements to print nothing.

Example:

Array 1: 1 1

Array 2: 3 3 3 3

Output:

1 3 1 3 0 3 0 3

My goal is:

1 3 1 3 3 3

which is to remove the 0s if I didnt input 0 in array

My code:

#include <errno.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

int main(void) {
    char * line = NULL;
    size_t len = 0;

    char * line2 = NULL;
    size_t len2 = 0;


    char ch;
    int counter = 0;

    char ch2;
    int counter2 = 0;

    int mSize;
    int mergedArray[20];

    int i = 0; // for loop
    int j = 0;

    int * myPtr;
    myPtr = (int * ) malloc(counter * sizeof(int));

    int * myPtr2;
    myPtr2 = (int * ) malloc(counter2 * sizeof(int));

    while (getline( & line, & len, stdin) != EOF) {
        //===============
        //value 1 for line 1
        ch = * line;

        printf("line 1: Test: %s\n", line);

        char * start = line;
        char * eon;
        long value;
        //===============
        //value 2 line 2
        getline( & line2, & len2, stdin);
        ch2 = * line2;
        printf("line 2: Test: %s\n", line2);

        char * start2 = line2;
        char * eon2;
        long value2;

        //==============
        errno = 0;

        //============loop for line 1 =================

        printf("=============\n");
        printf("Line 1\n");
        while ((value = strtol(start, & eon, 0)),
            eon != start &&
            !((errno == EINVAL && value == 0) ||
                (errno == ERANGE && (value == LONG_MIN || value == LONG_MAX))))

        {

            //getting the size of the line 
            counter++;

            start = eon;
            errno = 0;

            myPtr[counter] = value;
            // printf("Array #1 [%d] %d\n",counter , myPtr[counter]);

        } //end of while
        printf("Size: %d\n", counter);
        printf("=============\n");

        //============loop for line 2 =================
        printf("Line 2\n");
        while ((value2 = strtol(start2, & eon2, 0)),
            eon2 != start2 &&
            !((errno == EINVAL && value2 == 0) ||
                (errno == ERANGE && (value2 == LONG_MIN || value2 == LONG_MAX))))

        {
            //getting the size of the line 
            counter2++;

            start2 = eon2;
            errno = 0;


            myPtr2[counter2] = value2;
            //printf("Array #2 [%d] %d\n",counter2 , myPtr2[counter2]);

        } //end of while
        printf("Size: %d\n", counter2);
        printf("=============\n");

    for (i = 0; i < counter; i++) {
        mergedArray[i] = myPtr[i + 1]; // not used
    }

    mSize = counter + counter2;
   
    for (i = 0, j = counter; j < mSize && i < counter2; i++, j++) {
        mergedArray[j] = myPtr2[i + 1]; // not used
        //here I print out both arrays 
        printf("%d %d ", myPtr[i + 1], myPtr2[i + 1]);
    }
    } // end of main while



    return 0;
}

My output

Laith
  • 81
  • 5

1 Answers1

1

Your program is extremelly complicated. It is a good practice to separate login into functions. In the solution below you need to provide large enouth array for the destination array and both arrays to be merged.

#define MAX(a,b) ((a) > (b) ? (a) : (b))
#define SA(a) (sizeof(a)/sizeof((a)[0]))

size_t mergeArrays(int *dest, const int *src1, const int *src2, size_t size_1, size_t size_2)
{
    size_t pos = 0;
    for(size_t index = 0; index < MAX(size_1, size_2); index++)
    {
        if(index < size_1) dest[pos++] = src1[index];
        if(index < size_2) dest[pos++] = src2[index];
    }
    return pos;
}


int main(void)
{
    int arr1[] = {1,1};
    int arr2[] = {3,3,3,3,3};
    int dest[SA(arr1) + SA(arr2)];  
  
    size_t destsize = mergeArrays(dest, arr1, arr2, SA(arr1), SA(arr2));

    for(size_t index = 0; index < destsize; index++)
    {
        printf("%d ", dest[index]);
    }
    printf("\n");
}

https://godbolt.org/z/WG4v6T

Here you have the version reading from user and using dynamic memory allocation:

#define MAX(a,b) ((a) > (b) ? (a) : (b))
#define SA(a) (sizeof(a)/sizeof((a)[0]))

size_t mergeArrays(int *dest, const int *src1, const int *src2, size_t size_1, size_t size_2)
{
    size_t pos = 0;
    for(size_t index = 0; index < MAX(size_1, size_2); index++)
    {
        if(index < size_1) dest[pos++] = src1[index];
        if(index < size_2) dest[pos++] = src2[index];
    }
    return pos;
}

int *readArray(size_t *size)
{
    int *arr = NULL;
    
    printf("Enter size:");
    if(scanf(" %zu", size) != 1) goto func_return;
    arr = malloc(*size * sizeof(*arr));
    if(!arr) { free(arr); arr = NULL; goto func_return;}
    for(size_t index = 0; index < *size; index++)
    {
        if(scanf(" %d", &arr[index]) != 1) {free(arr); arr = NULL; goto func_return;}
    }

    func_return:
    return arr;
}


int main(void)
{
    size_t size1, size2;
    int *arr1 = readArray(&size1);
    int *arr2 = readArray(&size2);
    int *dest = NULL;

    if(arr1 && arr2) dest = malloc(size1 + size2);

    if(dest)
    {
        size_t destsize = mergeArrays(dest, arr1, arr2, size1, size2);

        for(size_t index = 0; index < destsize; index++)
        {
            printf("%d ", dest[index]);
        }
    }
    printf("\n");
    free(arr1);
    free(arr2);
    free(dest);
}

https://godbolt.org/z/5sMbYj

0___________
  • 60,014
  • 4
  • 34
  • 74
  • but how can I fill the array with user input? The reason I am using getline is to be able to make the array dynamic – Laith Oct 03 '20 at 23:52
  • Filling the data is unrelated to your question. The merging function is the same – 0___________ Oct 03 '20 at 23:54
  • 1
    @Leo second exmple uses dynamic memory allocation and user input. See how I use functions for repeating (or logically separated) tasks. – 0___________ Oct 04 '20 at 00:11