Drat018 Drat018 - 1 year ago 41
C Question

printf a selected numbers in an array

in my program the user enters an array and the pc generates a random array (with the max number picked from user) and then the program creates a third array that shows the number from the first array that are not in the second . however i only want to type the numbers once each but the program types them again and again until array is full .

for example if i entered 0 3 1 and generated number was 1 5 9 it would print 0 3 3

i want to print 0 3 only .
full code :

#include <stdio.h>
#define N 7
void input(int ar1[N]);
void max(int* mx);
int input2(int ar2[N], int mx);
int input3(int ar3[N], int ar1[N], int ar2[N]);

int main()
{
int ar1[N], mx, ar2[N], ar3[N], i;
input(ar1);
max(&mx);
input2(ar2, mx);
input3(ar3, ar1, ar2);
printf("array1 = %d %d %d %d %d %d %d\n", ar1[0], ar1[1], ar1[2], ar1[3], ar1[4], ar1[5], ar1[6]);
printf("array2 = %d %d %d %d %d %d %d\n", ar2[0], ar2[1], ar2[2], ar2[3], ar2[4], ar2[5], ar2[6]);
printf("array3 = %d %d %d %d %d %d %d\n", ar3[0], ar3[1], ar3[2], ar3[3], ar3[4], ar3[5], ar3[6]);
}

void input(int ar1[N])
{
int i;
printf("Enter 7 numbers: ");
for (i = 0; i < N; i++)
scanf("%d", &ar1[i]);
}

void max(int* mx)
{
printf("Enter random number bigger than 0 :- ");
scanf("%d", mx);
if (mx < 0)
{
printf("you have entered a number smaller than 0 please enter a number bigger than 0 :-");
scanf("%d", mx);
}
return mx;
}

int input2(int ar2[N], int mx)
{
int i;
srand(time(0));
for (i = 0; i < N; i++)
{
ar2[i] = 0 + rand() % (mx - 0 + 1);
printf("%d ", ar2[i]);
}
return ar2;
}

int input3(int ar3[N], int ar1[N], int ar2[N])
{
int i, j, a = 0;
for (i = 0; i < N; i++)
{
if (ar1[i] != ar2[0] && ar1[i] != ar2[1] && ar1[i] != ar2[2] && ar1[i] != ar2[3] && ar1[i] != ar2[4] && ar1[i] != ar2[5] && ar1[i] != ar2[6])
a = ar1[i];
ar3[i] = a;
}
return ar3;
}


Thanks for your help.

Answer Source

IIUC your problem can be reduced to removing duplicates from the array. I tried to fix it and hope that it works for you:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <assert.h>

#define N 7

void input(int ar1[N]);

int max(int * mx);

int input2(int ar2[N], int mx);
static int compact(int *array, int size)
{
    int i;
    int last = 0;
    assert(size >= 0);
    if (size <= 0)
        return size;
    for (i = 1; i < size; i++)
    {
        if (array[i] != array[last])
            array[++last] = array[i];
    }
    return(last + 1);
}
int i, a = 0;
int j = 0;
static void print(int *array, int size, const char *tag, const char *name)
{
    int k;
    printf("%s\n", tag);
    for (k = 0; k < i-j; k++)
        printf("%s[%d] = %d\n", name, k, array[k]);
}

int * input3(int ar3[N], int ar1[N], int ar2[N]);

int main() {
    int ar1[N], mx, ar2[N], ar3[N], i;
    input(ar1);
    max(&mx);
    input2(ar2, mx);
    input3(ar3, ar1, ar2);
    int a_size = sizeof(ar3) / sizeof(ar3[0]);
    a_size = compact(ar3, a_size);
    print(ar3, a_size, "\nAnswer:", "a");
    printf("array1 = %d %d %d %d %d %d %d\n", ar1[0], ar1[1], ar1[2], ar1[3], ar1[4], ar1[5], ar1[6]);
    printf("array2 = %d %d %d %d %d %d %d\n", ar2[0], ar2[1], ar2[2], ar2[3], ar2[4], ar2[5], ar2[6]);
}

void input(int ar1[N]) {
    int i;
    printf("Enter %d numbers: ", N);
    for (i = 0; i < N; i++)
        scanf("%d", &ar1[i]);
}

int max(int *mx) {
    printf("Enter random number bigger than 0 :- ");
    scanf("%d", mx);
    if (mx < 0) {
        printf("you have entered a number smaller than 0 please enter a number bigger than 0 :-");
        scanf("%d", mx);
    }
    return * mx;
}

int input2(int ar2[N], int mx) {
    int i;
    srand(time(0));
    for (i = 0; i < N; i++) {
        ar2[i] = 0 + rand() % (mx - 0 + 1);
        printf("%d ", ar2[i]);
    }
    return * ar2;
}

int * input3(int ar3[N], int ar1[N], int ar2[N]) {

    for (i = 0; i < N; i++) {
        if (ar1[i] != ar2[0] && ar1[i] != ar2[1] && ar1[i] != ar2[2] && ar1[i] != ar2[3] && ar1[i] != ar2[4] &&
            ar1[i] != ar2[5] && ar1[i] != ar2[6]) {
            //a = ar1[i];
            ar3[i-j] = ar1[i];
        }
        else {
            ++j;
            continue;
        }

    }
    return ar3;
}

Test

Enter 7 numbers: 2 4 6 8 9 11 12
Enter random number bigger than 0 :- 12
3 12 1 6 2 8 9 
Answer:
a[0] = 4
a[1] = 11
array1 = 2 4 6 8 9 11 12
array2 = 3 12 1 6 2 8 9
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download