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

Quote:

*Originally posted by M_Ghani *
**sorry Kasun but i think you should make the following**

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.

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;

}