ivyraveneve
ivyraveneve

Reputation: 43

How to incorporate an input array in this program

This is a code that has to take an input array from the user and input the same after removing the duplicates. However, I am unsure on how to incorporate an input array in this, and right now it has the elements hardcoded. This is my first week of programming so I apologize if this is a silly question. This is the code:

#include <stdio.h>
#include <stdbool.h>

#define nelems 8
int main()
{
    int l[nelems] = {1,2,3,1,4,4,5,6};    

    for(int m=0;m<nelems;m++)
    {
        bool wase = 0;
        for(int n=0;n<nelems && m>n;n++)          
        {
            if (l[m] == l[n] && m != n)
                wase = 1;
        }
        if (wase == 0){
            printf("%d\n", l[m]);
        }
    }

    return 0;
}

Upvotes: 1

Views: 110

Answers (3)

Twinkle
Twinkle

Reputation: 524

This is what you need.

#include <stdio.h>
#include <stdbool.h>

#define nelems 8
int main()
{
    int i;
    int l[nelems] ;
    for(i=0;i<nelems;i++)
    {
       printf("enter %d number :",i);
       scanf("%d",&l[i]);
    }

    for(int m=0;m<nelems;m++)
    {
        bool wase = 0;
        for(int n=0;n<nelems && m>n;n++)          
        {
            if (l[m] == l[n] && m != n)
                wase = 1;
        }
        if (wase == 0){
            printf("%d\n", l[m]);
        }
    }

    return 0;
}

Upvotes: 0

Frazzo
Frazzo

Reputation: 371

If you like int-type array, you can just declare another one:

int input[nelems];

and follow the user968000 advice, remembering that when you are typing the sequence in your console you have to put a white space between each number. To avoid that, I'd rather use char-type arrays, declared as follows:

char l[nelems] = {'1', '2', '3' /*etc.*/};
char input[nelems];

Then you make a for loop, as user968000 suggested:

int i;
for(i=0;i<nelems;i++)
  scanf("%c", &input[i]);

In this case you won't need the white spaces between the digits. Notice the '&' character in the scanf function: just put it as I showed, you'll surely learn what it is in next lessons.

So you have an input array and you can handle it as you want.

Upvotes: 0

user968000
user968000

Reputation: 1843

Try using a for loop and scanf.

int i;
for(i=0;i<nelems;i++){
    scanf("%d",&l[i]);
}

Upvotes: 1

Related Questions