0

i know that this is a simple code but i'm having problems with it. Problem to be solved

Get two sets of 10 integers from the user and store each set in a separate array. Find the set representing the intersection of the two sets entered and store the intersection in a third array. Then print out the set representing the intersection.

As a minimum you must have separate functions to accomplish each of the following:

- One function to input an array of 10 integers.

- One function to find the intersection of two arrays.

- One function to print the contents of the intersection array.

3
Contributors
3
Replies
4
Views
7 Years
Discussion Span
Last Post by kat_stephens
0

What problem are you having? You need to post the code you have tried. If you have not started yet, why not? Do the program in small steps -- do the first part first. When that is finished and tested, then move on to the second part.

0

please post ur code with it . its not possible for me to write the whole program. but I can figure out mistakes in your program.

0

ok here is my code it runs perfect but it is too complex still to turn in is there anything simpler that i can do?


/***********************************************************************

assign7.cpp This program will take the imput of two sets of ten numbers and output the intersect of the two sets.

Inputs: input1 - ten numbers
input2 - ten numbers


Outputs: output1 - print out the set representing the intersection


Written by: Katie Stephens Date: October 28, 2009

************************************************************************/


#include "stdafx.h"

void input(int array[]);
int find_intersection (int array1[], int array2[], int intersection[]);
void print_array(int array[], int matches);

int _tmain(int argc, _TCHAR* argv[])
{
int array1[10];
int array2[10];
int intersection[10];
int matches;

printf ("Entering array 1:\n");
input(array1);
printf ("Entering array 2:\n");
input(array2);
matches = find_intersection(array1, array2, intersection);
print_array(intersection, matches);
return 0;
}


/***********************************************************************

void input(int my_array[]) This function will take the input of the first array

Inputs: input1 - ten numbers from the user

************************************************************************/


void input(int my_array[])
{
int i;

for (i = 0; i < 10; i++)
{
printf ("Please enter an integer.");
scanf ("%d", &my_array);
}
}
/***********************************************************************

int find_intersection (int array1[], int array2[], int intersection[])
This function will take the input of the second array and find the intersection of the
first and second array.

Inputs: input1 - ten numbers from user

Outputs: output1 - the intersection of the two sets of arrays


************************************************************************/


int find_intersection (int array1[], int array2[], int intersection[])
{
bool duplicate = 0;
bool intersect = 0;
int i;
int j;
int k = 0;

for (i = 0; i < 10; i++) {
for (j = 0; j < i; j++) {
if (array1[j] == array1 && i != 0) {
duplicate = 1;
}
}

for (j = 0; j < 10; j++) {
if (array2[j] == array1) {
intersect = 1;
}
}

if (intersect == 1 && duplicate == 0) {
intersection[k] = array1;
k++;
}
duplicate = 0;
intersect = 0;
}
return k;
}
/***********************************************************************

void print_array(int array[], int size) This function will print the intersection of the two arrays


Inputs: input1 - none

Outputs: output1 - print out the intersection of the two arrays.

************************************************************************/


void print_array(int array[], int size)
{
int i;
for (i = 0; i < size; i++) {
printf ("%d: %d\n", i + 1, array);
}
}
Thread: need help
Forum: C

This article has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.