Help with Homework... Time is almost up.
I am currently working on my homework and I am having a little trouble with the assignment. This is the assignment:
Design, write and test a program to meet the following specifications.
1. Print a title for the program and then display a clear prompt for the user to enter a character in the range of ‘a’..’f’.
2. Validate the user input: print an error message if the input is out of range (not in the range ‘a’..’f’)
3. Process the valid input as follows
a. First, prompt for and read three integers
b. Print the following results based on the input from the user
Option Output
a The 3 integers in order from smallest to largest
b The absolute values of the integers
c The floating point average of the integers
d The number of integers that are the same
e The number of even and the number of odd values input
f The floating point average of the positive integers
Restrictions:
Only use the language constructs covered in class.
The options must be handled using a switch statement.
I know its a lot to ask, but could anyone please give me examples of how to code options a-f within the restrictions given? This is due to tommorow and I really want to be able to turn in my assignment so I don't get dropped (my teacher is very strict and also moving so fast that I am having trouble keeping up with this course). Thanks for any assistance!
Sincerely,
KT
Just an another common way to do (a)
Code:
(a)
#include<iostream>
using namespace std;
void Assend();
int input[2];
int i1,i2,i3;
void Assend(){
input[0]=i1;
input[1]=i2;
input[2]=i3;
int size = 3, temp;
// Bubble sort
for(int x=0; x<(size-1); x++)
for(int y=1; y<(size-x); y++){
if ( input[y-1] > input[y] ){
temp = input[y-1];
input[y-1]=input[y];
input[y] = temp;
}
}
cout<<input[0]<< ", " << input[1] << ", " << input[2] << ", " << endl;
}
void main()
{
char c;
cout<< "Enter a character a-f: " ;
cin>> c;
bool valied = (int)c>102;
if(valied)
cout<<"Charactor out of range"<<endl;
cout<< "Enter an integer: "<<endl;
cin>>i1;
cout<< "Enter a 2nd integer: "<<endl;
cin>>i2;
cout<< "Enter a 3rd integer: "<<endl;
cin>>i3;
switch(c)
{
case 'a':
Assend();
}
}
Re: Just an another common way to do (a)
Quote:
Originally posted by kasun
Code:
(a)
void Assend();
int input[2];
int i1,i2,i3;
void Assend(){
input[0]=i1;
input[1]=i2;
input[2]=i3;
int size = 3, temp;
...........................
..........
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
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;
}
Why Case A gives you problems
The reason why your having problems with Case A: has to do with the fact that you haven't covered all the possible combinations of the 3 integers there are 6 different combinations and you only have 4 of them
Newbieville... population.. 1
How in the heck did I miss that?!? That was it... was missing
1, 3, 2 and 2, 3, 1 as possible combinations. Thanks!
Just rearrange the statement so it comes after the switch
You still enter the 3 integers because that code still executes if you place that code after the switch it shouldn't execute you would also need to add a way to terminate the program after the error message maybe an exit call after the default message