473,326 Members | 2,113 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Functions

for my assignment I was told I have to use a function at one point. The one I put in doesn't work properly. Any help?
Expand|Select|Wrap|Line Numbers
  1. //variable declaration
  2.         float score[7];         //declares an erray of 8 float values
  3.         int s=0;
  4.         float sum=0;
  5.         int i = 0;
  6.         int n=0;
  7.         int position = 0;
  8.         float Max = 0;
  9.         for (i=0; i<=7; i++){
  10.                 cout << "Please enter each one of the eight judges scores and press enter after each one" << end$
  11.                 cin >> score[i];
  12.         }
  13.         float Min = score[0];
  14.         for (n=1; n<=7; n++) {
  15.                 if (Min> score[n]){
  16.                         Min = score[n];
  17.                         position = n;
  18.                 }
  19.         }
  20.         cout << fixed<< showpoint << setprecision(2)<< endl;
  21.         cout <<"Min number is " << Min << endl;
  22.         Max = score[0];
  23.         position = 0;
  24.  
  25.         for (n=1; n<=7; n++){
  26.                 if (Max < score[n]) {
  27.                         Max = score[n];
  28.                         position = n;
  29.  
  30.                 }
  31.         }
  32.         cout <<"Max number is " << Max << endl;
  33.  for (s=0; s<=7; s++)
  34.         {
  35.                 sum = sum +  score[s];
  36.         }
  37.         cout <<"Original sum " << sum << endl;
  38.         sum = sum - Min - Max;
  39.         cout << fixed<< showpoint << setprecision(2)<< endl;
  40.         cout << "The point received by the diver are: " << score[2] << endl;
  41.  
  42.  
Mar 23 '08 #1
7 1100
sicarie
4,677 Expert Mod 4TB
cronker01-

We have a policy against posting full code, so I removed some stuff.

Um, what was your assignment? Your function was pretty much useless - you could do that just as easily without.

The rule for functions is any repeatable task should be turned into a function. Do you get an average frequently? Don't spend time typing in the * wildcard and equals, just call a function to do it. Generally a single print line is not what you call a function for - especially if you only call it once.

So is there any repeatable task in your program? Is there a task that could be considered useful to make repeatable?
Mar 23 '08 #2
cronker01-

We have a policy against posting full code, so I removed some stuff.

Um, what was your assignment? Your function was pretty much useless - you could do that just as easily without.

The rule for functions is any repeatable task should be turned into a function. Do you get an average frequently? Don't spend time typing in the * wildcard and equals, just call a function to do it. Generally a single print line is not what you call a function for - especially if you only call it once.

So is there any repeatable task in your program? Is there a task that could be considered useful to make repeatable?
This assignment gives you 8 judges scores and you take the one highest and one lowest scores out and average the remianing 6. I did the program without a function and it worked fine. but part of the assignment is to add a function in so I just tried adding one later but the sum value is not right at the end.
Mar 23 '08 #3
sicarie
4,677 Expert Mod 4TB
This assignment gives you 8 judges scores and you take the one highest and one lowest scores out and average the remianing 6. I did the program without a function and it worked fine. but part of the assignment is to add a function in so I just tried adding one later but the sum value is not right at the end.
Okay, so I would recommend using a function to determine the average. You can pass it an array, or you can pass it all 6 scores, and return the average.

Does that make sense? Can you see how that would be done?
Mar 23 '08 #4
Okay, so I would recommend using a function to determine the average. You can pass it an array, or you can pass it all 6 scores, and return the average.

Does that make sense? Can you see how that would be done?
It does make sense but I'm not sure how to do the code.
Mar 24 '08 #5
sicarie
4,677 Expert Mod 4TB
What part are you having trouble with? You said you had the first part - where you accepted the input, computed the average, and printed it out. Just move the 'average' code into a function.

You will have to decide - probably based on your input, but you could do it any way you wanted - how you will pass that information, but the only code that will change is the call to the function instead of computing the average.
Mar 24 '08 #6
What part are you having trouble with? You said you had the first part - where you accepted the input, computed the average, and printed it out. Just move the 'average' code into a function.

You will have to decide - probably based on your input, but you could do it any way you wanted - how you will pass that information, but the only code that will change is the call to the function instead of computing the average.
I think I got it. Don't really know how but it still work. Thanks for taking a look though. I'm working on another question now that I'm having more problems with. It's forming an answer key for a true false quiz.
Mar 24 '08 #7
sicarie
4,677 Expert Mod 4TB
I think I got it. Don't really know how but it still work. Thanks for taking a look though. I'm working on another question now that I'm having more problems with. It's forming an answer key for a true false quiz.
Well, if you post what you have, even if you don't think its right, we can help you refine it. Your compiler, as well, will point you to the line number your errors are occurring on, and most compiler error messages can be Google searched to reveal a wealth of information.

For your question on creating an answer key for a T/F quiz, please start a new thread with you question and the corresponding code you are having difficulties with.
Mar 24 '08 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: hokiegal99 | last post by:
A few questions about the following code. How would I "wrap" this in a function, and do I need to? Also, how can I make the code smart enough to realize that when a file has 2 or more bad...
99
by: David MacQuigg | last post by:
I'm not getting any feedback on the most important benefit in my proposed "Ideas for Python 3" thread - the unification of methods and functions. Perhaps it was buried among too many other less...
21
by: Rubén Campos | last post by:
I haven't found any previous message related to what I'm going to ask here, but accept my anticipated excuses if I'm wrong. I want to ask about the real usefulness of the 'inline' keyword. I've...
17
by: cwdjrxyz | last post by:
Javascript has a very small math function list. However there is no reason that this list can not be extended greatly. Speed is not an issue, unless you nest complicated calculations several levels...
2
by: Bryan Olson | last post by:
The current Python standard library provides two cryptographic hash functions: MD5 and SHA-1 . The authors of MD5 originally stated: It is conjectured that it is computationally infeasible to...
7
by: Tim ffitch | last post by:
Hi I have created a VB dll file that contains common functions I use across various projects in VB, Access and Excel. Rather than have to code the functions in each I decided to use the dll...
23
by: Timothy Madden | last post by:
Hello all. I program C++ since a lot of time now and I still don't know this simple thing: what's the problem with local functions so they are not part of C++ ? There surely are many people...
14
by: v4vijayakumar | last post by:
Why we need "virtual private member functions"? Why it is not an (compile time) error?
7
by: Immortal Nephi | last post by:
My project grows large when I put too many member functions into one class. The header file and source code file will have approximately 50,000 lines when one class contains thousand member...
6
KevinADC
by: KevinADC | last post by:
This snippet of code provides several examples of programming techniques that can be applied to most programs. using hashes to create unique results static variable recursive function...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.