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;
}