# Thread: Help with Homework... Time is almost up.

1. ## Re: Re: Just an another common way to do (a)

Originally posted by M_Ghani
sorry Kasun but i think you should make the following
Code:
`int input[3];`
because input[2] means only two elements not three in the array but u needed here three elements of the array input

"The first element in the array is the 0th element, and the last element is the (n-1th) element, where n is the size of the array."
- MSDN, Arrays.

So, input[2] means actually 3 elements - 0, 1 and 2.

Thanks.

2. actually, he was right... the 'n' they were referring to was here:
Code:
```int input[n]; //there's the n

input[0]; //there's the 0 (first element)
...
input[n-1]; //there's the n-1 (last element)```
So, input[2] means actually 2 elements - 0 and 1 (n-1).

3. Originally posted by major_small
you should never tell anybody to do what their assignment explicity tells them not to do...
I understand. But when I post that code, he was already figured out a code appropriate for his class. So I was only thinking of pointing out another way to do it, not telling to use that method, I have no power to do that.

4. Originally posted by major_small
I dont' do flowcharts or trace tables or anything like that before programming... only for really big projects... and don't mix programming and building a house... that is one of the worst analogies i've ever heard...
It's a perfect analogy. You need to have a plan when you build a house or a program. It's just that many programs are more on the order of building a doghouse instead of a mansion -- the blueprints are simple enough that experience makes them unnessesary.

But inexperiance make them very useful.

5. I don't know about that... I never used them for small stuff, even when I was inexperienced...

6. >>because input[2] means only two elements not three in the array but u needed here three elements of the array input

by my counting input[2] has three elements: [0].[1].[2] !!

7. Originally posted by axon
>>because input[2] means only two elements not three in the array but u needed here three elements of the array input

by my counting input[2] has three elements: [0].[1].[2] !!
Not sure if you were joking axon, but the discussion of the array seems a little too confusing. It is simple:

When declaring an array, the number inside the brackets is the size of the array, so

int input[2];

declares an array with 2 elements. The elements can be accessed with the operator[] like this:

input[0] = i1;
input[1] = i2;

Attempting to access an element of the array outside of its bounds is undefined.

Most of the time, it will overwrite some other memory in your program. Often, it will cause a crash. Sometimes, if you are unlucky, you won't notice.

8. ## Thanks for the help... here is the current result

Ok, here is the current work (sorry about the formatting... don't know html tags to display it properly on this forum). I have few issues still, but overall I am pleased ( this is my first Computer Science class). My main problems still are the following:

1. Getting the program to immediately give an error if the input
given is not a-f instead of it asking for the integers first.

2. For case 'c', I need to find the average of the 3 integers
converted to the floating point average. The compiler
warns me that it is coverting it with a possible loss of
data. Am I doing this right or is there another way?

3. The inability to handle a zero input for case 'a' is still
present as I focused all my energy I finishing the rest
of the program first. I will be working on that in the
meantime.

4. Anything else that you feel like mentioning.

Code:
```
#include <iostream.h>
#include <math.h>

int main()
{
int val1;
int val2;
int val3;
int first;
int second;
int third;
char option;
float sum;
int same;
int even = 0;
int odd = 0;
float num1 = 0;
float num2 = 0;
float num3 = 0;

// Title, Input

cout << "Integer Calculation Program\n" << endl;
cout << "Enter a single character a-f\n" << endl;
cin >> option;
cout << "Enter three integer values." << endl;
cin >> val1 >> val2 >> val3;

switch (option)

/*
Case 'a' compares the 3 integers, assigns the values in order from smallest
to largest to the variables first, second, and third, and then displays
the output to the user.
*/

{
case 'a'  :

if (val1 <= val2 && val1 <= val3 && val2 <= val3)

{
first = val1;
second = val2;
third = val3;
}

if (val2 <= val1 && val2 <= val3 && val1 <= val3)

{
first = val2;
second = val1;
third = val3;
}

if (val3 <= val1 && val3 <= val2 && val2 <= val1)

{
first = val3;
second = val2;
third = val1;
}

if ( val3 <= val1 && val3 <= val2 && val1 <= val2)

{
first = val3;
second = val1;
third = val2;
}

cout << first << " " << second << " " << third << " " << endl;

break;
/*
Case 'b' calculates the absolute values of the 3 integers and then
displays the output to the user.
*/

case 'b'  :

val1 = abs (val1);
val2 = abs (val2);
val3 = abs (val3);

cout << val1 << ", " << val2 << ", and " << val3
<< " are the absolute values of your input." << endl;

break;
/*
Case 'c' adds the 3 integers and then finds the average floating point
value of their sum by dividing by 3.
*/

case 'c'  :

sum = (val1 + val2 + val3) / 3.0;
cout << sum << " is the average of the three integers given."
<< endl;

break;

/*
Case 'd' compares the 3 integers and determines and displays the number of
the values that are the same.
*/

case 'd'  :

if (val1 == val2 && val2 == val3)
same = 3;

if (val1 == val2 && val3 != val1)
same = 2;

if (val2 == val3 && val1 != val2)
same = 2;

if (val3 == val1 && val2 != val3)
same = 2;

if (val1 != val2 && val2 != val3 && val1 != val3)
same = 0;

cout << same << " of the integer values are the same."
<< endl;

break;

/*
Case 'e' determines how many of the 3 integer values are even and how many
are odd and then displays the information to the user.
*/

case 'e'  :

if ((val1 %2) == 0)
even++;
else
odd++;

if ((val2 %2) == 0)
even++;
else
odd++;

if ((val3 %2) == 0)
even++;
else
odd++;

cout << even << " of the values are even." << endl;
cout << odd << " of the values are odd." << endl;

break;

/*
Case 'f' determines if the 3 integers are positive, and then adds the
positive integers together and divides them by 3.0 to determine the
float point average of the positive integers.
*/

case 'f'  :

if (val1 > 0)
num1 = val1;
if (val2 > 0)
num2 = val2;
if (val3 > 0)
num3 = val3;
sum = (num1 + num2 + num3) /3.0;

cout << sum << " is the floating point average "
<< "of the positive integers."  << endl;

break;

/*
If any character besides a-f was given, then after entering the 3 integer
values, the program gives an error message informing the user of the
mistake.
*/

default : cout << "Not a valid option. You did not choose "
<< "a character a-f." << endl;
}

return 0;

}```

9. >>Not sure if you were joking axon,

lets just say I was joking

hey uncertain please use [code][/code] tags whenever posting any code! please edit your most recent post with the [code][/code] tags surrounding you code. Also, reread the rules, and other sticky notes on top of this forum.

10. ## That was simple...

Sometimes the most obvious just slips right past my eyes. Thanks for the tip Axon.

11. >>Sometimes the most obvious just slips right past my eyes. Thanks for the tip Axon.

np. looks much better, doesn't it?

12. ## Yep!

MUCH better!

13. for part 1 and 2, just have an if statement checking if the input is out of bounds...if it is display message and loop back. For 2) also an if statement if c, read in floats...pseudo code below:

Code:
```float a, b, c;
bool notDone = true;

while( notDone )
{
cout << "Integer Calculation Program\n" << endl;
cout << "Enter a single character a-f\n" << endl;
cin >> option;

if( option not a-f )
cout << "error" << endl;
else
notDone = false;
}//while
if( option is c)
{ cout << "Enter three floats." << endl;
cin >> a >> b >> c; //then use these in case c
}
else
{
cout << "Enter three integer values." << endl;
cin >> val1 >> val2 >> val3;
}```

14. the way you have your code before, the compiler is giving you warning because you are dividing an int by a float...

if someone inputs 3.3333 and you read this in into an int...it will only keep the 3. Some compilers will give you an error.

15. ## integers to floats

Well, the only problem is the assignment requires the input be integers so I have to convert the average of integers to floating point which the program is doing automatically from what I understand.

I can't use loops in this assignment. But I was experimenting with
using an if statement. I am not sure if this can work though as I am uncertain as to what statement to use for the if.

Code:
```char option;

cout << "Input a character a-f";

cin << option;

if (option ==  'a' || 'b' || 'c' || 'd' || 'e' || 'f')

else
cout << "Invalid input.";```