![]() |
|
#1
|
|||
|
|||
Array 1 dimensional help please asapHi everyone,
Could someone please help me write a program that displays the following output (the millions of gallons values are given)? We're supposed to use a for loop within a while loop and the frequency table must be generated by a loop. I would appreciate any help I can get. __________________________________________________ __________ Day no Millions of gallons __________________________________________________ __________ 1 123 2 134 3 122 4 128 5 116 6 96 7 83 8 144 9 143 10 156 11 128 12 138 13 121 14 129 15 117 16 96 17 87 18 148 19 149 20 151 21 129 22 138 23 127 24 126 25 115 26 94 27 83 28 142 __________________________________________________ __________ Sewage per day Frequency of occurrence __________________________________________________ __________ 81- 90 3 91-100 3 101-110 0 111-120 3 121-130 9 131-140 3 141-150 6 151-160 2 Press any key to continue |
|||
|
#2
|
||||
|
||||
|
We love to help. But we need something to help with, since we won't write it for you. Please define better what you need to accomplish. Don't forget to include the code you already have. We'll help you fix it.
__________________
The 3 Laws of the Procrastination Society: 1) Never do today that which can be put off until tomorrow 2) Tomorrow never comes |
|
#3
|
|||
|
|||
|
My problem is not only coding, but also I don't even know how to write the algorithm. So I don't have somewhere to begin except this:
CPP / C++ / C Code:
Last edited by LuciWiz : 18-Feb-2005 at 01:43.
Reason: Please insert your C code between [c] & [/c] tags
|
|
#4
|
|||
|
|||
|
can anyone help me?
|
|
#5
|
|||
|
|||
|
Quote:
OK that's a start: you have an array of ints that holds the daily sewage amounts (lots of sludge there). Suppose the assignment was to print out the daily amounts. Would you know how to do this? Well, you could have 28 cout<< statements, one for each day, or you could use a loop. Just to get us to the next step, here's my program to do that. If you compile and run it what do you get? CPP / C++ / C Code:
Now, do you understand what your assignment is? It seems that you want to print the following: 1. The number of days that the amount of sewage was greater than or equal to 81 and less than or equal to 90. 2. The number of days that the amound of sewage was greater than or equal to 91 and less than or equal to 100. 3. Etc. Now, you already know how to have a loop that looks at all of the daily amounts (and prints them out). Let's suppose that we want to count the number of days that the sewage amount is greater than 92 and less than or equal to 97. How would you do it? I would create a variable, say bin9297. I would set its initial value to zero. As I stepped through the loop I would see if the amount for that day was >90 and also <= 95. If this is true, I would increment the variable bin9295. If you have been given this assignment, I assume you have had relational operators (greater than, less than, less than or equal to, ...). You must have had if() statements. If you have had boolean operators ("||", "&&", etc.) Here's a way to do this. CPP / C++ / C Code:
So: you can make counters for each range of sewage that you want to keep track of. You can make a single loop with if statements that increments the appropriate counter for each day. Then print out the results in the required format. Regards, Dave |
|
#6
|
|||
|
|||
|
Thanks a lot Dave,
So far I have the following and it works. My only problem is that I just found out that my prof doesn't want 8 different if statements, she thinks its ineffecient to being wrong (which I totally disagree). She also wants a loop to generate the table. Do you or anyone else have any ideas? Thanks again CPP / C++ / C Code:
|
|
#7
|
|||
|
|||
|
Quote:
Well, looking at your code, it seems to do what you told us about. Actually your tests are not quite right. My example was an example, not a template for an exact implementation. For example, in your program, all of your tests like this: CPP / C++ / C Code:
should be something like this: CPP / C++ / C Code:
(You missed a couple of counts.) If the prof has some other requirements, that's pretty much between you and her. (As WaltP suggested, maybe you can give us a little more information regarding the precise requirements. Also, we have no way of knowing what you have covered in class that you are expected to use. You obviously know how to put a program together, given a hint or two.) As for 8 different if statements: If you are going to test for eight different conditions with if (something), there will be eight somethings. Instead of separate if() statements, you could use something like this (if -else) CPP / C++ / C Code:
That's eight nested statements. May be more efficient (?), but lots harder to keep track of (for me at least). Now, if you are required to have a loop to print out the count values, then I suppose that the count values should be an array: CPP / C++ / C Code:
Now, how do you print the values in a loop: CPP / C++ / C Code:
But now, the output just has the count value for each bin. How do you get the labelled output using a loop? I guess you could have an array of strings that is initialized to the text for each bin. You see, I can't possibly guess what your prof has in mind. How much of this stuff have you had in class (strings, arrays of strings, etc.???) Actually, there is a simple mathematical relation between the number of gallons and the index of the array of bins that could eliminate the requirement for eight if-else conditions and also eliminate the requirement for arrays of strings for the labelled output. More efficient? Maybe. More elegant? Maybe. Easier to understand? Maybe. What the prof had in mind? Who knows (not me). Regards, Dave |
|
#8
|
|||
|
|||
|
Something was mentioned about using a for loop within a while loop. We don't know anything about strings, this is our first lab after learning what an array is.
My prof writes: Some student errors that have showed up already - Your code should not consist of 8 separate if statements: if( XXX ) XXXXX; if( XXX ) XXXXX; if( XXX ) XXXXX; if( XXX ) XXXXX; if( XXX ) XXXXX; if( XXX ) XXXXX; if( XXX ) XXXXX; if( XXX ) XXXXX; This is inefficient to the point of being wrong - also not considered 'respectable' code. You can do a lot better - rethink the design. And the table should be generated by a loop, not just 8 separate cout statements: cout << "81 - 90" << XXXX cout << "91 - 100" << XXXX cout << "101 - 110" << XXXX ...... Instead, actually calculate the upper and lower bounds for each pass through the loop. One pass of the loop should display one row of the table. right now this is what my program looks like: CPP / C++ / C Code:
|
|
#9
|
|||
|
|||
|
Quote:
OK: make a loop that calculates the number of days within each bin. You go through this loop eight times. (Call this the "outer loop".) Before entering the outer loop, set lower and upper limits to 81 and 90. Each time through the outer loop, you must go through the entire array of gallon[] to count the number of days that the gallon[] value is between lower and upper. (That is, you create an "inner loop" to count the number of days between the limits) After you leave the inner loop each time, you can print out the count value. Then add 10 to lower and upper limits and continue the outer loop. Here is some pseudo-code for this: Code:
Note that this requires exactly as many comparisons as separate if() statements (namely 28 * 8) since you go through the outer loop 8 times and for each time through the outer loop you go through the inner loop 28 times. There is no way that you should tell your instructor that it's generally more efficient to go through the data as few times as possible (think of a database so large that it can't be held in memory all at once, so multiple passes through the data require many disk or other external memory accesses), and her idea of inefficient is no more inefficient than the method that she considers efficient, but file it away for your own personal future consideration. I have worked on systems where, if an employee came up with a plan that required multiple passes through a database where one pass could do the trick, he would have been mocked and derided until he ran out of the room sobbing. I have also worked in places where the boss had ideas that we were required to implement because of some emotional attachment to a method he had used for years, and weren't very efficient. We did it because that's how you get along in the world. No harm was done as far as product integrity was concerned, and specifications were met, so we let the boss be the boss. That is to say we implemented the inefficient algorithm as directed. (But we were really glad to see him go, when he got his promotion --- Free at last! Free at last! Thank you very much! Free at last!) Regards, Dave Last edited by davekw7x : 18-Feb-2005 at 17:17.
|
|
#10
|
|||
|
|||
|
Thank You So Much For Your Help I Got Everything Done The Way She Wants It. I Also Learned So Much From This. Thank You So Much.
Have A Great Weekend |
Recent GIDBlog
Vista ?Widgets? on Windows XP by LocalTech
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| template comiling problems - need expert debugger! | crq | C++ Forum | 1 | 01-Feb-2005 21:26 |
| Function and Array (w/ reference variables) question | brookeville | C++ Forum | 15 | 07-Dec-2004 01:11 |
| more array help (simulation project..) | dilmv | C++ Forum | 6 | 17-Oct-2004 07:51 |
| Speed up C++ code about 3d array! | Truong Son | C++ Forum | 0 | 16-Mar-2004 21:52 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The