3 Replies - 648 Views - Last Post: 14 May 2010 - 04:12 PM Rate Topic: -----

#1 jbabz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • 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)?
Was This Post Helpful? 0
  • +
  • -

#3 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6063
  • View blog
  • Posts: 23,515
  • 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.
Was This Post Helpful? 0
  • +
  • -

#4 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1641
  • View blog
  • 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
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1