Re: newbie (dropping numbers) new post

From: Andrew Falanga (notreal_at_fiction.com)
Date: 10/17/04


Date: Sun, 17 Oct 2004 11:25:28 -0600

Kathyoneal143 wrote:
> ok, here is the code I have without using the void statements only the equasion
> to drop the lowest number from the grades. the problem is, if I change up
> number 3 and number 4 numbers, I get a different answer. I used the numbers
> 80,84,60,100 and 90. putting the numbers in like that, I get 88 but, if I mix
> up the 100 and 60 then I get a grade of 81. can anyone tell me why it is not
> finding the lowest number and just dropping it when I tell it to ( - lowest)?
>
> #include <iostream>
> #include <iomanip>
> using namespace std;

Either use a #define or a constant int here. For example,

#define MAX 5
--- or ---
const int MAX = 5;

>
>
> int main()
>
>
> {
> int test1, test2, test3, test4,test5,average, averagescore,divide;

I'm not as diehard as others with "only one variable per line", but this
  borders on to many. It was confusing to read. Also, try using an int
array for the test scores. A further reason for this will be apparent
below. So, for the test scores we'd want something like

int scores[MAX]; // this will replace the test1, test2, etc. variables

Also, I think you'd probably want to make the divide, and possibly the
average and averagescore variables floats rather than ints.

>
>
>
>
> cout <<"This program will gather five test scores and\n";
cout << "This program will gather " << MAX << " test scores and" <<
std::endl;

> cout <<"drop the lowest score, giving you the average\n";
> cout <<"\n";
> cout <<"Please enter Five Test scores\n";
> cin >> test1>>test2>>test3>>test4>>test5;

Even though this does do what you want it to, I'd shy away from this
construct as well. It, too, is confusing to read. I would use some
sort of loop, especially, since the above construct cannot check for
errors. When it's just you learning the language, or similar
situations, this probably isn't an issue. However, you should become
conscious of error checking even at this point. Something like this

for (int i=0; ((i < MAX) && (cin.good())); i++)
   cin >> scores[i];

Please note that the above loop is only an example. It does not do any
except break out of the loop when MAX has been reached, or some sort of
entry error was detected on stdin. If it's an entry error, other
operations should be performed, but it's up to you to decide what.

>
>
> int lowest = test1; // this states that test 1 is the lowest number
> unless
> if (test2 < test1)lowest = test2; // test 2 is smaller than
> test 1 unless
> if (test3 < test2)lowest = test3; // test 3 is smaller than
> test 2 unless
> if (test4 < test3)lowest = test4; // test 4 is smaller than
> test 3 unless
> if (test5 < test4)lowest = test5; // test 5 is smaller than
> test 4.

Reason number 2 for the array idea for scores. (Actually, there are
three reasons, reason three would be that you could easily expand to
program to work on more than 5 scores.) Instead the several lines
above, you could create a loop to rearange the array with the lowest
score last.

for (int i=0; i < MAX; i++) {
   for (int j=0; j < MAX; j++) {
     if(scores[i] > scores[j]) {
       int temp;
       temp = scores[i];
       scores[i] = scores[j];
       scores[j] = temp;
     }
   }
}

Now, we'll average

for(int i=0; i < (MAX - 1); i++)
   average += scores[i];

Then

divide = average / (MAX - 1);

>
> average = (test1+test2+test3+test4+test5);// all test scores averaged together
> averagescore = average - lowest; // average score minus the lowest
> grade
> divide = averagescore /4; // average grade is then divided by 4
> cout << divide<< endl; // final grade after division
>
> return 0;
> }

By searching through the array and rearanging the numbers with the
lowest score last, we were able to average only the scores we wanted by
using MAX - 1. Now, my code by no means is the best and only answer.
Just one of the ways of doing it.

As to why your answers were different based on how you entered the
numbers? That probably has to do with your machine. In other words,
that's a hardware issue. Not necessarily your code. If I'm wrong in
that, someone here will correct.

Hope my code wasn't hard to understand. Happy coding and I hope
something of what I said will help.

Andy

----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= East/West-Coast Server Farms - Total Privacy via Encryption =---



Relevant Pages

  • Re: Looping Choice
    ... we start the main loop: ... If there's a lot happening in the loop, checking one int is ... No, that's not the reason. ... I would like to read your opinions on these statements:) ...
    (comp.lang.c)
  • Re: program doesnt "seem" to print "hello-out"
    ... executing it) ... int main ... What could be the reason? ... in the while loop, you will find out what really happenen. ...
    (comp.lang.c)
  • Re: Fridays the thirteenth. (And a little puzzle.)
    ... -- compiler) is the usual method ... int febdays ... -- We're going to go round a loop dealing with each year in turn. ... -- other languages call) ...
    (uk.people.silversurfers)
  • Re: C code is not generating required results.
    ... int main ... the array by the size of an element. ... prevent the user of your program from entering more characters than ... want to loop. ...
    (comp.lang.c)
  • Re: long double versions of functions in gcc under Cygwin
    ... rather than the nearest enclosing one) and a decent exception ... them it doesn't seem like goto usage would be affected ... int typfun() ... Why use a for loop when it is just a while loop in disguise? ...
    (comp.lang.c)