# Integer Arrays

Page 1 of 1

## 3 Replies - 851 Views - Last Post: 14 May 2010 - 04:12 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=173618&amp;s=4f31d2ede083c3d843824bf2f5c6d217&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 jbabz

Reputation: 0
• Posts: 15
• Joined: 30-April 10

# Integer Arrays

Posted 14 May 2010 - 03:47 PM

Hello..

I have a quick question for anyone who has a couple of spare minutes..

I have created this program:

```#include<stdio.h>
/* Function prints Intersection of arr1[] and arr2[]
m is the number of elements in arr1[]
n is the number of elements in arr2[] */
int printIntersection(int arr1[], int arr2[], int m, int n)
{
int i = 0, j = 0;
while(i < m && j < n)
{
if(arr1[i] < arr2[j])
i++;
else if(arr2[j] < arr1[i])
j++;
else  /* if arr1[i] == arr2[j] */
{
printf(" %d ", arr2[j++]);
i++;
}
}
}

int main()
{
int arr1[5] = {1, 2, 3, 4, 5};
int arr2[5] = {2, 7, 8, 9, 10};
int m = sizeof(arr1)/sizeof(arr1[0]);
int n = sizeof(arr2)/sizeof(arr2[0]);
printIntersection(arr1, arr2, m, n);
getchar();
return 0;
}
```

For this assignment,

DIRECTIONS: Write a program which will prompt the

user to enter values for two int arrays. Each array is

of length 5, with values such that 0 <= 'i' <= 99.

Values entered for each array must be UNIQUE.

Your program will will compute the set intersection of the

two arrays. That is, your program will display every value

which the two arrays have in common. For example, if array

A = {5, 4, 3, 2, 1} and array B = {2, 4, 6, 8, 10} then

their intersection is {2, 4}. If the two arrays have no

common elements, your program should print a 'NULL SET' message. }

EXAMPLES:

Enter five values: 1 2 3 4 5

Enter five values: 2 4 6 8 10

Intersection:2

Intersection:4

PROGRAM ENDS

Enter five values: 1 2 3 4 5

Enter five values: 6 7 8 9 10

Null Set

PROGRAM ENDS

Unfortunately I entered the example values into my code. Can anyone show me how to redo this correctly? My professor is asking for the student to be able to enter those values and the computer to evaluate the intersection.

Thank you!

Is This A Good Question/Topic? 0

## Replies To: Integer Arrays

### #2 japanir

• jaVanir

Reputation: 1012
• Posts: 3,025
• Joined: 20-August 09

## Re: Integer Arrays

Posted 14 May 2010 - 03:52 PM

declare the two int arrays as
```int arr1[5];
int arr2[5];
```

use scanf to get the numbers, loop 5 times for each array, scanf each iteration.
you can create a function to get the array.
also, should the program verify the input? (numbers should be unique bwtween 0 - 99)?

• Saucy!

Reputation: 6219
• Posts: 23,965
• Joined: 23-August 08

## Re: Integer Arrays

Posted 14 May 2010 - 04:04 PM

Why the heck are you doing < and > in an intersection? All you care about is that it's EQUAL. Smells like copy and paste.

### #4 PsychoCoder

Reputation: 1649
• Posts: 19,853
• Joined: 26-July 07

## Re: Integer Arrays

Posted 14 May 2010 - 04:12 PM

Wow JackOfAllTrades you couldn't be more right