Jump to content

Integer Arrays

- - - - -

  • Please log in to reply
7 replies to this topic

#1
JaaneKyun

JaaneKyun

    Newbie

  • Members
  • Pip
  • 5 posts
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!

#2
Neo_the_chosen_one

Neo_the_chosen_one

    Newbie

  • Members
  • Pip
  • 4 posts
Hi ,
you have to ask to enter the values , do you know scanf ?

#3
Ancient Dragon

Ancient Dragon

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 400 posts
If you don't know how to do what your prof wants then I strongly suspect you did not write the code you posted -- probably plagurized from someone else.
Visit Grandpa's Forums, a social networking forum, with family-oriented arcade games, blogs, discussion forums, and photo albums.

#4
Warrior

Warrior

    Programmer

  • Members
  • PipPipPipPip
  • 130 posts
As you made a couple of requests and showed some work of yours and because you are stuck i thought i shall help you
You said as values can be between 0 and 99 we can exploit this feature and create two char array of 100 characters each..which will serve as a lookup table.First all values are zeroed in both the tables and then as each number arrives we mark it as 1 by using the number as the index to the table.This way we can check repetition and print intersection.The code is very much self explanatory.

#include <stdio.h>

int main()
{
    int a[5];
    int b[5];
    int i;
    char b_table[100];
    char a_table[100];

    for(i=0;i<100;i++){
        b_table[i]=0;
        a_table[i]=0;
    }

    printf("Enter values for a\n");
    for(i=0;i<5;i++){
        scanf("%d",&a[i]);
        if(a[i]<0 || a[i]>99 || a_table[a[i]]){
            printf("Values can be only between 0 and 99 and no repeat\n");
            return 1;
        }
        a_table[a[i]]=1;
    }

    printf("Enter vaules for b\n");
    for(i=0;i<5;i++){
        scanf("%d",&b[i]);
        if(b[i]<0 || b[i]>99 || b_table[b[i]]){
            printf("Values can be only between 0 and 99 and no repeat\n");
            return 1;
        }
        b_table[b[i]]=1;
    }

    printf("Printing Intersection\n");
    for(i=0;i<5;i++)
        if(b_table[a[i]])
           printf("%d ",a[i]);
    return 0;
}

Warrior

#5
JaaneKyun

JaaneKyun

    Newbie

  • Members
  • Pip
  • 5 posts
Warrior.. Thank you so much.

#6
thechef

thechef

    Learning Programmer

  • Members
  • PipPipPip
  • 79 posts
You could also put all the numbers from both lists into a vector and call "unique." That would push all the non-unique values to the back of the vector, so you could start counting them from there.
I don't document code. If it was hard to write, it should be hard to read ;)

#7
Hassan Seth

Hassan Seth

    Newbie

  • Members
  • PipPip
  • 10 posts
Good Logic! Warrior! :)

#8
thechef

thechef

    Learning Programmer

  • Members
  • PipPipPip
  • 79 posts
Oops! I guess "unique" doesn't quite work that way. You would rather have to take advantage of sets I think. That would allow more flexibility in the number of input arguments and such. Good work though, Warrior!
I don't document code. If it was hard to write, it should be hard to read ;)




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users