Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Comparing arrays and printing three matching values (in C) I have an array with

ID: 3718657 • Letter: C

Question

Comparing arrays and printing three matching values (in C)

I have an array with ten values that are scanned in, say int input[10], and these five arrays:

int arr1[4] = {36, 37, 38, 39};
int arr2[3] = {19, 29, 49};
int arr3[3] = {18, 28, 48};
int arr4[3] = {17, 27, 47};
int arr5[3] = {16, 26, 46};

How would you make a loop so that these arrays are checked for values matching values in the input array? I want the arrays to be checked in order, for example ALL of arr1 is checked for values, then all of arr2, then arr3 and so on. Each time a value matches, it is printed, BUT the loop stops when three values have been found and printed

Explanation / Answer

#include<stdio.h>

#include<stdlib.h>

int main()

{

    int input[10];

   

    int arr1[4] = {36, 37, 38, 39};

    int arr2[3] = {19, 29, 49};

    int arr3[3] = {18, 28, 48};

    int arr4[3] = {17, 27, 47};

    int arr5[3] = {16, 26, 46};

   

    int i, j;

   

    printf( "Enter 10 numbers : " );

   

    // get user input

    for( i = 0 ; i< 10 ; i++ )

        scanf("%d", &input[i]);

       

    // store how many values have matched

    int count = 0;

   

    // check for values of arr1

    for( i = 0 ; i < 4 ; i++ )

    {

        for( j = 0 ; j < 10 ; j++ )

        {

            if( arr1[i] == input[j] )

            {

                count++;

               

                printf("%d ", arr1[i]);

                

                // if 3 numbers have matched

                if( count == 3 )

                    // exit the program

                    exit(0);

                   

                break;

            }

        }

    }

   

    // check for values of arr2

    for( i = 0 ; i < 3 ; i++ )

    {

        for( j = 0 ; j < 10 ; j++ )

        {

            if( arr2[i] == input[j] )

            {

                count++;

               

                printf("%d ", arr2[i]);

               

                // if 3 numbers have matched

                if( count == 3 )

                    // exit the program

                    exit(0);

                   

                break;

            }

        }

    }

   

    // check for values of arr3

    for( i = 0 ; i < 3 ; i++ )

    {

        for( j = 0 ; j < 10 ; j++ )

        {

            if( arr3[i] == input[j] )

            {

                count++;

               

                printf("%d ", arr3[i]);

               

                // if 3 numbers have matched

                if( count == 3 )

                    // exit the program

                    exit(0);

                   

                break;

            }

        }

    }

   

    // check for values of arr4

    for( i = 0 ; i < 3 ; i++ )

    {

        for( j = 0 ; j < 10 ; j++ )

        {

            if( arr4[i] == input[j] )

            {

                count++;

               

                printf("%d ", arr4[i]);

               

                // if 3 numbers have matched

                if( count == 3 )

                    // exit the program

                    exit(0);

                   

                break;

            }

        }

    }

   

    return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote