Qestions need solution please

Try making these questions as simplified as possible.

1. (30 points) You are given a one-dimensional array of ten ints. Write a program that prints “OUTLIER” if there is at least one point with a value that is at least twice the average of all the points. Otherwise print “OK.” This can be one in one traversal through the loop: accumulate the sum of the elements, and find the maximum, in one loop. Then at the end of the loop you compute the average and test whether you have one point that exceeds the average by a factor of at least 2.0. Print the value of the outlier, if there is one.
(Wikipedia: In statistics, an outlier is an observation that is numerically distant from the rest of the data.)
Here is sample data—but be sure not to take any advantage of particular characteristics on the sample, which does have an outlier:
const int MAXN = 10;
int A[MAXN] = {4, 32, 9, 7, 14, 12, 13, 17, 19, 18};

2. (30 points) You are given two arrays of ints, named A and B; one contains AMAXELEMENTS and the other contains BMAXELEMENTS.
Write a Boolean-valued function that returns true if there is at least one point in A that is the same as a point in B, and false if there is no match between the two arrays.
This will require two for loops, one to work through A, and one that works through B for every word in A.

3. (40 points) Write a function that determines whether a point (x, y) is in a square with its vertices at (-n, 0), (0, n), (n, 0) and (0, -n). “In,” in this case, means that the point may lie on the sides of the square. You do not need four if statements to do this; use absolute values. The equation of the side that lies in the first quadrant is x + y = n. Your function should have three arguments: x, y, and n. Your main program should call this function and write out IN THE SQUARE or NOT IN THE SQUARE.
Heh, like we are going to do this for you.
The "Heh" adds a mean burn to that post. lol
Seriously. We will give only hints and tips when dealing with question regarding formulation of a program. Multiple-choice questions we will flat-out refuse to answer.

Although I think I may have once given out all the obviously wrong answers to an MC question set.

-Albatross
HInts are nice, so lets see what u gt?
Perhaps you could make an attempt and paste your answer, then we can help explain what parts you have gotten wrong?
Ok let me start with #1

1.
#include<iostream>
#include <cmath>
using namespace std;
int main()
{
const int maxn = 10;
int i, max, nums[maxn] = {4,32,9,7,14,12,13,17,19,18};
int average;
max = nums[0];

for (i = 0; i < maxn; i++)
if (max < nums [i])
max = nums[i];

for (i = 0; i < maxn; i++)
if (nums[i] >= 2 * average)
cout <<"outlier"<< endl;
else
cout<<"ok"<< endl;
return 0;
}


Last edited on
You forgot to calculate the average.
That's a good start but I think you need to carefully examing the wording of the question.

It says you should be able to do this in one loop and gives
details about what needs to be done in the loop:

First we have the overview:


1. (30 points) You are given a one-dimensional array of ten ints.
Write a program that prints “OUTLIER” if there is at least
one point with a value that is at least twice the average of all
the points.

Otherwise print “OK.”


Then they tell us how to do it:

This can be one in one traversal through the loop:


So we only need one loop
1
2
3
for(i = 0; i < maxn; i++)
{
}


Then:

accumulate the sum of the elements


Okay, lets do that:

1
2
3
4
5
int sum = 0;
for(i = 0; i < maxn; i++)
{
	sum = sum + nums[i];
}


Then what?


, and find the maximum


Okay, we can add that:

1
2
3
4
5
6
7
8
9
int sum = 0;
for(i = 0; i < maxn; i++)
{
	sum = sum + nums[i];
	if(max < nums[i])
	{
		max = nums[i];
	}
}



, in one loop.


Job done... what next?


Then at the end of the loop you compute the average


Okay, here we go:

1
2
3
4
5
6
7
8
9
10
11
int sum = 0;
for(i = 0; i < maxn; i++)
{
	sum = sum + nums[i];
	if(max < nums[i])
	{
		max = nums[i];
	}
}

float average = sum / maxn;


Next...?


and test whether you have one point that exceeds the average
by a factor of at least 2.0.


Okay... if any point will exceed it it will be our maximum point 'max':

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int sum = 0;
for(i = 0; i < maxn; i++)
{
	sum = sum + nums[i];
	if(max < nums[i])
	{
		max = nums[i];
	}
}

float average = sum / maxn;

if(average > (max * 2))
{
}


What next?


Print the value of the outlier, if there is one.


So we add that:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
int sum = 0;
for(i = 0; i < maxn; i++)
{
	sum = sum + nums[i];
	if(max < nums[i])
	{
		max = nums[i];
	}
}

float average = sum / maxn;

if(average > (max * 2))
{
	cout << "OUTLIER: " << max << endl;
}
else
{
	cout << "OK" << endl;
}



Its good to be very methodical. They give you a lot of information in the question.
In fact they virtually walk you through it.
I wish people would stop replying to threads where children are asking for you to do their homework for them...
Children have to learn to program somewhere. I think its beneficial to see how the solution can be extracted from the question. Its not as if he is not trying.

I come here to help people because I benefited from the help of other people. I still do in fact.
1
2
3
if(average > (max * 2))
{
}

There's something wrong here,
average > (max*2)
, return of this expression can be true ?
Average can be less than maximum number or equal to maximum number.It enters that if block , only all the elements are equal to 0.

Write a program that prints “OUTLIER” if there is at least one point with a value that is at least twice the average of all the points

It shall be check with a loop
1
2
3
4
for( int j = 0 ; j < nmax ; j++)
   if( nums[j] >= 2*average ){
         cout << "OUTLIER" << endl;
          break; }
Last edited on
You are right. This is wrong:
1
2
3
if(average > (max * 2))
{
}


I think it should be this:
1
2
3
if(max >= (average * 2))
{
}


I think we only need to check the value of max. This is because, of all the points in our set, max is the *most likely* to fit the bill. And we only need to discover *one* such point to reach our conclusion.

So I don't think a second loop is required.
Topic archived. No new replies allowed.