I have a question

+3 votes
asked Nov 18 by Debes─Ślis Dangaus (150 points)
I have to make a program which: Enter an integer N. Enter N integers. Find N / 3 or more duplicate numbers between the entered numbers.

And I'm lost. I did something like this:

#include <stdio.h>

int main(int argc, char** argv)

{

int number;

   

    printf("Enter an integer: ");  

    

    scanf("%d", &number);

}

struct eleCount {

    int element;

    int count;

};

struct eleCount temp[];

Could you please help?

1 Answer

0 votes
answered 6 days ago by Peter Minarik (12,220 points)

So far so good. You asked the user to enter the number N and you already read it.

The next step is to repeat reading N numbers. For that you should use a loop (for, while or do-while). In this loop, you ask for the numbers one by one (just like you did for N -- number -- above).

When done, you should look for duplicates. I leave this for you for think about how to do it. (There could be many ways).

I've created the skeleton of the program. Please, give it a try and try to fill the missing parts.

#include <stdio.h>

int main()
{
    // Instructions to the user + asking for N
    printf("The program will read N number of integers then find duplicates in them.\n");
    printf("Please, enter N (how many numbers you'll want to check for duplicates): ");
    int count;
    scanf("%d", &count);
    
    int numbers[count]; // Now, that we know how many numbers we need, we can initialise the numbers array for the right count of elements
    
    // Reading the N numbers
    for (int i = 0; i < count; i++)
    {
        // TODO: Implement reading the numbers
        //       1. Ask the user to enter the "i"th number.
        //       2. Read the number and store it in the "numbers" array.
        // Note: you can access the "i"th element of the array "numbers" as: numbers[i]
    }
    
    // Searching for duplicates
    // TODO: Implement searching for duplicates
    //       1. Find a duplicate.
    //       2. Print it to the user.
    //       3. Go back to 1. or finish, when there are no numbers left.

    return 0;
}

If you get stuck, please come back and I (or others) will be more than happy to help.

Good luck! :)

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...